Skip to content

Commit

Permalink
Merge pull request #17 from Pasta-fantasia/16-error-cant-create-stop-…
Browse files Browse the repository at this point in the history
…loss-grouped_amount_canceled_orders_and_new_trades

fix: [ERROR] Can't create stop loss grouped_amount_canceled_orders_an…
  • Loading branch information
fransimo committed Jul 9, 2024
2 parents d3b18b0 + c9f050d commit 5c4238e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.1.3
- fix: [ERROR] Can't create stop loss grouped_amount_canceled_orders_and_new_trades #16

# 0.1.2
- fix: [ERROR] Error creating market sell order: binance Account has insufficient balance for requested action. #14

Expand Down
27 changes: 20 additions & 7 deletions elena_basics/strategies/common_sl_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _cancel_active_orders_with_lower_stop_loss(self, new_stop_loss: float) -> fl
self._logger.error(f"Error canceling order: {order.id}.")
return total_amount_canceled_orders, canceled_orders

def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price: float, band_length: float,
def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price: float, base_free: float, band_length: float,
band_mult: float, band_low_pct: float, minimal_benefit_to_start_trailing: float,
min_price_to_start_trailing: float):
# TRAILING STOP LOGIC
Expand All @@ -144,14 +144,12 @@ def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price:
self._metrics_manager.gauge("stop_price", self.id, stop_price, ["indicator"])

if stop_price < new_stop_loss * 0.8:
self._logger.error(
f"price ({stop_price}) is too far from new_stop_loss({new_stop_loss}) it may happend on test envs.")
self._logger.error(f"price ({stop_price}) is too far from new_stop_loss({new_stop_loss}) it may happend on test envs.")
new_stop_loss = 0
stop_price = 0

if new_stop_loss > estimated_close_price:
self._logger.warning(
f"new_stop_loss ({new_stop_loss}) should be never higher than last_close({estimated_close_price})")
self._logger.warning(f"new_stop_loss ({new_stop_loss}) should be never higher than last_close({estimated_close_price})")
new_stop_loss = 0
stop_price = 0

Expand All @@ -169,8 +167,16 @@ def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price:
grouped_amount_canceled_orders_and_new_trades = total_amount_canceled_orders + new_trades_on_limit_amount

if grouped_amount_canceled_orders_and_new_trades >= self.limit_min_amount():
new_order = self.stop_loss(amount=grouped_amount_canceled_orders_and_new_trades, stop_price=new_stop_loss,
price=stop_price)
# verify balance
max_sell = min(grouped_amount_canceled_orders_and_new_trades, base_free)
if max_sell < grouped_amount_canceled_orders_and_new_trades:
sale_diff = grouped_amount_canceled_orders_and_new_trades - max_sell
self._logger.error(f"Stop loss amount is higher than balance. Balance: {max_sell} but trying to sell {grouped_amount_canceled_orders_and_new_trades}.")
grouped_amount_canceled_orders_and_new_trades = max_sell
else:
sale_diff = 0.0

new_order = self.stop_loss(amount=grouped_amount_canceled_orders_and_new_trades, stop_price=new_stop_loss,price=stop_price)

if new_order:
canceled_orders.append("new_grouped_order")
Expand All @@ -180,6 +186,13 @@ def manage_trailing_stop_losses(self, data: pd.DataFrame, estimated_close_price:
if trade.exit_order_id in canceled_orders:
trade.exit_order_id = new_order.id
trade.exit_price = new_stop_loss # not real until the stop loss really executes.
if sale_diff > 0:
if trade.size >= sale_diff:
self._logger.error(f"Trade ID: {trade.id}, size {trade.size} changed to {trade.size - sale_diff}")
trade.size = self.amount_to_precision(trade.size - sale_diff)
sale_diff = 0.0
if sale_diff > 0:
self._logger.error(f"After checking all trades {sale_diff} was higher than any trade... that's not a rounding problem.")
else:
# TODO
self._logger.error("Can't create stop loss grouped_amount_canceled_orders_and_new_trades ")
Expand Down
5 changes: 4 additions & 1 deletion elena_basics/strategies/dca_conditional_lr_trailing_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def next(self) -> BotStatus:
self._logger.error("Cannot get balance")
return

base_symbol = self.pair.base
base_free = balance.currencies[base_symbol].free

data_points = int(max(self.lr_buy_longitude,self.band_length) + 10) # make sure we ask the enough data for the indicator
data = self.read_candles(page_size=data_points)

Expand All @@ -86,6 +89,6 @@ def next(self) -> BotStatus:
self.buy_based_on_budget(balance, estimated_close_price, min_amount, min_cost, self.spend_on_order)

# TRAILING STOP LOGIC
self.manage_trailing_stop_losses(data, estimated_close_price, self.band_length, self.band_mult, self.band_low_pct, self.minimal_benefit_to_start_trailing, self.min_price_to_start_trailing)
self.manage_trailing_stop_losses(data, estimated_close_price, base_free, self.band_length, self.band_mult, self.band_low_pct, self.minimal_benefit_to_start_trailing, self.min_price_to_start_trailing)

return self.status
10 changes: 5 additions & 5 deletions elena_basics/strategies/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def next(self) -> BotStatus:
self._logger.error("Cannot get balance")
return

base_symbol = self.pair.base
base_free = balance.currencies[base_symbol].free

# https://github.com/twopirllc/pandas-ta/issues/523
# In this case, macd aborts further calculation since close.size < slow + signal - 1 (32 < 26 + 9 - 1 = 34)

Expand Down Expand Up @@ -158,15 +161,12 @@ def next(self) -> BotStatus:

if sell_size > 0:
# verify balance
base_symbol = self.pair.base
# poner una sell size mayor para forzar la lógica
base_free = balance.currencies[base_symbol].free
max_sell = min(sell_size, base_free)
if max_sell < sell_size:
sale_diff = sell_size - max_sell
trade = trades_to_close[0]
self._logger.error(f"Selling too much, balance is {max_sell} but trying to sell {sell_size}. Trade ID: {trade.id}, size {trade.size} changed to {trade.size - sale_diff}")
trade.size = trade.size - sale_diff
trade.size = self.amount_to_precision(trade.size - sale_diff)
sell_size = max_sell

for order_id in orders_to_cancel:
Expand Down Expand Up @@ -202,6 +202,6 @@ def next(self) -> BotStatus:


# TRAILING STOP LOGIC
self.manage_trailing_stop_losses(data, estimated_close_price, self.sell_band_length, self.sell_band_mult, self.sell_band_low_pct, self.minimal_benefit_to_start_trailing, self.min_price_to_start_trailing)
self.manage_trailing_stop_losses(data, estimated_close_price, base_free, self.sell_band_length, self.sell_band_mult, self.sell_band_low_pct, self.minimal_benefit_to_start_trailing, self.min_price_to_start_trailing)

return self.status
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = elena_basics
version = 0.1.2
version = 0.1.3
author = "L Software Foundation"
author_email = "fransimo@gmail.com, pjover@gmail.com"
description = Sample project to link L + CCTX + Backtesting.py
Expand Down

0 comments on commit 5c4238e

Please sign in to comment.