Skip to content

Commit

Permalink
FIX: region persistance
Browse files Browse the repository at this point in the history
  • Loading branch information
firebird631 committed Jul 5, 2019
1 parent 543c11a commit 1a6a865
Show file tree
Hide file tree
Showing 10 changed files with 957 additions and 1,261 deletions.
1,944 changes: 801 additions & 1,143 deletions siis.sublime-workspace

Large diffs are not rendered by default.

165 changes: 80 additions & 85 deletions strategy/cryptoalpha/castrategytrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ def process(self, timeframe, timestamp):

# defines a target
if not entry.tp:
tp = self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[2][-10] * 1.05 # 1.0618
tp = self.timeframes[TP_TIMEFRAME].pivotpoint.last_resistances[2] * 1.05

gain = (tp - entry.p) / entry.p
loss = (entry.p - entry.sl) / entry.p

if loss != 0 and gain / loss < 1.0:
if loss != 0 and (gain / loss < 1.0):
continue

# not enought potential profit
Expand All @@ -226,6 +226,14 @@ def process(self, timeframe, timestamp):

retained_entries.append(entry)

# TP 50% entry
# entry_50pc = StrategySignal(0, 0)
# entry_50pc.dup(entry)
# entry_50pc.tp = np.max(self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[0])#[-1]
# entry_50pc.ptp = 0.25

# retained_entries.append(entry_50pc)

#
# process exits signals
#
Expand Down Expand Up @@ -274,15 +282,15 @@ def process(self, timeframe, timestamp):
if not trade.is_active() or trade.is_closing() or trade.is_closed():
continue

close_exec_price = self.instrument.close_exec_price(trade.dir)

#
# stop-loss update
#

stop_loss = trade.sl

# ATR stop-loss
trade_parent_tf = self.parent_timeframe(trade.timeframe)

sl = self.timeframes[TP_TIMEFRAME].atr.stop_loss(trade.direction)
# if (not trade.sl or close_exec_price > trade.entry_price) and sl > stop_loss:
if sl > stop_loss:
Expand Down Expand Up @@ -310,92 +318,77 @@ def process(self, timeframe, timestamp):
# if level >= stop_loss:
# stop_loss = level

if stop_loss > trade.sl:
pass
# not higher than immediat support
# if stop_loss <= self.timeframes[TP_TIMEFRAME].pivotpoint.last_supports[2]:
# trade.sl = stop_loss

# if stop_loss <= self.timeframes[TP_TIMEFRAME].pivotpoint.last_supports[1]:
# trade.sl = stop_loss

## if stop_loss <= self.timeframes[TP_TIMEFRAME].pivotpoint.supports[0][-1]:
## trade.sl = stop_loss

# if stop_loss <= self.timeframes[TP_TIMEFRAME].pivotpoint.last_pivot:
# trade.sl = stop_loss

#else:
# trade.sl = stop_loss
# elif stop_loss <= self.timeframes[TP_TIMEFRAME].pivotpoint.last_supports[1]:
# trade.sl = stop_loss # self.timeframes[TP_TIMEFRAME].pivotpoint.last_supports[1]

# trade 24 & 31 (en mai 2019....)
update_tp = False
close_exec_price = self.instrument.close_exec_price(trade.dir)

