Course Name: Natural Language Processing in Trading, Section No: 5, Unit No: 1, Unit type: Notebook
shouldn’t we add shift(-1) to returns as we decide to take trade today in open using signal at that time and close it next day open. so for the trade taken today teh returns have to be comapred with open price of next day. shouldnt the code be written as : prices[‘strategy_return’] = prices[‘signal’].shift(1) * prices[‘return’].shift(-1)
Hi Ganesh
you’re absolutely right that the trade logic is:
Signal generated at today’s open (9:30 AM)
Entry at today’s open price
Exit at next day’s open price
So the return for that trade is = next day’s open / today’s open - 1.
So, pct_change() already computes: return[t] = (Open[t] - Open[t-1]) / Open[t-1]
Here return[t] is already today’s open relative to yesterday’s open. Which is exactly the exit-to-entry return for a trade entered yesterday.
That’s why signal.shift(1) is the right pairing.
enter at yesterday’s open, exit at today’s open.
If you used prices[‘return’].shift(-1) instead, you’d be double-shifting. Essentially saying enter today but exit two days later, which doesn’t match the intended logic.