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 Mindset, 04-21-2026, 06:46 AM
            0 responses
            90 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            137 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            68 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by cmoran13, 04-16-2026, 01:02 PM
            0 responses
            120 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            72 views
            0 likes
            Last Post PaulMohn  
            Working...
            X