Replace is_reduced and is_increased properties with increased_count and reduced_count
This commit is contained in:
@@ -29,6 +29,9 @@ class Strategy(ABC):
|
||||
self.index = 0
|
||||
self.vars = {}
|
||||
|
||||
self.increased_count = 0
|
||||
self.reduced_count = 0
|
||||
|
||||
self.buy = None
|
||||
self._buy = None
|
||||
self.sell = None
|
||||
@@ -47,7 +50,6 @@ class Strategy(ABC):
|
||||
self.trade = None
|
||||
self.trades_count = 0
|
||||
|
||||
self._initial_qty = None
|
||||
self._is_executing = False
|
||||
self._is_initiated = False
|
||||
|
||||
@@ -69,24 +71,6 @@ class Strategy(ABC):
|
||||
for dna in self.hyperparameters():
|
||||
self.hp[dna['name']] = dna['default']
|
||||
|
||||
@property
|
||||
def is_reduced(self):
|
||||
"""
|
||||
Has the size of position been reduced since it was opened
|
||||
:return: bool
|
||||
"""
|
||||
if self.position.is_close:
|
||||
return None
|
||||
|
||||
return self.position.qty < self._initial_qty
|
||||
|
||||
@property
|
||||
def is_increased(self):
|
||||
if self.position.is_close:
|
||||
return None
|
||||
|
||||
return self.position.qty > self._initial_qty
|
||||
|
||||
def _broadcast(self, msg: str):
|
||||
"""Broadcasts the event to all OTHER strategies
|
||||
|
||||
@@ -403,7 +387,8 @@ class Strategy(ABC):
|
||||
self._stop_loss_orders = []
|
||||
self._take_profit_orders = []
|
||||
|
||||
self._initial_qty = None
|
||||
self.increased_count = 0
|
||||
self.reduced_count = 0
|
||||
|
||||
def on_cancel(self):
|
||||
"""
|
||||
@@ -668,6 +653,8 @@ class Strategy(ABC):
|
||||
self._execute_short()
|
||||
|
||||
def _on_open_position(self, order: Order):
|
||||
self.increased_count = 1
|
||||
|
||||
self._broadcast('route-open-position')
|
||||
|
||||
if self.take_profit is not None:
|
||||
@@ -729,7 +716,6 @@ class Strategy(ABC):
|
||||
)
|
||||
|
||||
self._open_position_orders = []
|
||||
self._initial_qty = self.position.qty
|
||||
self.on_open_position(order)
|
||||
self._detect_and_handle_entry_and_exit_modifications()
|
||||
|
||||
@@ -772,6 +758,8 @@ class Strategy(ABC):
|
||||
pass
|
||||
|
||||
def _on_increased_position(self, order: Order):
|
||||
self.increased_count += 1
|
||||
|
||||
self._open_position_orders = []
|
||||
|
||||
self._broadcast('route-increased-position')
|
||||
@@ -792,6 +780,8 @@ class Strategy(ABC):
|
||||
"""
|
||||
prepares for on_reduced_position() is implemented by user
|
||||
"""
|
||||
self.reduced_count += 1
|
||||
|
||||
self._open_position_orders = []
|
||||
|
||||
self._broadcast('route-reduced-position')
|
||||
|
||||
@@ -29,5 +29,5 @@ class Test13(Strategy):
|
||||
return []
|
||||
|
||||
def update_position(self):
|
||||
if self.is_reduced:
|
||||
if self.reduced_count > 0:
|
||||
self.take_profit = self.position.qty, 16
|
||||
|
||||
@@ -20,7 +20,7 @@ class Test14(Strategy):
|
||||
self.take_profit = qty, 13
|
||||
|
||||
def update_position(self):
|
||||
if self.is_reduced:
|
||||
if self.reduced_count > 0:
|
||||
self.stop_loss = self.position.qty, 4
|
||||
|
||||
def go_short(self):
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
from jesse.strategies import Strategy
|
||||
|
||||
|
||||
# test_is_increased
|
||||
class Test42(Strategy):
|
||||
def should_long(self) -> bool:
|
||||
return self.index == 0
|
||||
|
||||
def should_short(self) -> bool:
|
||||
return False
|
||||
|
||||
def go_long(self):
|
||||
self.buy = [
|
||||
(.5, 2),
|
||||
(.5, 4),
|
||||
]
|
||||
self.take_profit = [
|
||||
(.5, 6),
|
||||
(.5, 10),
|
||||
]
|
||||
|
||||
def update_position(self):
|
||||
if self.price < 4 and self.position.qty == .5:
|
||||
assert not self.is_increased
|
||||
assert not self.is_reduced
|
||||
|
||||
if self.position.qty == 1:
|
||||
assert self.is_increased
|
||||
assert not self.is_reduced
|
||||
|
||||
def go_short(self):
|
||||
pass
|
||||
|
||||
def should_cancel(self):
|
||||
return False
|
||||
@@ -1,31 +0,0 @@
|
||||
from jesse.strategies import Strategy
|
||||
|
||||
|
||||
# test_is_reduced
|
||||
class Test43(Strategy):
|
||||
def should_long(self) -> bool:
|
||||
return self.index == 0
|
||||
|
||||
def should_short(self) -> bool:
|
||||
return False
|
||||
|
||||
def go_long(self):
|
||||
self.buy = 1, 2
|
||||
self.take_profit = [
|
||||
(.5, 6),
|
||||
(.5, 10),
|
||||
]
|
||||
|
||||
def update_position(self):
|
||||
if self.position.qty == 1:
|
||||
assert not self.is_increased
|
||||
assert not self.is_reduced
|
||||
elif self.position.qty == .5:
|
||||
assert not self.is_increased
|
||||
assert self.is_reduced
|
||||
|
||||
def go_short(self):
|
||||
pass
|
||||
|
||||
def should_cancel(self):
|
||||
return False
|
||||
47
jesse/strategies/TestIncreasedAndReducedCount/__init__.py
Normal file
47
jesse/strategies/TestIncreasedAndReducedCount/__init__.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from jesse.strategies import Strategy
|
||||
|
||||
|
||||
# test_increaed_and_reduced_count
|
||||
class TestIncreasedAndReducedCount(Strategy):
|
||||
def should_long(self) -> bool:
|
||||
return self.index == 0
|
||||
|
||||
def should_short(self) -> bool:
|
||||
return False
|
||||
|
||||
def go_long(self):
|
||||
qty = 1
|
||||
self.buy = qty, self.price
|
||||
|
||||
def update_position(self):
|
||||
if self.position.qty == 1 and self.index == 1:
|
||||
assert self.reduced_count == 0
|
||||
|
||||
assert self.increased_count == 1
|
||||
# now increase position
|
||||
self.buy = 1, self.price
|
||||
|
||||
elif self.position.qty == 2:
|
||||
assert self.increased_count == 2
|
||||
|
||||
# reduce it by one
|
||||
self.take_profit = 0.5, self.price
|
||||
elif self.position.qty == 1.5:
|
||||
assert self.reduced_count == 1
|
||||
self.take_profit = 0.5, self.price
|
||||
else:
|
||||
assert self.reduced_count == 2
|
||||
|
||||
# close trade
|
||||
self.liquidate()
|
||||
|
||||
def prepare(self):
|
||||
if self.trades_count == 1:
|
||||
assert self.increased_count == 0
|
||||
assert self.reduced_count == 0
|
||||
|
||||
def go_short(self):
|
||||
pass
|
||||
|
||||
def should_cancel(self):
|
||||
return False
|
||||
@@ -218,15 +218,6 @@ def test_increasing_position_size_after_opening():
|
||||
assert t1.qty == 2
|
||||
assert t1.fee == 0
|
||||
|
||||
|
||||
def test_is_increased():
|
||||
single_route_backtest('Test42')
|
||||
|
||||
|
||||
def test_is_reduced():
|
||||
single_route_backtest('Test43')
|
||||
|
||||
|
||||
def test_is_smart_enough_to_open_positions_via_market_orders():
|
||||
set_up([
|
||||
(exchanges.SANDBOX, 'ETHUSDT', timeframes.MINUTE_1, 'Test05'),
|
||||
@@ -927,6 +918,10 @@ def test_has_active_entry_orders():
|
||||
single_route_backtest('TestHasEntryOrders')
|
||||
|
||||
|
||||
def test_increaed_and_reduced_count():
|
||||
single_route_backtest('TestIncreasedAndReducedCount')
|
||||
|
||||
|
||||
# def test_route_capital_isolation():
|
||||
# set_up(
|
||||
# [
|
||||
|
||||
Reference in New Issue
Block a user