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

419 lines
33 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 ADAPTEncoder;
CNet ADAPTEndpoints;
CNet ADAPTProbability;
CNet StateEncoder;
CNet EndpointEncoder;
CNet Actor;
//---
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(!ADAPTEncoder.Load(FileName + "Enc.nnw", temp, temp, temp, dtStudied, true) ||
!ADAPTEndpoints.Load(FileName + "Endp.nnw", temp, temp, temp, dtStudied, true) ||
!ADAPTProbability.Load(FileName + "Prob.nnw", temp, temp, temp, dtStudied, true)
)
{
CArrayObj *encoder = new CArrayObj();
CArrayObj *endpoint = new CArrayObj();
CArrayObj *prob = new CArrayObj();
if(!CreateTrajNetDescriptions(encoder, endpoint, prob))
{
delete endpoint;
delete prob;
delete encoder;
return INIT_FAILED;
}
if(!ADAPTEncoder.Create(encoder) ||
!ADAPTEndpoints.Create(endpoint) ||
!ADAPTProbability.Create(prob))
{
delete endpoint;
delete prob;
delete encoder;
return INIT_FAILED;
}
delete endpoint;
delete prob;
delete encoder;
}
//---
if(!StateEncoder.Load(FileName + "StEnc.nnw", temp, temp, temp, dtStudied, true) ||
!EndpointEncoder.Load(FileName + "EndEnc.nnw", temp, temp, temp, dtStudied, true) ||
!Actor.Load(FileName + "Act.nnw", temp, temp, temp, dtStudied, true))
{
CArrayObj *actor = new CArrayObj();
CArrayObj *endpoint = new CArrayObj();
CArrayObj *encoder = new CArrayObj();
if(!CreateDescriptions(actor, endpoint, encoder))
{
delete actor;
delete endpoint;
delete encoder;
return INIT_FAILED;
}
if(!Actor.Create(actor) || !StateEncoder.Create(encoder) || !EndpointEncoder.Create(endpoint))
{
delete actor;
delete endpoint;
delete encoder;
return INIT_FAILED;
}
delete actor;
delete endpoint;
delete encoder;
//---
}
//---
OpenCL = Actor.GetOpenCL();
StateEncoder.SetOpenCL(OpenCL);
EndpointEncoder.SetOpenCL(OpenCL);
ADAPTEncoder.SetOpenCL(OpenCL);
ADAPTEndpoints.SetOpenCL(OpenCL);
ADAPTProbability.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;
}
//---
ADAPTEndpoints.getResults(Result);
if(Result.Total() != 3 * NForecast)
{
PrintFormat("The scope of the Endpoints does not match forecat endpoints (%d <> %d)", 3 * NForecast, Result.Total());
return INIT_FAILED;
}
//---
ADAPTEncoder.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);
StateEncoder.Save(FileName + "StEnc.nnw", 0, 0, 0, TimeCurrent(), true);
EndpointEncoder.Save(FileName + "EndEnc.nnw", 0, 0, 0, TimeCurrent(), true);
ADAPTEncoder.Save(FileName + "Enc.nnw", 0, 0, 0, TimeCurrent(), true);
ADAPTEndpoints.Save(FileName + "Endp.nnw", 0, 0, 0, TimeCurrent(), true);
ADAPTProbability.Save(FileName + "Prob.nnw", 0, 0, 0, TimeCurrent(), true);
}
delete Result;
delete OpenCL;
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
if(id == 1001)
Train();
}
//+------------------------------------------------------------------+
//| Train function |
//+------------------------------------------------------------------+
void Train(void)
{
//---
vector<float> probability = GetProbTrajectories(Buffer, 0.9);
//---
vector<float> result, target;
matrix<float> targets, temp_m;
bool Stop = false;
//---
uint ticks = GetTickCount();
//---
for(int iter = 0; (iter < Iterations && !IsStopped() && !Stop); iter ++)
{
int tr = SampleTrajectory(probability);
int batch = GPTBars + 48;
int state = (int)((MathRand() * MathRand() / MathPow(32767, 2)) * (Buffer[tr].Total - 2 - PrecoderBars - batch));
if(state <= 0)
{
iter--;
continue;
}
ADAPTEncoder.Clear();
int end = MathMin(state + batch, Buffer[tr].Total - PrecoderBars);