Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Newbie: Need thoughts to implement an idea

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

    Newbie: Need thoughts to implement an idea

    Hi all

    as a part of my strategy I want to achieve the below

    within the current bar (when it’s still developing) if the price crosses the previous bar high I need to send a market order and also place a stop loss with the previous bar low.

    how can I achieve this?

    I know I have to use the ontickchange instead of the bar close.

    any ideas or easy ways is this can be done?


    #2
    Hello GauthamK,

    Thanks for your post.

    Correct, you would need to use Calculate.OnEachTick. You could also use Calculate,OnPriceChange. In either of these modes, the bars ago reference changes so that [0] is now the currently forming bar and [1] is the just closed bar. Close[0] will be the current price of the currently forming bar and will change as the price changes.

    You can create the condition of Close[0] > High[1] as a means to trigger your order. One area of concern would be that price can pull back and can oscillate which means that the condition can be true many times per bar. To prevent multiple orders, you would need to add further logic so that only one order is placed per bar, this is done by creating an int type variable and use that variable to hold the systems CurrentBar value when the order is placed and checking that the CurrentBar is not equal to that variable as part of the entry conditions.

    For the stop order, prior to placing the entry order, if you are using SetStopLoss() method, you would set that first to ensure that when the entry fills it will place the stop immediately where you expect it to be.

    Example:

    if (Close[0] > High[1] && CurrentBar != entryBar)
    {
    SetStopLoss(CalculationMode.Price, Low[1];
    EnterLong();
    entryBar = CurrentBar; // assign current bar to the int variable named entryBar
    }


    Reference: https://ninjatrader.com/support/help...currentbar.htm

    Notes:

    1) Order placement and filling are not instantaneous (internet transmission time) and price can move quickly in a short amount of time. It is possible that your stop loss order would be rejected because (in this example) price may have dramatically reversed. You may want to consider using a different type of stop-loss that relates to the actual entry price and one that can be adjusted later, for example, SetStopLoss(CalculationMode.Ticks, 10) would provide a stop 10 ticks from the actual entry price and this order would never be rejected.

    2) If using the strategy builder then we recommend using a fixed stop loss with the SetStopLoss() method and using the CalculationMode.Ticks. example: SetStopLoss(CalculationMode.Ticks, 10)
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul, can this be done without having Calculate.OnEachTick or Calculate.OnPriceChange?

      Comment


        #4
        Hello UltraNIX,

        Thanks for your reply.

        To meet the original posters condition, "within the current bar (when it’s still developing) if the price crosses the previous bar high", no. Calculate.OnBarClose does not access the currently forming bar until it is finished.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          What about adding additional data series of 1 Tick?

          Comment


            #6
            Hello UltraNIX,

            thanks for your reply.

            Yes, you can add a 1 tick series (or basically any granular data series) to provide more OnBarUpdate() events.

            This of course requires moving into MultiTimeFrame coding which some people do not want to do, but you are correct it is an option.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Thank you Paul for the reply. It definitely fits my needs i will make the change and see.

              Comment


                #8
                Paul,

                Where do I define the int entryBar; in my logic?

                Alternatively can i use below condition, to avoid the multiple entry orders?

                if (Position.MarketPosition == MarketPosition.Flat)
                {
                Only if position is flat, check for LONG conditions and enterLong
                }

                Comment


                  #9
                  Hello GauthamK,

                  Thanks for your reply.

                  I'm not sure if you are using the Strategy Builder or are coding directly in Ninjascript so I will advise both ways.

                  In the Strategy Builder, you would create the variable in the Inputs and variable page.

                  In a Ninjascript, I would suggest creating at the class level, for example:

                  public class Example : Strategy
                  {
                  private int entryBar;


                  The MarketPosition is always a good idea to use, however, an important concept to understand is that orders are placed and filled and reported back asynchronously to your code execution. So the "Marker Position" cannot be updated until the order has been sent from your strategies PC, received and filled at the exchange and then reported back to your strategy. When using Calculate.OnBarClose this typically would not be an issue. When using Calculate.OnEachTick or Calculate.OnPriceChange, your strategy code will execute far faster than the order process time takes and your code does not wait for the order to be filled before continuing its code execution, so this is where the multiple orders can occur and is why you need to add the entrybar check into your code.



                  Paul H.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by pdennis, Today, 07:43 AM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by NinjaTrader_Brandon, Today, 07:49 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post NinjaTrader_Brandon  
                  Started by b16_aln, Today, 05:15 AM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by Negustrader, Today, 01:01 AM
                  1 response
                  16 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by soon123, Yesterday, 10:50 PM
                  1 response
                  10 views
                  0 likes
                  Last Post NinjaTrader_ChrisL  
                  Working...
                  X