For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
why I take wrong value
-
I run below code and when I use sl and tp I take wrong value. can you help me. it give me a big value
my code:
import backtrader as bt import pandas as pd from datetime import datetime import math %matplotlib inline class firstStrategy(bt.Strategy): def __init__(self): # initializing rsi, slow and fast sma self.rsi = bt.indicators.RSI(self.data.close, period=21) self.fast_sma = bt.indicators.SMA(self.data.close, period=50) self.slow_sma = bt.indicators.SMA(self.data.close, period=100) self.crossup = bt.ind.CrossUp(self.fast_sma, self.slow_sma) self.sl_order, self.tp_order = None, None def log(self, txt, dt=None): dt = self.datas[0].datetime.date() print(f'{dt}, {txt}') def notify_trade(self, trade): if trade.isclosed: self.log(f'OPERATION RESULT --- Gross: {trade.pnl:.2f}, Net: {trade.pnlcomm:.2f} \n') def next(self): # process stop loss and take profit signals if self.position: # set stop loss and take profit prices # in case of trailing stops stop loss prices can be assigned based on current indicator value price_sl_long = self.position.price * 0.98 price_sl_short = self.position.price * 1.02 price_tp_long = self.position.price * 1.06 price_tp_short = self.position.price * 0.94 # cancel existing stop loss and take profit orders if self.sl_order: self.broker.cancel(self.sl_order) if self.tp_order: self.broker.cancel(self.tp_order) # check & update stop loss order sl_price = 0.0 if self.position.size > 0 and price_sl_long !=0: sl_price = price_sl_long if self.position.size < 0 and price_sl_short !=0: sl_price = price_sl_short if sl_price != 0.0: self.sl_order = self.order_target_value(target=0.0, exectype=bt.Order.Stop, price=sl_price) # check & update take profit order tp_price = 0.0 if self.position.size > 0 and price_tp_long !=0: tp_price = price_tp_long if self.position.size < 0 and price_tp_short !=0: tp_price = price_tp_short if tp_price != 0.0: self.tp_order = self.order_target_value(target=0.0, exectype=bt.Order.Limit, price=tp_price) if self.getposition(self.datas[0]).size>0: print(self.getposition(self.datas[0]).size) print(cerebro.broker.getvalue() ,'/',self.datas[0].open[0] ,': ',cerebro.broker.getvalue() / self.datas[0].open[0]) g=None if not self.position: if self.rsi > 30 and self.fast_sma > self.slow_sma: self.buy(size=g) else: if self.rsi < 70: self.sell(size=g) startcash = 100 cerebro = bt.Cerebro() cerebro.addstrategy(firstStrategy) # adding strategy in Cerebro engine df = pd.read_pickle("data_pairs_kucoin_future_1d.pkl").loc[15,'pair'] df['date'] = df['date'].astype(str) +' '+ df['time'].astype(str) df['date']= pd.to_datetime(df['date'], format='%d/%m/%Y %H:%M:%S') df.set_index('date', inplace=True) data = bt.feeds.PandasData(dataname=df) cerebro.adddata(data) cerebro.broker.setcommission(commission=0.002) cerebro.broker.setcash(startcash) cerebro.addsizer(bt.sizers.PercentSizer, percents=99) cerebro.run() portvalue = cerebro.broker.getvalue() pnl =((portvalue - startcash)/startcash)*100 print('Final Portfolio Value: ${}'.format(portvalue)) cerebro.plot()
and output
8.520526723470178 104.99950426026336 / 11.62 : 9.036101915685316 2021-08-28, OPERATION RESULT --- Gross: 5.09, Net: 4.68 2021-08-29, OPERATION RESULT --- Gross: -3.56, Net: -3.96 0.2627380781458939 100.65946181281458 / 11.806 : 8.526127546401371 2021-08-30, OPERATION RESULT --- Gross: -0.06, Net: -0.07 2021-08-31, OPERATION RESULT --- Gross: -0.06, Net: -0.07 2021-09-01, OPERATION RESULT --- Gross: -0.14, Net: -0.15 2021-09-02, OPERATION RESULT --- Gross: -0.47, Net: -0.49 2021-09-03, OPERATION RESULT --- Gross: 0.22, Net: 0.20 2021-09-05, OPERATION RESULT --- Gross: -0.14, Net: -0.17 2021-09-06, OPERATION RESULT --- Gross: -0.61, Net: -0.64 2021-09-07, OPERATION RESULT --- Gross: 0.45, Net: 0.42 2021-09-08, OPERATION RESULT --- Gross: 1.43, Net: 1.41 2021-09-09, OPERATION RESULT --- Gross: -0.21, Net: -0.23 2021-09-10, OPERATION RESULT --- Gross: 0.36, Net: 0.33 2021-09-11, OPERATION RESULT --- Gross: 0.45, Net: 0.43 2021-09-12, OPERATION RESULT --- Gross: -0.22, Net: -0.24 2021-09-13, OPERATION RESULT --- Gross: -0.69, Net: -0.72 2021-09-14, OPERATION RESULT --- Gross: -0.12, Net: -0.15 2021-09-14, OPERATION RESULT --- Gross: 0.00, Net: -0.02 7.528067370931027 114.82648303963997 / 13.23 : 8.679250418718063 2021-09-16, OPERATION RESULT --- Gross: 14.69, Net: 14.27 2021-09-17, OPERATION RESULT --- Gross: 6.86, Net: 6.41 2021-09-18, OPERATION RESULT --- Gross: 18.26, Net: 17.84 2021-09-19, OPERATION RESULT --- Gross: 5.67, Net: 5.30 2021-09-20, OPERATION RESULT --- Gross: 5.75, Net: 5.38 2021-09-21, OPERATION RESULT --- Gross: 13.32, Net: 12.99 2021-09-22, OPERATION RESULT --- Gross: -1.55, Net: -1.86 2021-09-22, OPERATION RESULT --- Gross: 0.00, Net: -0.28 15.14099665481633 162.43347071443745 / 10.507 : 15.459547988430328 2021-09-24, OPERATION RESULT --- Gross: -3.18, Net: -3.81 2021-09-25, OPERATION RESULT --- Gross: 14.51, Net: 13.89 2021-09-26, OPERATION RESULT --- Gross: -2.93, Net: -3.52 2021-09-26, OPERATION RESULT --- Gross: 6.28, Net: 5.71 15.017715059482354 151.68371442051077 / 11.396 : 13.310259250659069 2021-09-28, OPERATION RESULT --- Gross: -20.99, Net: -21.64 2021-09-29, OPERATION RESULT --- Gross: 9.01, Net: 8.43 2021-09-30, OPERATION RESULT --- Gross: -2.90, Net: -3.49 2021-10-01, OPERATION RESULT --- Gross: -9.97, Net: -10.56 2021-10-02, OPERATION RESULT --- Gross: -9.96, Net: -10.59 2021-10-03, OPERATION RESULT --- Gross: -3.25, Net: -3.91 2021-10-06, OPERATION RESULT --- Gross: -15.17, Net: -17.75 2021-10-07, OPERATION RESULT --- Gross: -13.07, Net: -15.71 2021-10-08, OPERATION RESULT --- Gross: -16.34, Net: -18.90 2021-10-10, OPERATION RESULT --- Gross: -25.91, Net: -31.14 Final Portfolio Value: $22541775390.541138
also plot