For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
How to calculate drawdown
-
I have a simple strategy and just wanted to calculate its drawdown. Tried understanding the documentation but was still confused as to how to apply it to my code.
Code is below :
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Dec 22 22:35:06 2021 """ from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime # For datetime objects import backtrader as bt import backtrader.indicators as btind import backtrader.feeds as btfeeds from getPriceData_series import getOHLC ohlc1 = getOHLC('TCS.NS','6mo', '1d') data1 = bt.feeds.PandasData(dataname=ohlc1) ohlc2 = getOHLC('BRITANNIA.NS','6mo', '1d') data2 = bt.feeds.PandasData(dataname=ohlc1) cash = 30000 investment = 20000 qty = int(investment/ohlc1['Close'][-1]) class MyStrategy(bt.Strategy): params = ( ('exitbars', 5), ('qty', qty), ('brokerage', 0.0003), ('cash', cash), ('maperiod', 20) ) 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] dataseries self.dataclose = self.datas[0].close self.sma = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.maperiod) # self.sma = self.datas[0].SMA # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = 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 enough cash if order.status in [order.Completed]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Qty: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, self.params.qty, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Qty: %.2f, Comm %.2f' % (order.executed.price, self.params.qty, order.executed.comm)) self.sellprice = order.executed.price self.sellcomm = order.executed.comm 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 notify_trade(self, trade): if not trade.isclosed: return self.log('GROSS %.2f, P/L per stock %.2f, NET %.2f' % (trade.pnl, trade.pnl/self.params.qty, trade.pnlcomm)) def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % 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 not self.position: # Not yet ... we MIGHT BUY if ... if self.dataclose[0] > self.sma[0]: # BUY, BUY, BUY!!! (with default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.buy(size = self.params.qty) else: # Already in the market ... we might sell if self.dataclose[0] < self.sma[0]: # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose[0]) # Keep track of the created order to avoid a 2nd order self.order = self.sell(size = self.params.qty) if __name__ == '__main__': cerebro = bt.Cerebro() cerebro.addstrategy(MyStrategy) cerebro.adddata(data1) cerebro.adddata(data2) cerebro.broker.setcash(MyStrategy.params.cash) cerebro.broker.setcommission(commission=MyStrategy.params.brokerage) # Add a FixedSize sizer according to the stake # cerebro.addsizer(bt.sizers.FixedSize, stake=TestStrategy.params.qty) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
Can someone edit the code and help me understand it thanks!