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 NullPointStrategies, Today, 05:17 AM
            0 responses
            43 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            124 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            65 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X