Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy glitch that creates a random periodic Buy/Sell loop

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

    Strategy glitch that creates a random periodic Buy/Sell loop

    Attached is a strategy that is very simple. It goes long when the indicator's trend signal is +1 and short if -1. I use it on a NinzaRenko chart. The indicator is a paid indicator, but maybe someone can see the problem in the script. Also attached is a screenshot. It is set to on each tick and Immediate Submit - Synchronize Accounts. I'm very happy with this strategy. If I can only fix this glitch! I made this in Strategy Builder and have no coding chops. Thank you folks!

    Click image for larger version

Name:	Screenshot 2023-11-08 223455.png
Views:	439
Size:	615.3 KB
ID:	1277243

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class T3PRenko6010Ba : Strategy
    {
    private ninZaT3Pro ninZaT3Pro1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "T3PRenko6010Ba";
    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)
    {
    }
    else if (State == State.DataLoaded)
    {
    ninZaT3Pro1 = ninZaT3Pro(Close, ninZa_MAType.EMA, 14, 3, 0.7, false, ninZaT3Pro_ChaosSmoothingMethod.DEMA, 5, false, 4);
    ninZaT3Pro1.Plots[0].Brush = Brushes.Gold;
    ninZaT3Pro1.Plots[1].Brush = Brushes.Transparent;
    AddChartIndicator(ninZaT3Pro1);
    }
    }

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

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if (ninZaT3Pro1.Signal_Trend[0] == 1)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if (ninZaT3Pro1.Signal_Trend[0] == -1)
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }

    // Set 3
    if (ninZaT3Pro1.Signal_Trend[0] == -1)
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 4
    if (ninZaT3Pro1.Signal_Trend[0] == 1)
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
    }

    }
    }
    }



    #2
    Hello vinnieray,

    That kind of result can happen because you are using OnEachTick processing. If the condition to trade remains true it will continue to produce trades until the condition is not true. You may want to try using BarsSinceEntryExecution to avoid that.



    That can be done in the builder as well, that is located in the Misc section in the condition builder.

    Comment


      #3
      Fantastic. I will update the script and test it. Thank you for the fast response!

      Comment


        #4
        Hi Jesse, I've tried to figure out how to use the BarSinceEntry in strategy builder and looked at other posts here. But I really can't make heads or tails of how to set this up as a condition to avoid my problem. Is there a video link to a specific tutorial for this? Another idea could be some way to have the Buy trade close - wait a few ticks - then open the new Sell trade. Maybe this would avoid the looping issue? Or maybe run two different strategies. One Long, and One Short? Due to the Renko bar size, I do want to avoid trading at Bar Close or next Bar.

        Comment


          #5
          Update here. I ran a modified version of the strategy last night as a test. It was only step 1 and step 2 - go long and sell when trend = -1. The same glitch occurred. So it's not the result of the reverse go short order coming in at the same time. Would I build a BarSinceEntry condition after the indicator trend = +1 input in strategy builder? What would the inputs be or syntax? See attached for more clarification.
          Attached Files

          Comment


            #6
            Hello vinnieray,

            To use BarsSinceEntry you would need to make a group condition because it returns -1 when no entry had been placed or a number of bars ago if one had been placed. In the group condition you would need to select "If Any" as the comparison and then add two conditions to the group. The first condition would check if the bars since entry was -1. The second condition would check if the value was greater than 0 or the number of bars you want to wait. That would make it so that if either of the conditions in the group become true the group is true, otherwise if neither condition is true the group is false.

            Comment


              #7
              Hi Jesse, Thank you for the direction. I was able to use your suggestions to avoid the glitch loop. But now I'm getting an order multiplying issue. I want the strategy to only buy, exit, sell, exit _ 1 contract. But as time passes as the strategy runs, it adds contracts - seemingly randomly. So it'll buy 2 contracts, then sell 3, etc.

              Last edited by vinnieray; 11-29-2023, 08:32 PM.

              Comment


                #8
                Hello vinnieray,

                The code provided does not have any position checking in your conditions so if your entry condition remains true for multiple bars it could continue to enter more orders. A starting point would be to add a condition to each of the entry sets to only submit entries when the position is flat, that would possibly solve that issue. You can also do the same for the exit conditions and check that you are in the related position before submitting the exit for that position.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                81 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                149 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                79 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                52 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                59 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X