āļāļģāļāļāļīāļāļēāļĒ
āļāļēāļĄāļāļģāļāļāļāļāļāļŦāļĨāļēāļĒāđ āļāļ āļāļāļāļāļĩāđāđāļĢāļēāļāļģāļĨāļąāļāļāļģāļāļēāļāļāļĒāđāļēāļāļŦāļāļąāļāđāļāļ·āđāļāļāļąāļāđāļāļĢāļĩāļĒāļĄāļāļąāļ§āļāļĒāđāļēāļāđāļāđāļāđāļĨāļ°āđāļāđāļāđāļāļāļāļēāļĢāđāļĢāļĩāļĒāļāļĢāļđāđāļāļāļāđāļāļĢāļ·āđāļāļāļāļēāļāļŠāđāļ§āļāļāļāļāđāļĢāļē
TensorFlow, PyTorch, Keras, Numpy, Pandas āđāļĨāļ°āđāļāđāļāđāļāļ .NET āļāļĩāļāļĄāļēāļāļĄāļēāļĒāđāļāļ·āđāļāđāļĢāļīāđāļĄāļāđāļāđāļāđāļāļēāļāļ āļēāļĒāđāļ cTrader
āļ āļēāļĢāļāļīāļāļāļāļāđāļĢāļēāļāļ·āļāļāļģāđāļŦāđāļāļēāļĢāđāļĢāļĩāļĒāļāļĢāļđāđāļāļāļāđāļāļĢāļ·āđāļāļāļ āļēāļĒāđāļ cTrader āļāđāļēāļĒāļāļķāđāļāļŠāļģāļŦāļĢāļąāļāļāļļāļāļāļ
āļāļāđāļŦāđāļŠāļāļļāļāļāļąāļāļāļēāļĢāļāđāļāļŦāļē!
*** āđāļāđāļāļāļĩāđāđāļĄāđāđāļāđāļāļģāļāļēāļĢāđāļāļĢāļāđāļāđ (āđāļāļĩāļĒāļāđāļāđāļāļīāļĄāļāđāļāđāļāļĄāļđāļĨāļāļāļāļĄāļē āļŊāļĨāļŊ) āđāļāđāļāđāļāļĩāļĒāļāđāļāđāļāļāļąāļ§āļāļĒāđāļēāļāļāļāļāļ§āļīāļāļĩāļāļĩāđāļāļļāļāļŠāļēāļĄāļēāļĢāļāđāļĢāļīāđāļĄāļŠāļĢāđāļēāļāđāļĄāđāļāļĨ AI āļāļāļāļāļļāļāđāļāļāđāļāļĒāđāļāđāđāļāđāļāđāļāļāļāļēāļĢāđāļĢāļĩāļĒāļāļĢāļđāđāļāļāļāđāļāļĢāļ·āđāļāļāļāļāļāđāļĢāļē
.......................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using NumSharp;
using np = NumSharp.np;
using Shape = NumSharp.Shape;
using PandasNet;
using static PandasNet.PandasApi;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class E7BBKGNumSharpSample : Robot
{
[Parameter("Version 1.01", DefaultValue = "Version 1.01")]
public string Version { get; set; }
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Bars Required", DefaultValue = 50, MinValue = 1, MaxValue = 10000, Step = 1)]
public int BarsRequired { get; set; }
[Parameter("Method Name", DefaultValue = MethodName.DataSplitPrints)]
public MethodName Mode { get; set; }
public enum MethodName
{
DataSplitPrints,
PandasPrints,
NDArrayPrints
}
protected override void OnStart()
{
// āđāļĢāļīāđāļĄāļāđāļāļāļąāļ§āļāļĩāđāļ§āļąāļāđāļāđ
}
protected override void OnBar()
{
try
{
if (Mode == MethodName.DataSplitPrints)
{
DataSplitPrints();
}
else if (Mode == MethodName.PandasPrints)
{
PandasPrints();
}
else if (Mode == MethodName.NDArrayPrints)
{
NDArrayPrints();
}
}
catch (Exception ex)
{
Print($"āļāđāļāļāļīāļāļāļĨāļēāļ: {ex.Message}");
if (ex.InnerException != null)
{
Print($"āļāđāļāļāļīāļāļāļĨāļēāļāļ āļēāļĒāđāļ: {ex.InnerException.Message}");
throw;
}
}
}
private float[,] GetDataSet()
{
int startBar = Bars.ClosePrices.Count - BarsRequired;
float[,] inputSignals = new float[BarsRequired, 5];
for (int i = 0; i < BarsRequired; i++)
{
int barIndex = startBar + i;
inputSignals[i, 0] = (float)Bars.OpenPrices[barIndex];
inputSignals[i, 1] = (float)Bars.HighPrices[barIndex];
inputSignals[i, 2] = (float)Bars.LowPrices[barIndex];
inputSignals[i, 3] = (float)Bars.ClosePrices[barIndex];
inputSignals[i, 4] = (float)Bars.TickVolumes[barIndex];
}
return inputSignals;
}
private float[,] GetTargetDataSet()
{
int startBar = Bars.ClosePrices.Count - BarsRequired;
float[,] inputSignals = new float[BarsRequired, 5];
for (int i = 0; i < BarsRequired; i++)
{
int barIndex = startBar + i;
inputSignals[i, 0] = (float)Bars.OpenPrices[barIndex];
inputSignals[i, 1] = (float)Bars.HighPrices[barIndex];
inputSignals[i, 2] = (float)Bars.LowPrices[barIndex];
inputSignals[i, 3] = (float)Bars.ClosePrices[barIndex];
inputSignals[i, 4] = (float)Bars.TickVolumes[barIndex];
}
return inputSignals;
}
/// NumSharp Data Split Prints
public void DataSplitPrints()
{
// āļāļĢāļąāļāļĢāļđāļāđāļāļāļāđāļāļĄāļđāļĨāļāļģāđāļāđāļēāđāļŦāđāļāļĢāļāļāļąāļāļĢāļđāļāđāļāļāļāļĩāđāđāļĄāđāļāļĨāļāļēāļāļŦāļ§āļąāļ
//var inputShape = new Shape(-1, BarsRequired, 5);
NDArray inputData = np.array<float>(GetDataSet());
Print("Input NDarray: " + string.Join(", ", inputData));
// āļāļĢāļąāļāļĢāļđāļāđāļāļāļāđāļāļĄāļđāļĨāđāļāđāļēāļŦāļĄāļēāļĒāđāļŦāđāļāļĢāļāļāļąāļāļĢāļđāļāđāļāļāļāļĩāđāđāļĄāđāļāļĨāļāļēāļāļŦāļ§āļąāļ
//var targetShape = new Shape(-1, 5);
NDArray targetData = np.array<float>(GetTargetDataSet());
Print("Target NDarray: " + string.Join(", ", targetData));
// āđāļāđāļāļāđāļāļĄāļđāļĨāđāļāđāļāļāļļāļāļāļķāļāđāļĨāļ°āļāļļāļāļāļāļŠāļāļ
int testSize = (int)(0.2 * inputData.shape[0]); // 20% āļŠāļģāļŦāļĢāļąāļāļāļēāļĢāļāļāļŠāļāļ
var (x_train, x_test) = (inputData[$":{inputData.shape[0] - testSize}"], inputData[$"{inputData.shape[0] - testSize}:"]);
var (y_train, y_test) = (targetData[$":{targetData.shape[0] - testSize}"], targetData[$"{targetData.shape[0] - testSize}:"]);
Print("āļāđāļāļĄāļđāļĨ X_train: " + string.Join(", ", x_train));
Print("āļāđāļāļĄāļđāļĨ X_test: " + string.Join(", ", x_test));
Print("āļāđāļāļĄāļđāļĨ Y_train: " + string.Join(", ", y_train));
Print("āļāđāļāļĄāļđāļĨ Y_test: " + string.Join(", ", y_test));
}
/// PandasNet Prints
public void PandasPrints()
{
// āđāļāļĨāļ float[,] āđāļāđāļ List<Series>
var inputData = GetDataSet();
var targetData = GetTargetDataSet();
var inputSeriesList = new List<Series>();
var targetSeriesList = new List<Series>();
for (int col = 0; col < inputData.GetLength(1); col++)
{
List<float> columnData = new List<float>();
for (int row = 0; row < inputData.GetLength(0); row++)
{
columnData.Add(inputData[row, col]);
}
inputSeriesList.Add(new Series(columnData.ToArray()));
}
for (int col = 0; col < targetData.GetLength(1); col++)
{
List<float> columnData = new List<float>();
for (int row = 0; row < targetData.GetLength(0); row++)
{
columnData.Add(targetData[row, col]);
}
targetSeriesList.Add(new Series(columnData.ToArray()));
}
// āļŠāļĢāđāļēāļ DataFrames
DataFrame inputDataFrame = new DataFrame(inputSeriesList);
DataFrame targetDataFrame = new DataFrame(targetSeriesList);
Print("Input DataFrame: " + inputDataFrame);
Print("Target DataFrame: " + targetDataFrame);
//Print("Input DataFrame: " + string.Join(", ", inputDataFrame));
//Print("Target DataFrame: " + string.Join(", ", targetDataFrame));
}
/// Simple NumSharp NDArrays Prints
public void NDArrayPrints()
{
if (Bars.ClosePrices.Count < BarsRequired)
return;
try
{
// āđāļĢāļĩāļĒāļāļāđāļāļĄāļđāļĨāļāļģāđāļāđāļēāđāļāļ float[,]
float[,] inputData = GetDataSet();
// āđāļāļĨāļāđāļāđāļ NDArray āđāļĨāļ°āļāļĢāļąāļāļĢāļđāļāđāļāļāđāļāđāļ (BarsRequired, 5)
NDArray inputNDArray = np.array(inputData); // NumSharp
Print("āļāđāļāļĄāļđāļĨ NumSharp NDarray āļāļģāđāļāđāļē: " + string.Join(", ", inputNDArray));
Print("āļĢāļđāļāļĢāđāļēāļ NumSharp NDarray āļāļģāđāļāđāļē: " + string.Join(", ", inputNDArray.shape));
int expectedLength = BarsRequired * 5;
Print($"āļāļ§āļēāļĄāļĒāļēāļ§ NumSharp NDarray āļāļĩāđāļāļēāļāļŦāļ§āļąāļ: {expectedLength}");
Print($"āļāļāļēāļ NumSharp NDarray āļāļģāđāļāđāļē: {inputNDArray.size}");
if (inputNDArray.size != expectedLength)
{
Print($"āļāļ§āļēāļĄāļĒāļēāļ§āđāļĄāđāļāļĢāļāļāļąāļ: āļāļ§āļēāļĄāļĒāļēāļ§āļāļĩāđāļāļēāļāļŦāļ§āļąāļ {expectedLength} āđāļāđāđāļāđāļāļāļēāļ {inputNDArray.size}");
return;
}
}
catch (Exception ex)
{
Print("āļāđāļāļāļīāļāļāļĨāļēāļ: " + ex.Message);
Print("āļĢāļēāļĒāļĨāļ°āđāļāļĩāļĒāļāļŠāđāļāļāđāļāļĢāļ: " + ex.StackTrace);
Exception innerException = ex.InnerException;
while (innerException != null)
{
Print("āļāđāļāļāļīāļāļāļĨāļēāļāļ āļēāļĒāđāļ: " + innerException.Message);
Print("āļĢāļēāļĒāļĨāļ°āđāļāļĩāļĒāļāļŠāđāļāļāđāļāļĢāļāļāļāļāļāđāļāļāļīāļāļāļĨāļēāļāļ āļēāļĒāđāļ: " + innerException.StackTrace);
innerException = innerException.InnerException;
}
}
}
}
}
āļŠāļĢāļļāļ
Key functionalities include:
- Data extraction from market bars (open, high, low, close prices, and tick volumes) over a configurable number of bars.
- Conversion of this data into formats compatible with machine learning workflows, including NumSharp NDArrays and Pandas DataFrames.
- Methods to split data into training and testing sets, and to print these datasets for inspection.
- Three operational modes selectable via parameters: DataSplitPrints, PandasPrints, and NDArrayPrints, each demonstrating different data handling approaches.
This sample code aims to facilitate machine learning development inside cTrader by providing foundational examples of data preparation and manipulation using popular ML libraries in a .NET context.