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

Stops getting breached + how to get a daily stop limit working

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

    Stops getting breached + how to get a daily stop limit working

    A simple strategy.

    DailyStopCount is a counter which counts the number of orders in a day which have been stopped.

    OnOrderUpdate() captures stop executions and increments a counter.

    Code:
    if(order.Name == "Stop loss" && order.OrderState == OrderState.Filled) {
        this.DailyStopCount++; 
    }
    OnBarUpdate() resets the counter at the start of each session.

    OnBarUpdate() checks the value of the counter and if it has reached a certain number, then it just exits. Otherwise, it calls EnterLong().

    Code:
    protected override void OnBarUpdate()
    {
        if (Bars.IsFirstBarOfSession)
        {  
            this.DailyStopCount = 0;            
         }
                
         if(this.DailyStopCount >= this.DailyStopLimit)
            return;        
                
         EnterLong();
     }
    Each trade has a Profit Target with the same number of ticks as the stop (just to make the graph scale nicely).

    Code:
    if (order.Name == "Buy" && order.OrderState == OrderState.Filled) {       
        SetStopLoss(CalculationMode.Ticks, this.StopLoss); 
        SetProfitTarget(CalculationMode.Ticks, this.StopLoss); 
     }
    With the StopLoss set to 10 ticks and the DailyStopLimit set to 2, I expected that:

    1. There would not be a single trade that losses more than 10 ticks. But there are some. (see PNG attached).

    2. There would not be a single day that losses more than 20 ticks. But there are some that lose 40. (see PNG attached).

    My questions are:

    1. Am I coding this up correctly?

    2. Is it possible to make it so that there is not a single trade that losses more than 10 ticks?

    3. Is it possible to make it so that there is no day that loses more than 20 ticks?

    4. Why does this strategy have some trades which lose more than 10 ticks and some days which loss more than 20 ticks?

    I am sure that I just misunderstand the way that NT works, but I just cannot figure out what the problem is.

    I understand that in trading there is no guarantee of stops etc... but this is simulation so I did expect that the stops would not be breached by these amounts. The Stop Loss is set to 10 pips in the example but two of the trades lose 19 pips.
    Attached Files

    #2
    Are some of your trades going across sessions? Were the next session gaps further than your stop loss/profit target?
    Last edited by sledge; 02-16-2018, 12:15 AM. Reason: grammer? Where->Were

    Comment


      #3
      Originally posted by sledge View Post
      Are some of your trades going across sessions? Where the next session gaps further than your stop loss/profit target?
      Thanks for responding.

      I have "Break At EOD" and "Exit On Session Close" both set to true.

      Comment


        #4
        Daily issue solved!

        The issue with the daily stop loss was because the SessionStart is at 18:00 but the DayStart is at midnight.

        This fixes the issue. Resetting the counter when the Date changes instead of FirstBarOfSession.

        Code:
        bool newDay=false;
                    if(CurrentBar > 0)
                        newDay = Time[0].Date != Time[1].Date;
                    
                    if (newDay)
                    {  
                        this.DailyStopCount = 0;
        }

        Comment


          #5
          Hi AlgoJason, I am trying to do the same but when using your code I get an error " The name 'order' does not exist in the current context. How you declare order? I am sorry for the newbie question but now starting to limit the daily loss.
          Thanks, Marcelo

          Comment


            #6
            Hello Marcelo,

            The order property comes from OnOrderUpdate(). (This code would need to be in OnOrderUpdate())

            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Radano, 06-10-2021, 01:40 AM
            19 responses
            605 views
            0 likes
            Last Post Radano
            by Radano
             
            Started by KenneGaray, Today, 03:48 AM
            0 responses
            4 views
            0 likes
            Last Post KenneGaray  
            Started by thanajo, 05-04-2021, 02:11 AM
            4 responses
            470 views
            0 likes
            Last Post tradingnasdaqprueba  
            Started by aa731, Today, 02:54 AM
            0 responses
            5 views
            0 likes
            Last Post aa731
            by aa731
             
            Started by Christopher_R, Today, 12:29 AM
            0 responses
            11 views
            0 likes
            Last Post Christopher_R  
            Working...
            X