• 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.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position.Quantity add up open positions weird

Collapse
X
  • 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).


    Code:
    //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)
                    )
                {
                    EnterLong(Quantity,"EnterLong");
                }
    
                double lastentryprice = 0;
                if (entryOrder != null && entryOrder.OrderState == OrderState.Filled)
                lastentryprice = entryOrder.AverageFillPrice;    
                Print(lastentryprice);    
    
                if (
                    (lastentryprice >=  Open[0])
                    && (PositionAccount.Quantity >= 1)
                    && (BarsInProgress == 0)
                    )
                {
                      EnterLong(0,Quantity,"EURUSD1");    
                }    
    
                if     (
                    Profit>= ProfitTarget
                    )                             
                {                        
                    ExitLong(0,Position.Quantity,"EXIT LONG EURUSD (ProfitTarget)","");
                }
    
    
            }                
        }
    }
    Attached Files

    #2
    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.
    https://drive.google.com/file/d/141h...w?usp=drivesdk

    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.
    http://ninjatrader.com/support/helpG...-us/export.htm
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      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 8.0.17.2 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 (8.0.17.0)

      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.

      Comment


        #4
        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

        Comment


          #5
          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.

          Comment


            #6
            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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by pjsmith, Today, 09:48 AM
            1 response
            7 views
            0 likes
            Last Post Alfred
            by Alfred
             
            Started by bc24fl, Today, 10:03 AM
            0 responses
            7 views
            0 likes
            Last Post bc24fl
            by bc24fl
             
            Started by Trader17, Yesterday, 07:50 PM
            1 response
            16 views
            0 likes
            Last Post NinjaTrader_Jim  
            Started by ct_fx, Today, 05:14 AM
            1 response
            15 views
            0 likes
            Last Post NinjaTrader_ChrisL  
            Started by HinduMVP, Today, 09:18 AM
            1 response
            8 views
            0 likes
            Last Post HinduMVP  
            Working...
            X