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
#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?
Comment