Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close if bar closes above entry bar

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

    Close if bar closes above entry bar

    Hi,

    I want to implement an exit strategy which is based on current bar closing above the entry price + a fixed number of ticks. Can you provide references where I can look up the code or provide me the reference code?

    Regards,

    Sandesh Rai

    #2
    Hello Sandesh,

    You will want to use IOrder in order to monitor the AverageFillPrice of your entry plus fixed number of ticks. You would then compare this to the close of the current bar with Close[0].
    http://www.ninjatrader.com/support/h...tml?iorder.htm

    You may also want to review the section on advanced order handling for more details on using IOrders.
    http://www.ninjatrader.com/support/h...r_handling.htm

    Here is some reference code for monitoring orders.
    http://www.ninjatrader.com/support/f...ead.php?t=5790

    Please let me know if I can be of further help.
    LanceNinjaTrader Customer Service

    Comment


      #3
      I could not understand this. I am not a c# programmer but I have been modifying existing code to make logic changes quite well.

      How would you code the following logic:

      1. Store trade entry price.
      2. If trade is still open, exit if any bar closes above entry price + 5 ticks (for short trades) and entry price - 5 ticks ( for long trades)

      OR

      1. Store trade entry price.
      2. If trade is still open, exit if any bar closes above high of entry bar (for short trades) and exit if any bar closes below the low of entry bar (for long trades)

      Comment


        #4
        Just do something like

        if (Close[0] >= Position.AvgPrice + (5 * TickSize))
        //exit short

        Comment


          #5
          Originally posted by sandeshrai View Post
          1. Store trade entry price.
          You can store the entry price with
          price = Position.AvgPrice;
          2. If trade is still open, exit if any bar closes above entry price + 5 ticks (for short trades) and entry price - 5 ticks ( for long trades)
          Code:
          if (Position.MarketPosition == MarketPosition.Long) 
          {     
          if (Close[0] <= Position.AvgPrice - (5 * TickSize))
          //exit long
          }
          
          if (Position.MarketPosition == MarketPosition.Short) 
          {
          if (Close[0] >= Position.AvgPrice + (5 * TickSize))
          //exit short     
          }
          2. If trade is still open, exit if any bar closes above high of entry bar (for short trades) and exit if any bar closes below the low of entry bar (for long trades)
          Code:
          if (Position.MarketPosition == MarketPosition.Long) 
          {        
          if (Close[0] <= Low[BarsSinceEntry()])
          //exit long
          }
          
          if (Position.MarketPosition == MarketPosition.Short) 
          {
          if (Close[0] >= High[BarsSinceEntry()])
          //exit short         
          }
          Position
          http://www.ninjatrader.com/support/h...?iposition.htm
          BarsSinceEntry
          http://www.ninjatrader.com/support/h...sinceentry.htm
          MarketPosition
          http://www.ninjatrader.com/support/h...etposition.htm
          LanceNinjaTrader Customer Service

          Comment


            #6
            Thanks Lance and Radical for your help. That worked very well.

            I would also appreciate your help to code the following logic:

            -After a trade is entered if any of the subsequent candles goes say +10 ticks and thereafter starts going negative again, exit the trade if it closes below the avg entry price (for long trades).

            Comment


              #7
              For a setup like this you would need to check if Close[0] > entry price +10 ticks

              Once this was true you would likely use a bool value to track this condition as true.

              You could then do something like the following

              if(YOURBOOLVALUE)
              {
              //check Close[0] to see if it's going < entry price
              //if it is exit
              }

              Please note: this is pseudo code and will not compile as is
              LanceNinjaTrader Customer Service

              Comment


                #8
                Thanks. That was very useful. I have been able to implement and test it. I am also getting better with my C#

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by newton1, Yesterday, 09:54 AM
                2 responses
                61 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by SuperDude123, Yesterday, 08:31 AM
                3 responses
                100 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by jeliner, Yesterday, 12:58 AM
                1 response
                32 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by ezrollin, Yesterday, 12:29 AM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by peregrine, Today, 05:49 PM
                0 responses
                13 views
                0 likes
                Last Post peregrine  
                Working...
                X