Lesson 7 of 15
Trend Following Returns
Trend Following Returns
Once we have a momentum signal, we can compute the strategy's returns. The idea is simple: trade in the direction of the signal, entering positions based on the prior day's signal.
Algorithm
For each day t from 1 to len(prices) - 1:
- Compute the daily return:
daily_ret = prices[t] / prices[t-1] - 1 - Read the signal from the previous day:
sig = signals[t-1] - Strategy return:
sig * daily_ret
This means:
- If the signal was
1(long), we earn the daily return. - If the signal was
-1(short), we earn the negative of the daily return. - If the signal was
0(flat), we earn nothing.
The result list has length len(prices) - 1.
Task
Implement trend_returns(prices, lookback) using the momentum signal from the previous lesson.
Python runtime loading...
Loading...
Click "Run" to execute your code.