Using tick data and timeseries data together
-
Hi,
I want to use simple strategy. I tried a lot ways but I can't.
Strategy must be use tick data (reading from csv), indicators must be use timeseries (resampled from tick) data
When latest price changed (on tick data):
if latest price (tick) >= indicator (ex: SMA(55)) latest bar close (based on timeseries (ex daily))
buy()
if latest price (tick) <= indicator (ex: SMA(20)) latest bar close (based on timeseries (ex daily))
sell()Could you help me?
Best,
ct
class TestStrategy(bt.Strategy): def log(self, txt, dt=None): dt = dt or self.datas[0].datetime.datetime(0) print('%s, %s' % (dt, txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # To keep track of pending orders self.order = None def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: # Buy/Sell order submitted/accepted to/by broker - Nothing to do return # Check if an order has been completed # Attention: broker could reject order if not enougth cash if order.status in [order.Completed]: if order.isbuy(): self.log('BUY EXECUTED, %.2f' % order.executed.price) elif order.issell(): self.log('SELL EXECUTED, %.2f' % order.executed.price) self.bar_executed = len(self) elif order.status in [order.Canceled, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') # Write down: no pending order self.order = None def next(self): up_value = <--- I need SMA(55) latest bar close down_value = <--- I need SMA(20) latest bar close self.log('Close, price: %.2f - up: %.2f' % (self.dataclose[0], up_value)) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return if self.position.size == 0 : # FLAT if self.dataclose[0] > up_value: self.log('BUY CREATE, %.2f' % self.dataclose[0]) self.order = self.buy() elif self.dataclose[0] < down_value: self.log('SELL CREATE, %.2f' % self.dataclose[0]) self.order = self.sell() if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Load the Data datapath = 'csv/df2_tick_reversed.csv' tick_data = btfeeds.GenericCSVData( dataname=datapath, timeframe=bt.TimeFrame.Ticks ) # cerebro.addindicator(TURTLE, period=10) # period_data = cerebro.resampledata(dataname=tick_data, timeframe=bt.TimeFrame.Days) # Add the Data Feed to Cerebro # cerebro.adddata(tick_data) cerebro.resampledata(dataname=tick_data, timeframe=bt.TimeFrame.Days) # cerebro.adddata(period_data) # Set our desired cash start cerebro.broker.setcash(100000.0) cerebro.broker.setcommission(commission=0.001) # 0.1% ... divide by 100 to remove the % # Add a strategy cerebro.addstrategy(TestStrategy) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() cerebro.plot(style='bar') # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
-
To simulate a raw tick data one can assign the value of the tick to the fields of the
OHLC
. SeeWith that in mind the value of
close
(for example) can be used as the tick value (And has to be added withaddata
)The 2nd stream, the resampled one, is created, as already present in the code, by issuing
resampledata
.The tick data would be in
data0
and the resampled indata1