The code below is a risk management for stocks:
Stoploss initial 4% becomes 2% in case of low volatilità.If position moves into profit, middle position (1 / 2) is closed with a target price that exceeds 0.9 ATR to 14 periods. The remaining 50% is managed with a trailingstop channel based on a maximum of 10 days (if short) and minimum at 10 days (if long) which is subtracted 0.25 ATR to 14 periods.
By testing the strategy on a series of 10 years, some stock, you see only the input data on the occurrence of the condition and the release date of the end of the series.
Here's the code:
#region Variables
private bool be1 = false; // Break Eaven
private bool be2 = false; // Break Eaven
#endregion
protected override void Initialize()
{
Add(ATR(14));
Add(SMA(50));
Add (MAX(High,10));
Add (LOW(Min,10);
TraceOrders = true;
CalculateOnBarClose = true;
#region Positions
private void GoLong()
{
SetStopLoss("StopLoss 4% LongBuy",CalculationMode.Percent,0.05,true);
SetProfitTarget("ProfitTarget 1/2 Position",CalculationMode.Price,Close[0] + ATR (14)[0]);
EnterLong(500, "LongBuy");// 500 Stock
}
private void GoShort()
{
SetStopLoss("StopLoss 4% ShortSell",CalculationMode.Percent,0.05,true);
SetProfitTarget("ProfitTarget 1/2 Position",CalculationMode.Price,Close[0] - ATR (14)[0]);
EnterShort(500, "ShortSell");
}
#endregion
#region OrderRouting
private void ManagerOrder ()
{
if (Position.MarketPosition == MarketPosition.Long)
{
if( Be1 && Close[0] > Position.AvgPrice && ATR(14)[0] > EMA(ATR(14), 14)[0])
SetStopLoss("StopLoss 4%LongBuy",CalculationMode.Percent,0.04,true);
ExitLongLimit(250,Position.AvgPrice + ATR(14) [0]*(0.9)*TickSize,"ExitLongBuy 1/2 Position","High Volatility");
SetTrailStop("ClosePositionLong High Volatility",CalculationMode.Price,MIN(Low,10)[0]-(ATR(14)[0])* (0.25)*TickSize,true);
if( Be2 && High[0] > Position.AvgPrice && ATR(14)[0] < EMA(ATR(14), 14)[0])
SetStopLoss("StopLoss 2%", CalculationMode.Percent, 0.02, true);
ExitLongLimit(250,Position.AvgPrice + ATR(14)[0]* (0.9) * TickSize , "ExitLongBuy 1/2 position", "Low Volatility ");
SetTrailStop("ClosePositionLong Low Volatility",CalculationMode.Price,MIN(Low, 10)[0] - (ATR(14)[0]) * (0.25) * TickSize,true);
}
if (Position.MarketPosition == MarketPosition.Short)
{
if( Be1 && Close[0] > Position.AvgPrice && ATR(14)[0] > EMA(ATR(14), 14)[0])
SetStopLoss("StopLoss 4%ShortSell",CalculationMode.Percent,0.04,false);
ExitLongLimit(250,Position.AvgPrice - ATR(14) [0]*(0.9)*TickSize,"ExitShortSell 1/2 Position", "High Volatility");
SetTrailStop("ClosePositionShort High Volatility",CalculationMode.Price,MAX(High,10)[0]+(ATR(14)[0])* (0.25)* TickSize,false);
if( Be2 && High[0] > Position.AvgPrice && ATR(14)[0] < EMA(ATR(14), 14)[0])
SetStopLoss("StopLoss 2% ShortSell", CalculationMode.Percent, 0.02, false);
ExitLongLimit(250,Position.AvgPrice - ATR(14)[0]* (0.9) * TickSize , "ExitShortSell 1/2 position", "Low Volatility ");
SetTrailStop("ClosePositionLong Low Volatility",CalculationMode.Price,MAX(High, 10)[0] + (ATR(14)[0]) * (0.25) * TickSize,false);
}
}
#endregion
protected override void OnBarUpdate()
{
ManagerOrder ();
if (Position.MarketPosition!= MarketPosition.Flat) return;
if (Close[0] > SMA(50)[0])
{
GoLong();
}
if (Close[0] < SMA(50)[0])
{
GoShort();
}
}
What did i do wrong?
Ciao.
Italy
Comment