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

how to know x bars ago market position ?

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

    how to know x bars ago market position ?

    Hi is there how to know if 3 bars ago position was Long ?

    string myPosition = Position.MarketPosition == MarketPosition[3] == "Long" ? "Long" : (Whatever);


    #2
    Hello fercho,

    Thanks for your post.

    There are no documented/supported methods for checking if you were in a long position 3 bars ago. This would require custom calculations.

    You could consider setting creating a Series<bool> variable named something like 'longPos', setting it to true when Position.MarketPosition == MarketPosition.Long, and setting the Series<bool> variable to false when you are not in a long position. Then you could check if longPos[3] is true to see if you are in a long position 3 bars ago.

    See the help guide documentation below for more information.

    Series<T>: https://ninjatrader.com/support/help...t8/seriest.htm
    Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandom, thanks for your reply, asked also to ChatGPT and answered:

      // Define a variable to store the market position three bars ago
      private MarketPosition positionThreeBarsAgo;

      protected override void OnBarUpdate()
      {
      // Check if there are enough bars to access the position three bars ago
      if (CurrentBar > 2)
      {
      // Retrieve the market position three bars ago
      positionThreeBarsAgo = Position.GetAt(CurrentBar - 3).MarketPosition;

      // Check if the position three bars ago was "Long"
      if (positionThreeBarsAgo == MarketPosition.Long)
      {
      // Your custom logic for when the position was "Long" three bars ago
      Print("Position was Long three bars ago");
      }
      }
      }


      proble is that GetAt is not recognized by NinjaTrader,

      by the way your answer is sort of what I'm currently using thanks

      Comment


        #4
        Hello fercho,

        Thanks for your post.

        GetAt() is a non-existent NinjaScript method.

        From our experience at this time, ChatGPT is not quite adequate to generate valid compilable NinjaScripts that function as the user has intended. We often find that the generated code will call non-existent properties and methods, use improper classes or inheritance, and may have incorrect logic. We highly encourage that you create a new NinjaScript yourself using the NinjaScript Editor, and use the code generated by ChatGPT as more as suggestions and guides when coding the script yourself, than using the actual code generated.

        While It would not be within our support model to correct these scripts at user request, we would be happy to provide insight for any direct specific inquiries you may have if you would like to create this script yourself. Our support is able to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.​
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        6 responses
        3,290 views
        1 like
        Last Post jgualdronc  
        Started by Touch-Ups, Today, 10:36 AM
        0 responses
        8 views
        0 likes
        Last Post Touch-Ups  
        Started by geddyisodin, 04-25-2024, 05:20 AM
        8 responses
        61 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by Option Whisperer, Today, 09:55 AM
        0 responses
        8 views
        0 likes
        Last Post Option Whisperer  
        Started by halgo_boulder, 04-20-2024, 08:44 AM
        2 responses
        24 views
        0 likes
        Last Post halgo_boulder  
        Working...
        X