Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    BertrandNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by funk10101, Today, 12:02 AM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by GLFX005, Today, 03:23 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Erick  
    Started by nandhumca, Yesterday, 03:41 PM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by The_Sec, Yesterday, 03:37 PM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by vecnopus, Today, 06:15 AM
    0 responses
    1 view
    0 likes
    Last Post vecnopus  
    Working...
    X