Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Open position with take profit and stop loss

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

    Open position with take profit and stop loss

    Hi,

    I want to run a strategy that opens position if the price crosses above previous bar's high.
    So, what I do is at the end of the current bar, I need to place buy limit order for one cent above the high of the current bar.
    In addition, in case the order is executed, then I want to place stop limit and take profit orders.
    The problem is that I don't how to do it

    I assume that I need to call NTBuyStopLimit("1", 100, High[0] + 0.01);
    I have the following questions:
    1. How do I know if the order was executed
    2. How to place stop limit and take profit orders
    3. How can I move the take profit and stop limit orders (manage position) ?

    10x,

    #2
    Hi Shayhz,

    Thank you for posting.

    You are correct in your buy stop limit order. However, I would use High[0] + 1*TickSize instead so that if you changed instruments and the tick size wasn't 0.01 it would place the order one tick above the High[0] price.

    You would place SetStopLoss and SetProfitTarget in the Initialize section of the code.
    I'm attaching two links on the SetStopLoss and SetProfitTarget.
    SetProfitTarget()
    SetStopLoss()

    I am also attaching a link to a reference sample on how to change the StopLoss and Profit Target with your position.
    Sample Price Modification on Stop Loss and Profit Target

    You can also use the TraceOrders = true in the initialize section to see if your orders are getting executed correctly or rejected.
    TraceOrders

    Please let me know if I can be of further assistance
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Thanks.
      I still don't understand when to place the stoploss and and take profit orders.
      You wrote to put it in the initialize method, but as far as I understand it, I need to place it only after I know that my buyStopLimit order was executed.
      So, I expect to have an event when an order is executed and then to place these orders.

      10x,

      Comment


        #4
        Hello shayhz,

        To clarify are you using the "DLL" Interface to submit order to NinjaTrader or are you trying to submit them via a NinjaScript Automated Strategy?



        Happy to be of further assistance.
        JCNinjaTrader Customer Service

        Comment


          #5
          I'm using NT with connection to MB Trading.
          I'm new to NT, so the only option I know is to open positions with NinjaScript.

          Comment


            #6
            Hello shayhz,

            Thanks for the clarification.

            We have quite a few reference samples that you may view that have this at the following link.

            Following are links to all available NinjaScript reference samples within this forum section: Strategy Reference Samples (NinjaTrader 8) - also applicable to NT7 Using a time filter to limit trading hours (http://www.ninjatrader.com/support/forum/showthread.php?t=3226) Using multiple entry/exit signals simultaneously



            You may want to start out by view the following sample that setups up the Stop Loss and Profit Target and Monitors them.




            Happy to be of further assistance.
            JCNinjaTrader Customer Service

            Comment


              #7
              Thanks for the info.
              I now understand how where to place the order and how to watch its progress (using OnExecution event).
              The only issue that I have (so far) is to set stop loss and take profit orders.
              I don't want to set it in the initialize method, since it changes according to the entry price and other things (e.g. candle size).

              So, I want to set it in the OnExecution method.
              I'm trying to setStopLoss and setProfitTarget but it takes only one of them.
              I assume that the reason is that according to the documentation, you can't call both of them (If a profit target order is generated in addition to a stop loss order, they are submitted as OCO (one cancels other)).

              Please advice.

              10x,

              Comment


                #8
                Hello shayhz,

                Thank you for your response.

                Please provide the code for theOnExecution() method and the Set() methods used in your strategy so that I may investigate this matter further.

                Comment


                  #9
                  protected override void OnExecution(IExecution execution)
                  {
                  if (execution == null)
                  {
                  return;
                  }

                  IOrder order = execution.Order;

                  if (order == null)
                  {
                  return;
                  }

                  try
                  {
                  if ((order.OrderState == OrderState.Filled) || (order.OrderState == OrderState.PartFilled))
                  {
                  //order was filled
                  //check if the order was buy or sell short (new position was opened)
                  if ((order.OrderAction == OrderAction.Buy) || (order.OrderAction == OrderAction.SellShort))
                  {
                  //new position is opened
                  //SetStopLoss("1",CalculationMode.Ticks, 1*TickSize*20, true);
                  SetStopLoss(order.FromEntrySignal,CalculationMode. Price, order.AvgFillPrice - TickSize*20, true);
                  SetProfitTarget(order.FromEntrySignal, CalculationMode.Price, order.AvgFillPrice + TickSize*20);
                  }
                  }
                  }
                  catch (Exception e)
                  {
                  Log(e.Message, LogLevel.Information);
                  }
                  }

                  Comment


                    #10
                    Hello shayhz,

                    Thank you for your response.

                    Which one is taken? The Stop Loss or Profit Target, or is it random?

                    Do you receive any errors messages in the Log tab of the NinjaTrader Control Center when the orders should be submitted?

                    I look forward to your response.

                    Comment


                      #11
                      Only the stop loss is taken and I don't have any error in the log.
                      BTW, according to the documentation, the take profit should be taken and the SL should be canceled.

                      Comment


                        #12
                        Hello shayhz,

                        Thank you for your response.

                        That makes sense, as you mentioned earlier they are submitted as OCO.

                        You may wish to use Exit() orders instead: http://www.ninjatrader.com/support/h...d_approach.htm

                        Please let me know if you have any questions.

                        Comment


                          #13
                          Hi,

                          Sorry, but I don't understand...
                          Should I still enter a position using buyStopLimit ? if not, this is problematic for because this is my all strategy.

                          About the exit, I want to manage the position and move stops by placing stop loss and take profit for my open position and I don't see how to do it.

                          Please advice.

                          10x,

                          Comment


                            #14
                            Hello shayhz,

                            Thank you for your response.

                            So let's break this down; you wish to submit the Profit Target and Stop Loss together but do not want the other to cancel when the other is filled? Is this correct?

                            For the entry what exactly are you asking here? Please detail what you are expecting to see and what is occurring.

                            Comment


                              #15
                              Hi,

                              Let's assume I want to have the following strategy:
                              In case the last candle is green, then enter a long position if the price crosses above the high of the current bar.
                              Once I enter a position, I want to place stop loss and take profit order according to the size of the last candle (not constant SL or TP).
                              Furthermore, in case the price goes up, I want to move the SL and TP with it (manage the posistion).

                              Is it possible to have a strategy like this ?

                              10x,

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Mizzouman1, Today, 07:35 AM
                              3 responses
                              17 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by RubenCazorla, Today, 09:07 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by i019945nj, 12-14-2023, 06:41 AM
                              7 responses
                              82 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by timmbbo, 07-05-2023, 10:21 PM
                              4 responses
                              158 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by tkaboris, Today, 08:01 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X