Skip to content

Commit

Permalink
fix for scanning coinbase added enums
Browse files Browse the repository at this point in the history
  • Loading branch information
markhollingworth-worthit committed Nov 9, 2021
1 parent 5a6b63c commit 9b9c978
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
13 changes: 11 additions & 2 deletions models/AppState.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,17 @@ def getLastOrder(self):
self.last_action = "SELL"
return

base = float(self.account.getBalance(self.app.getBaseCurrency()))
quote = float(self.account.getBalance(self.app.getQuoteCurrency()))
ac = self.account.getBalance()

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

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


# base = float(self.account.getBalance(self.app.getBaseCurrency()))
# quote = float(self.account.getBalance(self.app.getQuoteCurrency()))
#base = float(self.account.basebalance)
#quote = float(self.account.quotebalance)

Expand Down
6 changes: 3 additions & 3 deletions models/exchange/binance/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ def getHistoricalData(
"/api/v3/klines",
{
"symbol": market,
"interval": granularity,
"interval": granularity.to_short,
"startTime": startTime,
"limit": 300,
},
Expand All @@ -953,7 +953,7 @@ def getHistoricalData(
"/api/v3/klines",
{
"symbol": market,
"interval": granularity,
"interval": granularity.to_short,
"startTime": startTime,
"limit": 300,
},
Expand All @@ -963,7 +963,7 @@ def getHistoricalData(
resp = self.authAPI(
"GET",
"/api/v3/klines",
{"symbol": market, "interval": granularity, "limit": 300},
{"symbol": market, "interval": granularity.to_short, "limit": 300},
)

# convert the API response into a Pandas DataFrame
Expand Down
18 changes: 10 additions & 8 deletions models/exchange/coinbase_pro/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from threading import Thread
from websocket import create_connection, WebSocketConnectionClosedException
from models.helper.LogHelper import Logger
from models.exchange.Granularity import Granularity

MARGIN_ADJUSTMENT = 0.0025
DEFAULT_MAKER_FEE_RATE = 0.005
Expand Down Expand Up @@ -695,7 +696,8 @@ def __init__(self) -> None:
def getHistoricalData(
self,
market: str = DEFAULT_MARKET,
granularity: int = MAX_GRANULARITY,
# granularity: int = MAX_GRANULARITY,
granularity: Granularity = Granularity.ONE_HOUR,
websocket=None,
iso8601start: str = "",
iso8601end: str = "",
Expand All @@ -707,11 +709,11 @@ def getHistoricalData(
raise TypeError("Coinbase Pro market required.")

# validates granularity is an integer
if not isinstance(granularity, int):
if not isinstance(granularity.to_integer, int):
raise TypeError("Granularity integer required.")

# validates the granularity is supported by Coinbase Pro
if not granularity in SUPPORTED_GRANULARITY:
if not granularity.to_integer in SUPPORTED_GRANULARITY:
raise TypeError(
"Granularity options: " + ", ".join(map(str, SUPPORTED_GRANULARITY))
)
Expand All @@ -737,16 +739,16 @@ def getHistoricalData(
if iso8601start != "" and iso8601end == "":
resp = self.authAPI(
"GET",
f"products/{market}/candles?granularity={granularity}&start={iso8601start}",
f"products/{market}/candles?granularity={granularity.to_integer}&start={iso8601start}",
)
elif iso8601start != "" and iso8601end != "":
resp = self.authAPI(
"GET",
f"products/{market}/candles?granularity={granularity}&start={iso8601start}&end={iso8601end}",
f"products/{market}/candles?granularity={granularity.to_integer}&start={iso8601start}&end={iso8601end}",
)
else:
resp = self.authAPI(
"GET", f"products/{market}/candles?granularity={granularity}"
"GET", f"products/{market}/candles?granularity={granularity.to_integer}"
)

# convert the API response into a Pandas DataFrame
Expand All @@ -757,7 +759,7 @@ def getHistoricalData(
df = df.iloc[::-1].reset_index()

try:
freq = FREQUENCY_EQUIVALENTS[SUPPORTED_GRANULARITY.index(granularity)]
freq = FREQUENCY_EQUIVALENTS[SUPPORTED_GRANULARITY.index(granularity.to_integer)]
except:
freq = "D"

Expand All @@ -782,7 +784,7 @@ def getHistoricalData(
df["date"] = tsidx

df["market"] = market
df["granularity"] = granularity
df["granularity"] = granularity.to_integer

# re-order columns
df = df[
Expand Down
61 changes: 33 additions & 28 deletions pycryptobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,14 +1040,6 @@ def executeJob(
# if live
if _app.isLive():
if not _app.insufficientfunds:
_app.notifyTelegram(
_app.getMarket()
+ " ("
+ _app.printGranularity()
+ ") BUY at "
+ price_text
)

if not _app.isVerbose():
if not _app.isSimulation() or (
_app.isSimulation() and not _app.simResultOnly()
Expand All @@ -1060,12 +1052,13 @@ def executeJob(
text_box.center("*** Executing LIVE Buy Order ***")
text_box.singleLine()

account.basebalance = float(
account.getBalance(_app.getBaseCurrency())
)
account.quotebalance = float(
account.getBalance(_app.getQuoteCurrency())
)
ac = account.getBalance()

df_base = ac[ac["currency"] == _app.getBaseCurrency()]["available"]
account.basebalance = 0.0 if len(df_base) == 0 else float(truncate(float(ac[ac["currency"] == _app.getBaseCurrency()]["available"].values[0])))

df_quote = ac[ac["currency"] == _app.getQuoteCurrency()]["available"]
account.quotebalance = 0.0 if len(df_quote) == 0 else float(truncate(float(ac[ac["currency"] == _app.getQuoteCurrency()]["available"].values[0])))

# display balances
Logger.info(
Expand All @@ -1088,21 +1081,33 @@ def executeJob(
resp = _app.marketBuy(
_app.getMarket(), _state.last_buy_size, _app.getBuyPercent()
)
# Logger.debug(resp)
if not resp.empty:
_app.notifyTelegram(
_app.getMarket()
+ " ("
+ _app.printGranularity()
+ ") BUY at "
+ price_text
)
# Logger.debug(resp)

# display balances
account.basebalance = float(
account.getBalance(_app.getBaseCurrency())
)
account.quotebalance = float(
account.getBalance(_app.getQuoteCurrency())
)
Logger.info(
f"{_app.getBaseCurrency()} balance after order: {str(account.basebalance)}"
)
Logger.info(
f"{_app.getQuoteCurrency()} balance after order: {str(account.quotebalance)}"
)
# display balances
ac = account.getBalance()

df_base = ac[ac["currency"] == _app.getBaseCurrency()]["available"]
account.basebalance = 0.0 if len(df_base) == 0 else float(truncate(float(df[df["currency"] == _app.getBaseCurrency()]["available"].values[0])))

df_quote = ac[ac["currency"] == _app.getQuoteCurrency()]["available"]
account.quotebalance = 0.0 if len(df_quote) == 0 else float(truncate(float(df[df["currency"] == _app.getQuoteCurrency()]["available"].values[0])))

Logger.info(
f"{_app.getBaseCurrency()} balance after order: {str(account.basebalance)}"
)
Logger.info(
f"{_app.getQuoteCurrency()} balance after order: {str(account.quotebalance)}"
)
else:
Logger.warning("Unable to place order")
else:
Logger.warning("Unable to place order, insufficient funds")
# if not live
Expand Down
8 changes: 4 additions & 4 deletions scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from models.exchange.Granularity import Granularity
from models.exchange.ExchangesEnum import Exchange

GRANULARITY = ""
GRANULARITY = Granularity(Granularity.ONE_HOUR)
try:
with open("scanner.json", encoding='utf8') as json_file:
config = json.load(json_file)
Expand All @@ -24,13 +24,13 @@
for quote in config[ex.value]["quote_currency"]:
if ex == Exchange.BINANCE:
api = BPublicAPI()
GRANULARITY = Granularity.convert_to_enum(3600).to_short
# GRANULARITY = Granularity.convert_to_enum(3600).to_short
elif ex == Exchange.COINBASEPRO:
api = CPublicAPI()
GRANULARITY = Granularity.convert_to_enum(3600).to_integer
# GRANULARITY = Granularity.convert_to_enum(3600).to_integer
elif ex == Exchange.KUCOIN:
api = KPublicAPI()
GRANULARITY = Granularity.convert_to_enum(3600).to_medium
# GRANULARITY = Granularity.convert_to_enum(3600).to_medium
else:
raise ValueError(f"Invalid exchange: {ex}")

Expand Down

0 comments on commit 9b9c978

Please sign in to comment.