I am having a terrible time with “SELF”. Here is my code which is used to execute a LEND order in Kucoin. It is partially taken from https://github.com/Kucoin/kucoin-python-sdk/blob/master/kucoin/margin/margin.py . When I run this Python script I get “NameError: name ‘self’ is not defined”.
#pip install kucoin-python
from kucoin.client import Margin
self.key = "610...d1"
self.secret = "e32...766"
self.passphrase = "n..7T"
order = self.client.create_lend_order(
currency="USDT",
size="18",
dailyIntRate="0.080",
term="28",
)
print(order)
def create_lend_order(self, currency, size, dailyIntRate, term):
"""
https://docs.kucoin.com/#post-lend-order
:param currency: Currency to lend (Mandatory)
:type: str
:param size: Total size (Mandatory)
:type: str
:param dailyIntRate: Daily interest rate. e.g. 0.002 is 0.2% (Mandatory)
:type: str
:param term: Term (Unit: Day) (Mandatory)
:type: int
:return:
{
"orderId": "5da5a4f0f943c040c2f8501e"
}
"""
params = {
'currency': currency,
'size': size,
'dailyIntRate': dailyIntRate,
'term': term
}
return self._request('POST', '/api/v1/margin/lend', params=params)
def _request(self, method, uri, timeout=5, auth=True, params=None):
uri_path = uri
data_json = ''
version = 'v1.0.7'
if method in ['GET', 'DELETE']:
if params:
strl = []
for key in sorted(params):
strl.append("{}={}".format(key, params[key]))
data_json += '&'.join(strl)
uri += '?' + data_json
uri_path = uri
else:
if params:
data_json = json.dumps(params)
uri_path = uri + data_json
headers = {}
if auth:
now_time = int(time.time()) * 1000
str_to_sign = str(now_time) + method + uri_path
sign = base64.b64encode(
hmac.new(self.secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest())
if self.is_v1api:
headers = {
"KC-API-SIGN": sign,
"KC-API-TIMESTAMP": str(now_time),
"KC-API-KEY": self.key,
"KC-API-PASSPHRASE": self.passphrase,
"Content-Type": "application/json"
}
else:
passphrase = base64.b64encode(
hmac.new(self.secret.encode('utf-8'), self.passphrase.encode('utf-8'), hashlib.sha256).digest())
headers = {
"KC-API-SIGN": sign,
"KC-API-TIMESTAMP": str(now_time),
"KC-API-KEY": self.key,
"KC-API-PASSPHRASE": passphrase,
"Content-Type": "application/json",
"KC-API-KEY-VERSION": "2"
}
headers["User-Agent"] = "kucoin-python-sdk/" + version
url = urljoin(self.url, uri)
if method in ['GET', 'DELETE']:
response_data = requests.request(method, url, headers=headers, timeout=timeout)
else:
response_data = requests.request(method, url, headers=headers, data=data_json,
timeout=timeout)
print(response_data)
So I added these lines to the start of the script:
def __init__(self):
self.key = "610...d1"
self.secret = "e32...766"
self.passphrase = "n..7T"
But then I get similar errors on the other “self” statements in the script (self.create). What else would I need to add to the SELF definition OR Is there a way to call the script without the SELF variables.