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 Mindset, 04-21-2026, 06:46 AM
        0 responses
        88 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        134 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        68 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        119 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        69 views
        0 likes
        Last Post PaulMohn  
        Working...
        X