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

Trying to average in after checking Positions[]

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

    Trying to average in after checking Positions[]

    I have a multi-instrument strategy and wrote a dictionary to keep track of the entries. As this is a short duration trade I have it set to exit after 4 bars and removes the entry for the dictionary to keep from triggering after the trade is over. The issue is that the strategy doesn't average in because the MarketPosition is always flat. I have ran through all the available positions[] and they are all flat even after entering into a position. Is there a specific way to get the positions managed by the strategy? I am also unsure on how to remove the dictionary entry after tp/sl of the original entry as I had been using the Positions[].MarketPosition to check if there is still a position but again, it doesn't seem to work as it always returns MarketPosition.Flat regardless of which Position array I check

    Code:
                foreach (KeyValuePair<string, tradestruct> position in openPositionsDict)
                {
                    if (position.Value.barID == BarsInProgress)
                    {
                        if (BarsSinceEntryExecution(position.Value.barID, position.Value.signalname, 0) > 4)
                        {
                            if (position.Value.direction == "long")
                            {
                                ExitLong(position.Value.barID, position.Value.qty, position.Value.signalname + "TimeoutExit", position.Value.signalname);
                                ExitLong(position.Value.barID, position.Value.qty, position.Value.signalname + "AverageTimeoutExit", "Average in Long" + position.Value.signalname);
                                itemsToRemove.Add(position.Key);
                            }
                        }
                    }
                    if (position.Value.tickBarID == BarsInProgress && Positions[position.Value.tickBarID].MarketPosition != MarketPosition.Flat)
                    {
                        if (position.Value.direction == "long")
                        {
                            if (position.Value.buyprice > Closes[position.Value.tickBarID][0])
                            {
                                Print("Buy in now");
                                // Set profit target
                                SetProfitTarget("Average in Long" + position.Value.signalname, CalculationMode.Price, position.Value.tp);
                                // Set stop loss
                                SetStopLoss("Average in Long" + position.Value.signalname, CalculationMode.Price, position.Value.sl, false);
                                // Enter Long
                                EnterLong(position.Value.tickBarID, position.Value.qty, "Average in Long" + position.Value.signalname);
                                itemsToRemove.Add(position.Key);
                            }
                        }
                    }
                }
                
                foreach (string item in itemsToRemove)
                {
                    openPositionsDict.Remove(item);
                }
                itemsToRemove.Clear();​
    Last edited by mintos; 02-05-2024, 02:08 PM.

    #2
    Hello mintos,

    Thank you for your post.

    Are you adding the instruments using AddDataSeries()?



    To see if the positions are being properly updated, please print both the position.ToString() in OnPositionUpdate(), and the execution.ToString() in OnExecutionUpdate().

    https://ninjatrader.com/support/helpGuides/nt8/onpositionupdate.htm

    https://ninjatrader.com/support/helpGuides/nt8/onexecutionupdate.htm



    Note that positions won't be changed right after an order is submitted in OnBarUpdate(), as the orders take time to fill and change the position. This is why we are printing them in OnPositionUpdate() and OnExecutionUpdate().

    I look forward to your response.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Ok, So I do get an output from those commands. I have attached those output as a .txt file. However I still get MarketPosition.Flat in all the Positions[].MarketPosition indexes. I use AddDataSeries and have 3 charts aside from the main chart.
      Code:
      //MainChart
      AddDataSeries(Ticker1, Data.BarsPeriodType.Minute, Timeframe1);
      //Tick Chart
      AddDataSeries(Ticker1, Data.BarsPeriodType.Minute, Timeframetick);
      //HT Trend Chart
      AddDataSeries(Ticker1, Data.BarsPeriodType.Minute, TimeframeZigZag);
      Attached Files

      Comment


        #4
        Hello mintos,

        Is the primary instrument also YM 03-24? Your AddDataSeries() calls are not adding a new instrument, just the same instrument in different time frames. This would mean this is not a multi instrument script, therefore only Posiitons[0] is going to be updated.

        From the Help Guide:

        "Index value is based on the the array of Bars objects added via the AddDataSeries() method. For example:

        First Bars is ES 1 Minute
        Secondary Bars is ES 5 Minute
        Third Bars is NQ 5 Minute

        Positions[0] == ES position
        Positions[1] == Always a flat position, ES position will always be Positions[0]
        Positions[2] == NQ position"

        Positions - https://ninjatrader.com/support/help.../positions.htm

        Please let me know if you have any further questions. ​
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Hello, the primary instrument is NQ, as the original code is rather long I only posted a small portion. In total there are 4 instruments that are being traded and each has 3 timeframes. Primary instrument is just randomly selected by me to get the strategy going. What would be the Positions[] for each of the instrument? Does it default to smallest timeframe or whichever is called first?
          Code:
           
                          Ticker1 = "YM 03-24";
                          Ticker2 = "FDAX 03-24";
                          Ticker3 = "ES 03-24";
                          Ticker4 = "NQ 03-24";​
                          // Main chart
                          AddDataSeries(Ticker1, Data.BarsPeriodType.Minute, Timeframe1);
                          AddDataSeries(Ticker2, Data.BarsPeriodType.Minute, Timeframe2);
                          AddDataSeries(Ticker3, Data.BarsPeriodType.Minute, Timeframe3);
                          AddDataSeries(Ticker4, Data.BarsPeriodType.Minute, Timeframe4);
                          // Tick chart
                          AddDataSeries(Ticker1, Data.BarsPeriodType.Minute, Timeframetick);
                          AddDataSeries(Ticker2, Data.BarsPeriodType.Minute, Timeframetick);
                          AddDataSeries(Ticker3, Data.BarsPeriodType.Minute, Timeframetick);
                          AddDataSeries(Ticker4, Data.BarsPeriodType.Minute, Timeframetick);
                          // HT Trend chart
                          AddDataSeries(Ticker1, Data.BarsPeriodType.Minute, TimeframeZigZag);
                          AddDataSeries(Ticker2, Data.BarsPeriodType.Minute, TimeframeZigZag);
                          AddDataSeries(Ticker3, Data.BarsPeriodType.Minute, TimeframeZigZag);
                          AddDataSeries(Ticker4, Data.BarsPeriodType.Minute, TimeframeZigZag);​
          Last edited by mintos; 02-06-2024, 04:31 PM.

          Comment


            #6
            Hello mintos,

            It go in the order in which the data series is added, starting with the primary series.

            Positions[0] will be your primary NQ instrument.
            Positions[1] will be a flat position. An NQ position will always be Positions[0].
            Positions[2] will be a YM position.

            And so on, as described in the Positions help guide page from my previous post.

            Please let me know if you have any further questions.
            Gaby V.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Jonafare, 12-06-2012, 03:48 PM
            5 responses
            3,986 views
            0 likes
            Last Post rene69851  
            Started by Fitspressorest, Today, 01:38 PM
            0 responses
            2 views
            0 likes
            Last Post Fitspressorest  
            Started by Jonker, Today, 01:19 PM
            0 responses
            2 views
            0 likes
            Last Post Jonker
            by Jonker
             
            Started by futtrader, Today, 01:16 PM
            0 responses
            8 views
            0 likes
            Last Post futtrader  
            Started by Segwin, 05-07-2018, 02:15 PM
            14 responses
            1,792 views
            0 likes
            Last Post aligator  
            Working...
            X