Skip to content

Commit

Permalink
update kucoin api with websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
markhollingworth-worthit committed Nov 16, 2021
1 parent 9e52e16 commit c8ef712
Show file tree
Hide file tree
Showing 7 changed files with 598 additions and 132 deletions.
24 changes: 16 additions & 8 deletions models/AppState.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,18 @@ def getLastOrder(self):
self.last_action = "SELL"
return

ac = self.account.getBalance()

df_base = ac[ac["currency"] == self.app.getBaseCurrency()]["available"]
base = 0.0 if len(df_base) == 0 else float(df_base.values[0])

df_quote = ac[ac["currency"] == self.app.getQuoteCurrency()]["available"]
quote = 0.0 if len(df_quote) == 0 else float(df_quote.values[0])
base = 0.0
quote = 0.0

ac = self.account.getBalance()
try:
df_base = ac[ac["currency"] == self.app.getBaseCurrency()]["available"]
base = 0.0 if len(df_base) == 0 else float(df_base.values[0])

df_quote = ac[ac["currency"] == self.app.getQuoteCurrency()]["available"]
quote = 0.0 if len(df_quote) == 0 else float(df_quote.values[0])
except:
pass
orders = self.account.getOrders(self.app.getMarket(), "", "done")
if len(orders) > 0:
last_order = orders[-1:]
Expand Down Expand Up @@ -270,7 +274,11 @@ def getLastOrder(self):

# If Kucoin returns emoty response, on a shared trading account, could multiple buy same pair
if self.app.getExchange() == Exchange.KUCOIN and self.minimumOrderBase(base, actionchk=True) and self.minimumOrderQuote(quote, actionchk=True):
self.last_action = "BUY"
if self.last_action == "BUY":
return
else:
self.last_action = "WAIT"
Logger.warning('Kucoin temporary state set to "WAIT".')
elif order_pairs_normalised[0] < order_pairs_normalised[1]:
self.minimumOrderQuote(quote)
self.last_action = "SELL"
Expand Down
16 changes: 8 additions & 8 deletions models/PyCryptoBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ def getHistoricalData(

if iso8601start != "" and iso8601end == "":
return api.getHistoricalData(
market, granularity.to_medium, iso8601start
market, granularity, iso8601start
)
elif iso8601start != "" and iso8601end != "":
return api.getHistoricalData(
market, granularity.to_medium, iso8601start, iso8601end
market, granularity, iso8601start, iso8601end
)
else:
return api.getHistoricalData(market, granularity.to_medium)
return api.getHistoricalData(market, granularity, websocket)
else: # returns data from coinbase if not specified
api = CBPublicAPI()

Expand Down Expand Up @@ -512,7 +512,7 @@ def is1hEMA1226Bull(self, iso8601end: str = "", websocket=None):
self.ema1226_1h_cache = df_data
elif self.exchange == Exchange.KUCOIN:
api = KPublicAPI(api_url=self.getAPIURL())
df_data = api.getHistoricalData(self.market, Granularity.ONE_HOUR.to_medium)
df_data = api.getHistoricalData(self.market, Granularity.ONE_HOUR, websocket)
self.ema1226_1h_cache = df_data
else:
return False
Expand Down Expand Up @@ -548,7 +548,7 @@ def is1hSMA50200Bull(self, iso8601end: str = "", websocket=None):
self.sma50200_1h_cache = df_data
elif self.exchange == Exchange.KUCOIN:
api = KPublicAPI(api_url=self.getAPIURL())
df_data = api.getHistoricalData(self.market, Granularity.ONE_HOUR.to_medium)
df_data = api.getHistoricalData(self.market, Granularity.ONE_HOUR, websocket)
self.sma50200_1h_cache = df_data
else:
return False
Expand Down Expand Up @@ -578,7 +578,7 @@ def isCryptoRecession(self, websocket=None):
df_data = api.getHistoricalData(self.market, Granularity.ONE_DAY, websocket)
elif self.exchange == Exchange.KUCOIN:
api = KPublicAPI(api_url=self.getAPIURL())
df_data = api.getHistoricalData(self.market, Granularity.ONE_DAY.to_medium)
df_data = api.getHistoricalData(self.market, Granularity.ONE_DAY, websocket)
else:
return False # if there is an API issue, default to False to avoid hard sells

Expand Down Expand Up @@ -611,7 +611,7 @@ def is6hEMA1226Bull(self, iso8601end: str = "", websocket=None):
self.ema1226_6h_cache = df_data
elif self.exchange == Exchange.KUCOIN:
api = KPublicAPI(api_url=self.getAPIURL())
df_data = api.getHistoricalData(self.market, Granularity.SIX_HOURS.to_medium)
df_data = api.getHistoricalData(self.market, Granularity.SIX_HOURS, websocket)
self.ema1226_6h_cache = df_data
else:
return False
Expand Down Expand Up @@ -641,7 +641,7 @@ def is6hSMA50200Bull(self, websocket):
df_data = api.getHistoricalData(self.market, Granularity.SIX_HOURS, websocket)
elif self.exchange == Exchange.KUCOIN:
api = KPublicAPI(api_url=self.getAPIURL())
df_data = api.getHistoricalData(self.market, Granularity.SIX_HOURS.to_medium)
df_data = api.getHistoricalData(self.market, Granularity.SIX_HOURS, websocket)
else:
return False

Expand Down
2 changes: 1 addition & 1 deletion models/TradingAccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def getBalance(self, currency=""):
if self.mode == 'live':
model = KAuthAPI(self.app.getAPIKey(), self.app.getAPISecret(), self.app.getAPIPassphrase(), self.app.getAPIURL())
df = model.getAccounts()
if isinstance(df, pd.DataFrame):
if isinstance(df, pd.DataFrame) and len(df) > 0:
if currency == '':
# retrieve all balances
return df
Expand Down
Loading

0 comments on commit c8ef712

Please sign in to comment.