Skip to content

Commit

Permalink
fix trailingbuypcnt bug
Browse files Browse the repository at this point in the history
Fixed a problem that certain market conditions could cause the waiting price to keep resetting which will prevent a buy.
  • Loading branch information
chriskoepf committed Dec 19, 2021
1 parent 3dedf7d commit 7afead8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
44 changes: 21 additions & 23 deletions models/Strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,40 +489,38 @@ def checkTrailingBuy(self, app, state, price: float = 0.0):
# If buy signal, save the price and check if it decreases before buying.
trailing_buy_logtext = ""
waitpcnttext = ""
if self.state.trailing_buy == 0:
self.state.waiting_buy_price = price
if state.trailing_buy == 0:
state.waiting_buy_price = price
pricechange = 0
elif self.state.trailing_buy == 1 and self.state.waiting_buy_price > 0:
elif state.trailing_buy == 1 and state.waiting_buy_price > 0:
pricechange = (
(price - self.state.waiting_buy_price) / self.state.waiting_buy_price * 100
(price - state.waiting_buy_price) / state.waiting_buy_price * 100
)
if price <= self.state.waiting_buy_price:
self.state.waiting_buy_price = price
if price < state.waiting_buy_price:
state.waiting_buy_price = price
waitpcnttext += f"Price decreased - resetting wait price. "

waitpcnttext += f"** {self.app.getMarket()} - "
if pricechange < self.app.getTrailingBuyPcnt(): # get pcnt from config, if not, use 0%
self.state.action = "WAIT"
self.state.trailing_buy = 1
if self.app.getTrailingBuyPcnt() > 0:
trailing_buy_logtext = f" - Wait Chg: {_truncate(pricechange,2)}%/{self.app.getTrailingBuyPcnt()}%"
waitpcnttext += f"Waiting to buy until {self.state.waiting_buy_price} increases {self.app.getTrailingBuyPcnt()}% - change {_truncate(pricechange,2)}%"
waitpcnttext += f"** {app.getMarket()} - "
if pricechange < app.getTrailingBuyPcnt(): # get pcnt from config, if not, use 0%
state.action = "WAIT"
state.trailing_buy = 1
if app.getTrailingBuyPcnt() > 0:
trailing_buy_logtext = f" - Wait Chg: {_truncate(pricechange,2)}%/{app.getTrailingBuyPcnt()}%"
waitpcnttext += f"Waiting to buy until {state.waiting_buy_price} increases {app.getTrailingBuyPcnt()}% - change {_truncate(pricechange,2)}%"
else:
trailing_buy_logtext = f" - Wait Chg: {_truncate(pricechange,2)}%"
waitpcnttext += f"Waiting to buy until {self.state.waiting_buy_price} stops decreasing - change {_truncate(pricechange,2)}%"
waitpcnttext += f"Waiting to buy until {state.waiting_buy_price} stops decreasing - change {_truncate(pricechange,2)}%"
else:
self.state.trailing_buy = 0
trailing_buy_logtext = f" - Ready Chg: {_truncate(pricechange,2)}%/{self.app.getTrailingBuyPcnt()}%"
waitpcnttext += f"Ready to buy. {self.state.waiting_buy_price} change of {_truncate(pricechange,2)}% is above setting of {self.app.getTrailingBuyPcnt()}%"
self.state.waiting_buy_price = 0

if self.app.isVerbose() and (
not self.app.isSimulation()
or (self.app.isSimulation() and not self.app.simResultOnly())
trailing_buy_logtext = f" - Ready Chg: {_truncate(pricechange,2)}%/{app.getTrailingBuyPcnt()}%"
waitpcnttext += f"Ready to buy. {state.waiting_buy_price} change of {_truncate(pricechange,2)}% is above setting of {app.getTrailingBuyPcnt()}%"

if app.isVerbose() and (
not app.isSimulation()
or (app.isSimulation() and not app.simResultOnly())
):
Logger.info(waitpcnttext)

return self.state.action, self.state.trailing_buy, trailing_buy_logtext
return state.action, state.trailing_buy, trailing_buy_logtext


def getAction(self, app, price, dt):
Expand Down
3 changes: 1 addition & 2 deletions pycryptobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ def executeJob(
# handle overriding wait actions (e.g. do not sell if sell at loss disabled!, do not buy in bull if bull only)
if strategy.isWaitTrigger(_app, margin, goldencross):
_state.action = "WAIT"
_state.trailing_buy = 0
immediate_action = False

if _app.enableImmediateBuy():
Expand Down Expand Up @@ -1160,7 +1159,6 @@ def executeJob(
_state.last_buy_size,
_app.getBuyPercent(),
)
_state.trailing_buy = 0

now = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
_app.notifyTelegram(
Expand Down Expand Up @@ -1204,6 +1202,7 @@ def executeJob(
)
state.last_api_call_datetime -= timedelta(seconds=60)
telegram_bot.add_open_order()
_state.trailing_buy = 0
except:
Logger.warning("Unable to place order")
state.last_api_call_datetime -= timedelta(seconds=60)
Expand Down

0 comments on commit 7afead8

Please sign in to comment.