Warrior_EA/references/MQL5/Experts/EDL/StudyActor3.mq5

323 lines
23 KiB
MQL5

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Research.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"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "Trajectory.mqh"
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
input int Iterations = 100000;
input int LatentLayer = 8;
input float DiscountFactor = 0.99f;
bool TrainMode = true;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet Scheduler;
CFQF Actor;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat State;
CBufferFloat *Result;
vector<float> SchedulerResult;
vector<float> check;
vector<float> ActorResult;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ResetLastError();
if(!LoadTotalBase())
{
PrintFormat("Error of load study data: %d", GetLastError());
return INIT_FAILED;
}
//--- load models
float temp;
if(!Scheduler.Load(FileName + "Sch.nnw", temp, temp, temp, dtStudied, true))
{
PrintFormat("Error of load scheduler model: %d", GetLastError());
return INIT_FAILED;
}
if(!Actor.Load(FileName + "Act3.nnw", dtStudied, true))
{
CArrayObj *actor = new CArrayObj();
CArrayObj *scheduler = new CArrayObj();
if(!CreateDescriptions(actor, scheduler))
{
delete actor;
delete scheduler;
return INIT_FAILED;
}
if(!Actor.Create(actor))
{
delete actor;
delete scheduler;
return INIT_FAILED;
}
delete actor;
delete scheduler;
//---
}
//---
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;
}
Actor.SetOpenCL(Scheduler.GetOpenCL());
Actor.SetUpdateTarget(Iterations+10);
//---
Scheduler.getResults(Result);
if(Result.Total() != AccountDescr)
{
PrintFormat("The scope of the scheduler does not match the account description (%d <> %d)", AccountDescr, Result.Total());
return INIT_FAILED;
}
//---
Actor.GetLayerOutput(0, Result);
int inputs = Result.Total();
if(!Scheduler.GetLayerOutput(LatentLayer, Result))
{
PrintFormat("Error of load latent layer %d", LatentLayer);
return INIT_FAILED;
}
if(inputs != Result.Total())
{
PrintFormat("Size of latent layer does not match input size of Actor (%d <> %d)", Result.Total(), inputs);
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)
{
//---
Actor.Save(FileName + "Act3.nnw", TimeCurrent(), true);
delete Result;
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
if(id == 1001)
Train();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool LoadTotalBase(void)
{
int handle = FileOpen(FileName + ".bd", FILE_READ | FILE_BIN | FILE_COMMON | FILE_SHARE_READ);
if(handle < 0)
return false;
int total = FileReadInteger(handle);
if(total <= 0)
{
FileClose(handle);
return false;
}
if(ArrayResize(Buffer, total) < total)
{
FileClose(handle);
return false;
}
for(int i = 0; i < total; i++)
if(!Buffer[i].Load(handle))
{
FileClose(handle);
return false;
}
FileClose(handle);
//---
return true;
}
//+------------------------------------------------------------------+