okWarrior_EA/references/MQL5/Experts/FAQ/Study.mq5

277 lines
22 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 = 10000;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet DOT;
CNet Decoder;
CNet Actor;
CNet Critic;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat bState;
CBufferFloat bAccount;
CBufferFloat bGradient;
CBufferFloat bProbs;
CBufferFloat *Result;
vector<float> check;
vector<float> STD_Actor;
vector<float> STD_Goal;
//---
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(!DOT.Load(FileName + "DOT.nnw", temp, temp, temp, dtStudied, true) ||
!Decoder.Load(FileName + "Dec.nnw", temp, temp, temp, dtStudied, true) ||
!Actor.Load(FileName + "Act.nnw", temp, temp, temp, dtStudied, true) ||
!Critic.Load(FileName + "Crt.nnw", temp, temp, temp, dtStudied, true)
)
{
CArrayObj *dot = new CArrayObj();
CArrayObj *decoder = new CArrayObj();
CArrayObj *actor = new CArrayObj();
CArrayObj *critic = new CArrayObj();
if(!CreateDescriptions(dot, decoder, actor, critic))
{
delete dot;
delete decoder;
delete actor;
delete critic;
return INIT_FAILED;
}
if(!DOT.Create(dot) ||
!Decoder.Create(decoder) ||
!Actor.Create(actor) ||
!Critic.Create(critic))
{
delete dot;
delete decoder;
delete actor;
delete critic;
return INIT_FAILED;
}
delete dot;
delete decoder;
delete actor;
delete critic;
}
//---
OpenCL = DOT.GetOpenCL();
Decoder.SetOpenCL(OpenCL);
Actor.SetOpenCL(OpenCL);
Critic.SetOpenCL(OpenCL);
//---
Actor.getResults(Result);
if(Result.Total() != NActions)
{
PrintFormat("The scope of the actor does not match the actions count (%d <> %d)", NActions, Result.Total());
return INIT_FAILED;
}
//---
DOT.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;
}
//---
if(!bGradient.BufferInit(MathMax(AccountDescr, NForecast), 0) ||
!bGradient.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))
{
Actor.Save(FileName + "Act.nnw", 0, 0, 0, TimeCurrent(), true);
DOT.Save(FileName + "DOT.nnw", 0, 0, 0, TimeCurrent(), true);
Decoder.Save(FileName + "Dec.nnw", 0, 0, 0, TimeCurrent(), true);
Critic.Save(FileName + "Crt.nnw", 0, 0, 0, TimeCurrent(), true);
}
delete Result;
delete OpenCL;
}
//+------------------------------------------------------------------+