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

Resetting counters when transitioning to "flat"

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

    Resetting counters when transitioning to "flat"

    I have written a strategy that can enter a trade after the market opens.

    For the sake of discussion, an example is below:

    Let's say it goes "Long", remains in a trade for 60 minutes, then exits that trade and goes "Flat". 45 minutes later, an entry signal is valid again and goes "Short" for 150 minutes, etc.

    The trade might "Open" and "Close" multiple times during the trading day.

    When the trade transitions from "Long/Short" to "Flat", I would like to reset certain counters to "zero". The issue is the counter can start before the trade is "Long/Short", so having the counter equal "zero" when flat will not work.

    I also have a dozen "Exit" possibilities on my trades. (They are in various locations/constructs around the strategy.) I am trying to find a way not to type the following in every location I have "Exit" code:

    CounterAA = 0
    CounterBB = 0
    CounterCC = 0
    etc.

    Also, the strategy is enabled on hundreds of stocks at the same time.


    What NinjaScript code can I use to reset the counters when a trade is completely cycled through and transitions to "flat", but is still available for use?

    Please be specific where it would be placed, and how it would be used: OnBarUpdate(), OnStateChange(), OnOrderUpdate(), etc.

    Thanks in advance.
    Last edited by ArmKnuckle; 08-07-2020, 03:33 PM.

    #2
    Have you looked at OnPositionUpdate()?

    You'll only get events where,

    Code:
    position.MarketPosition == MarketPosition.Flat
    after a position has truly exited.

    Also, I believe OnPositionUpdate has a BarsInProgress context, so you'll get
    all the various Long/Short/Flat events for each stock that you take positions in.

    Comment


      #3
      Hello ArmKnuckle, thanks for your question.

      We have the OnPositionUpdate() event method for this purpose:



      That event is called when the position changes from any other position state. There's an example use case on that page.

      If you have any further questions please let me know.

      Chris L.NinjaTrader Customer Service

      Comment


        #4
        Thanks! Greatly appreciate it.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by geddyisodin, 04-25-2024, 05:20 AM
        8 responses
        60 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        4 responses
        3,287 views
        1 like
        Last Post jgualdronc  
        Started by Option Whisperer, Today, 09:55 AM
        0 responses
        5 views
        0 likes
        Last Post Option Whisperer  
        Started by halgo_boulder, 04-20-2024, 08:44 AM
        2 responses
        22 views
        0 likes
        Last Post halgo_boulder  
        Started by mishhh, 05-25-2010, 08:54 AM
        19 responses
        6,189 views
        0 likes
        Last Post rene69851  
        Working...
        X