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

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 -



        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by RaddiFX, Today, 10:15 AM
        2 responses
        13 views
        0 likes
        Last Post RaddiFX
        by RaddiFX
         
        Started by patrickmlee007, Today, 09:33 AM
        2 responses
        17 views
        0 likes
        Last Post patrickmlee007  
        Started by magnatauren, 08-15-2020, 02:12 PM
        5 responses
        206 views
        0 likes
        Last Post RaddiFX
        by RaddiFX
         
        Started by rene69851, 05-02-2024, 03:25 PM
        1 response
        22 views
        0 likes
        Last Post rene69851  
        Started by ETFVoyageur, Yesterday, 07:05 PM
        5 responses
        45 views
        0 likes
        Last Post ETFVoyageur  
        Working...
        X