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

Disable Strategy After First Winning Trade

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

    Disable Strategy After First Winning Trade

    How would I program my strategy to stop trading after the first positive trade of the day? I have a condition that limits the hours to between 7:00AM and 3PM cst.

    I have two possible entries, Long or Short and 4 possible exits, LongStop, LongTarget or ShortStop, ShortTarget.

    What I want is to have the strategy keep running until either a LongTarget or ShortTarget exit is made. But if it hits LongStop or ShortStop I'd like to have it keep trading.

    Thank you.

    #2
    Hello rnsaeger,

    You can detect the positive trade from the SystemPerformance collection when the position updates.
    https://ninjatrader.com/support/help...erformance.htm
    https://ninjatrader.com/support/help...collection.htm
    https://ninjatrader.com/support/help.../nt8/trade.htm
    https://ninjatrader.com/support/help...tionupdate.htm

    To disable a strategy call CloseStrategy().
    https://ninjatrader.com/support/help...sestrategy.htm

    Code:
    protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
    {
    if (SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count() - 1].ProfitCurrency > 0)
    CloseStrategy("Close strategy order");
    }
    Last edited by NinjaTrader_ChelseaB; 08-02-2022, 09:49 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea-

      I am trying to do the same thing as rnsaeger. In playback mode, I am running into the issue of these SystemPerformace collections not being reset after each trading day. Is there a way to do this? I think the easiest way for me to accomplish this is to have a condition only allows entry executions if the following evaluates as true:

      SystemPerformance.RealTimeTrades.WinningTrades.Cou nt < 1

      How can I reset the Winning Trades Count as the beginning of each day for the purpose of backtesting in playback connection?

      Thank you,

      Nick

      Comment


        #4
        Hello Nick,

        Are you certain you are trying to do the same thing as rnsaeger?

        rnsaeger is trying to:
        program my strategy to stop trading after the first positive trade of the day?
        This is what you are trying to do? This would not be related to resetting the winning trades on a new day.

        To stop trading after the first winning trades you could check that the winning trades count is greater than 0 as well as checking if the last trade profit is above 0 as suggested in post #2.

        Code:
        if (SystemPerformance.RealTimeTrades.WinningTrades.Count > 0)
        {
        CloseStrategy("Closing strategy order");
        }
        How can I reset the Winning Trades Count as the beginning of each day
        This sounds like a completely different inquiry.

        I am understanding this as you want to reset the winning trades count at midnight (or on the first bar of a new session). This would require tracking this your self with a variable.
        If my understanding is correct, please let me know and I will move this to a new thread for this new unrelated inquiry.
        Last edited by NinjaTrader_ChelseaB; 08-07-2022, 08:45 PM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by njmeyer713 View Post
          Chelsea-

          I am trying to do the same thing as rnsaeger. In playback mode, I am running into the issue of these SystemPerformace collections not being reset after each trading day. Is there a way to do this? I think the easiest way for me to accomplish this is to have a condition only allows entry executions if the following evaluates as true:

          SystemPerformance.RealTimeTrades.WinningTrades.Cou nt < 1

          How can I reset the Winning Trades Count as the beginning of each day for the purpose of backtesting in playback connection?

          Thank you,

          Nick

          You can try this:

          Code:
          protected override void OnBarUpdate()
          {​
               if (Bars.IsFirstBarOfSession)
               {
               PriorLosingTrades = SystemPerformance.AllTrades.LosingTrades.Count;
               PriorWinningTrades = SystemPerformance.AllTrades.WinningTrades.Count;
               }
           }
          
          
          // Insert these before your entry logic to prevent any new entries for the session:
          
          // Stop trading after 1 losing trade
          if (SystemPerformance.AllTrades.LosingTrades.Count > PriorLosingTrades)
               return;
          
          // Stop trading after 2 winning trades
          if (SystemPerformance.AllTrades.WinningTrades.Count > PriorWinningTrades + 1)
               return;
          
          
          
          // Add to your Properties region:
          
          #region Properties
          [NinjaScriptProperty]
          [Range(0, int.MaxValue)]
          private int PriorLosingTrades
          { get; set; }
          
          [NinjaScriptProperty]
          [Range(0, int.MaxValue)]
          private int PriorWinningTrades
          { get; set; }​
          #endregion
          Hope this helps.

          Last edited by fredb987; 03-29-2023, 10:41 AM.

          Comment


            #6
            Originally posted by fredb987 View Post


            You can try this:

            Code:
            protected override void OnBarUpdate()
            {​
            if (Bars.IsFirstBarOfSession)
            {
            PriorLosingTrades = SystemPerformance.AllTrades.LosingTrades.Count;
            PriorWinningTrades = SystemPerformance.AllTrades.WinningTrades.Count;
            }
            }
            
            
            // Insert these before your entry logic to prevent any new entries for the session:
            
            // Stop trading after 1 losing trade
            if (SystemPerformance.AllTrades.LosingTrades.Count > PriorLosingTrades)
            return;
            
            // Stop trading after 2 winning trades
            if (SystemPerformance.AllTrades.WinningTrades.Count > PriorWinningTrades + 1)
            return;
            
            
            
            // Add to your Properties region:
            
            #region Properties
            [NinjaScriptProperty]
            [Range(0, int.MaxValue)]
            private int PriorLosingTrades
            { get; set; }
            
            [NinjaScriptProperty]
            [Range(0, int.MaxValue)]
            private int PriorWinningTrades
            { get; set; }​
            #endregion
            Hope this helps.

            I got an error CS1518 on the region Properties lines with private int PriorLosingTrades and private int PriorWinningTrades

            Comment


              #7
              Hello jhudas88,

              It looks like you have logic that is not within the scope of a method.

              // Insert these before your entry logic to prevent any new entries for the session:

              // Stop trading after 1 losing trade
              if (SystemPerformance.AllTrades.LosingTrades.Count > PriorLosingTrades)
              return;

              // Stop trading after 2 winning trades
              if (SystemPerformance.AllTrades.WinningTrades.Count > PriorWinningTrades + 1)
              return;
              This code should be within the scope of OnBarUpdate() (between the curly braces of the OnBarUpdate action block).​
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Jonker, Today, 01:19 PM
              0 responses
              1 view
              0 likes
              Last Post Jonker
              by Jonker
               
              Started by futtrader, Today, 01:16 PM
              0 responses
              5 views
              0 likes
              Last Post futtrader  
              Started by Segwin, 05-07-2018, 02:15 PM
              14 responses
              1,790 views
              0 likes
              Last Post aligator  
              Started by Jimmyk, 01-26-2018, 05:19 AM
              6 responses
              838 views
              0 likes
              Last Post emuns
              by emuns
               
              Started by jxs_xrj, 01-12-2020, 09:49 AM
              6 responses
              3,294 views
              1 like
              Last Post jgualdronc  
              Working...
              X