Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trailing stop #769

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion octobot_trading/personal_data/orders/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,8 @@ def ensure_order_id(self):
self.order_id = order_util.generate_order_id()

def to_dict(self):
filled_price = self.filled_price if self.filled_price > 0 else self.origin_price
filled_price = self.filled_price if self.filled_price > 0 \
else self.origin_price if not self.origin_stop_price else self.origin_stop_price
return {
enums.ExchangeConstantsOrderColumns.ID.value: self.order_id,
enums.ExchangeConstantsOrderColumns.SYMBOL.value: self.symbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@


class TrailingStopLimitOrder(trailing_stop_order.TrailingStopOrder):
UNINITIALIZED_LIMIT_PRICE = -1

def __init__(self, trader, side=enums.TradeOrderSide.SELL, limit_price=UNINITIALIZED_LIMIT_PRICE):
super().__init__(trader, side)
def __init__(self, trader, side=enums.TradeOrderSide.SELL,
trailing_percent=trailing_stop_order.TrailingStopOrder.UNINITIALIZED_TRAILING_PERCENT
):
super().__init__(trader, side, trailing_percent)
self.order_type = enums.TraderOrderType.TRAILING_STOP_LIMIT
self.limit_price = limit_price

async def on_filled(self):
await trailing_stop_order.TrailingStopOrder.on_filled(self)
# TODO replace with chained order ?
await self.trader.create_artificial_order(enums.TraderOrderType.SELL_LIMIT
if self.side is enums.TradeOrderSide.SELL else enums.TraderOrderType.BUY_LIMIT,
self.symbol, self.origin_stop_price,
self.origin_quantity,
self.limit_price
if self.limit_price != self.UNINITIALIZED_LIMIT_PRICE else
self.origin_stop_price)
if not self.trader.simulate and self.is_self_managed():
# TODO replace with chained order ?
await self.trader.create_artificial_order(enums.TraderOrderType.SELL_LIMIT
if self.side is enums.TradeOrderSide.SELL
else enums.TraderOrderType.BUY_LIMIT,
self.symbol, self.origin_stop_price,
self.origin_quantity,
self.origin_stop_price)
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def set_trailing_percent(self, trailing_percent):
Set trailing percent and reset event and tasks
:param trailing_percent: the new trailing percent
"""
self.trailing_percent = trailing_percent
self.trailing_percent = decimal.Decimal(f"{abs(trailing_percent)}")
await self._reset_events(self.origin_price, self.creation_time)

async def _reset_events(self, new_price, new_price_time):
Expand All @@ -67,9 +67,10 @@ def _create_hit_events(self, price_events_manager, new_price, new_price_time):
:param price_events_manager: the price events manager to use
:param new_price: the new trailing price
"""
self.origin_stop_price = self._calculate_stop_price(new_price)
if self.trailing_stop_price_hit_event is None:
self.trailing_stop_price_hit_event = price_events_manager.new_event(
self._calculate_stop_price(new_price), new_price_time,
self.origin_stop_price, new_price_time,
self.side is enums.TradeOrderSide.BUY, self.allow_instant_fill)
if self.trailing_price_hit_event is None:
# don't allow instant fill since this event should only be triggered by next recent trades and prices
Expand Down Expand Up @@ -143,17 +144,24 @@ async def _on_price_hit(self):
f"replacing stop...")
await self._reset_events(decimal.Decimal(str(prices_manager.mark_price)), prices_manager.mark_price_set_time)

def on_fill_actions(self):
self.filled_price = self.origin_stop_price
self.filled_quantity = self.origin_quantity
self._update_total_cost()
order_class.Order.on_fill_actions(self)

async def on_filled(self):
"""
Create an artificial when trailing stop is filled
"""
await order_class.Order.on_filled(self)
# TODO replace with chained order ?
await self.trader.create_artificial_order(enums.TraderOrderType.SELL_MARKET
if self.side is enums.TradeOrderSide.SELL
else enums.TraderOrderType.BUY_MARKET,
self.symbol, self.origin_stop_price,
self.origin_quantity, self.origin_stop_price)
if not self.trader.simulate and self.is_self_managed():
# TODO replace with chained order ?
await self.trader.create_artificial_order(enums.TraderOrderType.SELL_MARKET
if self.side is enums.TradeOrderSide.SELL
else enums.TraderOrderType.BUY_MARKET,
self.symbol, self.origin_stop_price,
self.origin_quantity, self.origin_stop_price)

def _clear_event_and_tasks(self):
"""
Expand Down