Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 myprior1double myprior2double myprior3double 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[] { prior1prior2prior3}; // 1 = Up Bar
    Lista.Add( new TestList(prior1prior2prior31));
    }
    if(
    Close[0]< Open[0])
    {
    data[trnum] = new double[] { prior1prior2prior3}; // 0 = Down Bar
    Lista.Add( new TestList(prior1prior2prior30));
    }

    int N Lista.Count();
    int[] classCts = new int[2]; // up bar, down bar
    for (int i 0N; ++i)
    {
    int c = (int)data[i][3]; // class is at [3]
    ++classCts[c];
    }

    double[][] means = new double[2][];
    for (
    int c 02; ++c)
    means[c] = new double[3];

    for (
    int i 0N; ++i)
    {
    int c = (int)data[i][3];
    for (
    int j 03; ++j// EMA, SMA, RSI
    means[c][j] += data[i][j];
    }
    for (
    int c 02; ++c)
    {
    for (
    int j 03; ++j)
    means[c][j] /= classCts[c];
    }

    double[][] variances = new double[2][];
    for (
    int c 02; ++c)
    variances[c] = new double[3];

    for (
    int i 0N; ++i)
    {
    int c = (int)data[i][3];
    for (
    int j 03; ++j)
    {
    double x data[i][j];
    double u means[c][j];
    variances[c][j] += (u) * (u);
    }
    }
    for (
    int c 02; ++c)
    {
    for (
    int j 03; ++j)
    variances[c][j] /= classCts[c] - 1;
    }

    double[] evidenceTerms = new double[2];
    for (
    int c 02; ++c)
    {
    evidenceTerms[c] = classProbs[c];
    for (
    int j 03; ++j)
    evidenceTerms[c] *= condProbs[c][j];
    }

    double sumEvidence 0.0;
    for (
    int c 02; ++c)
    sumEvidence += evidenceTerms[c];

    double[] predictProbs = new double[2];
    for (
    int c 02; ++c)
    predictProbs[c] = evidenceTerms[c] / sumEvidence;


    }

    static 
    double ProbDensFunc(double udouble vdouble x)
    {
    double left 1.0 Math.Sqrt(Math.PI v);
    double right Math.Exp( -(u) * (u) / (v) );
    return 
    left right;
    }

    static 
    double ProbDensFuncStdDev(double udouble vdouble x)
    {
    double left = ( u) > ? (u) : (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.
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by manueldecastro, Today, 10:26 AM
    0 responses
    1 view
    0 likes
    Last Post manueldecastro  
    Started by RaddiFX, Yesterday, 09:55 PM
    4 responses
    29 views
    0 likes
    Last Post RaddiFX
    by RaddiFX
     
    Started by geotrades1, Today, 08:33 AM
    5 responses
    16 views
    0 likes
    Last Post geotrades1  
    Started by Entwaze, 02-19-2024, 07:13 PM
    2 responses
    68 views
    0 likes
    Last Post MalachiHatfield  
    Started by tkaboris, Today, 08:32 AM
    5 responses
    13 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X