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:

    https://ninjatrader.com/support/foru...ad.php?t=19289


    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.
      JimNinjaTrader Customer Service

      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:

          https://ninjatrader.com/support/help...orhandling.htm

          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.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by aeadon, Today, 02:27 AM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by op1031, Today, 01:52 AM
            0 responses
            1 view
            0 likes
            Last Post op1031
            by op1031
             
            Started by Aramunno, Yesterday, 10:11 PM
            2 responses
            16 views
            0 likes
            Last Post Aramunno  
            Started by sdauteuil, Yesterday, 09:38 PM
            0 responses
            5 views
            0 likes
            Last Post sdauteuil  
            Started by fscabrera03, Yesterday, 09:23 PM
            0 responses
            11 views
            0 likes
            Last Post fscabrera03  
            Working...
            X