Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can't enable the strategy

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

    Can't enable the strategy

    Hello all.

    I'm not a programmer but I managed, with the help of AI get a strategy to compile without errors but when I try to ENABLE it it automatically DISABLES.
    If anyone could help me out and fix this that would be AWESOME!
    Code:
    #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 MelodyBOT2 : Strategy
        {
            private MACDBBLINES MACDBBLINES1;
            private WaveTrendV2 WaveTrendV21;
    
            private double trailingStopTicks = 24;
            private double profitTriggerTicks = 12;
            private double highestProfitPrice;
            private double stopPrice;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"";
                    Name                                        = "MelodyBOT2";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 2;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = true;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.ByStrategyPosition;
                    BarsRequiredToTrade                            = 40;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                    Fast                    = 5;
                    Slow                    = 6;
                    Smooth                    = 5;
                    Channel                    = 3;
                    Average                    = 10;
                    LongSize                    = 1;
                    ShortSize                    = 1;
                    TP2                    = 200;
                    TrailSize                    = 24;
                    TP1                    = 200;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {                
                    MACDBBLINES1                = MACDBBLINES(Close, Convert.ToInt32(Fast), Convert.ToInt32(Slow), Convert.ToInt32(Smooth), Brushes.Lime, Brushes.Red, 1, 10, 50, Brushes.LightBlue, true, Brushes.Gold, Brushes.Gold);
                    WaveTrendV21                = WaveTrendV2(Close, Convert.ToInt32(Channel), Convert.ToInt32(Average));
                    SetProfitTarget(@"Entry1", CalculationMode.Ticks, 200);
                    SetTrailStop(@"Entry1", CalculationMode.Ticks, 24, false);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 1)
                    return;
    
                 // Set 1
                if (
                     // Condition group 1
                    ((CrossAbove(MACDBBLINES1.Macd, 0, 1))
                     && (CrossAbove(WaveTrendV21, 0, 1)))
                     // Condition group 2
                     || ((MACDBBLINES1.Macd[0] > 0)
                     && (CrossAbove(WaveTrendV21, 0, 1)))
                     // Condition group 3
                     || ((WaveTrendV21[0] > 0)
                     && (CrossAbove(MACDBBLINES1.Macd, 0, 1))))
                {
                    Draw.Text(this, @"MrMelodyRenkoAutoTrader Text_1", @"Long", 0, 0);
                    Draw.ArrowUp(this, @"MrMelodyRenkoAutoTrader Arrow up_1", false, 0, 0, Brushes.Blue);
                    EnterLong(Convert.ToInt32(LongSize), @"Entry1");
                }
    
                 // Set 2
                if (
                     // Condition group 1
                    ((CrossBelow(MACDBBLINES1.Macd, 0, 1))
                     && (CrossBelow(WaveTrendV21, 0, 1)))
                     // Condition group 2
                     || ((MACDBBLINES1.Macd[0] < 0)
                     && (CrossBelow(WaveTrendV21, 0, 1)))
                     // Condition group 3
                     || ((WaveTrendV21[0] < 0)
                     && (CrossBelow(MACDBBLINES1.Macd, 0, 1))))
                {
                    Draw.Text(this, @"MrMelodyRenkoAutoTrader Text_2", @"Short", 0, 0);
                    Draw.ArrowDown(this, @"MrMelodyRenkoAutoTrader Arrow down_1", false, 0, 0, Brushes.Blue);
                    EnterShort(Convert.ToInt32(ShortSize), @"Entry1");
                }
    
                     // Custom trailing stop logic
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    // Existing long logic
    
                    if (Highs[1][0] >= highestProfitPrice)
                    {
                        highestProfitPrice = Highs[1][0];
                    }
    
                    if (Close[0] - highestProfitPrice >= profitTriggerTicks * TickSize)
                    {
                        stopPrice = Close[0] - trailingStopTicks * TickSize;
                        SetStopLoss("LongTrailing", CalculationMode.Price, stopPrice, false);
                    }
                }
                else if (Position.MarketPosition == MarketPosition.Short)
                {
                    // Existing short logic
    
                    if (Lows[1][0] <= highestProfitPrice)
                    {
                        highestProfitPrice = Lows[1][0];
                    }
    
                    if (highestProfitPrice - Close[0] >= profitTriggerTicks * TickSize)
                    {
                        stopPrice = Close[0] + trailingStopTicks * TickSize;
                        SetStopLoss("ShortTrailing", CalculationMode.Price, stopPrice, false);
                    }
                }
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Fast", Order=1, GroupName="Parameters")]
            public int Fast
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Slow", Order=2, GroupName="Parameters")]
            public int Slow
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Smooth", Order=3, GroupName="Parameters")]
            public int Smooth
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Channel", Order=4, GroupName="Parameters")]
            public int Channel
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Average", Order=5, GroupName="Parameters")]
            public int Average
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="LongSize", Order=6, GroupName="Parameters")]
            public int LongSize
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="ShortSize", Order=7, GroupName="Parameters")]
            public int ShortSize
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="TP2", Order=8, GroupName="Parameters")]
            public int TP2
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="TrailSize", Order=9, GroupName="Parameters")]
            public int TrailSize
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="TP1", Order=10, GroupName="Parameters")]
            public int TP1
            { get; set; }
            #endregion
    
        }
    }
    ​

    #2
    Hello yanikphoto,

    Thank you for your post.

    When a script compiles without errors, this does not ensure that it will not encounter runtime errors. From our experience at this time, AI tools such as ChatGpt are not quite adequate to generate valid compilable NinjaScripts that function as the user has intended. We often find that the generated code will call non-existent properties and methods, use improper classes or inheritance, and may have incorrect logic. We highly encourage that you create a new NinjaScript yourself using the NinjaScript Editor, and use the code generated by ChatGpt or other AI tools as suggestions and guides when coding the script yourself, rather than using the actual code generated.

    While It would not be within our support model to correct these scripts at user request, we would be happy to provide insight for any direct specific inquiries you may have if you would like to create this script yourself. Our support is able to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior. What I suggest in this case is to check the Log tab of the Control Center - Do you see any errors when you enable the strategy? If so, what do the errors report? This can be helpful information to understand why the strategy is disabling and to help decide the next steps for debugging.

    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one-on-one educational services.

    Please let me know if I may be of further assistance.​

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    54 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
    71 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