@run-out Noted. tks.
kian hong Tan
@kian hong Tan
Posts made by kian hong Tan
RE: ATR Trailing Stop Query
@run-out tks for the solution. The stop order can be cancelled now.
I'm still trying to figure out if I understand everything correctly. Let's say if I don't specify the manual bracket orders inside a list, the stop loss and take profit orders will be cancelled automatically once the position is closed, right?
ATR Trailing Stop Query
@run-out firstly, I want to thank you for sharing the code on updating the stop loss price. I have problem writing codes for ATR trailing stop until I saw your post. I have modified the codes to my needs and the ATR trailing stop works as expected. However, when I introduce another exit condition (self.b_crossdown) and the exit condition is met, the stop order is still outstanding. Hence when the price meets the stop order price, a position will be opened. How can I cancel the stop order? I have tried putting self.so =  to clear the stop order but it doesn't work. I have also tried to cancel the order by indicating self.cancel(self.so). But the below error message is shown. Hope you are able to guide me on this. tks.
def next(self): ''' Simply log the closing price of the series from the reference ''' # self.log('Close, %.2f' % self.dataclose) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return if not self.position: # not in the market self.so =  if self.buy_sig: #self.log('BUY CREATE, %.2f' % self.dataclose) self.buyprice = self.dataclose self.b_stop_price = self.buyprice - (self.atr * self.p.loss_atr_mult) self.b_profit_price = self.buyprice + (self.atr * self.p.profit_atr_mult) self.order = self.buy(transmit=False) self.so = [self.sell(exectype=bt.Order.Stop, price=self.b_stop_price, parent=self.order,transmit=False)] self.po = [self.sell(exectype=bt.Order.Limit, price=self.b_profit_price, parent=self.order,transmit=True)] else: # in the market if self.position.size > 0: close = self.dataclose b_stop_price = self.b_stop_price atrdist = self.atr * self.p.loss_atr_mult if len(self.so) > 0: # Update only if greater than self.b_stop_price = max(b_stop_price, close - atrdist) self.so.created.price = self.b_stop_price else: pass if self.position.size > 0 and self.b_crossdown: #self.log('EXIT BUY CREATE, %.2f' % self.dataclose) self.order = self.close()
RE: Extending Datafeeds GenericCSV Attribute Error
@vladisld thanks, it works now. I have also changed the date format. :)
Extending Datafeeds GenericCSV Attribute Error
Hi guys, I ran into similar error and I couldn't solve it despite checking all similar queries and read the documentation on Extending a Datafeed. I wanted to add VWAP to the data feed. I have also checked this out - https://community.backtrader.com/topic/1584/extending-a-datafeed/3 , still I dont know how aliasing works. Appreciate if anyone here can guide me on this. Many thanks.
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-6-7b3e612ced80> in <module> 328 params = (('vwap', 7),) 329 --> 330 data = bt.feeds.GenericCSV_w_VWAP( 331 dataname= os.path.join(directory,'BTCUSDT-15m.csv'), 332 AttributeError: module 'backtrader.feeds' has no attribute 'GenericCSV_w_VWAP'
if __name__ == '__main__': cerebro = bt.Cerebro() cerebro.addstrategy(HmaCross) class GenericCSV_w_VWAP(GenericCSVData): lines = ('vwap',) params = (('vwap', 7),) data = bt.feeds.GenericCSV_w_VWAP( dataname= os.path.join(directory,'BTCUSDT-15m.csv'), fromdate=datetime.datetime(2020, 7, 1), todate=datetime.datetime(2020, 9, 1), nullvalue=0.0, timeframe=bt.TimeFrame.Minutes, compression=15, dtformat=('%Y-%m-%d %H:%M:%S'), datetime=0, time=-1, high=2, low=3, open=1, close=4, volume=5, openinterest=-1, vwap=7 ) cerebro.adddata(data)
Source CSV Data:
RE: backtrader-plotting control panel doesn't show up in Jupyter notebook
@Dimasik007 Another user and me have this issue too. See this post - https://community.backtrader.com/topic/813/bokeh-integration-interactive-webbrowser-plotting/53 . You can try changing the toolbar location ("left/above/below") as suggested by @Yang-Fu .
b = Bokeh(style='bar', tabs='multi', scheme=Tradimo(), filename='test_bt_plot.html', toolbar_location='left')
I'm using jupyter notebook too. If you want the plot to be shown in a new browser you can try
RE: Queries on Drawdown Calculation
@ab_trader Both screenshots are results from bt. The only thing is that i manually calculate the peak, trough and drawdown after trade closure (1st screenshot), and use them to compare against the log in the 2nd screenshot (drawdown log from drawdown analyzer, only show till 2nd trade). So I wanna know if the equity (broker value) peaks and troughs in bt consider unrealized loss/drawdown in the calculation? tks.
RE: Queries on Drawdown Calculation
Hello @ab_trader , I'm trying to understand drawdown calculations and saw your response on similar query here - https://community.backtrader.com/topic/1270/drawdown-analysis-appears-to-be-incorrect .
So based on what you comment, our drawdown calculations are based on closed trade, without considering in-trade drawdown. In-trade drawdown refers to unrealized loss/drawdown, when the trade is still open. Is my understanding correct?