Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What did I do wrong?

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

    What did I do wrong?

    I'm in the process of learning NinjaScript. I'm trying to port my strategies over from Tradestation's Easylanguage, starting with this simple breakout one:

    Code:
    If High > Highest(High,BreakoutPeriod)[1] then
        Buy 1 Contract next bar at market;
    If Low < Lowest(Low, BreakoutPeriod)[1] then 
        SellShort 1 Contract next bar at market;
    
    
    If marketposition <> 1 then 
        MaxTradeLow = -999999;
    If marketposition <> -1 then 
        MinTradeHigh = 999999;
    
    
    If marketposition = 1 and L > MaxTradeLow[1] then 
        MaxTradeLow = L;
    If marketposition = -1 and H < MinTradeHigh[1] then 
        MinTradeHigh = H;
    
    
    Sell Next Bar at (MaxTradeLow -ATRFactor * AvgTrueRange(ATRPeriod)) stop;
    BuyToCover Next Bar at (MinTradeHigh +ATRFactor * AvgTrueRange(ATRPeriod)) stop;
    It's just a simple n-bar breakout with a chandelier type stop. Here's what I got when I rewrote it in NinjaScript (after stripping it down just to the essential code:

    Code:
            
            protected override void Initialize(){
                CalculateOnBarClose = true;
            }
    
            protected override void OnBarUpdate(){
                
                //Enter on a high or low breakout
                if(High[0] > MAX(High, BreakoutPeriod)[1])
                    EnterLong();
                if(Low[0] < MIN(Low, BreakoutPeriod)[1])
                    EnterShort();
                
                //Clear stop data between trades
                if(Position.MarketPosition != MarketPosition.Long)
                    MaxTradeLow = -99999;
                if(Position.MarketPosition != MarketPosition.Short)
                    MinTradeHigh = 99999;
                
                //Stop code for long trades
                if(Position.MarketPosition == MarketPosition.Long){
                    
                    //Find the highest Low since the trade started
                    if(Low[0] > MaxTradeLow)
                        MaxTradeLow = Low[0];
                    
                    SetStopLoss(CalculationMode.Price, MaxTradeLow-ATRFactor * ATR(ATRPeriod)[0]);
                
                //Stop code for short trades    
                }else if(Position.MarketPosition == MarketPosition.Short){
                    
                    //Find the lowest High since the trade started
                    if(High[0] < MinTradeHigh)
                        MinTradeHigh = High[0];
                    
                    SetStopLoss(CalculationMode.Price, MinTradeHigh+ATRFactor * ATR(ATRPeriod)[0]);
                }
                    
            }
    When I backtest this code all the trades are wrong. I manually checked the entries, and they seem fine. It's the stops that are killing me. I tried debugging them as best I can with Print() statements, but to no avail. What part did I port incorrectly?

    #2
    Someone will follow up on Monday.
    RayNinjaTrader Customer Service

    Comment


      #3
      Have you printed the values of

      MaxTradeLow-ATRFactor * ATR(ATRPeriod)[0]
      MinTradeHigh+ATRFactor * ATR(ATRPeriod)[0]

      To see if they're correctly being calculated?

      Also, I would plot the ATR on the chart to get an idea of where you should see the exit occurring.

      Hope this is helpful.
      mrlogik
      NinjaTrader Ecosystem Vendor - Purelogik Trading

      Comment


        #4
        Hi Lycurgus, first of all welcome to our support forums!

        I would suggest either resetting your stop loss value to the intial one when flat as demonstrated in this reference sample - http://www.ninjatrader-support2.com/...ead.php?t=3222

        Or you replace SetStopLoss with the ExitLongStop / ExitShortStop methods -



        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 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
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X