• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


How to stop a strategy after a daily profit target is reached.

  • Filter
  • Time
  • Show
Clear All
new posts

    How to stop a strategy after a daily profit target is reached.

    Is there a way to stop a trading strategy after a daily profit target is reached. For instance after 4 ticks/$$$/%. I know there is one for each individual trade, but what about for a daily limits?

    Thanks. I will continue to search in the help section to see if its in there.

    Hello olingerc,

    For this you can work with Trade Performance class and reset values for each day.

    This sample is pretty close to what you're looking for:
    Ryan M.NinjaTrader Customer Service


      Is there anthing in the code to check for "Maximum favorable Excursion" value? that is basically the value I am looking for. I will check out the link to see if I can get what I desire. Thanks.


        I extracted the correct part of the code but when I compile I get this error "priorTradesCount" wherever priortrades" is mentioned it says "does not exist in current context" I imagine in NT 6.5 this existed? What is the equivalent for NT7?



          You should be able to import that sample in either version. It sounds like you are missing the variable declaration.

          #region Variables
          private int priorTradesCount = 0;
          private double priorTradesCumProfit = 0;

          You can custom code MFE if you want it calculated while you're in a trade:

          Understanding Average MFE
          Where MFE (max. favorable excursion) is defined as (best price trade reached – entry price), quantity is defined as the number of contracts traded, and point value is defined as the monetary conversion of each point (e.g. 100 for currency pairs).

          Example with long position:
          if (Position.MarketPosition == MarketPosition.Long)
          myDouble = High[BarsSinceEntry()] - Position.AvgPrice;
          Ryan M.NinjaTrader Customer Service


            Ryan I am having trouble with this till. I believe I have the correct code in the correct location but during a back test it does not calculate. I can set the profit target to lets say 10,000 and during a back test it will only test up to 10,000 and nothing after. or if its 1000 it would test until a profit of 1,000 is met and nothing after. Why is this so? If I set it to RealtimeTrades would it only calculate the current day's live account information?

            How do I get my strategy to exit during the intraday after lets say a profit of 1,000 is reached.
            This is what I have so far
            // At the start of a new session
            if (Bars.FirstBarOfSession)
            // Store the strategy's prior cumulated realized profit and number of trades
            priorTradesCount = Performance.AllTrades.Count;
            priorTradesCumProfit Performance.AllTrades.TradesPerformance.Currency.C umProfit;

            // NOTE: Using .AllTrades will include both historical virtual trades as well as real-time trades.
            //If you want to only count profits from real-time trades please use .RealtimeTrades.

            //Prevents further trading if the current session's realized profit exceeds $1000

            if (Performance.AllTrades.TradesPerformance.Currency. CumProfit - priorTradesCumProfit >= 1000)
            //TIP FOR EXPERIENCED CODERS: This only prevents trade logic in the context of the OnBarUpdate() method. If you are utilizing
            other methods like OnOrderUpdate() or OnMarketData() you will need to insert this code segment there as well.

            // Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
            Last edited by olingerc; 05-06-2011, 03:03 PM.


              Code looks OK and I don't see where you are deviating from the sample at all. just ran the sample here and it's working as expected. You have more than one session, right?

              In a backtest there are no real time trades so that won't work for you. Attach the script (exported zip file) you're using and screenshots of your backtest setup and will give it a run here.
              Ryan M.NinjaTrader Customer Service


                Yes I was having issues with the back testing. No changes were showing up during a back test. Assuming I change it to RealtimeTrades it would actually work live during a intraday cession. But unfortunately if I cant back test this then I am not willing to spend months of playing around with a live account only to realize that I would of made more money if I just hadn't done it to begin with.


                  I know this is an old thread, but in case anyone else comes along...

                  IncludeTradeHistoryInBacktest = true;

                  That should fix the issue OP was experiencing.


                  Latest Posts


                  Topics Statistics Last Post
                  Started by rcsingleton, Today, 09:47 PM
                  0 responses
                  Last Post rcsingleton  
                  Started by catinabag, Today, 07:33 PM
                  4 responses
                  Last Post catinabag  
                  Started by Goldy, Today, 08:22 PM
                  0 responses
                  Last Post Goldy
                  by Goldy
                  Started by forrestang, Today, 07:44 PM
                  0 responses
                  Last Post forrestang  
                  Started by Johnny90, Today, 07:29 PM
                  0 responses
                  Last Post Johnny90