Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Machine Learning Code

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

    Machine Learning Code

    Trying to convert this code into NT8 and running into some issues
    https://medium.com/@mcostajr/applied...es-304e9f434c8

    errors:
    private List Lista; gives a generic list error

    data[trnum] no idea what trnum is supposed to be

    classProbs & condProbs also no idea where they came from

    Full code
    PHP 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
    {
    double[][] data;
    
    private List Lista;
    
    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;
    }
    }
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    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][];
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    Lista = new List();
    }
    }
    
    protected override void OnBarUpdate()
    {
    prior1 = EMA(8)[0] - EMA(8)[5];
    prior2 = SMA(8)[0] - SMA(8)[13];
    prior3 = RSI(13)[0] - RSI(13)[5];
    
    if(Close[0]> Open[0])
    {
    data[trnum] = new double[] { prior1, prior2, prior3, 1 }; // 1 = Up Bar
    Lista.Add( new TestList(prior1, prior2, prior3, 1));
    }
    if(Close[0]< Open[0])
    {
    data[trnum] = new double[] { prior1, prior2, prior3, 0 }; // 0 = Down Bar
    Lista.Add( new TestList(prior1, prior2, prior3, 0));
    }
    
    int N = Lista.Count();
    int[] classCts = new int[2]; // up bar, down bar
    for (int i = 0; i < N; ++i)
    {
    int c = (int)data[i][3]; // class is at [3]
    ++classCts[c];
    }
    
    double[][] means = new double[2][];
    for (int c = 0; c < 2; ++c)
    means[c] = new double[3];
    
    for (int i = 0; i < N; ++i)
    {
    int c = (int)data[i][3];
    for (int j = 0; j < 3; ++j) // EMA, SMA, RSI
    means[c][j] += data[i][j];
    }
    for (int c = 0; c < 2; ++c)
    {
    for (int j = 0; j < 3; ++j)
    means[c][j] /= classCts[c];
    }
    
    double[][] variances = new double[2][];
    for (int c = 0; c < 2; ++c)
    variances[c] = new double[3];
    
    for (int i = 0; i < N; ++i)
    {
    int c = (int)data[i][3];
    for (int j = 0; j < 3; ++j)
    {
    double x = data[i][j];
    double u = means[c][j];
    variances[c][j] += (x - u) * (x - u);
    }
    }
    for (int c = 0; c < 2; ++c)
    {
    for (int j = 0; j < 3; ++j)
    variances[c][j] /= classCts[c] - 1;
    }
    
    double[] evidenceTerms = new double[2];
    for (int c = 0; c < 2; ++c)
    {
    evidenceTerms[c] = classProbs[c];
    for (int j = 0; j < 3; ++j)
    evidenceTerms[c] *= condProbs[c][j];
    }
    
    double sumEvidence = 0.0;
    for (int c = 0; c < 2; ++c)
    sumEvidence += evidenceTerms[c];
    
    double[] predictProbs = new double[2];
    for (int c = 0; c < 2; ++c)
    predictProbs[c] = evidenceTerms[c] / sumEvidence;
    
    
    }
    
    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;
    }
    }
    } 
    

    anyone have an idea how to get this working in NT8?
    Last edited by kluwer420; 08-02-2022, 09:14 AM.

    #2
    Hello kluwer420,

    Thanks for your post.

    This would go beyond the level of support we would be able to provide in the Support department at NinjaTrader. In the support department at NinjaTrader, it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

    It would be up to you to take debugging steps to understand where the exact line(s) of code throwing the errors are.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    That said, this forum thread will be open for other community members to share their insights on the topic.

    Let us know if we may assist further.
    <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

    Latest Posts

    Collapse

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