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

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.
      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:



          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 yertle, Today, 08:38 AM
            0 responses
            4 views
            0 likes
            Last Post yertle
            by yertle
             
            Started by Mestor, 03-10-2023, 01:50 AM
            15 responses
            378 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by samish18, Yesterday, 08:57 AM
            10 responses
            27 views
            0 likes
            Last Post samish18  
            Started by matty89, 03-02-2020, 08:31 AM
            34 responses
            3,039 views
            1 like
            Last Post NinjaTrader_BrandonH  
            Started by kujista, Today, 05:44 AM
            3 responses
            14 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X