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

Detecting exit via profit target or stop loss

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

    Detecting exit via profit target or stop loss

    Hi,
    In writing a Ninjascript strategy, I sometimes want to make sure I am only in one trade at a time. I usually do this by setting a boolean variable when I enter a trade and resetting it when I exit a trade. But if I exit the trade via a profit target or stop loss, how do I detect this?
    Or is there an easier way to prevent the strategy entering more than one trade at a time?
    Thanks.

    #2
    I would use the OnPositionUpdate function - maybe for all of your exits so you can reset your boolean in one place. You'll have to check the new position - flat, long, short etc.

    hope this helps
    Dave

    Comment


      #3
      You would either need to work with the Exit() methods to be able to work with the IOrder objects to check for the individual fillstates - http://www.ninjatrader-support.com/H...V6/IOrder.html

      A simpler way would be working with Position.MarketPosition to check the states as Dave mentioned - http://www.ninjatrader-support.com/H...tPosition.html
      BertrandNinjaTrader Customer Service

      Comment


        #4
        Thank you both for your replies. I assume that a market position of flat means that you are out of the trade. Is that correct? Also, is there any difference between using the OnPositionUpdate function as Dave suggests and just checking the value of Position.MarketPosition in the OnBarUpdate function? I notice that if I use the strategy wizard to check if the value of current market position is flat, it translates that to
        if
        (Position.MarketPosition == MarketPosition.Flat)
        in OnBarUpdate(). Can you access OnPositionUpdate from the wizard?
        Thanks for your help.
        Regards,
        Annette

        Comment


          #5
          Yes, flat means you're out of the trade.

          The reason I say use OnPositionUpdate is that that will be called when a position changes (e.g. log to flat) On BarUpdate will only be called with a new trade price. That may or may not be when you want to check the position.

          Looking at what you want to do though, I'd say one that you may want to do something like this -

          OnBarUpdate

          - check position first
          - if flat, check for trade entry
          - if not flat, manage position (check profit, decide when to exit etc)

          Comment


            #6
            Thanks for the advise Dave - Annette, unfortunately you can't work in OnPositionUpdate() in the wizard.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by sightcareclickhere, Today, 01:55 PM
            0 responses
            1 view
            0 likes
            Last Post sightcareclickhere  
            Started by Mindset, 05-06-2023, 09:03 PM
            9 responses
            258 views
            0 likes
            Last Post ender_wiggum  
            Started by Mizzouman1, Today, 07:35 AM
            4 responses
            18 views
            0 likes
            Last Post Mizzouman1  
            Started by philmg, Today, 01:17 PM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_ChristopherJ  
            Started by cre8able, Today, 01:01 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X