How to code a trading system that works with Backtrader?
-
Hi there
I am new to python, and programming. So, excuse naivety in my doubts
I have have implemented a class for a trading system that I learned recently. I have been trying to figure out how to use this class with backtrader to pass on the entry and exit signals.
The system is purely based on price action, and does not use any of the standard time series indicators.
The class methods calculates various attributes and related price points one bar at a time. I have used pandas DataFrame for holding the OHLC data, and fashioned a for loop to process the DataFeed (as BackTrader calls it).
All trading decisions are based on the attributes calculated.
Sample code:
class myStrategy(): def __init__(): #initialize parameters def resample(): #build the trading timeframe OHLC data from input price data def Attr1(): #calculate attribute1 def Attr2(): #calculate attribute2 #depends on Attr1 #. #. def Attr5(): #calculate attributeN #depends on Attr4(t-1), Attr5(t-1) #Attr4(t) also depends on Attr5(t-1) myObj = myStrategy() for idx,row in myObj.df.iterrows(): #call methods to calculate attributes # #print the relevant data
Thanks
-
@hlad
Only last two attributes are dependent on previous bar attributes. Rest are calculated from the current bar price data -
@hlad said in How to code a trading system that works with Backtrader?:
myObj = myStrategy() for idx,row in myObj.df.iterrows(): #call methods to calculate attributes # #print the relevant data
This is not how backtrader works. Your logic has to be implemented in
next
which is called with each bar. You'd probably want to follow the Docs - QuickstartIt will give you a skeleton to work with.
To add your dataframe instead of the CSV examples from the Quickstart use the
PandasData
data feed: Docs - Pandas DataFeed Example -
@backtrader said in How to code a trading system that works with Backtrader?:
next wh
As per my understanding:
- I will get the price data for current bar from backtrader class with following notations
self.dataopen[-1] , self.datahigh[-1], self.datalow[-1] and self.dataclose[-1]
please, confirm if this is correct
- I will have to repurpose my class functions to work within the
class myStrategy(bt.Strategy): ...
and decouple these functions from pandas data frame
- make trading decisions and call
self.sell() or self.buy()
How to specify the entry price?
-
@hlad said in How to code a trading system that works with Backtrader?:
please, confirm if this is correct
Please read: Docs - Platform Concepts, with emphasis in the section:
Indexing: 0 and -1
And yes, you have to re-purpose if you want to use backtrader. If you want to use
buy
/sell
/close
or other family of methods like Docs - Target Orders (order_target_xxx
) is up to you. -
@backtrader said in How to code a trading system that works with Backtrader?:
And yes, you have to re-purpose if you want to use backtrader. If you want to use buy/sell/close or other family of methods like D
thanks
will work on it and get back if I am stuck again
-
earlier problem solved, removing it
-
Hi
if i have multiple symbols in a portfolio to be tested, I will be passing the symbols along with the strategy to
cerebro
.Does backtrader create separate objects for each symbol . Then process them separately barwise. Or it just has separate data feeds per symbol? which are used to calculate values of the indicators.
-
@hlad said in How to code a trading system that works with Backtrader?:
Does backtrader create separate objects for each symbol
Each data feed is an object. The question is probably meant to ask something entirely different.
-
Yes, my indicator / system stores the previous state, and uses it to arrive at decision for next trading day.
It's like
- Compare today and previous day for parameters
- Use today's OHLC to and parameters to get price levels for tomorrow's price action
- The current state and price action relative to price action tomorrow will decide the next state.
The current implementation stores this state in variable inside the Class object created for the Stock being tested.
And the process of calculating the state is the most time consuming part of the whole logic. Doing it afresh every iteration will slow down the backtest considerably@backtrader said in How to code a trading system that works with Backtrader?:
@hlad said in How to code a trading system that works with Backtrader?:
Does backtrader create separate objects for each symbol
Each data feed is an object. The question is probably meant to ask something entirely different.
Is each feed capable of storing such state variables?
-
@hlad said in How to code a trading system that works with Backtrader?:
Is each feed capable of storing such state variables?
Why shouldn't? The statement of problem is really unclear.
You can store things in additional lines defined for the data feed and you store and access the entire history. You can add your own attributes and store as many as you like in for example a
list
or just the last value in a simple instance attribute.