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

GetProfitLoss() cancels stop loss order???

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

    GetProfitLoss() cancels stop loss order???

    My strategy ordinarily does about 119 trades in 5 days.

    I simply added code to call GetProfitLoss() so as to add some trailing stop functionality.

    However, the first stop loss gets canceled somehow and the strategy stays short for the entire 5 days.

    After debugging, I narrowed it down to this line of code and surrounded it with Print() statements and include order trading so you can see the damage.

    Code:
    Print( "Right before GetProfitLoss() call.");
    double currProfitLoss = Position.GetProfitLoss( Close[0], PerformanceUnit.Points);
    Print( "Right after GetProfitLoss() call.");
    Now the the Output Window with order tracing:
    Code:
    3/24/2008 4:59:55 AM Entered internal PlaceOrder() method at 3/24/2008 4:59:55 AM: Action=SellShort OrderType=Limit Quantity=1 LimitPrice=99.99 StopPrice=0 SignalName='cross low' FromEntrySignal=''
    3/24/2008 4:59:55 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='cross low' Mode=Ticks Value=4 Currency=0 Simulated=False
    Right before GetProfitLoss() call.
    3/24/2008 4:59:56 AM Cancelled pending exit order, since associated position is closed: Order='NT-00001/Back101' Name='Stop loss' State=Working Instrument='$USDJPY' Action=BuyToCover Limit price=0 Stop price=100.06 Quantity=1 Strategy='ProbabilityHLM' Type=Stop Tif=Gtc Oco='NT-00000' Filled=0 Fill price=0 Token='518045a11ec349fe90b3cc5c00dace06' Gtd='12/1/2099 12:00:00 AM'
    Clearly, when calling the GetProfitLoss(), NinjaTrader canceled my stop loss.

    Very dangerous. You need to make this a TOP priority bug fix.

    Fortunately, there's a workaround. I wrote my own ProfitLoss() method by using Position.AvgPrice Price depending on whether long or short.

    Sincerely,
    Wayne

    #2
    Do you have a simple-as-possible strategy that demonstrates this? Thanks.

    I could not reproduce with the attached strategy. What version of NT are you on?

    Code:
    3/29/2008 2:56:52 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=0 Currency=20 Simulated=False
    3/29/2008 2:56:52 PM Entered internal PlaceOrder() method at 3/29/2008 2:56:52 PM: Action=Buy OrderType=Market Quantity=100 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
    3/29/2008 2:57:00 PM Before GetProfitLoss()
    3/29/2008 2:57:00 PM Profit Loss: 0
    3/29/2008 2:57:00 PM After GetProfitLoss()
    3/29/2008 2:57:00 PM Before GetProfitLoss()
    3/29/2008 2:57:00 PM Profit Loss: 0
    3/29/2008 2:57:00 PM After GetProfitLoss()
    3/29/2008 2:57:00 PM Before GetProfitLoss()
    3/29/2008 2:57:00 PM Profit Loss: 0
    3/29/2008 2:57:00 PM After GetProfitLoss()
    3/29/2008 2:57:00 PM Before GetProfitLoss()
    3/29/2008 2:57:00 PM Profit Loss: 0
    3/29/2008 2:57:00 PM After GetProfitLoss()
    3/29/2008 2:57:00 PM Before GetProfitLoss()
    3/29/2008 2:57:00 PM Profit Loss: 0.999999999999091
    3/29/2008 2:57:00 PM After GetProfitLoss()
    3/29/2008 2:57:00 PM Before GetProfitLoss()
    3/29/2008 2:57:00 PM Profit Loss: 2.00000000000102
    3/29/2008 2:57:00 PM After GetProfitLoss()
    3/29/2008 2:57:00 PM Before GetProfitLoss()
    3/29/2008 2:57:00 PM Profit Loss: 2.00000000000102
    3/29/2008 2:57:00 PM After GetProfitLoss()
    What you have found is likely an OCO taking effect in your code. Check for the order being closed out by another order. Maybe a profit target or an Exit on Close or a reversal order.
    Attached Files
    Last edited by NinjaTrader_JoshP; 03-29-2008, 04:01 PM.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      This demonstrates it perfectly. You'll see the stop loss canceled in the output window. Also, you'll see the strategy stay long forever and right through the stop.

      Thanks for your attention to this. I'm still fiddling to get my own ProfitLoss() function working. I can't wait to get a production version of this system.

      Wayne

      Code:
      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Strategy;
      using NinjaTrader.Gui.Chart;
      #endregion
      
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
          /// <summary>
          /// Play the probabilities on the 10 minute bars
          /// </summary>
          [Description("Play the probabilities on the 10 minute bars")]
          public class GetProfitLossBug : Strategy
          {
              #region Variables
              #endregion
      
              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
                  TraceOrders = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  if( Position.MarketPosition == MarketPosition.Flat ) {
                      EnterLong( 10, "test signal");
                      SetStopLoss("test signal", CalculationMode.Ticks, 20, false);
                  } else {
                      double currProfitLoss = Position.GetProfitLoss( Close[0], PerformanceUnit.Points);
                  } 
              }
              #region Properties
              #endregion
              
      
          }
      }

      Comment


        #4
        Hi wayneFH,

        I used your strategy in backtesting and in real-time to no avail.

        Code:
        1/2/2008 5:11:00 AM Entered internal PlaceOrder() method at 1/2/2008 5:11:00 AM: Action=Buy OrderType=Market Quantity=10 LimitPrice=0 StopPrice=0 SignalName='test signal' FromEntrySignal=''
        1/2/2008 5:11:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='test signal' Mode=Ticks Value=20 Currency=0 Simulated=False
        1/2/2008 5:12:00 AM Before Profit Loss
        1/2/2008 5:12:00 AM After Profit Loss
        1/2/2008 5:13:00 AM Before Profit Loss
        1/2/2008 5:13:00 AM After Profit Loss
        1/2/2008 5:14:00 AM Before Profit Loss
        1/2/2008 5:14:00 AM After Profit Loss
        1/2/2008 5:15:00 AM Before Profit Loss
        1/2/2008 5:15:00 AM After Profit Loss
        1/2/2008 5:16:00 AM Entered internal PlaceOrder() method at 1/2/2008 5:16:00 AM: Action=Buy OrderType=Market Quantity=10 LimitPrice=0 StopPrice=0 SignalName='test signal' FromEntrySignal=''
        1/2/2008 5:16:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='test signal' Mode=Ticks Value=20 Currency=0 Simulated=False
        1/2/2008 5:17:00 AM Entered internal PlaceOrder() method at 1/2/2008 5:17:00 AM: Action=Buy OrderType=Market Quantity=10 LimitPrice=0 StopPrice=0 SignalName='test signal' FromEntrySignal=''
        1/2/2008 5:17:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='test signal' Mode=Ticks Value=20 Currency=0 Simulated=False
        1/2/2008 5:18:00 AM Before Profit Loss
        1/2/2008 5:18:00 AM After Profit Loss
        1/2/2008 5:19:00 AM Before Profit Loss
        1/2/2008 5:19:00 AM After Profit Loss
        1/2/2008 5:20:00 AM Before Profit Loss
        1/2/2008 5:20:00 AM After Profit Loss
        1/2/2008 5:21:00 AM Entered internal PlaceOrder() method at 1/2/2008 5:21:00 AM: Action=Buy OrderType=Market Quantity=10 LimitPrice=0 StopPrice=0 SignalName='test signal' FromEntrySignal=''
        1/2/2008 5:21:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='test signal' Mode=Ticks Value=20 Currency=0 Simulated=False
        1/2/2008 5:22:00 AM Before Profit Loss
        1/2/2008 5:22:00 AM After Profit Loss
        These are the prints from backtesting. There is no mention of the stop loss being canceled and looking at the trades, the stop loss closes the position many times. In real-time the situation is the same.

        Please provide me with step-by-step instructions to reproduce what you are seeing. Also please ensure you are on NT6.5 beta 10.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Okay. I do have NT6.5.0.10 and I tried it again. It still fails.

          So it must be related to the parameters set in the Strategy Analyzer.
          I'm running mine on $USDJPY.

          These are how mine are set:

          Data Series:
          Type: second
          Value: 1

          Time Frame:
          From: 3/24/2008
          To: 3/28/2008

          Exclude Weekend: True
          Sessions begin: 12:00 am
          ends: 12:00 am

          Include commission: false
          Label: GetProfitLossBug
          Min bars: 20

          Fill Type: Liberal
          Slippage: 0

          Entries per dir: 1
          Entry handling: unique entries
          Exit on close: true;

          Set order quantity: by strategy
          Time in force: GTC

          I tried changing a few...but to no avail. It still fails.

          Wayne

          Comment


            #6
            Ok. Great got it now. The issue is not that GetProfitLoss() cancels the order it is that something else crashed which causes the strategy to automatically close all positions which in turn cancels the stop loss because the position is suppose to be closed by the fail-safe already.

            Thanks for reporting the bug. We will look into what is actually causing it see what can be done about it.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Right. I see the following log message now. I should have checked the logs. Glad you can reproduce it.

              Code:
              Error on calling 'OnBarUpdate' method for strategy 'GetProfitLossBug': Object reference not set to an instance of an object.   Error on calling 'OnBarUpdate' method for strategy   'GetProfitLossBug': Object reference not set to an instance of an object.
              NOTE:

              If anyone else has this problem, here's ProfitLoss() method I wrote. It only calculates points. So you have to modify it if you want to calculate % or currency.
              Code:
                      protected double ProfitLoss(double price) {
                          double profitLoss;
                          if( Position.MarketPosition == MarketPosition.Short) {
                              profitLoss = Close[0] - Position.AvgPrice;
                          } else    if( Position.MarketPosition == MarketPosition.Long) {
                              profitLoss = Position.AvgPrice - Close[0];
                          } else {
                              profitLoss = 0;
                          }
                          profitLoss = Math.Floor(profitLoss / TickSize + .5) * TickSize;
                          return profitLoss;
                      }
              Suggestion: Add it to your @Strategy.cs class so you have it wherever you need it.

              Wayne

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by kevinenergy, 02-17-2023, 12:42 PM
              117 responses
              2,764 views
              1 like
              Last Post jculp
              by jculp
               
              Started by Mongo, Today, 11:05 AM
              5 responses
              15 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by SightCareAubetter, Today, 12:55 PM
              0 responses
              3 views
              0 likes
              Last Post SightCareAubetter  
              Started by traderqz, Today, 12:06 AM
              8 responses
              16 views
              0 likes
              Last Post traderqz  
              Started by SightCareAubetter, Today, 12:50 PM
              0 responses
              2 views
              0 likes
              Last Post SightCareAubetter  
              Working...
              X