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 NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        65 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        139 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X