• 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


Position.Quantity add up open positions weird

  • Filter
  • Time
  • Show
Clear All
new posts

    Position.Quantity add up open positions weird

    "Position.Quantity" add up open positions weird.

    How did Position.Quantity get 18,490,000? Image attached.
    when I only have 2 positions 10,000 + 10,000 = 20,000
    when I EXIT LONG EURUSD (ProfitTarget).

    //This namespace holds Strategies in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.Strategies
        public class LastEntryPrice : Strategy
            private Order entryOrder;
            protected override void OnStateChange()
                if (State == State.SetDefaults)
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "LastEntryPrice";
                    Calculate                                    = Calculate.OnEachTick;
                    EntriesPerDirection                            = 999999999;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = false;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 9999999999;
                    StartBehavior                                = StartBehavior.ImmediatelySubmit;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 0;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;            
                else if (State == State.Configure)
                else if (State == State.DataLoaded)
            protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
        if (order.Name == "EnterLong")
          entryOrder = order;
            protected override void OnBarUpdate()
                int Quantity = 10000;
                double Profit = 
                    PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])
                Account a = Account.All.First(t => t.Name == "Playback101"); // Account Display Name
                double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar);
                double ProfitTarget = Balance*0.005;
                 // Set 1
                if (
                    (Open[0]-Low[0] >= 0.00003)
                double lastentryprice = 0;
                if (entryOrder != null && entryOrder.OrderState == OrderState.Filled)
                lastentryprice = entryOrder.AverageFillPrice;    
                if (
                    (lastentryprice >=  Open[0])
                    && (PositionAccount.Quantity >= 1)
                    && (BarsInProgress == 0)
                if     (
                    Profit>= ProfitTarget
                    ExitLong(0,Position.Quantity,"EXIT LONG EURUSD (ProfitTarget)","");
    Attached Files

    Hello johnnybegoode,

    The behavior that I experienced was not the same as what is showing in the screenshot you have provided.

    As this text of the code was provided instead of an exported script, I copied the code into a new script and tested.

    Below is a link to a video of a test of this code.

    I'm showing that there are many entries that lead to a large position and then the EXIT LONG EURUSD (ProfitTarget) exits that large position.

    Was the start behavior of this script changed to adopt account position in the strategy parameters when adding an instance of the strategy?

    Attached is an export of the script I tested.

    Using this export are you getting different behavior than what I have shown in the video?

    As a tip, To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.
    Attached Files
    Chelsea B.NinjaTrader Customer Service


      Thanks for the video!
      I was expecting and looking for what you got in the video.

      I renamed mine as OLD and import your script and it did the weirdly huge "Entry/Exit" positions on my NT8 too.
      It is not the script.

      Then I use other Dates on Market Replay that I had not run the strategy on before
      and it works like in the video for a while.

      However, the problem returns after some testing and coding.
      Removing and Re-downloading Market Replay does not help.

      Deleting NinjaTrader.sdf does not help.

      I'm using version 64-bit.
      I had tried repairing but the problem persists.

      The weird position size also sometimes does mysteriously appear under the position of the strategy tab.
      (See photo)

      Then again, I have another strategy that is running in a real-time simulation (forward testing) that is getting weirdly huge positions.
      using Position.Quantity. on another PC and NT8 (

      Is there a way to clean caches caused by strategy testing if there are any?
      Attached Files
      Last edited by johnnybegoode; 03-07-2019, 12:00 AM.


        Hello johnnybegoode,

        What is the issue you are trying correct?

        You are expecting the very large positions with the extremely large number of entry orders right?
        The logic of the script is allowing for many new entries in the same direction.

        The inquiry is why is the 'EXIT LONG EURUSD (ProfitTarget)' appearing early in the orders display, is this correct?

        The exit amount appears correct, but all of the entry orders that make up the position are missing, is this correct?

        And to confirm, using the script I have provided, the exit appearing to early occurred on your end?

        At what time and date, and on what instrument did you start the playback?
        At what time and date did the early exit appear?

        Chelsea B.NinjaTrader Customer Service


          I reset and cleared the history of 2 Simulated FXCM accounts, but there are still POSITION and UNREALIZED under the strategy tab that I could not get rid of.
          I'd even tried deleting the whole DB folder.
          Attached Files
          Last edited by johnnybegoode; 03-21-2019, 12:32 AM.


            Hello johnnybegoode,

            The Strategies tab is showing a strategy position not an account position.

            This means you have either run the strategy once before or you have enabled it and there are historical orders.

            You can code the script to not evaluate historically with if (State == State.Historical) return; and then remove the instances of the strategy and add a new instance.
            Chelsea B.NinjaTrader Customer Service


            Latest Posts


            Topics Statistics Last Post
            Started by JulieG, Today, 12:40 AM
            0 responses
            Last Post JulieG
            by JulieG
            Started by jimjoe123, Yesterday, 09:28 PM
            2 responses
            Last Post NinjaTrader_ChrisSch  
            Started by 12VMan, 08-20-2019, 09:29 AM
            2 responses
            Last Post Lancer
            by Lancer
            Started by tgn55, Yesterday, 09:50 PM
            0 responses
            Last Post tgn55
            by tgn55
            Started by tgn55, Yesterday, 08:57 PM
            1 response
            Last Post NinjaTrader_ChrisSch