Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What am I doing wrong?

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

    What am I doing wrong?

    Hi,

    Trying to add code to stop trading if up or down 500 for the day. Seems simple, but I seem to be incapable of making it work. Can you tell me what I am doing wrong?

    Here are the code snippets:

    if(Bars.FirstBarOfSession && FirstTickOfBar)
    {
    DailyProfit = 0;
    }

    This should reset the variable DailyProfit at the beginning of each day

    When a trade is exited this code is executed:

    {
    DailyProfit = DailyProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
    ExitShort("SysXS", "");
    return;
    }

    This should increment/decrement the variable DailyProfit by the amount gained/lost on the Current Trade.

    If the conditions are not met for an exit the following code should be executed:

    if (Position.MarketPosition == MarketPosition.Long
    && DailyProfit + Position.GetProfitLoss(Close[0], PerformanceUnit.Currency)>500)
    {
    ExitLong("XLProfitMade", "");
    return;
    }

    Code exists to check for similar conditions long or short, up 500 or down 500 and need not be shown here as it is virtually identical. It also contains a flag that tells the entry conditions not to take a new trade, and that flag is reset in the opening segment and not shown here.

    So where is my error? The code will execute if an INDIVIDUAL TRADE reaches the 500 +/-, but I am wanting to quit for the day if the entire day's P/L is +/- 500.

    Help please?

    Thanks,

    Jamie

    #2
    Jamie,

    You'll want to try and employ this technique instead demonstrated on this reference sample: http://www.ninjatrader.com/support/f...ead.php?t=4084
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the response. I tried each approach. None worked. Is there a way to obtain the profit/loss for TODAY'S session only? I want to stop trading if today is +/- X number of dollars. However, I do not wish to stop forever. I want the system to resume taking trades again the next day.

      Thanks,

      Jamie

      Comment


        #4
        Hello,

        When you say it doesnt work what specifically doesnt work about it.

        Have you added in print statements to see what the code execution looks like to see whats going on and whats not getting run and what is?

        I look forward to assisting you further.
        BrettNinjaTrader Product Management

        Comment


          #5
          What doesn't work depends on the implementation. In some cases, it stopped trading (forever) after the first day or two. In most cases, it applied the parameters to the current trade and not the day as a whole. For example if my profit target is 500, and I closed out a trade with 300 profit, the next trade should exit if it reaches plus 200 at any point. And then the system should stop trading for the day, but resume again the next session.

          Comment


            #6
            Ok,

            can you please supply code you used for these results I can take a quick look. A snippit will be fine.

            I look forward to assisting you further.
            BrettNinjaTrader Product Management

            Comment


              #7
              This is the current incarnation. I have tried several others.

              if(Bars.FirstBarOfSession && FirstTickOfBar)
              {
              TradesSoFar = 0;
              todayPL = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
              }

              if (Performance.AllTrades.TradesPerformance.Currency. CumProfit - todayPL > 500
              || Performance.AllTrades.TradesPerformance.Currency.C umProfit - todayPL < -500)
              {
              if (Position.MarketPosition == MarketPosition.Long)
              ExitLong("XLCash");
              else if (Position.MarketPosition == MarketPosition.Short)
              ExitShort("XSCash");
              TradesSoFar = TradesAllowed;
              return;
              }

              Comment


                #8
                Hello,

                Issue is here: - todayPL.

                You can see that your setting todayPL right at the start of the session and only setting it wont. The todayPL variable wont update. We need to have another block in here that Adds to todayPL the realized profit for the day.

                In the same manner the sample adds good trades or bad trades.

                Let me know if I can be of further assistance.
                BrettNinjaTrader Product Management

                Comment


                  #9
                  I have tried various ways of incrementing/decrementing todayPL. None have worked. What would you suggest?

                  Comment


                    #10
                    Would suggest this sample method here:

                    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;
                                    priorTradesCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;
                                    
                                    /* 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 or if realized losses exceed $400.
                                Also prevent trading if 10 trades have already been made in this session. */
                                if (Performance.AllTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit >= 1000
                                    || Performance.AllTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit <= -400
                                    || Performance.AllTrades.Count - priorTradesCount > 10)
                                {
                                    /* 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.
                                    return;
                                }
                    BrettNinjaTrader Product Management

                    Comment


                      #11
                      Hi Brett,

                      Again, I appreciate your help and your quick response. This snippet is one that I have tried. In fact the primary difference between this snippet and my current version is in the variable name (todayPL in mine; priorTradesCumProfit in the snippet). I could be missing something very obvious (wouldn't be the first time), but to me this is the same as what I have that is not working.

                      Comment


                        #12
                        No problem.

                        So what the samples doing here is its storing all the PnL for the entire strategy since it started at the first bar of session.

                        Then its taking the current PnL at runtime during the day and subtracting it from the stored Prior PnL. Which then gives you a value of PnL ofr the current day.

                        Which then can be checked.

                        It will only work if the trade is closed however.

                        Will not work for a trade that is live.
                        BrettNinjaTrader Product Management

                        Comment


                          #13
                          I did think of that in the interim and added a check for Open Position Profit/Loss. It still does not work. I added a print statement and the variables seem to be as you would expect, but the code is not executed. I even changed the target numbers just for fun, and still no dice.

                          This is the code:

                          Print("PAT:" + Performance.AllTrades.TradesPerformance.Currency.C umProfit + "TPL:" + todayPL + "GPL:" + Position.GetProfitLoss(Close[0],PerformanceUnit.Currency));

                          if (Performance.AllTrades.TradesPerformance.Currency. CumProfit - todayPL + Position.GetProfitLoss(Close[0],PerformanceUnit.Currency) > 300
                          || Performance.AllTrades.TradesPerformance.Currency.C umProfit - todayPL + Position.GetProfitLoss(Close[0],PerformanceUnit.Currency) < -1000)
                          {
                          if (Position.MarketPosition == MarketPosition.Long)
                          ExitLong("XLCash");
                          else if (Position.MarketPosition == MarketPosition.Short)
                          ExitShort("XSCash");
                          return;
                          }


                          The first trade never hits either parameter (+300, or minus 1000) and exits on the close. 320 loss. So far so good. The second trade (on the second day) Enters and gets pounded right away. But the code does not execute even though the variables seem to indicate that it should. From the Print Log:

                          PAT:-319.999999999993TPL:-319.999999999993GPL:-989.999999999998 (Trade is in bad shape)
                          PAT:-319.999999999993TPL:-319.999999999993GPL:-1030 (Trade should be killed)
                          PAT:-319.999999999993TPL:-319.999999999993GPL:-1110 (Nope. Keeps going. Exits on the close).

                          Seems like it is something very small that I am messing up, but what?

                          Jamie

                          Comment


                            #14
                            Originally posted by jhall View Post
                            This is the current incarnation. I have tried several others.

                            if(Bars.FirstBarOfSession && FirstTickOfBar)
                            {
                            TradesSoFar = 0;
                            todayPL = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
                            }

                            if (Performance.AllTrades.TradesPerformance.Currency. CumProfit - todayPL > 500
                            || Performance.AllTrades.TradesPerformance.Currency.C umProfit - todayPL < -500)
                            {
                            if (Position.MarketPosition == MarketPosition.Long)
                            ExitLong("XLCash");
                            else if (Position.MarketPosition == MarketPosition.Short)
                            ExitShort("XSCash");
                            TradesSoFar = TradesAllowed;
                            return;
                            }
                            Try using some Print() statements to track Performance.AllTrades.TradesPerformance.Currency.C umProfit. I am not sure that it runs dynamically, but cannot be certain. If it does not update dynamically, then your code might never trigger.
                            Last edited by koganam; 08-11-2011, 02:11 PM. Reason: Corrected spelling

                            Comment


                              #15
                              This code will only stop you from opening a new trade. It wont get you out of a current one if its >300. You would need to track this on your own live to do this since the Performance class only updates when the trade is closed.
                              BrettNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              607 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              353 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              560 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              561 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X