Warrior_EA/references/MQL5/Experts/CFPI/Study.mq5

337 lines
26 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;
input int BatchSize = 256;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet Actor;
CNet Critic1;
CNet Critic2;
CNet StateEncoder;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat State;
CBufferFloat Account;
CBufferFloat Actions;
CBufferFloat Gradient;
CBufferFloat *Result;
vector<float> check;
//---
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(!StateEncoder.Load(FileName + "Enc.nnw", temp, temp, temp, dtStudied, true) ||
!Critic1.Load(FileName + "Crt1.nnw", temp, temp, temp, dtStudied, true) ||
!Critic2.Load(FileName + "Crt2.nnw", temp, temp, temp, dtStudied, true))
{
Print("Cann't load Critic models");
return INIT_FAILED;
}
if(!Actor.Load(FileName + "Act.nnw", temp, temp, temp, dtStudied, true))
{
Print("Init new models");
CArrayObj *actor = new CArrayObj();
CArrayObj *critic = new CArrayObj();
if(!CreateDescriptions(actor, critic, critic))
{
delete actor;
delete critic;
return INIT_FAILED;
}
if(!Actor.Create(actor))
{
delete actor;
delete critic;
return INIT_FAILED;
}
delete actor;
delete critic;
}
//---
OpenCL = Actor.GetOpenCL();
Critic1.SetOpenCL(OpenCL);
Critic2.SetOpenCL(OpenCL);
StateEncoder.SetOpenCL(OpenCL);
//---
StateEncoder.TrainMode(false);
Critic1.TrainMode(false);
Critic2.TrainMode(false);
//---
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;
}
//---
StateEncoder.GetLayerOutput(0, Result);
if(Result.Total() != (HistoryBars * BarDescr))
{
PrintFormat("Input size of State Encoder doesn't match state description (%d <> %d)", Result.Total(), (HistoryBars * BarDescr));
return INIT_FAILED;
}
//---
StateEncoder.getResults(Result);
int latent_state = Result.Total();
Critic1.GetLayerOutput(0, Result);
if(Result.Total() != latent_state)
{
PrintFormat("Input size of Critic1 doesn't match output State Encoder (%d <> %d)", Result.Total(), latent_state);
return INIT_FAILED;
}
//---
Critic2.GetLayerOutput(0, Result);
if(Result.Total() != latent_state)
{
PrintFormat("Input size of Critic2 doesn't match output State Encoder (%d <> %d)", Result.Total(), latent_state);
return INIT_FAILED;
}
//---
Actor.GetLayerOutput(0, Result);
if(Result.Total() != latent_state)
{
PrintFormat("Input size of Actor doesn't match output State Encoder (%d <> %d)", Result.Total(), latent_state);
return INIT_FAILED;
}
//---
Gradient.BufferInit(AccountDescr, 0);
//---
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);
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> rewards, rewards1, rewards2, target_reward;
vector<float> action, action_beta;