Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limit strategy to X lossing trades

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

    Limit strategy to X lossing trades

    Hello to everyone.

    I have a doubt. I want to limit the strategy to one lossing trade per session. I have some examples os how to limit the strategy to daily loss limit.

    I have this code:

    if (Bars.IsFirstBarOfSession&&IsFirstTickOfBar)
    {
    MaxLossingTrades = 0;
    }


    if(SystemPerformance.AllTrades.LosingTrades.Count > 0)
    {
    MaxLossingTrades++;
    Print(MaxLossingTrades);
    }

    But it's not working fine...

    Thanks a lot...

    #2
    Possible solution

    I think that the solution is here:




    Thanks....

    Comment


      #3
      Hello brokerbombero,

      Thanks for opening the thread.

      It sounds like you have found a solution. If you have any additional questions, please don't hesitate to write back.

      Comment


        #4
        Solution

        The code that a used is:

        if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
        {
        contadorLossingTrades = 0; //CounterlossingTrades
        contadorProfitableTrades = 0; //Counterwinningtrades
        NumeroAnteriorTradesSesion = SystemPerformance.AllTrades.Count;
        }


        if ((SystemPerformance.AllTrades.Count - NumeroAnteriorTradesSesion) >= 1 && SystemPerformance.AllTrades.Count != NumeroAnteriorTrades)
        {


        NumeroAnteriorTrades = SystemPerformance.AllTrades.Count;

        //we take the last trade, to check if is winer o loser
        Trade trade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];

        if (trade.ProfitCurrency > 0)
        {

        contadorProfitableTrades++;

        }

        else if (trade.ProfitCurrency < 0)
        {

        contadorLossingTrades++;

        }
        }

        //Only trade if the condition is true...the MaxLossingtrades and MaxProfitabletrades are properties...
        if (contadorLossingTrades < MaxLossingTrades && contadorProfitableTrades < MaxProfitableTrades)
        {
        //Code for the Strategy here......
        }

        Comment


          #5
          Problems with the Stop

          Now I have this problems with the Stop in the case of the image.

          In RealTime the strategy if the trailStop is rejected the Strategy stoped, because they have "Default behavior of a strategy".

          I would like that the Strategy does not stoped for this reason.

          How can I exit of the position if the stop is rejected.

          I was trying with this:



          I'am not sure how to do this....
          Thaks a lot...
          Attached Files

          Comment


            #6
            Hello brokerbombero,

            You are correct that you can use RealtimeErrorHandling to handle cases where orders are rejected. When you set this property to IgnoreAllErrors in OnStateChange(), you can then capture the occurrence in OnOrderUpdate(). Here you can then attempt to exit the position with an ExitLong() or ExitShort() call to exit your position. The sample code in the help documentation provides an example for how this can be implemented.

            I would suggest to review the Orders tab of the control center, or to test with TraceOrders enabled to see why the rejection had occurred so you may adjust your strategy to avoid such a scenario.

            For further direction on Order Objects and using OnOrderUpdate() I suggest to review the SampleOnOrderUpdate strategy and the documentation linked within.

            SampleOnOrderUpdate - https://ninjatrader.com/support/foru...ead.php?t=7499

            If you have any specific questions on sample code provided, please don't hesitate to ask.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            87 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            47 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            66 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            69 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            58 views
            0 likes
            Last Post CarlTrading  
            Working...
            X