@tw00000 no I did not try much...
Posts made by Benoît Zuber
RE: Where is position opening date stored?
Thanks! In fact I would like to check the opening date of a few specific positions after a cerebro run finished. I can make a dictionary containing the last opening date for each position within the strategy but I was wondering if there was a built-in function.
position sizing when you have short positions in your portfolio
I have a conceptual question regarding position sizing. So far I have not been margin trading. I have calculated new position sizing as a function of my account value. Namely I have sized each position to risk 0.2 % of my account value.
Now I want to add shorting to my strategy. I am not too sure how to calculate new position sizing if I have some short positions in my account. Let us take an example.
- I start with $1000 cash. My account value is therefore $1000.
- Then I get a short position for $400 of asset A.
My new account value is $600, and my new cash value is $1400.
Let us now consider that I want to get a long position of asset B. If I bought it before I shorted asset A, asset B position size would have been, say, $200.
If I bought i after I shorted asset A, asset B position size would have been 200*600/1000 = $120.
Do you guys think this is the right thing to do, or should I have bought in both cases for $200 of asset B? If you think the latter is better, how would you calculate position sizing? would you calculate it as a function of (account value + absolute value of all short postions) ?
allow cash to get negative
Is it possible to let broker trade even if there is not enough cash? Say, get_cash() reports $1000. My strategy says it should buy some asset for a value of $2000. Backtrader broker normal behaviour will be not to execute the order since the trade would cost more cash than available.
I would like to let the trade happen and allow cash to get negative (equivalent to having borrowed cash). Is this possible? Or should I determine that I have not enough cash, and add a certain amount of cash to my account?
I'd prefer the first option, since I don't want my account value to increase when I borrow. I'd rather like to keep track of my dept in account value. My position sizing indeed depends on account value and I don't want the strategy to start oversizing positions because it sees fresh cash coming and does not understand that this is borrowed money and that the net account value does not increase when money is borrowed.
RE: Anyone use backtrader to do live trading on Bitcoin exchange?
@søren-pallesen Directly from your favorite exchange, using the ccxt module, which is embedded in backtrader (see Ed Bartosh's branch, you'll find instructions above in this thread on how to install it).
Alternatively, you can use cryptocompare.com API. They keep the daily OHLCV of pretty much any cryptocurrency from the beginning. There you can specify if you want the volume-weighted average price across all listed exchanges, or if you prefer the price at a particular exchange.
Instructions on the API are available at https://min-api.cryptocompare.com/
A python wrapper for this API is available at https://github.com/stefs304/cryCompare
RE: progress bar / ETA during optimization
@backtrader Sorry I am still kind of lost. In my main function I understand that I should have
cerebro.optstrategy(MyStrategy, **param_range, printlog=False) cerebro.optcallbacks(my_callback) cerebro.run()
But where do I define my_callback? And what should it look like? Will it call some method of the strategies? If yes, which one?
I was trying to do the dumbest test, i.e. print("finished") after each strategy finishes, but I couldn't get it to work. How would you do that?
progress bar / ETA during optimization
While running a large optstrategy it would be useful to be get an ETA or to monitor the percentage of parameter combinations that have been completed so far. I have tried using tqdm package but it get confused by the multiprocesses. tqdm can deal with multiprocessing but it needs to be called by the function that distributes the jobs. I don't think that it is possible to do that without modifying cerebro code.
I also implemented a simple iteration counter in strategy stop method :
progress_i = 0 class MyStrat(bt.Strategy): . . . def stop(self): global progress_i progress_i += 1 if progress_i % 100 == 0: print("%d jobs finished" %progress_i) def optimize(): . . . cerebro.optstrategy(MyStrat, **parameter_ranges) optrets = cerebro.run() return optrets
The problem here is that if I have 48 processors, then I'll have 48 times (or maybe 47) outputs saying "100 jobs finished". I could tweak things so that progress_i is multiplied by the number of processes but I am not sure that I can specify that only one slave process prints out its process. This means I'll have 47 or 48 lines printed for each progress update, which is not very nice.
Has anybody any suggestion to make a somewhat prettier output?