Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy to Buy/Sell on Trend Change

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

    #31
    Hello Jesse,
    I did it from the strategy builder. and here are the screenshots:

    The below condition should check if the current bar is 30 ticks higher than the previous bar then enter a long trade and sell after 10 ticks stop loss should be 15 ticks.
    Click image for larger version

Name:	image.png
Views:	175
Size:	20.5 KB
ID:	1314836
    Click image for larger version

Name:	image.png
Views:	157
Size:	18.3 KB
ID:	1314837

    While this condition should check if the current bar is 30 ticks lower than the previous bar then enter a short trade, profit target is 10 ticks and stop loss will be 15 ticks
    Click image for larger version

Name:	image.png
Views:	167
Size:	21.4 KB
ID:	1314838
    Click image for larger version

Name:	image.png
Views:	166
Size:	18.4 KB
ID:	1314839

    How can i achieve this through the strategy builder as the code was generated by the actions i made on the builder.

    Thanks!​​

    Comment


      #32
      Hello maaxk,

      That is what that code is doing. I had explained how that code works in the last message:

      Your code appears to be doing that, you are checking if the close is greater than the high of the previous bar plus 30 ticks and entering long. The second set checks if the close is less than the low of the previous bar minus 30 ticks.
      I see that you are using targets so this will not specifically be just a reversal strategy, it also has the possibility to exit the trade between the conditions becoming true.

      The targets will exit at the values you specified as the offsets.

      Because this is a reversal strategy that means the targets won't always be what fills to exit the trade, you may also see that the position reverses if the opposite entry condition becomes true.

      Comment


        #33
        Hello Jesse, have modified it. Look the first condition for the long trades is working fine can you please reverse it for the short trades as the problem is with the short trades and its very problematic:
        namespace NinjaTrader.NinjaScript.Strategies
        {
        public class Order8tt : Strategy
        {
        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Place and order when trend is transitioning and close after some points.";
        Name = "Order8tt";
        Calculate = Calculate.OnBarClose;
        EntriesPerDirection = 1;
        EntryHandling = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy = true;
        ExitOnSessionCloseSeconds = 30;
        IsFillLimitOnTouch = false;
        MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution = OrderFillResolution.Standard;
        Slippage = 0;
        StartBehavior = StartBehavior.WaitUntilFlat;
        TimeInForce = TimeInForce.Gtc;
        TraceOrders = false;
        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade = 20;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;
        }
        else if (State == State.Configure)
        {
        AddDataSeries(Data.BarsPeriodType.Tick, 1);
        SetProfitTarget(@"", CalculationMode.Ticks, 4);
        SetStopLoss("", CalculationMode.Ticks, 8, true);
        }
        }

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

        if (CurrentBars[0] < 1)
        return;

        // Set 1
        if (Close[0] > (Low[1] + (30 * TickSize)) )
        {
        EnterLong(Convert.ToInt32(DefaultQuantity), "");
        }

        // Set 2
        if (Close[0] < (High[1] + (30 * TickSize)) )
        {
        EnterShort(Convert.ToInt32(DefaultQuantity), "");
        }

        }
        }
        }

        I’ve made the modifications we discussed, but I'm encountering some issues with the updated code. It seems to be working fine for long trades, where it checks if the current bar’s price is 30 ticks higher than the previous one. However, for short trades, the code isn’t functioning as expected. Specifically, it's placing orders for short trades even when the price is rising, which shouldn't meet the conditions for a short trade.

        My goal is to have two distinct conditions: one for long trades and another for short trades. For long trades, the condition is that the current bar's price should be 30 ticks higher than the previous bar’s price. For short trades, the condition should be that the current bar's price is 30 ticks lower than the previous bar’s price.

        Could you please help me troubleshoot and refine the code to ensure these conditions are correctly implemented?

        Thank you!

        Comment


          #34
          Hello maaxk,

          You need to use -30 for set 2 to subtract 30 ticks.

          Comment


            #35
            Hi Jesse,

            I hope you're doing well.

            The strategy we discussed earlier worked as expected. Now, I'm looking to implement a new strategy. I would like to place a long order when the price goes down by 20 ticks from the previous high. Could you please guide me on how to achieve this? Below is my code:
            /This namespace holds Strategies in this folder and is required. Do not change it.
            namespace NinjaTrader.NinjaScript.Strategies
            {
            public class midband11p : Strategy
            {
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Enter long once the pricres decreases by 11 points after the previous high.";
            Name = "midband11p";
            Calculate = Calculate.OnEachTick;
            EntriesPerDirection = 1;
            EntryHandling = EntryHandling.AllEntries;
            IsExitOnSessionCloseStrategy = true;
            ExitOnSessionCloseSeconds = 30;
            IsFillLimitOnTouch = false;
            MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
            OrderFillResolution = OrderFillResolution.Standard;
            Slippage = 0;
            StartBehavior = StartBehavior.WaitUntilFlat;
            TimeInForce = TimeInForce.Gtc;
            TraceOrders = false;
            RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
            StopTargetHandling = StopTargetHandling.PerEntryExecution;
            BarsRequiredToTrade = 20;
            // Disable this property for performance gains in Strategy Analyzer optimizations
            // See the Help Guide for additional information
            IsInstantiatedOnEachOptimizationIteration = true;
            }
            else if (State == State.Configure)
            {
            AddDataSeries(Data.BarsPeriodType.Tick, 1);
            SetProfitTarget(CalculationMode.Ticks, 30);
            SetStopLoss(CalculationMode.Ticks, 20);
            }
            }

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

            if (CurrentBars[0] < 1)
            return;

            // Set 1
            if (Close[0] < (High[0] + (20 * TickSize)) )
            {
            EnterLong(3, "");
            }

            }
            }
            }​


            Thank you!

            Comment


              #36
              Hello maaxk,

              In the future please create a new thread for any new unrelated questions that you have, each thread should only focus on one item to keep the reply count down.

              Again you need to use negative numbers if you want to subtract an amount of ticks, you are using positive 20.

              Comment


                #37
                Hello Jesse,

                i have created a new thread : https://forum.ninjatrader.com/forum/...-previous-high

                Thanks!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                557 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                324 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                545 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                547 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X