Back

Stop Loss / Take Profit on Algorithm Framework

Hi everyone,

I am new to this whole algorithm framework and was just wondering whether it's possible to set a stop loss and take profit orders for my strategy? I know it's possible to set those orders using the classical algorithm but was just wondering for the algorithm framework, do I implement this within the Execution Model, Risk Management Model, or the Alpha Model?

Any help would be greatly appreciated! Thanks everyone.

Update Backtest







0

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.


Probably best to go in Risk Management in the Framework - see here for example on trailing stop losses. If they are simple fixed stop losses, you can just define some variable in your Alpha Model and do a if-then condition in the Risk Management model

0

Hi Adam, it looks like the referenced TrailingStop example only works for Long positions (quantity > 0) as it only uses the highs of a security and don't distinguish between long and short positions. 

@Ben Tay  If I'm right in my assumption, you should extend the referenced example so that it also works for short positions (if needed). 

0

edit: Ofir was so kind and has already provided a solution for that --> Long and Short Trailing Stop Loss

1

Thank you, Arthur Asenheimer and Adam W 

Maybe could you advise me how do I set fixed stop losses based on values at the time that the order was placed?

For example, take a Moving Average Crossover strategy when the fast MA crosses above the slow MA...

  • The Alpha Model would generate an Up insight
  • Would like the stop loss for this order to be placed at the slow MA and be fixed there
How do I go about doing so? 
0

You can do something like this:

class MyAlgorithm(QCAlgorithm):
def Initialize(self):

# List of symbols
self.symbols = ['SPY', 'AAPL',]

# Dictionary of stop loss targets
self.symbolStopLossTargets = {symbol: None for symbol in self.symbols}


self.AddAlpha(MyAlphaModel())
self.AddRiskManagement(MyRiskModel)

class MyAlphaModel(AlphaModel):
def Update(self, algorithm, data):
insights = []
for symbol in algorithm.symbols:
fastEMA = #
slowEMA = #

# Long if fast > slow
if fastEMA > slowEMA:
insight = Insight.Price(symbol, timedelta(*args), InsightDirection.Up)
algorithm.symbolStopLossTargets[symbol] = slowEMA # Update stop loss targets

insights.append(insight)
return insights

class MyRiskModel(RiskManagementModel):
def ManageRisk(self, algorithm, targets):
targets = []
for kvp in algorithm.Securities:
symbol = kvp.Key
security = kvp.Value

if security.Holdings.IsLong:

# Check if closing price is below the stop loss
close = security.Close
if close <= algorithm.symbolStopLossTargets[symbol.Symbol.Value]:
targets.append(PortfolioTarget(security.Symbol, 0)
return targets

 

0

Hi Ben,

This strategy is not possible to implement in the Algorithm Framework design without violating the separation of concerns principle. I recommend using the classic algorithm design for this.

Best,
Derek Melchin

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed