Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help, Risk Management

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help, Risk Management

    Hello everyone,
    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

    #2
    Italy, unfortunately I'm not sure what the exact issue is you have with the code - if it works ok on some instruments, but not on others, please print the calculated values to the output window to check why this may be the case, is the ATR returning the values you would expect for those? Is the tick size setup correctly as well?

    Thanks

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    571 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    331 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    549 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    549 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X