Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy with daily profit goal still shows trades afterwards?

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

    Strategy with daily profit goal still shows trades afterwards?

    I have a strategy that stops once a goal has been reached or certain amount has been lost. The strategy works perfectly in real-time and market replay. It doesn't works with the Strategy Analyzer. It would the goal/loss limit reached but keeps trading.

    Is there a way to fix this? Maybe Historical or market data instead of on bar update. I'm using the sample PNL with Realtimetrades in this thread. http://www.ninjatrader-support.com/v...ead.php?t=4084

    I don't understand why it works correctly except when I try to use the Strategy Analyzer

    #2
    Here is a link that explains the differences between real-time and backtesting that might help explain the results:

    JessicaNinjaTrader Customer Service

    Comment


      #3
      Ok so we know backtesting is not the same as real time.

      But, the question is, how do we make Ninja obey a simple daily profit/loss goal?

      There has to be a simple way, just a couple lines of code, to tell Ninja to make this work.

      I read the PnL example strategy. That is way too much code, if there is a cut/paste solution in there somewhere can someone be so kind as to post it here?

      The one thing I saw that stood out was:
      Code:
      TradesCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit -
                                    priorTradesCumProfit +
                                    Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
      But priorTradesCumProfit is set to 0 in variables, and it is never again touched in the entire script and I am having trouble getting this to work in my script.

      The goals work fine on real time, but in backtest they are cumulative instead of resetting each day.

      Mike

      Comment


        #4
        ctrlbrk,

        The code in the example is what you need to achieve what you want. It is as stripped down as it can be.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          ^^^ Josh, the code isn't the problem. ctrlbrk is saying when he goes to backtest, it doesn't function like it does in real-time. The same goes for me when I tried to use the strategy analyzer. ctrlbrk, the best I could do is at least have it start up correctly by time code.

          In my strategy I have it set up to to start trading at 9:45 AM and stop at 3:59 PM.

          Code:
          // Only trade between 7:45 AM and 1:45 PM 
          
          if (ToTime(Time[0]) >= 74500 && ToTime(Time[0]) <= 134500) 
          {     
          // Strategy logic goes here 
          }
          But in backtests and strategy analyzer, the program doesn't start trading before 9:45 but still after it reaches its profit goal/loss, it WILL show other trades. That's the best I could do with what very little programming skill I have. If it could be called skill at that.

          Comment


            #6
            Yes I use the Time[0] restrictions as well, but what I really need is for it to obey my daily goals and stop for that day.

            For now, I have to sort the 'periods' tab by drawdown and I manually look the list over to see what would have been excluded if Ninja would have obeyed the daily loss limit I have in place in the script.

            The problem lies with Performance.Realtimetrades not getting counted as realtime (I think) within the historical backtest. So, now comes the huge amounts of code from PnLtest (example strategy) to try to keep a running count of what the real trades did.

            The problem is, I can't get that code to work. Ninja has said that everything in that same PnLtest file is necessary, but a lot of it is just printing results, so I tried to remove the fluff and just get the lines that actually did the calculations (see my above post). The problem is, it doesn't work.

            I simply want a variable that works in backtesting that shows me how much realized PnL for the day is. That is all I am asking for. I cannot understand how this can be that hard, so I do hope someone can help.

            That way I can do the simple: if (realizedpnl < -200) stoptrading; type of thing.


            Thanks!

            Mike

            Comment


              #7
              Not sure I follow guys. Why are you bringing in time filters? Try just backtesting the SamplePnL. You should find it works as expected. Once the PnL reaches either $1000,-$400, or has made 10 trades already it stops trading.

              This line right here is does exactly that ctrlbrk
              Performance.AllTrades.TradesPerformance.Currency.C umProfit - priorTradesCumProfit <= -400

              That is how you get PnL. It is as simple as it can possibly be.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Is priorTradesCumProfit some system variable? Because in the PnL script it is set to 0 under variables then never touched anywhere else that I could find.

                The Time[0] has nothing to do with what I was talking about.

                Comment


                  #9
                  ctrlbrk,

                  It is not a system variable. In the code it is set in OnBarUpdate() at the start of every new session. Does this not appear in your version of SamplePnL?

                  Code:
                  // 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;
                      [COLOR=Red]priorTradesCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;[/COLOR]
                                  
                      /* 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. */
                  }
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Ok, so that is the problem. No it is not in mine, because I have something called "PnLTest" which I thought was the reference sample. I will look for "PnLSample" which is apparently different...

                    Comment


                      #11
                      Josh P.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by jaybedreamin, Today, 05:56 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post jaybedreamin  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      6 responses
                      18 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by Jon17, Today, 04:33 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post Jon17
                      by Jon17
                       
                      Started by Javierw.ok, Today, 04:12 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post Javierw.ok  
                      Started by timmbbo, Today, 08:59 AM
                      2 responses
                      10 views
                      0 likes
                      Last Post bltdavid  
                      Working...
                      X