Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

buying when breaking out of a range

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

    buying when breaking out of a range

    What is the easiest way to have the strategy builder place a buy or sell order at the exact moment it is breaking a range? For example when price breaks the highest point of last 10 bars, it buys when the price is crossing over that exact point. Exact price isn't very important, I just want the same bar its happening.

    I see lots of examples in this forum, but when I try to back test them, it is always buying on the bar after I want it to, or sometimes 2-3 after it is supposed to. I have tried using both the MAX & Doncian Channels, but no matter what I can only get it to buy/sell only after bars close. I have tried using the high order fill, and it happens regardless of the settings. I can get strategies to work when using Ask > MAX or Doncian Upper, but when I use ask cross above MAX or Doncian it will not execute the strategy.

    All I need is a market order placed when its making a new high at the time its starting to make new highs. Thank you!

    #2
    Hello,

    To place a buy or sell order at the exact moment it is breaking a range in NinjaTrader using the Strategy Builder, you can follow these steps to ensure the orders are placed within the same bar that the condition is met. This typically involves setting the strategy to calculate on each tick, rather than on bar close. Here’s how you can do it: Step-by-Step Guide
    1. Set Calculate Mode to OnEachTick:
      • This ensures that the strategy checks the conditions on every tick within the bar, rather than waiting for the bar to close.
    2. Use the Strategy Builder to Define Conditions:
      • For a buy order when the price breaks above the highest point of the last 10 bars.
    Creating the Strategy in Strategy Builder
    1. Open Strategy Builder:
      • Go to New > Strategy Builder.
    2. Set Calculate Mode:
      • In the Default properties page, set Calculate to OnEachTick.
    3. Add Condition for Buying:
      • In the Conditions and Actions page, create a new condition:
        • For the left side of the condition, select Price -> Close.
        • Choose Crosses above as the condition.
        • For the right side of the condition, select Indicator -> MAX -> MAX(Close, 10), which represents the highest close of the last 10 bars.
      • Add an action to place a market order:
        • Choose Order Management -> Enter long position.
    4. Add Condition for Selling:
      • Similarly, you can add a condition for selling when the price breaks below the lowest point of the last 10 bars using MIN(Close, 10).
    5. Finalization:
      • Name your strategy and save it.
    Example Code


    If you prefer coding directly in NinjaScript, here is an example of how you can implement this logic using NinjaScript:

    Code:
    #region Using declarations
    using System;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript.StrategyAnalyzerColumns;
    using NinjaTrader.NinjaScript.Strategies;
    using NinjaTrader.NinjaScript.Indicators;
    #endregion
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class BreakoutStrategy : Strategy
        {
            private int period = 10;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Strategy to buy when price breaks the highest point of last 10 bars.";
                    Name = "BreakoutStrategy";
                    Calculate = Calculate.OnEachTick; // Ensure calculation on each tick
                    EntriesPerDirection = 1;
                    EntryHandling = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy = true;
                    ExitOnSessionCloseSeconds = 30;
                    StopTargetHandling = StopTargetHandling.ByStrategyPosition;
                    IsFillLimitOnTouch = false;
                    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                    StartBehavior = StartBehavior.WaitUntilFlat;
                    TimeInForce = TimeInForce.Gtc;
                    TraceOrders = false;
                    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                    BarsRequiredToTrade = 20;
                }
                else if (State == State.Configure)
                {
                    // Add any configuration code here
                }
            }
    
            protected override void OnBarUpdate()
            {
                // Ensure we have enough bars to proceed
                if (CurrentBar < period)
                    return;
    
                double highestHigh = High[HighestBar(High, period)];
                
                // Check for breakout and place buy order
                if (CrossAbove(Close, highestHigh, 1))
                {
                    EnterLong();
                }
            }
        }
    }
    ​
    Explanation:
    1. Calculate Mode:
      • Calculate = Calculate.OnEachTick; ensures that the strategy checks the condition on each tick.
    2. Condition for Breakout:
      • HighestBar(High, period) gets the highest bar within the specified period.
      • CrossAbove(Close, highestHigh, 1) checks if the close price crosses above the highest high within the period.
    3. Placing Orders:
      • EnterLong() places a market order to buy when the breakout condition is met.

    By setting the calculate mode to OnEachTick, the strategy will respond to intra-bar price movements, allowing you to place orders as soon as the conditions are met within the bar, rather than waiting for the bar to close.

    Comment


      #3
      Thank you sir!!! I think the issue I have been running into is I have been using ask instead of close, because I thought the close was only going to update on bar close.

      I honestly cannot express enough gratitude lol, I was very stuck on this. Your explanation made the lights click.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      633 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      567 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X