No, it is not possible.
Posts made by backtrader
RE: whats the difference between a line delay and negative lookup
your thoughts please
Run a speed test and use the syntax which better suits you.
RE: How to execute a buy sell order in backtrader?
I made a simple moving average crossover stratergy to buy and sell AAPL stocks
Sorry, but you haven't.
class MyStrategy(bt.Strategy): def __init__(self): self.sma= bt.ind.SMA(period=5)
If you only have
1moving average, it is difficult to conceive that you have a moving average crossover strategy.
You will argue ... "it's a crossover nevertheless ... i was righ". No, you are not. Precision is important: you want to play with money at some point int time.
TradeAnalyzerresult shows that only two trades
Change to a moving average crossover.
def next(self): if self.sma>self.data.close: # current sma greater than current close price print('BUY CREATE, %.2f' % self.data.close) self.buy() elif self.sma<self.data.close: # current sma lesser than current close price print('SELL CREATE, %.2f' % self.data.close) self.sell()
You are not even reversing the position, you are at some point in time opening a position and simply closing it at some other point in time. You have
TradeAnalyzerwhich can only tell you what has happened and not why.
Do the following:
- Decide if you want a moving average crossover or another type of crossover.
- Learn about the
closemethod to close an open position or reverse the position using the appropriate size
- You may want to check the position size
notify_orderand log what the notifications tell you.
RE: How to feed a custom pandas dataframe in backtrader?
class PandasData(bt.feed.PandasData): linesoverride = True # discard usual OHLC structure # datetime must be present and last
In this case it is quite unclear why you would want to discard the
OHLCstructure when you apparently have one. This is for sure not code copied from anywhere. And it won't probably work. You have also copied the comment that says that "datetime must present and last", but don't define a
I am rather sure that you are not looking to override the preexisting datetime-OHLC-volume structure (i.e.: the predefined lines)
And see: Docs - Extending a Data Feed
I understand params is method's parameters but lines I have never come across this concept in programming
It's not a programming concept. It's a trading concept. You get all the
closeprices, plot them along the date axis and join them with a line ... and you have a
line. The same you have with any other component of a time series. Call it
arrayif that makes your life easier.
df = bt.feeds.PandasData(dataname=data) #df=PandasData(dataname=data) cerebro.adddata(df)
Furthermore if you define a custom class and you don't use it ... because you use the internal data feed.
AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'pct'
It will for sure never find your
class PandasData(bt.feed.PandasData): linesoverride = True # discard usual OHLC structure # datetime must be present and last lines = ('adj_close','pct','pct2','pct3') params = ( ('datetime', None), ('open', -1), ('high', -1), ('low', -1), ('close', -1), ('volume', -1), ('openinterest', None), ('adj_close', -1), ('pct', -1), ('pct2', -1), ('pct3', -1), )
Taking into account that you know the order in which your columns are in the dataframe, it seems rather strange to redefine the parameters and set all of them to
-1. The only reason to redefine the parameters would be to assign fixed values to them, to make sure you are locating the columns.