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

8 years ago
'''
'''
from .trade import Trade
4 years ago
import numpy
8 years ago
class Executor(object):
'''
'''
def __init__(self, series, max_hold_bars):
8 years ago
'''
Constructor
'''
self.series = series
self.max_hold_bars = max_hold_bars
8 years ago
4 years ago
def execute(self, sig_vectors, long=True, stop=None, tp=None):
8 years ago
self.trades = []
4 years ago
8 years ago
in_trade = False
current_entry_price = None
bar_counter = 0
entry_bar = 0
4 years ago
stop_price = 0
tp_price = 0
8 years ago
4 years ago
for i in range(0, self.series.length()):
8 years ago
if not in_trade:
4 years ago
has_signal = sig_vectors[i]
8 years ago
4 years ago
if has_signal and i + 1 < self.series.length():
8 years ago
in_trade = True
current_entry_price = self.series.get_open(i + 1)
4 years ago
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
8 years ago
bar_counter = 0
else:
bar_counter += 1
4 years ago
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))
8 years ago
return self.trades