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

First tick on Position

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

    First tick on Position

    I have a use for setting variables when Position.MarketPosition == MarketPosition.Long only at the first tick of when it is Long. I don't need it setting this variable during every bar of the Long position - just during the first tick when Long is detected. Is there something like IsFirstTickOfLong just as there is IsFirstTickOfBar?

    If not, it could be a useful feature I presume.

    #2
    Hello Bluebeep,

    There would not be anything like this in NinjaScript as this would relate to your custom logic and how you want it to work. You can accomplish this type of goal by using variables and logic along with the existing script properties.

    If your script is already running OnEachTick, that would solve part of the question as you could perform checks for each tick. Checking for the position is also something you can do from code so really the only part you would need to figure out would be the setting it once per position part.

    This could likely be accomplished by using an object, or even the order's object.

    Here is one simple way to do that:



    Code:
    private object placeholderObject = null;
    protected override void OnBarUpdate()
    {
        if(Position.MarketPosition == MarketPosition.Flat)
        {
            placeholderObject = null;    
            //when you are flat, reset the object to null so the next time a position is entered you can do this again
        }
        if(Position.MarketPosition != MarketPosition.Flat && placeholderObject == null)
        {
            placeholderObject = new object();
            //set your variables here one time
        }
    
    }
    A different approach could be to use the OnExecutionUpdate override to observe when an order fills, you could assign the Order object to a variable similar to what is shown above. Using the order object and position information you could accomplish the same general task of having a null/not null variable to control timing.


    I look forward to being of further assistance.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks NinjaTrader_Jesse. This will work

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Kaledus, Today, 01:29 PM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by frankthearm, Yesterday, 09:08 AM
      13 responses
      45 views
      0 likes
      Last Post frankthearm  
      Started by PaulMohn, Today, 12:36 PM
      2 responses
      16 views
      0 likes
      Last Post PaulMohn  
      Started by Conceptzx, 10-11-2022, 06:38 AM
      2 responses
      55 views
      0 likes
      Last Post PhillT
      by PhillT
       
      Started by yertle, Yesterday, 08:38 AM
      8 responses
      37 views
      0 likes
      Last Post ryjoga
      by ryjoga
       
      Working...
      X