Thanks. Nice to see you again on this blog, I was starting to think that you intended to abandon the project!
Latest posts made by lvaghi
-
RE: Problem trying to port Lowpass filter to backtrader... help!
-
RE: Wavelet bandpass indicator for backtrader
@bigdavediode Could you kindly suggest some introductory material about wawelets? That would help me in the effort of understanding how to use your code. Many thanks in advance, and thank you for sharing your work.
-
RE: How can I use Pyfolio with backtrader?
Hi Michael,
I see that nobody answered your question. So I think the following lines of coding
could be a starting point for you:# Add a strategy cerebro.addstrategy(TestStrategy,df_alloc) # Analyzer cerebro.addanalyzer(btanalyzers.SharpeRatio, _name='mysharpe') cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio') # Set our desired cash start cerebro.broker.setcash(initcash) cerebro.broker.setcommission(commission=0.0) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything strat = cerebro.run() #cerebro.plot(numfigs = 1,grid = True,iplot=True) # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) print('Sharpe Ratio:', strat[0].analyzers.mysharpe.get_analysis()) strat[0].analyzers.mysharpe.pprint() pyfoliozer = strat[0].analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() import pyfolio as pf pf.create_full_tear_sheet( returns, positions=positions, transactions=transactions, #gross_lev=gross_lev, live_start_date='2008-01-01', # This date is sample specific round_trips=True) pf.create_simple_tear_sheet(returns) pf.create_returns_tear_sheet(returns)
Of course you will have to install pyfolio! The instruction are here: https://quantopian.github.io/pyfolio/
Pyfolio will give you lots of information (not all of unquestionable utility). I think it is more important to
get first a good synthetic view, and for this I like (and strongly suggest) the one contributed by Richard O' Regan:
https://community.backtrader.com/topic/670/it-s-here-a-beta-you-can-use-right-now-essential-trade-statistics-all-in-one-place
Only after screening with this tool I would adventure into the fine tuning of a strategy with pyfolio.
Good luck... -
Problem trying to port Lowpass filter to backtrader... help!
I am trying to port to backtrader an indicator (Lowpass filter), but I am stuck.
Here you can see the code that works on the usual timeseries (those where
[0] is the index of the oldest value instead of the current value as is in the
backtrader's lines):#coding=utf-8 #LOWPASS ''' The lowpass filter is similar to a simple moving average, but with less delay. Each elemt of the lowpass (LP) series is a combination of the last three values of the input data (the current one ant the two preceding) and the last two values of the same LP series. ''' import matplotlib.pyplot as plt import pylab import random import numpy as np import pandas as pd import sys def squarewave(): wave = [] for i in range(1,500): if (i/100) %2 == 0: wave.append(10) else: wave.append(-10) return wave class LOWPASS(object): def series(self, l_data,period): a = 2.0 / (1 + period) LP = [] LP.append(l_data[0]) LP.append(l_data[0]) swNaN = 1 for x in range(2,len(l_data)): if np.isnan(l_data[x-2]): LP.append(np.nan) else: #print x if swNaN == 1: #initializes LP vith the first element (not NaN) of data series. swNaN = 0 LP[x-1] = LP[x-2] = l_data[x] LP.append((a - 0.25 * a * a) * l_data[x] + 0.5 * a * a * l_data[x-1] \ - (a - 0.75 * a * a) * l_data[x-2] \ + 2 * (1. - a) * LP[x-1] \ - (1. - a) * (1. - a) * LP[x-2]) return LP if __name__ == '__main__': mylist = squarewave() myLOWPASS = LOWPASS() #mylist=[10,20,30,40,50,60,70,80,90,100,101,102,103,104,105,106,107,108,109,110] #mylist= range(1,500,10) + range(501,1500,1) #mylist = [] #for i in range(0,1000): # mylist.append(random.randint(1,101)) # mylist.append(100*np.sin(i)) print mylist lowpassed = myLOWPASS.series(mylist,25) print "lowpassed: ", lowpassed df = pd.DataFrame() df['mylist']= mylist df['lowpass']=lowpassed print df df.plot.line() plt.show()
The problem I have when converting this code to backtrader is with the initialization of the LP line:
LP.append(l_data[0]) LP.append(l_data[0])
In backtrader I do not know how to set the first two elements of the LP line to the oldest value of the data
line: obviously my understanding of backtrader is too limited... any help will be appreciated.