Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Track if Profit Target Price Touched Before Entry

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

    Track if Profit Target Price Touched Before Entry

    Hello,

    Need help on code development and needed help to determine if Ninjascript already has this code/logic developed somewhere.

    I trade on limit entry orders.

    Code Needed:

    1. While the strategy is running, how can I track if a certain price on the chart is touched/traded?

    I trade breakout of support and resistances, and I do not want to enter a trade if target price was touched before limited order filled on retracement

    If Limit Order is Open and Profit Target Price is Touched, I need the strategy to cancel the order.

    Thanks,

    #2
    Hello simple_goodoboy,

    You can use the Close[0] price to track the current price. If CalculateOnBarClose is true or the script is processing historical data, this will update when the bar closes. If CalculateOnBarClose is false and the script is processing realtime data this will be continuously updated with the current price.

    For example:

    if (CrossAbove(Close, 1000.25, 1))
    {
    Print("price has crossed above 1000.25");
    }

    This would print when the price crosses above 1000.25.



    You can also use GetCurrentAsk and GetCurrentBid instead of the close price in real-time only.




    With support and resistance these generally come from the Pivots indicator, however, you may be referring to a custom indicator.

    For example:
    protected override void OnBarUpdate()
    {
    double R2 = Pivots(PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 100).R2[0];
    Print(R2);
    }



    For checking the price or states of any order, this can be done with an IOrder object.


    In general any custom strategies would need to be custom programmed.
    However, below I am providing a link to the file sharing section of the forums for NinjaTrader 7 strategies where you can look for any strategies you may want to use or copy code from.


    Last, below is a link with some general information about getting started with NinjaScript.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for responding ChelseaB,

      You provided some great ideas for me because and my trading style of entering on limit orders and tracking if my predefined profit target price is touched before before limit entry order.

      Originally posted by NinjaTrader_ChelseaB View Post
      Hello simple_goodoboy,

      You can use the Close[0] price to track the current price. If CalculateOnBarClose is true or the script is processing historical data, this will update when the bar closes.

      So let's say for example, I am tracking Close[0] while limit buy order is open pending fill, and Close[0] hits my profit target. I will cancel the limit buy order.
      However, on the next bar (or few bars), my condition will become true again, I don't want to enter another limit buy order cause the breakout is over, it has touched profit target already. I do not want to enter another trade until the next resistance or support breaks.

      Is there anything I can code or use in the strategy wizard for this scenario? I probably need a variable to set for a scenario like this and clears when can strategy can start executing again.

      Originally posted by NinjaTrader_ChelseaB View Post

      For example:

      if (CrossAbove(Close, 1000.25, 1))
      {
      Print("price has crossed above 1000.25");
      }

      This would print when the price crosses above 1000.25.


      Good idea. If 1000.25 is my profit target and Close price is above 1000.25, I want to cancel the limit buy order. Then I want to not take another trade til the next resistance is broke. For example, lets say the next resistance is 1002.25. Is there a way I can add some logic, like a hold_strategy variable equal 1 if order is canceled.

      Originally posted by NinjaTrader_ChelseaB View Post
      For example:
      protected override void OnBarUpdate()
      {
      double R2 = Pivots(PivotRange.Daily, HLCCalculationMode.DailyBars, 0, 0, 0, 100).R2[0];
      Print(R2);
      }
      What's the difference between HLCCalculationMode.DailyBars and HLCCalculationMode.CalcFromIntradayData ? I am not sure which option to select in the Strategy Wizard.

      Thank you
      Last edited by simple_goodoboy; 03-05-2017, 09:03 PM.

      Comment


        #4
        Hi simple_goodoboy,

        The logic is up to you.

        However, when you mention "I will cancel the limit buy order" if you are referring to a profit target (and stop loss) you use SetProfitTarget() and SetStopLoss(). When using these, if the target is filled, the stop is automatically cancelled. If the stop is filled, the target is automatically cancelled.



        When HLC Calcualtion mode is set to Calculated from intraday data, this will use intra-day data (minute, tick) to calculate the high, low, and close of a session. When HLC Calcualtion mode is set to Daily bars, this will use Daily bars to get the high, low, and close of that daily bar.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hi simple_goodoboy,

          However, when you mention "I will cancel the limit buy order" if you are referring to a profit target (and stop loss) you use SetProfitTarget() and SetStopLoss().
          Hello ChelseaB,

          Thanks for the response.

          This is what I mean

          Strategy Example:
          buy rsi>70
          sell rsi<-70
          Limit order for entry = close[0] - 5 ticks
          sell order for entry = close[0] + 5 ticks
          profit target = +40 ticks
          stop loss = -20 ticks


          So if buy (rsi>70) signal occurs and limit buy order (close[0] - 5 ticks) never fills because price continues up, and target (+40 ticks) price is touched, then I want to cancel the limit buy order that is pending and wait for the next signal.

          so i am thinking this example below will be needed for cancel order.

          if (long order open) AND (close[0] > profit target)
          cancel order

          Questions:

          1. How do I code "long order open" and "profit target" ? Or how do I use the strategy wizard to use logic with profit target or long order open.

          2. Once the order is canceled or profit target filled, will another buy signal occur because after order is canceled RSI > 70 will still be true? If so, I assume I will need to use one of the variables0 thru 10 to set a flag to not trade pending sell signal (RSI<-70). Is my assumption correct?


          Thank you so much
          Last edited by simple_goodoboy; 03-07-2017, 10:14 PM.

          Comment


            #6
            Hello simple_goodoboy,

            I'm not quite understanding.

            A profit target would be a working exit order for an open position.

            When you mention "if (long order open)" are you referring to a working entry order that has not filled?
            Code:
            private IOrder myEntryOrder;
            
            if (myEntryOrder != null && myEntryOrder.OrderState == OrderState.Working)
            {
            // entry order is working
            }
            
            else if (myEntryOrder == null)
            {
            // order is null, place order
            myEntryOrder = EnterLongLimit(Low[0] - 5 * TickSize);
            }


            When you mention "(close[0] > profit target)", what profit target are you referring to?
            Is there an open position from a previous order that this profit target is protecting? Which order does this refer to?

            If you have an open position and you are using SetProfitTarget and this profit target is working, you can capture the profit target's limit price in OnOrderUpdate().


            I have not seen your conditions. I cannot say if they will be true or false. I recommend you add prints to your script and find out if these evaluate as true or false.


            However, you should explicitly state in your script what you would like to happen. If you want a flag to wait until the next bar or another reset action you can use a bool. (If you are using the Wizard, you could use one of the Variable0-Variable9 variables in place of a bool)

            If you want to ensure one order at a time, I would recommend that after you cancel an order, you set the IOrder variable to null. In the entry condition, check that the IOrder variable is null before allowing a new order to be placed.

            You may find an example I've posted on the forums helpful.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello simple_goodoboy,

              I'm not quite understanding.

              A profit target would be a working exit order for an open position.

              When you mention "if (long order open)" are you referring to a working entry order that has not filled?
              Code:
              private IOrder myEntryOrder;
              
              if (myEntryOrder != null && myEntryOrder.OrderState == OrderState.Working)
              {
              // entry order is working
              }
              
              else if (myEntryOrder == null)
              {
              // order is null, place order
              myEntryOrder = EnterLongLimit(Low[0] - 5 * TickSize);
              }


              When you mention "(close[0] > profit target)", what profit target are you referring to?
              Is there an open position from a previous order that this profit target is protecting? Which order does this refer to?

              If you have an open position and you are using SetProfitTarget and this profit target is working, you can capture the profit target's limit price in OnOrderUpdate().


              I have not seen your conditions. I cannot say if they will be true or false. I recommend you add prints to your script and find out if these evaluate as true or false.


              However, you should explicitly state in your script what you would like to happen. If you want a flag to wait until the next bar or another reset action you can use a bool. (If you are using the Wizard, you could use one of the Variable0-Variable9 variables in place of a bool)

              If you want to ensure one order at a time, I would recommend that after you cancel an order, you set the IOrder variable to null. In the entry condition, check that the IOrder variable is null before allowing a new order to be placed.

              You may find an example I've posted on the forums helpful.
              http://ninjatrader.com/support/forum...604#post478604
              Thank you ChelseaB for your response. Really appreciate the help.

              I will respond shortly. I am busy away from home now.

              Thanks,

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by GLFX005, Today, 03:23 AM
              0 responses
              1 view
              0 likes
              Last Post GLFX005
              by GLFX005
               
              Started by XXtrader, Yesterday, 11:30 PM
              2 responses
              11 views
              0 likes
              Last Post XXtrader  
              Started by Waxavi, Today, 02:10 AM
              0 responses
              6 views
              0 likes
              Last Post Waxavi
              by Waxavi
               
              Started by TradeForge, Today, 02:09 AM
              0 responses
              14 views
              0 likes
              Last Post TradeForge  
              Started by Waxavi, Today, 02:00 AM
              0 responses
              3 views
              0 likes
              Last Post Waxavi
              by Waxavi
               
              Working...
              X