Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

strategy compiles but not available to add-to-chart

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

    strategy compiles but not available to add-to-chart

    hi, i wrote a strategy and when i try to add it to a chart it is nowhere to be found under "strategies"

    the code is here:
    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
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class rsiextreme : Strategy
        {
            private RSI rsi14;
            private SMA sma16_67Hour;
            private double stopLossPercentage = 0.01; // 1% stop loss
            private double rsiEntryOverbought = 65;
            private double rsiEntryOversold = 35;
            private double rsiExitOverbought = 60;
            private double rsiExitOversold = 40;
            private int maPeriod; // Moving average period based on the bar interval
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = "RSI with dynamic MA period for 16.67-hour duration, mean reversion, and stop loss.";
                    Name = "rsiextreme";
                    Calculate = Calculate.OnEachTick;
                    IsOverlay = false;
                    AddDataSeries(Data.BarsPeriodType.Minute, 5); // Default 5-minute bars, adjust as needed
                }
                else if (State == State.Configure)
                {
                    CalculateMAPeriod();
                    rsi14 = RSI(Close, 14, 1);
                    sma16_67Hour = SMA(Close, maPeriod);
                    
                    AddPlot(Brushes.Blue, "RSI");
                    AddPlot(Brushes.Red, "SMA");
    
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < maPeriod)
                    return;
    
                // Recalculate the moving average period if the bar interval changes
                if (BarsInProgress == 0)
                {
                    CalculateMAPeriod();
                    sma16_67Hour = SMA(Close, maPeriod);
                }
    
                // Long Entry
                if (rsi14[1] < rsiEntryOversold && Close[1] > sma16_67Hour[1])
                {
                    EnterLong();
                }
    
                // Long Exit Condition #1
                if (Position.MarketPosition == MarketPosition.Long && rsi14[1] > rsiExitOverbought)
                {
                    ExitLong();
                }
    
                // Long Exit Condition #2
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    double stopLossPrice = Position.AveragePrice - (stopLossPercentage * Position.AveragePrice);
                    if (Low[0] <= stopLossPrice)
                    {
                        ExitLong("StopLoss");
                    }
                }
    
                // Short Entry
                if (rsi14[1] > rsiEntryOverbought && Close[1] < sma16_67Hour[1])
                {
                    EnterShort();
                }
    
                // Short Exit Condition #1
                if (Position.MarketPosition == MarketPosition.Short && rsi14[1] < rsiExitOversold)
                {
                    ExitShort();
                }
    
                // Short Exit Condition #2
                if (Position.MarketPosition == MarketPosition.Short)
                {
                    double stopLossPrice = Position.AveragePrice + (stopLossPercentage * Position.AveragePrice);
                    if (High[0] >= stopLossPrice)
                    {
                        ExitShort("StopLoss");
                    }
                }
            }
    
            // Method to calculate the moving average period based on the current bar interval
            private void CalculateMAPeriod()
            {
                // Calculate the period in minutes for 16.67 hours
                double minutes = 16.67 * 60;
                // Calculate the period based on the current bar interval
                double barInterval = BarsPeriod.Value; // Bar interval in minutes
                maPeriod = (int)Math.Round(minutes / barInterval);
            }
        }
    }
    ​
    Can anyone help me or any idea why it would compile without errors but not be able to add to any chart? No messages in log either. Thanks

    #2
    Hello agclub,

    Thank you for your post.

    If your strategies are not showing up in the list of Available strategies then that suggests there is an issue with the strategies themselves. Specifically, there is likely an error in OnStateChange().

    Keep in mind that while it may compile successfully, the compiler cannot check "run time" logic errors, which can only occur when you run (or load) the strategy.

    A good first step here is to check the "Log" tab of the Ninjatrader Control center and look for any errors related to the strategies as if there is a run time error is would show in the log tab.

    If there are no log errors, check (in the Ninjascript editor open each strategy) to see if the strategies have the same name (IE you made a copy of the strategy and made your changes but unintentionally left the name the same as an existing one).

    In general, you want the filename of the strategy (this is what Ninjascript editor uses to open the script) to be the same as the public class name and most importantly that inside OnStateChange and within State.SetDefaults, the Name = field (This is what the drop-down or strategy selector uses) shows the same name as the public class name.​

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    52 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
    43 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    47 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X