Cannot find out why my orders are rejected at tick by tick data frame
-
from future import (absolute_import, division, print_function, unicode_literals)
import sys, argparse
import os.path
import pandas as pd
import backtrader as bt
import datetime
import backtrader.analyzers as btanalyzers
from strategies import GoldenCross, BuyHold, KevStrategy
import argparse
import backtrader.feeds as btfeedsif name == 'main':
def parse_args(): parser = argparse.ArgumentParser( description='Resampling script down to tick data') parser.add_argument('--dataname', default='', required=False, help='File Data to Load') parser.add_argument('--timeframe', default='ticks', required=False, choices=['ticks', 'microseconds', 'seconds', 'minutes', 'daily', 'weekly', 'monthly'], help='Timeframe to resample to') parser.add_argument('--compression', default=1, required=False, type=int, help=('Compress n bars into 1')) return parser.parse_args() args = parse_args() cerebro = bt.Cerebro(stdstats=False, cheat_on_open=True) cerebro.addstrategy(KevStrategy) # a = np.zeros((156816, 36, 53806), dtype='uint8') modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join(modpath, '/Users/kevinlayani/PycharmProjects/Algo_trading_binance/data_mining/BTCUSDT-trades-2022-03-30.csv') data = bt.feeds.GenericCSVData(dataname=datapath, fromdate=datetime.datetime(2022, 3, 29), todate=datetime.datetime(2022, 4, 1), nullvalue=0.0, dtformat='%Y-%m-%d %H:%M:%S.%f', timeframe=bt.TimeFrame.Ticks, datetime=4, high=2, low=3, open=1, close=1, volume=2, openinterest=-1) # range=6, # Handy dictionary for the argument timeframe conversion tframes = dict( ticks=bt.TimeFrame.Ticks, microseconds=bt.TimeFrame.MicroSeconds, seconds=bt.TimeFrame.Seconds, minutes=bt.TimeFrame.Minutes, daily=bt.TimeFrame.Days, weekly=bt.TimeFrame.Weeks, monthly=bt.TimeFrame.Months) # Resample the data data = cerebro.resampledata(data, timeframe=tframes[args.timeframe], compression=args.compression) # add a writer cerebro.addwriter(bt.WriterFile, csv=True) cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(300000.0) # Add a FixedSize sizer according to the stake # cerebro.addsizer(bt.sizers.FixedSize, stake=300) # Set the commission - 0.1% ... divide by 100 to remove the % cerebro.broker.setcommission(commission=0.00075) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.plot(style='bar')
class KevStrategy(bt.Strategy):
# params = (('order_percentage', 0.95), ('ticker', 'BTC'))def log(self, txt, dt=None): # Logging function fot this strategy dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] data series self.dataclose = self.datas[0].close self.range = self.datas[0].volume # the volume parameter refer to the Range self.time_price = self.datas[0].datetime self.bar_executed = len(self) # To keep track of pending orders and buy price/commission self.size = 200 self.order = None self.buyprice = None self.buycomm = None self.current_scenario = 0 self.avgprice = 0 self.ATR = 0 self.next_up = 1.004 self.next_down = 0.996 self.total_qty = 0 # Variables for the up trend self.before_300_up = 0 self.prev_level = 0 self.ref_time_up = self.time_price self.prev_level = 0 self.stop_loss = 0 self.up_trend = 1.005 self.stop_size = 0.4 self.stop_size, self.stop_size = max(self.stop_size, 0.32), min(self.stop_size, 10) def check_up_trend(self, tm, price): if self.before_300_up == 0: self.before_300_up = (price+0.0) self.ref_time_up = (tm+0.0) self.stop_loss = self.before_300_up * (100 - self.stop_size) / 100 else: chg = timedelta(milliseconds=(tm - self.ref_time_up)*100) # .total_seconds() upt = (price - self.before_300_up) / self.before_300_up * 100 if price > self.before_300_up * self.up_trend and self.current_scenario != 8: print("UP trend detected at a rate of : ", upt, 'in price:', price, 'before_300_up:', self.before_300_up,'time:', self.time_price) self.prev_level = (self.current_scenario+0) self.stop_size = upt self.stop_loss = self.before_300_up * (1 + (self.stop_size * 0.8) / 100) self.current_scenario = 8 if self.current_scenario == 8 and price - price * ((self.stop_size * 0.2) / 100) > self.stop_loss: stop_loss = price - price * ((self.stop_size * 0.2) / 100) print("New high observed: Updating stop loss to: ", stop_loss) if chg >= timedelta(milliseconds=300*100) and not self.current_scenario == 8: print('No uptrend in last 300 seconds') self.before_300_up = 0 return """ def update_ATR(self): self.ATR = ((sum(self.range.get(ago=0, size=50))/50)/100)*2.5 self.next_up = (self.ATR + 1.0015) self.next_up, self.next_up = max(self.next_up, 1.0015), min(self.next_up, 1.5) self.next_down = (0.9985 - self.ATR) self.next_down, self.next_down = max(self.next_down, 0.94), min(self.next_down, 0.994) return""" """ def update_ATR(self): self.ATR = ((sum(self.dataclose.get(ago=0, size=20000))/20000)/100)*2 self.next_up = (self.ATR + 1.0015) self.next_up, self.next_up = max(self.next_up, 1.0015), min(self.next_up, 1.5) self.next_down = (0.9985 - self.ATR) self.next_down, self.next_down = max(self.next_down, 0.94), min(self.next_down, 0.994) return""" """class AverageTrueRange(bt.Strategy): def __init__(self): self.dataclose = self.datas[0].close self.ATR = (sum(self.dataclose(ago=0, size=20000))/20000)/100)*2 self.datahigh = self.datas[0].high self.datalow = self.datas[0].low def next(self): range_total = 0 for i in range(-13, 1): true_range = self.datahigh[i] - self.datalow[i] range_total += true_range ATR = range_total / 14 self.log(f'Close: {self.dataclose[0]:.2f}, ATR: {ATR:.4f}')""" 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 enough cash if order.status in [order.Completed]: if order.isbuy(): self.log( 'BUY EXECUTED, Size: %.2f, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.size, order.executed.price, order.executed.value, order.executed.comm,)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Size: %.2f, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.size, order.executed.price, order.executed.value, order.executed.comm, )) self.bar_executed = len(self) elif order.status in [order.Canceled, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') self.order = None return order.executed.comm def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) # self.log('Range, %.2f' % self.range[0]) # self.update_ATR() self.check_up_trend(self.time_price, self.dataclose[0]) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return # Check if we are in the market if self.current_scenario == 0: if not self.position: self.log('BUY CREATE, %.2f' % self.dataclose[0]) self.order = self.buy(size=self.size) if self.position: self.avgprice = self.dataclose[0] + 0.0 self.total_qty = self.size self.current_scenario = 1 print('avgprice: ', self.avgprice) print('current_scenario: 0 (bought)') # print(self.position) elif self.current_scenario == 1: if self.position and self.dataclose[0] >= self.avgprice * self.next_up: # (1 + ((self.next_up - 1) * 1.2)) self.log('SELL CREATE, %.2f' % self.dataclose[0]) print('current_scenario: 0 (soled)') self.current_scenario = 0 self.total_qty = 0 self.order = self.sell(size=self.size) self.before_300_up = 0 elif self.position and self.dataclose[0] <= self.avgprice * self.next_down: # (1 - ((1 - self.next_down) * 0.8)) self.log('BUY CREATE, %.2f' % self.dataclose[0]) self.order = self.buy(size=self.size) self.avgprice = ((self.dataclose[0] + 0.0) + self.avgprice)/2 self.current_scenario = 2 self.total_qty = self.size*2 print('avgprice: ',self.avgprice) print('current_scenario: 1 (bought)')
outputs:
2022-03-30, BUY CREATE, 47100.01
13250,BTCUSDT-trades-2022-03-30,26500,2022-03-30 23:59:59.999989,47100.01,0.02618,1233.0782618,47100.01,0.02618,0.0,BTCUSDT-trades-2022-03-30,26500,2022-03-30 23:59:59.999989,47100.01,0.02618,1233.0782618,47100.01,0.02618,0.0,KevStrategy,13250,738244.9999999999
2022-03-30, Order Canceled/Margin/Rejected
2022-03-30, Close, 47100.02
2022-03-30, BUY CREATE, 47100.02
13251,BTCUSDT-trades-2022-03-30,26502,2022-03-30 23:59:59.999989,47100.02,0.01358,639.6182716,47100.02,0.01358,0.0,BTCUSDT-trades-2022-03-30,26502,2022-03-30 23:59:59.999989,47100.02,0.01358,639.6182716,47100.02,0.01358,0.0,KevStrategy,13251,738244.9999999999
2022-03-30, Order Canceled/Margin/Rejected
2022-03-30, Close, 47100.01
2022-03-30, BUY CREATE, 47100.01 -
Thanks solved
-
@kevkev Do you mind sharing how it was solved?
-
@Pierre-Cilliers-0
I used the wrong method for that this one is the correct one:if name == 'main':
"""def parse_args(): parser = argparse.ArgumentParser( description='Resampling script down to tick data') parser.add_argument('--dataname', default='', required=False, help='File Data to Load') parser.add_argument('--timeframe', default='ticks', required=False, choices=['ticks', 'microseconds', 'seconds', 'minutes', 'daily', 'weekly', 'monthly'], help='Timeframe to resample to') parser.add_argument('--compression', default=1, required=False, type=int, help=('Compress n bars into 1')) return parser.parse_args() args = parse_args()""" cerebro = bt.Cerebro(stdstats=False) # cheat_on_open=True cerebro.addstrategy(KevStrategy) # a = np.zeros((156816, 36, 53806), dtype='uint8') modpath = os.path.dirname(os.path.abspath(sys.argv[0])) datapath = os.path.join(modpath, '/Users//PycharmProjects/Algo_trading_binance/data_mining/BTCUSDT_trades_2022_03_300.csv') # modpath1 = os.path.dirname(os.path.abspath(sys.argv[0])) # datapath1 = os.path.join(modpath1, '/Users/kevinlayani/PycharmProjects/Algo_trading_binance/data_mining/2022_30minutes_BTCUSDT.csv') data = bt.feeds.GenericCSVData(dataname=datapath, fromdate=datetime.datetime(2022, 3, 29), todate=datetime.datetime(2022, 4, 1), nullvalue=0.0, dtformat='%Y-%m-%d %H:%M:%S.%f', tmformat='%H:%M:%S:%f', timeframe=bt.TimeFrame.Ticks, # compression=1, datetime=4, close=1, volume=2, openinterest=-1) """data2 = bt.feeds.GenericCSVData(dataname=datapath1, fromdate=datetime.datetime(2022, 3, 30), todate=datetime.datetime(2022, 3, 31), nullvalue=0.0, dtformat='%Y-%m-%d %H:%M:%S', tmformat='%H:%M:%S', # timeframe=bt.TimeFrame.Ticks, compression=1, datetime=0, open=1, high=2, low=3, close=4, volume=5, openinterest=-1)""" cerebro.adddata(data, name='data') #cerebro.resampledata(data2, timeframe=bt.TimeFrame.Minutes, name='mins') # Set our desired cash start cerebro.broker.setcash(300000.0) # Add a FixedSize sizer according to the stake # cerebro.addsizer(bt.sizers.FixedSize, stake=300) # Set the commission - 0.1% ... divide by 100 to remove the % cerebro.broker.setcommission(commission=0.00075) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.plot()
-
@Pierre-Cilliers-0 don't pay attention to the sections that are unmarked
-
@kevkev Great, thanks man