if close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[2][-1]:
#Terminal.inst().info("Price above R2 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
if stop_loss < self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[1][-1]:
trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[1][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[1][-1]:
#Terminal.inst().info("Price above R1 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
if stop_loss < self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[0][-1]:
trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[0][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[1][-1]:
#Terminal.inst().info("Price above R0 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
if stop_loss < self.timeframes[TP_TIMEFRAME].pivotpoint.pivot[-1]:
trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.pivot[-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.pivot[-1]:
#Terminal.inst().info("Price above pivot %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
# if stop_loss < self.timeframes[TP_TIMEFRAME].pivotpoint.supports[0][-1]:
# trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.supports[0][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.supports[0][-1]:
#Terminal.inst().info("Price above S0 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
#if trade.sl < self.timeframes[TP_TIMEFRAME].pivotpoint.supports[1][-1]:
# trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.supports[1][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.supports[1][-1]:
#Terminal.inst().info("Price above S1 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
#if trade.sl < self.timeframes[TP_TIMEFRAME].pivotpoint.supports[2][-1]:
# trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.supports[2][-1]

#
# target update
#

tp = self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[2][-1] * 1.05 # 618

# enought potential profit (0.5% min target)
if (tp - close_exec_price) / close_exec_price > 0.005 and update_tp:
trade.tp = tp

#gain = (trade.tp - close_exec_price) / close_exec_price
#loss = (close_exec_price - trade.sl) / close_exec_price

gain = (trade.tp - trade.entry_price) / trade.entry_price
loss = (trade.entry_price - trade.sl) / trade.entry_price
if len(self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[2]) > 0:
update_tp = False

if close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[2][-1]:
#Terminal.inst().info("Price above R2 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
if stop_loss < self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[1][-1]:
trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[1][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[1][-1]:
#Terminal.inst().info("Price above R1 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
if stop_loss < self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[0][-1]:
trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[0][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[1][-1]:
#Terminal.inst().info("Price above R0 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
if stop_loss < self.timeframes[TP_TIMEFRAME].pivotpoint.pivot[-1]:
trade.sl = self.timeframes[TP_TIMEFRAME].pivotpoint.pivot[-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.pivot[-1]:
Terminal.inst().info("Price above pivot %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
#if stop_loss < self.timeframes[TP_TIMEFRAME].pivotpoint.supports[0][-1]:
# trade.sl = sl # self.timeframes[TP_TIMEFRAME].pivotpoint.supports[0][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.supports[0][-1]:
Terminal.inst().info("Price above S0 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
#if trade.sl < self.timeframes[TP_TIMEFRAME].pivotpoint.supports[1][-1]:
# trade.sl = sl #self.timeframes[TP_TIMEFRAME].pivotpoint.supports[1][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.supports[1][-1]:
Terminal.inst().info("Price above S1 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
#if trade.sl < self.timeframes[TP_TIMEFRAME].pivotpoint.supports[2][-1]:
# trade.sl = sl #self.timeframes[TP_TIMEFRAME].pivotpoint.supports[2][-1]

elif close_exec_price > self.timeframes[TP_TIMEFRAME].pivotpoint.supports[1][-2]:
Terminal.inst().info("Price above S2 %s %s" % (trade.id, self.instrument.market_id), view='content')
update_tp = True
#if close_exec_price < self.timeframes[TP_TIMEFRAME].pivotpoint.supports[1][-2]:
# trade.sl = sl #self.timeframes[TP_TIMEFRAME].pivotpoint.supports[2][-1]

#
# target update
#

tp = self.timeframes[TP_TIMEFRAME].pivotpoint.resistances[int(2*trade.partial_tp)][-1] * 1.05

# enought potential profit (0.5% min target)
if (tp - close_exec_price) / close_exec_price > 0.005 and update_tp:# and tp > trade.tp:
trade.tp = tp

#gain = (trade.tp - close_exec_price) / close_exec_price
#loss = (close_exec_price - trade.sl) / close_exec_price

gain = (trade.tp - trade.entry_price) / trade.entry_price
loss = (trade.entry_price - trade.sl) / trade.entry_price

pl = (close_exec_price - trade.entry_price) / trade.entry_price

# if loss != 0 and gain / loss < 1.0 or pl < -0.035: # and close_exec_price < trade.entry_price:
# @todo or at max loss config and as initial sl (worst value)
if pl < -0.035: # and close_exec_price < trade.entry_price:
# not enough chance to win on this trade, cut the loss
retained_exit = StrategySignal(TP_TIMEFRAME, timestamp)
retained_exit.signal = StrategySignal.SIGNAL_EXIT
retained_exit.dir = 1
retained_exit.p = close_exec_price

Terminal.inst().info("No more chance to win cut the trade %s %s %s" % (trade.id, self.instrument.market_id, gain / loss), view='content')

#
Expand All @@ -413,12 +406,12 @@ def process(self, timeframe, timestamp):

# retained long entry do the order entry signal
for entry in retained_entries:
self.process_entry(timestamp, entry.price, entry.tp, entry.sl, entry.timeframe)
self.process_entry(timestamp, entry.price, entry.tp, entry.sl, entry.timeframe, entry.partial_tp)

# streaming
self.stream()

def process_entry(self, timestamp, price, take_profit, stop_loss, timeframe):
def process_entry(self, timestamp, price, take_profit, stop_loss, timeframe, partial_tp):
trader = self.strategy.trader()
market = trader.market(self.instrument.market_id)

Expand Down Expand Up @@ -482,9 +475,9 @@ def process_entry(self, timestamp, price, take_profit, stop_loss, timeframe):
# do_order = False

# if self.trades and (self.trades[-1].dir == direction) and ((timestamp - self.trades[-1].entry_open_time) < self.trade_delay):
if self.trades and (self.trades[-1].dir == direction) and ((timestamp - self.trades[-1].entry_open_time) < timeframe):
# the same order occurs just after, ignore it
do_order = False
#if self.trades and (self.trades[-1].dir == direction) and ((timestamp - self.trades[-1].entry_open_time) < timeframe):
# # the same order occurs just after, ignore it
# do_order = False

self.unlock()

Expand All @@ -498,6 +491,8 @@ def process_entry(self, timestamp, price, take_profit, stop_loss, timeframe):
# the new trade must be in the trades list if the event comes before, and removed after only it failed
self.add_trade(trade)

trade.partial_tp = partial_tp

if trade.open(trader, self.instrument.market_id, direction, order_type, order_price, order_quantity, take_profit, stop_loss, order_leverage):
# notify
self.strategy.notify_order(trade.id, trade.dir, self.instrument.market_id, market.format_price(price),
Expand Down
15 changes: 6 additions & 9 deletions strategy/cryptoalpha/casuba.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,44 +640,41 @@ def process4(self, timestamp, last_timestamp, candles, prices, volumes):
elif self.rsi.last < 0.6: # initial: 0.6
level1_signal = 1

if bbawe > 0 and level1_signal >= 0:
if bbawe > 0 and level1_signal > 0:
signal = StrategySignal(self.tf, timestamp)
signal.signal = StrategySignal.SIGNAL_ENTRY
signal.dir = 1
signal.p = self.price.close[-1]

# Terminal.inst().info("Entry long %s %s" % (self.data.instrument.symbol, self.tf), view='content')

if self.tomdemark.c.tdst:
signal.sl = self.tomdemark.c.tdst

# Terminal.inst().info("Entry long %s %s" % (self.data.instrument.symbol, self.tf), view='content')

elif bbawe < 0 and level1_signal < 0:
# exit signal
signal = StrategySignal(self.tf, timestamp)
signal.signal = StrategySignal.SIGNAL_EXIT
signal.dir = 1
signal.p = self.price.close[-1]
# signal = None

if self.tomdemark:
self.tomdemark.compute(last_timestamp, self.price.timestamp, self.price.high, self.price.low, self.price.close)

if self.tomdemark.c.c >= 9 and self.tomdemark.c.d < 0 and level1_signal < 0:
if 0:#self.tomdemark.c.c >= 8 and self.tomdemark.c.d < 0 and (level1_signal < 0 or bbawe < 0):
# setup complete and trend change
signal = StrategySignal(self.tf, timestamp)
signal.signal = StrategySignal.SIGNAL_EXIT
signal.dir = 1
signal.p = self.price.close[-1]

# Terminal.inst().info("Exit long %s %s c8p-c9 (%s%s)" % (self.data.instrument.symbol, self.tf, self.tomdemark.c.c, 'p' if signal.p else ''), view='content')
signal = None

elif self.tomdemark.c.c >= 2 and self.tomdemark.c.d > 1:
elif 0:#2 <= self.tomdemark.c.c <= 5 and self.tomdemark.c.d > 0 and (level1_signal < 0):
# cancelation
signal = StrategySignal(self.tf, timestamp)
signal.signal = StrategySignal.SIGNAL_EXIT
signal.dir = 1
signal.p = self.price.close[-1]
Terminal.inst().info("Cancel long %s %s c8p-c9 (%s%s)" % (self.data.instrument.symbol, self.tf, self.tomdemark.c.c, 'p' if signal.p else ''), view='content')

return signal

Expand Down
16 changes: 8 additions & 8 deletions strategy/region/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def parameters(self):
return {
'label': "undefined",
'name': self.name(),
'id': self.id(),
'id': self.id,
'stage': self.stage_to_str(),
'direction': self.direction_to_str(),
'timeframe': self.timeframe_to_str(),
Expand All @@ -176,7 +176,7 @@ def dumps(self):
Override this method and add specific parameters for dumps parameters for persistance model.
"""
return {
'id': self.id(),
'id': self.id,
'type': self.name(),
'stage': self._stage, # "entry" if self._stage == Region.STAGE_ENTRY else "exit" if self._stage == Region.STAGE_EXIT else "both",
'direction': self._dir, # "long" if self._dir == Region.LONG else "short" if self._dir == Region.SHORT else "both",
Expand Down Expand Up @@ -279,7 +279,7 @@ def parameters(self):
return {
'label': "Range region",
'name': self.name(),
'id': self.id(),
'id': self.id,
'stage': self.stage_to_str(),
'direction': self.direction_to_str(),
'timeframe': self.timeframe_to_str(),
Expand All @@ -289,15 +289,15 @@ def parameters(self):
}

def dumps(self):
data = super.dumps()
data = super().dumps()

data['low'] = self._low
data['high'] = self._high

return data

def loads(self, data):
super.loads(data)
super().loads(data)

self._low = data.get('low', 0.0)
self._high = data.get('high', 0.0)
Expand Down Expand Up @@ -352,7 +352,7 @@ def parameters(self):
return {
'label': "Range region",
'name': self.name(),
'id': self.id(),
'id': self.id,
'stage': self.stage_to_str(),
'direction': self.direction_to_str(),
'timeframe': self.timeframe_to_str(),
Expand All @@ -364,7 +364,7 @@ def parameters(self):
}

def dumps(self):
data = super.dumps()
data = super().dumps()

data['low-a'] = self._low_a
data['high-a'] = self._high_a
Expand All @@ -375,7 +375,7 @@ def dumps(self):
return data

def loads(self, data):
super.loads(data)
super().loads(data)

self._low_a = data.get('low-a', 0.0)
self._high_a = data.get('high-a', 0.0)
Expand Down
6 changes: 6 additions & 0 deletions strategy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,17 @@ def terminate(self):
self._timestep_thread = None

for k, appl in self._appliances.items():
if not appl:
continue

# stop all workers
if appl.running:
appl.stop()

for k, appl in self._appliances.items():
if not appl:
continue

# join them
if appl.thread.is_alive():
appl.thread.join()
Expand Down
Loading

0 comments on commit 1a6a865

Please sign in to comment.