Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Self unchecking of strategy enable box

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

    Self unchecking of strategy enable box

    Created with the help of AI since I'm a beginner coder, compiled first attempt, loaded on small frame chart to see it work, hit enable and apply, it triggered a couple of times successfully going through entry and stop. Loaded it onto the larger frame chart I will be working with to see it work there and it stopped working because it's self unchecking the enable box a second after hitting apply. Removed that version of it and reloaded the code again from scratch, no problem compiling and again...self unchecking.

    Why would this happen?

    *EDIT...closed out application and restarted it and now it's staying enabled...hmmm. Waiting for it to work though this may take awhile to get it triggered but opened the set up screen multiple times to see if enabled box still checked and it is. Weird.

    Demo account...this maybe why?
    Last edited by MrTicka; 03-04-2025, 11:48 PM.

    #2
    No, I don't think it's because it's a demo account. A strategy usually it will uncheck itself if the strategy creates some sort of runtime error, causing the strategy to become disabled. Check the output to see if there is some error showing up.
    Last edited by rockmanx00; 03-05-2025, 12:04 AM.

    Comment


      #3
      It's still doing it and I have to restart the app to fix it. It only seems to want to work in one size chart frame. It isn't obvious how to use the output screen when I open it so I'll have to do some reading tomorrow morning. Time to get some sleep now. Thanks.

      Comment


        #4
        Hello MrTicka,

        Are there errors on the Log tab of the Control Center when the strategy disables?

        What is the full error message?

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hey Chelsea, nice to see you again, if you're the Chelsea from the emails trying to get registration sorted. If you are, thanks for the help there. I've only been using this platform for about a week so I'm still figuring out where everything is. I'll have to do some reading to inform myself better before I ask questions I may not even understand the answers to. haha

          Comment


            #6
            Hello MrTicka,

            The very same Chelsea.

            The Control Center is the main window of the NinjaTrader desktop application.

            On this at the top you will see the New menu, Tools menu, Workspaces menu etc.

            At the bottom of this you will see the tabs Orders, Executions, Strategies, Positions, Accounts, Log, and Messages.

            Select the log tab of the Control Center.

            Enable the strategy again. Once it automatically disables, look at the Log tab for any errors in orange.

            Report back what these are.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I was testing this morning since it would only load on a smaller frame chart it was triggering all over the place in a way I wouldn't be using it though it was fun watching it go crazy so these 'unchecking' errors were buried so deep from last night I didn't see them. I didn't think to try it again to have one up top which your reply mentioned. I did it and it says:

              "Strategy 'ThreeCandleTrailStrategy': Error on calling 'OnBarUpdate' method on bar 2: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

              The invalid reason doesn't make sense to me. It could be lack of understanding of the coding I suppose.

              Comment


                #8
                Hello MrTicka,

                The script encountered a runtime error which terminated the script.

                The error is an invalid index error.

                See the forum post below.


                Add prints one line above all lines of code where index used in the OnBarUpdate() override method logic block.
                Add prints above any Draw method calls within this method as well.

                In the prints, print the Time[0], print CurrentBar, print BarsInProgress if the script calls AddDataSeries(), and print the index being used or bars ago value being used.

                Below is a link to a support article on adding debugging prints to understand behavior.


                Save the output to a text and attach this to your next post.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9

                  protected override void OnBarUpdate()
                  {
                  // Ensure we have enough bars to analyze (at least 3)
                  if (BarsInProgress != 0 || CurrentBars[0] < 2)
                  return;​



                  Hmmm....so it's this bit from my code that is the problem. For some reason I've read the explanation a handful of times and I'm still not sure what it needs to be changed to. I'm going to try to figure it out, I want to learn but I'm a boomer....lol

                  Comment


                    #10
                    Are you using multi data series for this? You have CurrentBar, and you have CurrentBars. Usually you would use CurrentBar to represent the CurrentBar for the main instrument, and CurrentBars using [0] or [1] to differentiate between the CurrentBar of each dataseries. If you only have the one, then try:
                    if (BarsInProgress != 0 || CurrentBar < 2)
                    return;

                    Comment


                      #11
                      rockmanx00,

                      Thanks for the input. That change you suggested didn't solve it but I started messing around with the numbers and hit upon the fix. CurrentBar < 2 needed to be CurrentBar < 3 because it's a 3 bar signal. It does work now and it seems like I can apply it to any chart frame or instrument without unchecking.

                      Now I have to figure how to change the way the trailing stop operates because it's locking onto the bottom of the previous candle instead of trailing tick for tick like it does in the ATM auto trailing stop. This is allowing for some gain to be lost and some loss to increase. Unless there is a way to have the code look to the ATM strategy to accomplish this. Is that even possible?

                      Eventually, i probably will add another condition or two to filter out the weakest signals but I'm building this incrementally to learn about it.

                      I know that's going through this awkwardly but I'm trying to piece it together bit by bit and function by function for the learning process. Of course, I use forums, videos, other reading materials and even AI to help learn.

                      Thanks again for the input, it actually inspired me to mess with the other values in there to see if it worked. Any other advice on where to get info on the other trailing stop issue would be greatly appreciated.

                      Last edited by MrTicka; 03-05-2025, 09:30 PM.

                      Comment


                        #12
                        I can't even imagine how to do it with strategy builder, just because I'm used to a little coding at this point.
                        Are you using OnEachTick or OnPriceChange?

                        One way to do it would be to use the Close[0] (of the current candle, when using OnEachTick or OnPriceChange, that just means where price is currently at).
                        It would depend on if you are long or short, but (assuming long in this case) you would make StopLevel = Close[0] - TrailingAmount (Let's say 8 ticks).
                        So once your conditions to trigger the trail are met, you can then assign that as your StopLevel to give it it's first value.
                        After that, you would OnBarUpdate, Make StopLevel = Math.Max(StopLevel, Close[0] - TrailingAmount); What that would do is compare the current Close[0[ - your trailing amount with your old level and take the higher of the two. That is one way to give your trail the ability to move up without moving back down.
                        For shorts, your would use Math.Min.

                        That's one way you could do it. If you have problems let me know

                        Comment


                          #13
                          Well, I changed the 'calculate' to 'on each tick' which changed the calculation of the 3 candle signal and messed it up ruining the signal while it also allowed for the trail stop to calculate each tick. Solved one but broke the other.

                          I looked at the code and saw the way it has all the user defined values group together. I wondered if I separate out the trail stop ticks into its own section, it will have two separate drop downs, one for the candle calculation and one for the trailstop calculation.

                          So I can have another section like this and it will make what I'm looking for....but will it work? Hm....I'm about to try it. I may just create a completely different custom strategy with the different script and if it doesn't work I don't screw up the original instead of modifying the first. Wish me luck. welp...here goes. haha

                          if (State == State.SetDefaults)
                          {​
                          trailStopTicks = 10; // Default trailing stop distance in ticks
                          Calculate = Calculate.OnBarClose; // Ensures we evaluate trailstop
                          }
                          else if (State == State.Configure)​

                          I also found a page that tells me how to link ATM strategy to a ninjascript. I just started reading it to see if that is possible.
                          Last edited by MrTicka; 03-05-2025, 10:26 PM.

                          Comment


                            #14
                            "I looked at the code and saw the way it has all the user defined values group together. I wondered if I separate out the trail stop ticks into its own section, it will have two separate drop downs, one for the candle calculation and one for the trailstop calculation."

                            Are you talking about in the Properties Menu?

                            When you create the Properties section, you have things like:
                            [NinjaScriptProperty]
                            [Display(Name = "Debug", Description = "Sets Debug boolean on and prints check statements", Order = 1, GroupName = "Debug")]
                            public bool Debug
                            { get; set; }​

                            or

                            [NinjaScriptProperty]
                            [Range(1, int.MaxValue)]
                            [Display(Name = "Starting Moving Average", Description = "First Moving Average Period", Order = 1, GroupName = "MA Ribbon")]
                            public int startingMA
                            { get; set; }​

                            See the GroupName? You can organize things that way, changing the order number to arrange them in the order you want within each group.

                            Back at the top, in between the Namespace and the Public Class you can insert. for example:
                            [Gui.CategoryOrder("Debug", 1)]
                            [Gui.CategoryOrder("Parameters", 2)]
                            [Gui.CategoryOrder("Button Placement", 3)]
                            [Gui.CategoryOrder("MA Ribbon", 4)]​

                            You just need to change the strings "xxx" to the GroupNames you want and those numbers are the order you want the categories to appear.

                            Anyway, you can set your TrailStopTicks in Defaults, that's fine.
                            I would suggest in most cases to use Calculate.OnEachTick, OR Calculate.OnPriceChange, so Calculate triggers more often. It would depend entirely on how your strategy works. IF you decide to use OnBarClose, you have to consider all your calculations trigger once that bar closes. So if you wanted to enter once price breaks the previous candle's high, OnBarClose isn't the best option because it won't enter until the close. If you wanted to wait for confirmation of a candle close, that is a different story and a one of the ways you would use OnBarClose.

                            What is your three bar strategy and how are you planning on entering and exiting? I would determine how you want to enter and exit and determine if you are able to use OnBarClose, or if it's better to use another Calculation. Then you can work your programming logic around that.
                            Last edited by rockmanx00; 03-05-2025, 10:45 PM.

                            Comment


                              #15
                              I want the signal for entry to be calculated on candle close but want the trailing stop calculated by tick, I would assume on price change would give the same effect with a trailing stop but I'm not sure. So trying to code them for separate calculations.

                              It's being used on range chart so when the signal bar closes it's desired for entry. Now the script currently is working, except changing the calculation to 'by tick' changes both entry and trailstop so the entry will come before the bar closes and may not be a proper signal for entry condition when it finally does close.

                              So changing to 'by tick' fixes the trailing stop but breaks the entry signal, changing it to 'on close' fixes signal entry but is less than ideal trail stop since it creates a stop that follows price bar by bar at each bar close, not tick by tick as price moves.

                              Trying to separate the calculation of the bar(at close) and trailing stop(by tick or price I suppose) is what is sought. How to get those calculated separately is this query.

                              The fix I tried wasn't it, sure it couldn’t be that simple, haha. Tried another route and it was even worse for errors. Went searching for more answers about how to add a separate calculation for the trailing stop with the code but hadn't found that answer yet.

                              Unsure of how to get what I'd like to do from your example.
                              Last edited by MrTicka; 03-05-2025, 11:53 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              53 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              130 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              70 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              44 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X