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 bortz, 11-06-2023, 08:04 AM
        47 responses
        1,607 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        9 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        19 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        6 views
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        16 views
        0 likes
        Last Post Javierw.ok  
        Working...
        X