okWarrior_EA/references/MQL5/Experts/GCPC/StudyEncoder.mq5

243 lines
18 KiB
MQL5

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Study.mq5 |
//| Copyright DNG<EFBFBD> |
//| https://www.mql5.com/en/users/dng |
//+------------------------------------------------------------------+
#property copyright "Copyright DNG<00>"
#property link "https://www.mql5.com/en/users/dng"
#property version "1.00"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define Study
#include "Trajectory.mqh"
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
input int Iterations = 1e4;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet Encoder;
CNet Decoder;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat State;
CBufferFloat *Result;
CBufferFloat LastEncoder;
CBufferFloat Gradient;
vector<float> STD;
//---
COpenCLMy *OpenCL;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ResetLastError();
if(!LoadTotalBase())
{
PrintFormat("Error of load study data: %d", GetLastError());
return INIT_FAILED;
}
//--- load models
float temp;
if(!Encoder.Load(FileName + "Enc.nnw", temp, temp, temp, dtStudied, true) ||
!Decoder.Load(FileName + "Dec.nnw", temp, temp, temp, dtStudied, true))
{
Print("Init new models");
CArrayObj *encoder = new CArrayObj();
CArrayObj *decoder = new CArrayObj();
if(!CreateTrajNetDescriptions(encoder, decoder))
{
delete encoder;
delete decoder;
return INIT_FAILED;
}
if(!Encoder.Create(encoder) || !Decoder.Create(decoder))
{
delete encoder;
delete decoder;
return INIT_FAILED;
}
delete encoder;
delete decoder;
//---
}
//---
OpenCL = Encoder.GetOpenCL();
Decoder.SetOpenCL(OpenCL);
//---
Encoder.getResults(Result);
if(Result.Total() != EmbeddingSize)
{
PrintFormat("The scope of the Encoder does not match the embedding size count (%d <> %d)", EmbeddingSize, Result.Total());
return INIT_FAILED;
}
//---
Encoder.GetLayerOutput(0, Result);
if(Result.Total() != (HistoryBars * BarDescr))
{
PrintFormat("Input size of Encoder doesn't match state description (%d <> %d)", Result.Total(), (HistoryBars * BarDescr));
return INIT_FAILED;
}
//---
Decoder.GetLayerOutput(0, Result);
if(Result.Total() != EmbeddingSize)
{
PrintFormat("Input size of Decoder doesn't match Encoder output (%d <> %d)", Result.Total(), EmbeddingSize);
return INIT_FAILED;
}
//---
if(!LastEncoder.BufferInit(EmbeddingSize, 0) ||
!Gradient.BufferInit(EmbeddingSize, 0) ||
!LastEncoder.BufferCreate(OpenCL) ||
!Gradient.BufferCreate(OpenCL))
{
PrintFormat("Error of create buffers: %d", GetLastError());
return INIT_FAILED;
}
//---
if(!EventChartCustom(ChartID(), 1, 0, 0, "Init"))
{
PrintFormat("Error of create study event: %d", GetLastError());
return INIT_FAILED;
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
if(!(reason == REASON_INITFAILED || reason == REASON_RECOMPILE))
{
Encoder.Save(FileName + "Enc.nnw", 0, 0, 0, TimeCurrent(), true);
Decoder.Save(FileName + "Dec.nnw", Decoder.getRecentAverageError(), 0, 0, TimeCurrent(), true);