index ( retscumprod(first_loc max(index notnull(argmax( index values[first_loc return index def trend_signal(retslookbacklag)signal pd rolling_sum(retslookbackmin_periods=lookback return signal shift(lagusing this functionwe can (naivelycreate and test trading strategy that trades this momentum signal every fridayin [ ]signal trend_signal(returns in [ ]trade_friday signal resample(' -fri'resample(' 'fill_method='ffill'in [ ]trade_rets trade_friday shift( returns we can then convert the strategy returns to return index and plot them (see figure - )in [ ]to_index(trade_retsplot(figure - spy momentum strategy return index suppose you wanted to decompose the strategy performance into more and less volatile periods of trading trailing one-year annualized standard deviation is simple measure of volatilityand we can compute sharpe ratios to assess the reward-to-risk ratio in various volatility regimesvol pd rolling_std(returns min_periods= np sqrt( financial and economic data applications www it-ebooks info