I'm trying to speed up my trading strategy backtesting.
Right now, I have
for i in trange(1, len(real_choice), disable=not backtesting, desc="Converting HOLDs and calculating backtest correct/incorrect... [3/3]"):
if (advice[i] == "HOLD"):
advice[i] = advice[i-1]
if (real_choice[i] == "HOLD"):
real_choice[i] = real_choice[i-1]
if advice[i] == real_choice[i]:
correct[i] = "CORRECT"
else:
correct[i] = "INCORRECT"
This part of the code takes the longest, so I want to speed it up.
I'm learning Python so this was simple and worked but now I'm paying for it with how long the backtests take.
Is there a way to do this faster?
you can use
np.where
to compare two columns and assign a value to those rowsbut to make it look more pandas it would be
with some time comparisons (Full Code)
method 2 took a shorter amount of time to compute