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].


    You may also want to review the section on advanced order handling for more details on using IOrders.


    Here is some reference code for monitoring orders.


    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

          BarsSinceEntry

          MarketPosition
          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 Gerik, Today, 09:40 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by RookieTrader, Today, 09:37 AM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by alifarahani, Today, 09:40 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by KennyK, 05-29-2017, 02:02 AM
                3 responses
                1,285 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by AttiM, 02-14-2024, 05:20 PM
                11 responses
                186 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X