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 nandhumca, Today, 08:47 PM
                0 responses
                2 views
                0 likes
                Last Post nandhumca  
                Started by BullyWig Trader, Today, 07:15 PM
                0 responses
                2 views
                0 likes
                Last Post BullyWig Trader  
                Started by ezrollin, Today, 05:13 PM
                1 response
                10 views
                0 likes
                Last Post ezrollin  
                Started by ineuron, Today, 04:29 PM
                0 responses
                8 views
                0 likes
                Last Post ineuron
                by ineuron
                 
                Started by topa11, Today, 04:21 PM
                0 responses
                15 views
                0 likes
                Last Post topa11
                by topa11
                 
                Working...
                X