Announcement

Collapse
No announcement yet.

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.

      Comment


        #4
        Thanks! Greatly appreciate it.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        79 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        40 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        63 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        63 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        54 views
        0 likes
        Last Post CarlTrading  
        Working...
        X