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

Triggering strategy by movement of price up or down X number of points

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

    Triggering strategy by movement of price up or down X number of points

    I am trying to determine if its possible to trigger a strategy by entering a trade when price moves up or down by X number of points or ticks in a bar in real time. It would be used as part of my strategy to trigger trades in scalping when I am looking for explosive moves in prices. I am also setting a min. trading volume threshold that must be met as well, which I figured out.

    Any help someone can provide would be greatly appreciated!

    Thanks
    Jeff
    Jdmtrader
    NinjaTrader Ecosystem Vendor - JDM Indicators

    #2
    Hello Jeff,

    Thanks for your post.

    TickSize could be used to offset a price by a certain number of ticks.

    For example, you could compare the current close price (Close[0]) to the previous close price offset by say 10 ticks (Close[1] + 10 * TickSize) to see if the current price is greater than the previous close price plus 10 ticks.

    The code for the above example would look something like this:

    Code:
    if (Close[0] > Close[1] + 10 * TickSize)
    {
      //do something here.
    }
    See this help guide page for more information about TickSize: https://ninjatrader.com/support/help...8/ticksize.htm

    Please let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      This doesnt seem to be working as desired. Its triggering trades before it moves up 10 ticks.

      Here is code I have created with the intent of having a trade triggered when price moves up 10 ticks from the opening price of a new bar formed. Is this code going to work to accomplish this?

      }
      else if (State == State.Configure)
      {
      SetTrailStop(@"", CalculationMode.Ticks, 10, false);
      }
      }

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      // Set 1
      if ((Close[0] > (Open[0] + (10 * TickSize)) )
      && (IsRising(Close) == true))
      {
      EnterLong(Convert.ToInt32(DefaultQuantity), "");
      }

      // Set 2
      if ((IsFalling(Close) == true)
      && (Close[0] < (Open[0] + (10 * TickSize)) ))
      {
      EnterShort(Convert.ToInt32(DefaultQuantity), "");
      }​
      Jdmtrader
      NinjaTrader Ecosystem Vendor - JDM Indicators

      Comment


        #4
        Hello Jeff,

        Thanks for your note.

        Yes, that seems to be correct. Set 1 would be checking if the current Close price is greater than the current Open price plus 10 ticks. Set 2 would be checking if the current Close price is less than the current Open price plus 10 ticks.

        If you are wanting to check if the current Close price is less than the current Open price minus 10 ticks, you would need to modify Set 2 so that it would read as (Close[0] < (Open[0] + (-10 * TickSize))).

        To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.

        https://ninjatrader.com/support/foru...121#post791121

        Let us know if we may assist further.​
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          My strategy is opening orders but is cancelling them when a new opportunity is found in the middle of an open trade. How do I prevent a strategy from cancelling an open order and creating a new order? I want to make sure my current open order is filled by hitting my stop or TP and not allow a new order to be created until that happens.
          Jdmtrader
          NinjaTrader Ecosystem Vendor - JDM Indicators

          Comment


            #6
            Hello Jeff,

            Thanks for your note.

            Position.MarketPosition could be used to check if your strategy is in a Flat market position before placing orders.

            You could add if (Position.MarketPosition == MarketPosition.Flat) in your condition to place an entry order so that the orders are only placed when you are in a Flat market position.

            See this help guide page for more information about Position.MarketPosition and sample code: https://ninjatrader.com/support/help...etposition.htm

            Please let me know if I may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Thanks. That worked. How would I specify the trailing stop so it wont start moving until after the price has moved up at least 20 ticks. Then when it starts moving, it will move in 10 tick increments or steps. The initial stop would 10 ticks.
              Jdmtrader
              NinjaTrader Ecosystem Vendor - JDM Indicators

              Comment


                #8
                Hello Jeff,

                Thanks for your notes.

                This would require using Exit methods in your script and adding coding logic to implement a trailing stop like you described.

                Please note that you would not be able to use the Exit methods and the Set methods in the Stop and Targets window as that creates a violation of the managed approach internal order handling rules, linked here: https://ninjatrader.com/support/help...d_approach.htm

                You must use only Exit methods in the script and remove any Set methods.

                My colleague Chelsea has created educational examples of strategy builder breakeven and trailing stop in the strategy builder here:
                https://ninjatrader.com/support/foru...der#post806596

                Please let us know if we may assist further.​
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks. One thing I really think could help me would be to allow the strategy to cancel an open order and enter a new one but ONLY when the current open order is profitable.

                  I believe I would need to use Position.GetUnrealizedProfitLoss() to determine if my current order is profitable or not but I am not sure. If there is a way to specify in currency how much I would need to be up before allowing that order to be cancelled for a new one, that would be even better. But at the very least, I would only want to allow an order to be cancelled so long as the current open order is profitable. This will allow a change in direction of the market to be taken advantage of and a new order to be placed. This would also allow me to not need to use a trailing stop hypothetically as change in market direction would serve as my stop and put me in, hopefully a new order that is following the trend I wish to take.
                  Jdmtrader
                  NinjaTrader Ecosystem Vendor - JDM Indicators

                  Comment


                    #10
                    Hello Jeff,

                    Thanks for your note.

                    Yes, Position.GetUnrealizedProfitLoss() could be used to get the Unrealized PnL value of the strategy position. To get that value in currency, you would pass in PerformanceUnit.Currency for the PerformanceUnit unit property.

                    Position.GetUnrealizedProfitLoss(PerformanceUnit unit, [double price])

                    Note that the double price property is optional to use when calling this method in the script as stated in the help guide page.

                    You may consider comparing this to a numerical value in your script to check if the Unrealized PnL is greater than a specific value.

                    See this help guide page for more information about Position.GetUnrealizedProfitLoss(): https://ninjatrader.com/support/help...profitloss.htm

                    Let me know if I may further assist.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      I setup the (Close[0] < (Open[0] + (10 * TickSize))). However, I was hoping to use points, not ticks, to define when it triggers an order. Is there a way to do that? Also, I want to make sure that if I gets into an order during a 1 min candle, for example and then closes the order. It wont open a new order until the next candle opens.
                      Jdmtrader
                      NinjaTrader Ecosystem Vendor - JDM Indicators

                      Comment


                        #12
                        Hello Jeff,

                        Thanks for your note.

                        Unfortunately, you would need to offset the price by ticks because there are no methods or properties available to offset the price by points.

                        To have the strategy process logic and place trades at the close of each bar, Calculate.OnBarClose should be used when running the strategy.

                        Calculate: https://ninjatrader.com/support/help.../calculate.htm

                        You could also try using BarsSinceExitExecution to have the strategy wait a set number of bars before entering an order after an exit was placed.

                        BarsSinceExitExecution: https://ninjatrader.com/support/help...texecution.htm

                        Please let me know if I may assist you further.
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          Ok. Is there a way to trigger an order based on ticks per second? So, for example, I would want to trigger a trade when the rate at which the price is moving exceeds 20 ticks per second of speed. Im trying to determine the momentum of a price and the ticks per second is a way to quickly determine at any moment if price is accelerating past a certain speed. https://forum.ninjatrader.com/forum/...hem#post252644
                          Jdmtrader
                          NinjaTrader Ecosystem Vendor - JDM Indicators

                          Comment


                            #14
                            Hello Jeff,

                            Thanks for your note.

                            You could consider adding a 1-second data series to your script using the AddDataSeries() method and come up with some custom logic in your script to count the number of ticks per second similar to the forum thread you shared.

                            Note that this would require unlocking the script from the Strategy Builder and manually coding your logic into the script.

                            AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm

                            Further, deanz in the forum thread you shared posted a TicksPerSecond script that you may try to use. You could reach out to deanz on the forums directly for information about how to use their script in a custom NinjaScript strategy.

                            Let me know if you have further questions.
                            Brandon H.NinjaTrader Customer Service

                            Comment


                              #15
                              You said earlier, "If you are wanting to check if the current Close price is less than the current Open price minus 10 ticks, you would need to modify Set 2 so that it would read as (Close[0] < (Open[0] + (-10 * TickSize)))."

                              How would I use ticksize and be able to define it as a negative number? It doesnt let me do that in the strategy builder.

                              Click image for larger version  Name:	image.png Views:	0 Size:	44.3 KB ID:	1242560

                              This is the main part of the code that is generated. In set 2, it does NOT show the negative symbol. MovementUp/DownTrigger is where I define the tick amount.

                              if (BarsInProgress != 0)
                              return;

                              if (CurrentBars[0] < 1)
                              return;

                              // Set 1
                              if ((IsRising(Close) == true)
                              && (Position.MarketPosition == MarketPosition.Flat)
                              && (Volume[0] <= MaxVolume)
                              && (Volume[0] >= MinVolume)
                              && (Close[0] > (Open[0] + (MovementUpTrigger * TickSize)) ))
                              {
                              EnterShort(Convert.ToInt32(DefaultQuantity), "");
                              }

                              // Set 2
                              if ((IsFalling(Close) == true)
                              && (Position.MarketPosition == MarketPosition.Flat)
                              && (Volume[0] <= MaxVolume)
                              && (Volume[0] >= MinVolume)
                              && (Close[0] < (Open[0] + (MovementDownTrigger * TickSize)) ))
                              {
                              EnterLong(Convert.ToInt32(DefaultQuantity), "");
                              }

                              }​
                              Jdmtrader
                              NinjaTrader Ecosystem Vendor - JDM Indicators

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by giulyko00, Yesterday, 12:03 PM
                              2 responses
                              10 views
                              0 likes
                              Last Post giulyko00  
                              Started by r68cervera, Today, 05:29 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post r68cervera  
                              Started by geddyisodin, Today, 05:20 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post geddyisodin  
                              Started by JonesJoker, 04-22-2024, 12:23 PM
                              6 responses
                              36 views
                              0 likes
                              Last Post JonesJoker  
                              Started by GussJ, 03-04-2020, 03:11 PM
                              12 responses
                              3,241 views
                              0 likes
                              Last Post Leafcutter  
                              Working...
                              X