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

279 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 = 1000;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet Agent;
CFQF RTG;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat State;
CBufferFloat *Result;
vector<float> Actions;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ResetLastError();
if(!LoadTotalBase())
{
PrintFormat("Error of load study data: %d", GetLastError());
return INIT_FAILED;
}
//--- load models
float temp;
if(!Agent.Load(FileName + "Act.nnw", temp, temp, temp, dtStudied, true) ||
!RTG.Load(FileName + "RTG.nnw", dtStudied, true))
{
Print("Init new models");
CArrayObj *agent = new CArrayObj();
CArrayObj *rtg = new CArrayObj();
if(!CreateDescriptions(agent,rtg))
{
delete agent;
delete rtg;
return INIT_FAILED;
}
if(!Agent.Create(agent) ||
!RTG.Create(rtg))
{
delete agent;
delete rtg;
return INIT_FAILED;
}
delete agent;
delete rtg;
}
//---
Agent.getResults(Result);
if(Result.Total() != NActions)
{
PrintFormat("The scope of the agent does not match the actions count (%d <> %d)", NActions, Result.Total());
return INIT_FAILED;
}
//---
Agent.GetLayerOutput(0, Result);
if(Result.Total() != (NRewards + BarDescr * NBarInPattern + AccountDescr + TimeDescription + NActions))
{
PrintFormat("Input size of Agent doesn't match state description (%d <> %d)", Result.Total(), (NRewards + BarDescr * NBarInPattern + AccountDescr + TimeDescription + NActions));
return INIT_FAILED;
}
//---
RTG.getResults(Result);
if(Result.Total() != NRewards)
{
PrintFormat("The scope of the RTG does not match the rewards count (%d <> %d)", NRewards, Result.Total());
return INIT_FAILED;
}
//---
RTG.GetLayerOutput(0, Result);
if(Result.Total() != (BarDescr * NBarInPattern + AccountDescr + TimeDescription + NActions))
{
PrintFormat("Input size of RTG doesn't match state description (%d <> %d)", Result.Total(), (BarDescr * NBarInPattern + AccountDescr + TimeDescription + NActions));
return INIT_FAILED;
}
RTG.SetUpdateTarget(1000000);
//---
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)
{
//---
Agent.Save(FileName + "Act.nnw", 0, 0, 0, TimeCurrent(), true);
RTG.Save(FileName + "RTG.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();
}
//+------------------------------------------------------------------+
//| Train function |
//+------------------------------------------------------------------+
void Train(void)
{
int total_tr = ArraySize(Buffer);
uint ticks = GetTickCount();
//---
bool StopFlag = false;
for(int iter = 0; (iter < Iterations && !IsStopped() && !StopFlag); iter ++)
{
int tr = (int)((MathRand() / 32767.0) * (total_tr - 1));
int i = (int)((MathRand() * MathRand() / MathPow(32767, 2)) * MathMax(Buffer[tr].Total - 2 * HistoryBars,MathMin(Buffer[tr].Total,20)));
if(i < 0)