Hello , When i add the risk management function to my algorithm ,it seems that it s not working as the report doesn't recognize the risk control.
Can someone help me on this please ?
from Risk.TrailingStopRiskManagementModel import TrailingStopRiskManagementModel
import numpy as np
import pandas as pd
import datetime
class NadionResistanceAutosequencers(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 1, 1)
self.SetEndDate(2020,1,1)# Set Start Date
self.SetCash(100000)
self.Security=["LMT","MSFT","GM","MS","FB","NKE","AAL","TSLA","MS","GS","AMZN","T","BA","BLK","EA","C","KO","AMD","MMM","AXP","F","HON","SPY"]
for s in self.Security:
self.AddEquity(s,Resolution.Daily)
self.SetRiskManagement(TrailingStopRiskManagementModel(0.03))
self.SetExecution(ImmediateExecutionModel())
# Set Strategy Cash
# self.AddEquity("SPY", Resolution.Minute)
def OnData(self,data):
S=["LMT","MSFT","GM","MS","FB","NKE","AAL","TSLA","MS","GS","AMZN","T","BA","BLK","EA","C","KO","AMD","MMM","AXP","F","HON","SPY"]
M=self.History(S,360,Resolution.Daily)
M=M.close
V=M.unstack(level=0)
Daily_returns=np.log(V/V.shift(1))
Daily_returns_mean = Daily_returns.mean()
SPY_index_return = Daily_returns_mean[-1]
SPY_index_Var = Daily_returns.var()[-1]
covariance_index_stock = Daily_returns.cov()
Beta = covariance_index_stock.iloc[:,-4]/SPY_index_Var
Rcapm = 0.02 + Beta * (SPY_index_return-0.02)
SR = (Rcapm-0.02)/SPY_index_Var
for i in range(len(S)-1):
if SR[i]<0:
self.SetHoldings(S[i],0.10)
else :
return
for i in range(len(S)-1):
if SR[i]>0:
self.SetHoldings(S[i],-0.10)
else:
return
Rahul Chowdhury
Hey Wawes,
This risk management model seems to be working normally here. One way to notice this is by checking the difference between a backtest with the risk management model enabled and a backtest without it enabled.
If that hasn't answered your question, please let me know.
Wawes23
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!