Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit at price instead of when bar close

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

    Exit at price instead of when bar close

    hello

    i have this code
    if (Low[0] <= StopLoss && Position.MarketPosition == MarketPosition.Long && Position.AveragePrice > StopLoss)

    {
    Print(" STOP LOSS HIT: " + StopLoss + " " + Close[0] + " symbol " + Instrument.FullName);
    // Trailing stop has been hit; do whatever you want here
    ExitLong(Convert.ToInt32(NumberOfContract),"Stop Loss" ,"Long");

    what is happening let us say the stop loss is at 2988 and price is trading at 3000 and started going down on the 5 min candle, it will hit the 2988 and keeps going down, the trade will close when that 5 min close instead of the 2988 how can I change the code so that when price hit 2988 it closes. in other words what is the code to set a stop buy limit order at 2988

    any help is appreciated

    #2
    Hello babouin77,

    Thank you for your reply.

    In order for the exit order to be submitted right at the time the desired price is hit, you would need to run the script On Each Tick instead of On Bar Close. If you want your entries to be On Bar Close and your exits to be On Each Tick, you can certainly separate logic so some logic is only performed on the first tick of a bar and some logic executes on every tick. Please see the help guide example below for more information about separating your logic in this way:



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      ok fine but how can I put a buy limit order at the price (where I see on the chart the buy limit order)

      Comment


        #4
        Hello babouin77,

        Thank you for your reply.

        To clarify, are you wanting to place a sell limit order? If you are in a long position, a buy limit order would increase your position if hit, not close your current long position.

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          that is correct my bad... how can I do that instead of my code above.

          Comment


            #6
            Hello babouin77,

            Thank you for your note.

            Is there a reason you're not simply using SetStopLoss to automatically set a stop loss for you when an order fills? For example, here's an example from our help guide that goes over how you can place a stop and target when your order fills and even modify your stop loss if you wish:



            That way there's already a protective order which you can modify if necessary and you don't have to worry about placing one later.

            Thanks in advance; I look forward to assisting you further.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              I am trailing with the indicator and on every close the stop is moving with the indicator. I used calculation.price but it is not doing it. r u saying instead of using exit long use setstoploss ? i looked at this example the stop did not trail up as it is supposed to

              Comment


                #8
                Hello babouin77,

                Thank you for your reply.

                The example shows how you can move the stop loss after it's placed - in this particular example it simply moves once and you'd need to modify the logic to move it more than once. I will create a more specific example and post it for you tomorrow.

                Thanks in advance; I look forward to assisting you further.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  thank you i appreciate it.

                  Comment


                    #10
                    Hello babouin77,

                    Thank you for your patience.

                    I've located several good examples made by one of my colleagues that may be helpful - there's a version for using Set methods as well as one using Exit methods:



                    Please let us know if we may be of further assistance to you.
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      i used set stop loss at the stop loss level it just did not work. i thoguht there was an easier way to make it exit at the price level rather than the close of the candle. I want to enter at the close but exit when certain price is hit

                      Comment


                        #12
                        Hello babouin77,

                        Thank you for your reply.

                        I would refer to the previous example provided in post #2 on how to separate some logic to calculate once per bar and have some logic calculate on each tick. This shows exactly how to enter only once per bar but to monitor for exit conditions and allow intrabar exits.

                        Please let us know if we may be of further assistance to you.
                        Kate W.NinjaTrader Customer Service

                        Comment


                          #13
                          the example shows entry per tick and exit per bar... do you have one entry per bar and exit per tick? or did i read that wrong?

                          Comment


                            #14
                            Hello babouin77,

                            Thank you for your reply.

                            No, in that example entries can only be submitted once per bar, on the first tick of the bar. However, exits may occur on each tick of the bar. I would suggest importing and testing this, as well as viewing the code in the NinjaScript editor as there are comments in the code to help clarify the logic.

                            Please let us know if we may be of further assistance to you.
                            Kate W.NinjaTrader Customer Service

                            Comment


                              #15
                              ok will do. thanks for the explanation.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by AveryFlynn, Today, 04:57 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post AveryFlynn  
                              Started by RubenCazorla, 08-30-2022, 06:36 AM
                              3 responses
                              77 views
                              0 likes
                              Last Post PaulMohn  
                              Started by f.saeidi, Yesterday, 12:14 PM
                              9 responses
                              23 views
                              0 likes
                              Last Post f.saeidi  
                              Started by Tim-c, Today, 03:54 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Tim-c
                              by Tim-c
                               
                              Started by FrancisMorro, Today, 03:24 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post FrancisMorro  
                              Working...
                              X