You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

74 lines
2.8 KiB

'''
'''
from .trade import Trade
import numpy
class Executor(object):
'''
'''
def __init__(self, series, max_hold_bars):
'''
Constructor
'''
self.series = series
self.max_hold_bars = max_hold_bars
def execute(self, sig_vectors, long=True, stop=None, tp=None):
self.trades = []
in_trade = False
current_entry_price = None
bar_counter = 0
entry_bar = 0
stop_price = 0
tp_price = 0
for i in range(0, self.series.length()):
if not in_trade:
has_signal = sig_vectors[i]
if has_signal and i + 1 < self.series.length():
in_trade = True
current_entry_price = self.series.get_open(i + 1)
if stop is not None:
if long:
stop_price = current_entry_price * (1 - stop)
else:
stop_price = current_entry_price * (1 + stop)
if tp is not None:
if long:
tp_price = current_entry_price * (1 + tp)
else:
tp_price = current_entry_price * (1 - tp)
entry_bar = i + 1
bar_counter = 0
else:
bar_counter += 1
if long:
if stop is not None and self.series.get_low(i) < stop_price:
self.trades.append(Trade(current_entry_price, stop_price, entry_bar, i, Trade.LONG))
in_trade = False
elif tp is not None and self.series.get_high(i) > tp_price:
self.trades.append(Trade(current_entry_price, tp_price, entry_bar, i, Trade.LONG))
in_trade = False
else:
if stop is not None and self.series.get_high(i) > stop_price:
self.trades.append(Trade(current_entry_price, stop_price, entry_bar, i, Trade.SHORT))
in_trade = False
elif tp is not None and self.series.get_low(i) < tp_price:
self.trades.append(Trade(current_entry_price, tp_price, entry_bar, i, Trade.SHORT))
in_trade = False
if in_trade:
if bar_counter >= self.max_hold_bars:
in_trade = False
if long:
trade_dir = Trade.LONG
else:
trade_dir = Trade.SHORT
self.trades.append(Trade(current_entry_price, self.series.get_close(i), entry_bar, i, trade_dir))
return self.trades