@run-out thank you very much
Best posts made by petehe
-
RE: how to inspect the data in the datafeed
Latest posts made by petehe
-
Question about Stop Loss Trading article
Hi All
I have a question regarding Stop Loss Trading article
https://www.backtrader.com/blog/posts/2018-02-01-stop-trading/stop-trading/
why do we need to have a parent child order to do stop loss?def next(self): if not self.position and self.crossup > 0: if self.buy_order: # something was pending self.cancel(self.buy_order) # not in the market and signal triggered if not self.p.buy_limit: self.buy_order = self.buy(transmit=False) else: price = self.data.close[0] * (1.0 - self.p.buy_limit) # transmit = False ... await child order before transmission self.buy_order = self.buy(price=price, exectype=bt.Order.Limit, transmit=False) # Setting parent=buy_order ... sends both together if not self.p.trail: stop_price = self.data.close[0] * (1.0 - self.p.stop_loss) self.sell(exectype=bt.Order.Stop, price=stop_price, parent=self.buy_order) else: self.sell(exectype=bt.Order.StopTrail, trailamount=self.p.trail, parent=self.buy_order)
I thought we could we can use something like below to do both buy and stop loss together
self.buy(size=1, exectype=bt.Order.StopTrail, trailamount=0.25)
-
RE: question about sample code mixing-timeframes
@petehe said in question about sample code mixing-timeframes:
it can't work out
should be I can't work out
-
question about sample code mixing-timeframes
I was reading the sample code below, it can't work out
- if pp is PivotPoint object, what is pp1 if pp1=pp(), what is pp1.s1 and pp.s1()?
- In cerebro.addstrategy(St, multi=args.multi) what is parameter multi?
class St(bt.Strategy): params = (('multi',True),) def __init__(self): self.pp = pp = btind.PivotPoint(self.data1) pp.plotinfo.plot = False # deactivate plotting if self.p.multi: pp1 = pp() # couple the entire indicators self.sellsignal = self.data0.close < pp1.s1 else: self.sellsignal = self.data0.close < pp.s1() def next(self): txt = ','.join( ['%04d' % len(self), '%04d' % len(self.data0), '%04d' % len(self.data1), self.data.datetime.date(0).isoformat(), '%.2f' % self.data0.close[0], '%.2f' % self.pp.s1[0], '%.2f' % self.sellsignal[0]]) print(txt) def runstrat(): args = parse_args() cerebro = bt.Cerebro() data = btfeeds.BacktraderCSVData(dataname=args.data) cerebro.adddata(data) cerebro.resampledata(data, timeframe=bt.TimeFrame.Months) cerebro.addstrategy(St, multi=args.multi) cerebro.run(stdstats=False, runonce=False) if args.plot: cerebro.plot(style='bar') def parse_args(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Sample for pivot point and cross plotting') parser.add_argument('--data', required=False, default='../../datas/2005-2006-day-001.txt', help='Data to be read in') parser.add_argument('--multi', required=False, action='store_true', help='Couple all lines of the indicator') parser.add_argument('--plot', required=False, action='store_true', help=('Plot the result')) return parser.parse_args() if __name__ == '__main__': runstrat()
-
RE: how to inspect the data in the datafeed
@run-out thank you very much
-
how to inspect the data in the datafeed
Hi All
I am new to backtrader. I used to build strategy with pandas and it was easy to check the data that you are back testing and verify the strategy is working correctly.
I am not sure what's the best way to inspect OHLC data in the data feeds, it is bit confusing for me.
Can someone shed some light on it?
-
RE: pyfolio issue need help
format it a bit better
if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() datapath = 'datas\\orcl-1995-2014.txt' data = bt.feeds.YahooFinanceCSVData( dataname=datapath, # # Do not pass values before this date fromdate=datetime.datetime(2000, 1, 1), # # Do not pass values before this date todate=datetime.datetime(2010, 12, 31), # # Do not pass values after this date reverse=False ) # Add the Data Feed to Cerebro cerebro.adddata(data) cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio') thestrats = cerebro.run() pyfstats = thestrats[0].analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfstats.get_pf_items() import pyfolio as pf pf.create_full_tear_sheet( returns, positions=positions, transactions=transactions, gross_lev=gross_lev, live_start_date='2000-01-01', # This date is sample specific round_trips=True )
-
pyfolio issue need help
I was doing a very simple test on pyfolio, but it gives me an error below,
Does anyone know how this can be fixed?
TypeError: create_full_tear_sheet() got an unexpected keyword argument 'gross_lev'
code block
if name == 'main':
# Create a cerebro entity
cerebro = bt.Cerebro()
datapath = 'datas\orcl-1995-2014.txt'
data = bt.feeds.YahooFinanceCSVData(
dataname=datapath,
# # Do not pass values before this date
fromdate=datetime.datetime(2000, 1, 1),
# # Do not pass values before this date
todate=datetime.datetime(2010, 12, 31),
# # Do not pass values after this date
reverse=False
)
# Add the Data Feed to Cerebro
cerebro.adddata(data)
cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
thestrats = cerebro.run()
pyfstats = thestrats[0].analyzers.getbyname('pyfolio')
returns, positions, transactions, gross_lev = pyfstats.get_pf_items()
import pyfolio as pf
pf.create_full_tear_sheet(
returns,
positions=positions,
transactions=transactions,
gross_lev=gross_lev,
live_start_date='2000-01-01', # This date is sample specific
round_trips=True
)
····# code block