how to make an expert system based on backtrader?
-
First of all, thank you for providing such an awesome backtesting framework!
I want to make an expert system based on backtrader which has the following features:
- It can continuously monitor specified instruments bar data;
- It holds existing positions infomation;
- When the system has got new data, It will run the selected strategy to check enter signal and exit signal;
- If the enter signal has happened, I will enter position manually by a real trade, meanwhile add positions in expert system;
- If the exit signal has happened, I will exit position manually by a real trade, meanwhile remove positions in expert system.
My idea is:
When I finish my daily work,I will run the expert sytem by the update-to-date data and place an order by the suggestions.
ps. My daily work is not about tradings.Can someone give me some advice on how to use backtrader APIs to make such an expert sytem?
Sorry for my poor english.
Thanks in advance!
-
Without entering into the details of the operational procedures laid out above (which somehow conflict with each other), the critical part of your idea is:
-
Position information
Execution is manual and no system has the chance to keep record of the execution. You input that in the expert to keep track of position information
It is the critical part because it is a complete development and you have to 1st create custom backtrader components to read the position back from the expert.
Rather than advice, it seems you may need some serious coding.
-
-
I was thinking about use of bt as an expert in real trading without connection to the broker. In case of end-of-day trading (and looks like this is your case) I would do the following -
- code the trading system in bt in the regular way
- setup output (to console, to the text file) the way that system print for you signal (BUY, SHORT, CLOSE etc) and position size
- run bt at the end of the day with updated stock data and start date same as real trading start date, get signals and position sizes
- perform signals by yourself next day on your brokerage account
- bt run on the next day close should give you equivalent state between bt and real brokerage
- further running bt every day with the start date as your real trading start date will give you more or less synchronized real and bt trading
-
@backtrader
Thank you for your suggestions.In regard to coding, I think I can achieve an expert system with an modified interactive brokers. For example, I modify IB Broker source code, redirect the positions-related operations to my expert system.
By a fake IB Broker as a link between bt and the expert system, I don't need to meddle with backtrader source code.
IMO, this is the easiest way to do this, but I am not quite sure.
Can you give me further suggestions?
Thank you!
-
Thank you for your reply, I have two questions:
-
If you can't open a position with the open price on next day( bt default algorithm), how do you notify BT of the new position informations?
-
How do you make bt to load the existing positions instead of the calculated ones when it restarted and backtested again?
-
-
I am not sure what did you mean under expert system additional to bt here
By a fake IB Broker as a link between bt and the expert system, I don't need to meddle with backtrader source code.
I believe the easiest way for end-of-day trading is to use bt as an expert system without any additions. bt guides - you follow. It generates signals and position size every night, and you process it - put orders to broker's account.
- If you can't open a position with the open price on next day( bt default algorithm), how do you notify BT of the new position informations?
Add an exception for that day in the bt strategy logic and it will skip it during next evening run. No position will be open in the bt.
- How do you make bt to load the existing positions instead of the calculated ones when it restarted and backtested again?
I don't see any need to get data from the broker's account. On each daily run bt will give you the same positions and signals it gave you on previous days. You just sync the position size to broker's account.
-
Summarizing:
-
@ab_trader is looking to just use the signals and doesn't care about the
cash
,value
andcommissions
the backtrader ecosystem can calculate.In that scenario there is no need to load the positions, because only the signals are important.
The broker is being used for backtesting purposes and not for trading (the real broker may not even be supported)
-
@phoenix.che is looking for the system to keep track of the current position even if the orders are manually passed on to the broker.
In this scenario, the guess is that the actual position may play a role in what the backtrader ecosystem decides in the next step
The 2nd scenario seems to be better served by something like:
- The broker can read existing positions from an on-line source to perform calculations
-
-
@backtrader if you don't mind I would correct your summary slightly:
@ab_trader is looking to use the signals and position sizes provided by bt, and mirror them to broker account by hands. bt will keep position sizes, cash, value and commissions.
In that scenario there is no need to load the positions, cause they are mirrored by trader and are the same in bt and at broker account. In case of the discrepancies bt strategy can be easily adjusted.
The broker is being used for signal and position sizes providing as well as for tracking of the cash and value (no need to support the real broker).
I believe this summary reflects my proposals better. :)
-
This post is deleted!