@Sajil-Thamban said in I am new to backtrader and want to build a strategy, details below:
can the first signal be a sell signal (say if the first crossover is on the downside)
I the __init__ section include a variable set to None:
self.trades_occured = False
Then include this in the long section to reject the trade if it's the first crossover. The second crossover must be the opposite so this should work.
if not self.position and self.crossover == 1 and self.trades_occured:
Then once the first sell happens change self.trades_occured to True.
if not self.position and self.crossover == -1:
self.trades_occured = True
self.short_sell_order = self.sell_bracket(....
@gminorcoles I am just a user of the bt, not an owner. Sometimes I share pieces of the code. @backtrader is an owner.
if you want/can offer any enhancements, then just create pull request with that enhancement. @backtrader will check it and incorporate if it will be suitable from his point of view.
May I suggest as an additional option just zero-padding, symmetrization, or smooth padding out the short data feed to the length of the longest line? Then all the data will start at the same bar. At the end you can simply trim the output to match the actual individual line length if you wish.
@blonc said in Strategy Selection Pattern:
The concept I am trying to achieve is to have many strategies all being monitored by a master strategy. This way, the master strategy could be using a markov chain to monitor returns to turn on and off individual strategies based on the total returns of all strategies. It seems the best path is to , correct me if wrong, put the strategies logic into indicators and bring them into one master strategy.
@backtrader said in Strategy Selection Pattern:
Sub-strategies is not a pattern which does exist in backtrader. Using Indicator instances could do the trick.
Was also thinking about utilizing the concept of having a "master" strategy turning on and off "micro strategies", so I'm glad I found this post :)
I can think of a lot of use cases for using a master strategy that based on a wide overview of a market/s can control what micro strategies are suited to be used.
the idea of using indicators as sub-strategies seem brilliant (haven't tested it yet).
but from my understanding strategies can be better analyzed and give valuable insights. I hope we can wider the discussion just to explore more solutions (as @backtrader mentioned nested strategies are not supported)
one idea I have that I don't think break backtrader structure is:
declaring a 'master' that run first and based on whatever user logic it creates a 'signal dictionary' that is then passed to all other micro strategies. allowing them to decide if to run or not in next().
this way all strategies are kept in the same level of operation (not nested)
@scottz1 Answering my own question, I found how analyzers can easily access attributes in the strategy. This would allow me to avoid creating a duplicate ATR indicator in the analyzer, but I've run into a problem with this. If I use a buystop order to open a trade, the strategy doesn't know the fill price until notify_order. I therefore use strategy notify_order order.Completede to calculate a stop loss in ATR units and submit a stop loss order. My analyzer needs to know that stop loss measurement, but... Analyzer notify_trade is called before strategy notify_order. What would be a good design for this?
@ab_trader said in IndexError: array index out of range:
In the __init()__ indexing should be done with (), not with  brackets, ref Docs - Lines: DELAYED indexing:
Pointed out some days ago by @ab_trader
I'm learn to use backtrader ,and I'm trying to use the Oandatest.py with some problem. errormsg is the same with you.
I want to know how to run oandatest.py succeed, I have oanda account and token. and pip install oandapy.
but I can't run it succeed. please give me a hand. thanks very much.
So I can loop through all my data in the Next..
self.datas is an array and you can loop over it.
How do I plot the portfolio performance? instead of backtrader printing 1000 different charts...
Depends on what you have defined as performance for your portfolio. There is for example a TimeReturn observer which is intended to plot exactly what the name implies.
If you want to plot other performance indicator, you will need to define it as an indicator or observer
The default is to plot the evolution of the cash and the actual portfolio value which should give a quick indication as to where the system is
And only a single chart will be printed per strategy.
Is there an integrated solution to do this in Backtrader?
There are several samples, blog and documentation pages. See for example:
Docs - Benchmarking
Here the performance is defined as the comparison against another asset.