Please refer to Section 3 Unit 14 of the Python for Trading Course
Both, in the video and text, they code Market Returns and Strategy Returns but they do not explain how to interpret results.
So, what do both plots mean? where are the buy/sell signals and where is the signal strength?
Thank you
Thanks for your query, Angel.
MARKET RETURN
So, market return along a timeline of multiple days is a product of the daily percentage changes in the price. For example, imagine these to be the returns of multiple days of a stock:
03-01-2017 0.005949 1 ( means bought on 02-01-2017 ) 04-01-2017 -0.000794 -1 ( means sold on 03-01-2017 ) 05-01-2017 0.003578 -1 06-01-2017 -0.003301 1 09-01-2017 0.000000 1 10-01-2017 0.002826 -1 11-01-2017 -0.002510 -1
The first day, 03-01-2017 has a percentage change of 0.005949 between the price of the day before 02-01-2017 and the day now. Similarly, the percentage change between the 03-01-2017 and 04-01-2017 is -0.000794. Now, what is the market return on 04-01-2017?
Suppose you have shares worth X on 02-01-2017. What will their value be on 03-01-2017? X * ( 1 + 0.005949 ) because 0.005949 is the percentage change. Now, what will be its value on the 04-01-2017? It will be X * ( 1 + 0.005949 ) * ( 1 + ( -0.000794 ) ) . This means that the percentage change of any day will also take into account the percentage changes of days before that.
Hence, the line in the code : ( df['returns'] + 1 ).cumprod(), simply takes a sequential product of the returns when 1 has been added to them as we did.
STRATEGY RETURN
Now that we're clear on what market return is let's now talk about strategy return.
So, now imagine a strategy signal such that for each day from 03-01-2017 to 11-01-2017 you have the option to either buy or sell. Where buy is encoded is 1 and sell is encoded as -1. And each signal represents the decision you should take on the day before the given day. Like 1 corresponding to 03-01-2017 means, you should buy it on 02-01-2017 and -1 corresponding to 10-01-2017 means you should have already sold it on 9-01-2017.
Now, let's see 4 scenarios of the strategy signal.
Scenario 1: Imagine a signal of +1 and a percentage increase of 0.005949 for the next day. +1 means buy. So buying on this day will make the value of your holding increase by a factor of ( 1 + 0.005949 )
Scenario 2: Imagine a signal of -1 and a percentage descrease of 0.005949 for the next day. -1 means sell. So selling on this day will make the value of your holding increase by a factor of ( 1 -1*(- 0.005949) ) = ( 1 + 0.005949 ). If you're wondering why, the reason is you sold it off when it dropped by 0.005949 percent. This means you're own holding, because they never reduced, value 0.005949 percent more than the market price that day.
Scenario 3: Imagine a signal of +1 and a percentage decrease of 0.005949 for the next day. +1 means buy. So buying on this day will make the value of your holding decrease by a factor of ( 1 - 0.005949 ). Why? Because you bought a day right before it was about to fall.
Scenario 4: Imagine a signal of -1 and a percentage increase of 0.005949 for the next day. -1 means sell. So selling on this day will make the value of your holding decrease by a factor of ( 1 - 0.005949 ). Why? Because you sold it off right before it was supposed to increase.
Hence the code,
df['stratergy_returns'] = df['signal'].shift(1) * df['returns']
Where we shift the signal to a day forward.
Then these changes are cumulated just like we did in the case of market returns,
( df['stratergy_returns'] + 1 ).cumprod()
THE GRAPHS
In https://quantra.quantinsti.com/startCourseDetails?cid=28§ion_no=3&unit_no=14 , the blue graph corresponds to this cumulated stratergy return and the red graph corresponds to the cumulated market return both of which we just calculated.
Do revert if more explanation is needed. Thanks.
This in response to:
"Thank you for your quick response.
------------------------------------------------------------------------------------------------------------------------------------------------
Thanks for getting back, Angel.
I assume you're talking about the ebook: https://quantra.quantinsti.com/startCourseDetails?cid=28§ion_no=3&unit_no=20
df.loc[ df['RSI'] > df['RSI_slow'] ,'Signal' ] = +1
df.loc[ df['RSI'] < df['RSI_slow'] ,'Signal' ] = -1