Access strategy data or set strategy parameters during live trading from another thread
-
Hi All,
Does backtrader have the ability to access strategy data or set strategy parameters during live trading? For example, if you wanted to change an indicator parameter without having to restart the live trading session or you wanted to access indicator data when ever you want it.
If not, does anyone have any recommendations for the best way to do this? I was thinking sockets? Any other suggestions?
-
It's probably not easy to block the
bt
internal loop and wait for an input. You will have to fetch the parameter from outside the strategy.sockets
are too low level. If you generate the parameters in another python script and want to serve it,zeromq
is a better alternative.If you update the parameters manually, just read from a
file
, or, maybe fancier,redis
. -
@yeahaa828 said in Access strategy data or set strategy parameters during live trading from another thread:
If not, does anyone have any recommendations for the best way to do this? I was thinking sockets? Any other suggestions?
If you are accessing from another thread as the title indicates, there is no need to have a provision for anything. You already have access to the strategy.
How you receive your signal (a socket as you mention) to trigger the change is not relevant. What you need is to make sure that your "parameter-changing" code executes without the main thread code taking over (i.e.: the thread scheduler switches amongst threads)
The easiest way is a lock which will be acquired by each of the threads, making sure that until the lock is released, no other thread will be able to execute anything.
-
Hi All,
Thanks for everyone's input, if anyone's is interested I ended up parsing a queue to my strategy and running it in a separate thread. I used different events inside the queue to trigger inputs and outputs from the strategy while trading live. I could then use the data to display in the main thread which in my case a web application using Flask. It worked well!
Cheers.
-
@yeahaa828 I just came across this post. I’m also trying to change parameters for a live trading cerebro instance. Would you mind sharing a code snippet of how you send messages to your strategy’s queue from the other thread?
-
@yeahaa828 If you can share the code to that flask app, that would be really useful