Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

issue with strategy on showing up as available

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

    issue with strategy on showing up as available

    my code compiles , but it is not showing up as available to pick as a strategy

    here is the 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 MachineLearning : Strategy

    {

    region Variables

    private double[][] data;

    private List<TestList> Lista;

    private double prior1, prior2, prior3;

    private int trnum = 0; // Initialize 'trnum' as it's not declared in your original code.

    // Assuming these variables are required for computation, declare and initialize them.

    private double[] classProbs = new double[2];

    private double[][] condProbs = new double[2][];

    #endregion



    private int barCounter;

    private int Barcounterbars = 15; // Set this according to your strategy

    private double ATRvaluerange = 2.0; // Set this according to your strategy

    private double ProfitMultiplier = 2.0; // Set this according to your strategy

    private double MaxProfitTarget = 100.0; // Set this according to your strategy

    private int Contracts = 1; // Set this according to your strategy

    private Order exitLongOrder;

    private Order exitShortOrder;

    private bool OkToTrade = true;




    private ATR atr;




    region Nested Class

    private class TestList

    {

    public double lprior1;

    public double lprior2;

    public double lprior3;

    public double lside;




    public TestList(double myprior1, double myprior2, double myprior3, double myside)

    {

    lprior1 = myprior1;

    lprior2 = myprior2;

    lprior3 = myprior3;

    lside = myside;

    }

    }

    #endregion



    protected override void OnStateChange()

    {

    if (State == State.SetDefaults)

    {



    InitializeDefaults();



    }

    else if (State == State.Configure)

    {

    AddChartIndicator(atr);

    }

    else if (State == State.DataLoaded)

    {

    InitializeData();

    }

    }



    region Initialization

    private void InitializeDefaults()

    {

    Description = @"Enter the description for your new custom Strategy here.";

    Name = "MachineLearning";

    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;

    IsInstantiatedOnEachOptimizationIteration = true;

    data = new double[1000][];

    atr = ATR(14); // Initialize ATR with a period of 14, you can modify this according to your strategy

    }



    private void InitializeData()

    {

    Lista = new List<TestList>();

    }

    #endregion







    protected override void OnBarUpdate()

    {

    UpdateIndicators();

    ProcessBarUpdate();



    if (CurrentBar > 0)

    {

    barCounter++;

    ManageExit();

    }

    }



    region OnBarUpdate Helpers

    private void UpdateIndicators()

    {

    prior1 = EMA(8)[0] - EMA(8)[5];

    prior2 = SMA(8)[0] - SMA(8)[13];

    prior3 = RSI(13, 3)[0] - RSI(13, 3)[5];










    }



    private void ProcessBarUpdate()

    {

    int label;




    if (Close[0] > Open[0])

    {

    label = 1; // Up bar

    }

    else

    {

    label = 0; // Down bar

    }

    data[trnum] = new double[] { prior1, prior2, prior3, label };

    Lista.Add(new TestList(prior1, prior2, prior3, label));

    trnum++;




    // If enough data is collected, begin training the classifier

    if (trnum > 1000)

    {

    TrainClassifier();

    MakePrediction();

    }

    }

    #endregion



    region Additional Methods

    private void TrainClassifier()

    {

    // Calculating the probabilities for the classes (Up bar and Down bar)

    int upBarCount = 0;

    foreach (var item in Lista)

    {

    if (item.lside == 1)

    {

    upBarCount++;

    }

    }




    classProbs[0] = (double)(Lista.Count - upBarCount) / Lista.Count;

    classProbs[1] = (double)upBarCount / Lista.Count;




    // Calculating the conditional probabilities for the features (EMA, SMA, RSI)

    double sumPrior1Up = 0, sumPrior1Down = 0;

    double sumPrior2Up = 0, sumPrior2Down = 0;

    double sumPrior3Up = 0, sumPrior3Down = 0;

    foreach (var item in Lista)

    {

    if (item.lside == 1) // Up bar

    {

    sumPrior1Up += item.lprior1;

    sumPrior2Up += item.lprior2;

    sumPrior3Up += item.lprior3;

    }

    else // Down bar

    {

    sumPrior1Down += item.lprior1;

    sumPrior2Down += item.lprior2;

    sumPrior3Down += item.lprior3;

    }

    }




    condProbs[0] = new double[] { sumPrior1Down / (Lista.Count - upBarCount), sumPrior2Down / (Lista.Count - upBarCount), sumPrior3Down / (Lista.Count - upBarCount) }; // Conditional probabilities for Down bar

    condProbs[1] = new double[] { sumPrior1Up / upBarCount, sumPrior2Up / upBarCount, sumPrior3Up / upBarCount }; // Conditional probabilities for Up bar

    }



    private void MakePrediction()

    {

    // Calculating the probabilities for the next bar

    double[] nextBarFeatures = new double[] { EMA(8)[0] - EMA(8)[5], SMA(8)[0] - SMA(8)[13], RSI(13, 3)[0] - RSI(13, 3)[5] };







    double probUpBar = classProbs[1];

    double probDownBar = classProbs[0];




    for (int i = 0; i < nextBarFeatures.Length; i++)

    {

    probUpBar *= ProbDensFunc(nextBarFeatures[i], condProbs[1][i], nextBarFeatures[i]);

    probDownBar *= ProbDensFunc(nextBarFeatures[i], condProbs[0][i], nextBarFeatures[i]);

    }




    // Making a decision based on the calculated probabilities

    if (probUpBar > probDownBar)

    {

    EnterLong(@"myEntrylong");

    }

    else

    {

    EnterShort(@"myEntryshort");

    }

    }

    #endregion






    region Additional Methods 2

    static double ProbDensFunc(double u, double v, double x)

    {

    double left = 1.0 / Math.Sqrt(2 * Math.PI * v);

    double right = Math.Exp(-(x - u) * (x - u) / (2 * v));

    return left * right;

    }




    static double ProbDensFuncStdDev(double u, double v, double x)

    {

    double left = (x - u) > 0 ? (x - u) : (x - u) * -1;

    double right = left / v;

    return right;

    }

    #endregion



    private void ManageExit()

    {

    // Perform the exit logic only every 15 bars

    if (barCounter % Barcounterbars != 0)

    return;



    // declare a variable to hold the ATR value

    double atrValue = atr[0];



    // Ensure the ATRvalue value never exceeds set ATRvaluerange

    atrValue = Math.Min(atrValue, ATRvaluerange);




    // declare a variable to hold the profit target value

    double profitTarget = atrValue * ProfitMultiplier;



    // Ensure the profit target value never exceeds set value

    profitTarget = Math.Min(profitTarget, MaxProfitTarget);




    if (Position.MarketPosition == MarketPosition.Long)

    {

    // exit long position based on ATR range formula

    exitLongOrder = ExitLongLimit(Contracts, Position.AveragePrice + (profitTarget), @"myTarget", @"myEntrylong");



    OkToTrade = false;

    }

    else if (Position.MarketPosition == MarketPosition.Short)

    {

    // exit short position based on ATR range formula

    exitShortOrder = ExitShortLimit(Contracts, Position.AveragePrice - (profitTarget), @"myTarget", @"myEntryshort");



    OkToTrade = false;

    }

    }







    }

    }


    thanks for the help , anton







    #2
    Hello Anton,

    Thanks for your post.

    Typically a strategy will not be accessible in the Strategies window if there is an issue with the code in the OnStateChange() method of the script.

    When working with any issue, it is important to take steps to isolate the issue so that the exact line causing the behavior can be found. This is a process of elimination and a process of debugging by adding prints.

    You should reduce and debug your strategy by commenting out code to determine what exactly in your script is causing an issue. Once you comment out some code, recompile, and see the strategy appearing in the Strategies window, the last section of the commented-out code is likely the culprit in your script.

    Below is a link to a forum post that demonstrates reducing code and debugging.
    https://ninjatrader.com/support/foru...121#post791121
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thanks found the issue

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, 03-13-2026, 05:17 AM
      0 responses
      96 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      154 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      82 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      54 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      72 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X