Vigiar
1
0
Derivar
Você já tinha feito uma derivação do repositório okWarrior_EA, anteriormente
0
okWarrior_EA/references/MQL5/Experts/DIAYN/Study.mq5

344 linhas
26 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 float DiscountFactor = 0.99f;
bool TrainMode = true;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CFQF Actor;
CFQF Scheduler;
CNet Discriminator;
int Models = 1;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat State1;
CBufferFloat *Result;
vector<float> ActorResult;
vector<float> SchedulerResult;
vector<float> DiscriminatorResult;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ResetLastError();
if(!LoadTotalBase())
{
PrintFormat("Error of load study data: %d", GetLastError());
return INIT_FAILED;
}
//--- load models
float temp;
if(!Actor.Load(FileName + "Act.nnw", dtStudied, true) ||
!Scheduler.Load(FileName + "Sch.nnw", dtStudied, true) ||
!Discriminator.Load(FileName + "Dscr.nnw", temp, temp, temp, dtStudied, true))
{
CArrayObj *actor = new CArrayObj();
CArrayObj *scheduler = new CArrayObj();
CArrayObj *discriminator = new CArrayObj();
if(!CreateDescriptions(actor, scheduler, discriminator))
{
delete actor;
delete scheduler;
delete discriminator;
return INIT_FAILED;
}
if(!Actor.Create(actor) ||
!Scheduler.Create(scheduler) ||
!Discriminator.Create(discriminator))
{
delete actor;
delete scheduler;
delete discriminator;
return INIT_FAILED;
}
delete actor;
delete scheduler;
delete discriminator;
//---
#ifdef FileName
if(!Actor.UpdateTarget(FileName + ".nnw") ||
!Scheduler.UpdateTarget(FileName + ".nnw"))
#else
if(!Actor.UpdateTarget("FQF.upd") ||
!Scheduler.UpdateTarget("FQF.nnw"))
#endif
return INIT_FAILED;
}
//---
Discriminator.getResults(Result);
if(Result.Total() != NSkills)
{
PrintFormat("The scope of the discriminator does not match the skills count (%d <> %d)", NSkills, Result.Total());
return INIT_FAILED;
}
Scheduler.getResults(Result);
Scheduler.SetUpdateTarget(MathMax(Iterations / 100, 50000));
if(Result.Total() != NSkills)
{
PrintFormat("The scope of the scheduler does not match the skills count (%d <> %d)", NSkills, Result.Total());
return INIT_FAILED;
}
Actor.getResults(Result);
Actor.SetUpdateTarget(MathMax(Iterations / 100, 50000));
if(Result.Total() != NActions)
{
PrintFormat("The scope of the actor does not match the actions count (%d <> %d)", NActions, Result.Total());
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 + "Act.nnw", TimeCurrent(), true);
Scheduler.Save(FileName + "Sch.nnw", TimeCurrent(), true);
Discriminator.Save(FileName + "Dscr.nnw", 0, 0, 0, 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 SaveTotalBase(void)
{
int total = ArraySize(Buffer);
if(total < 0)
return true;
int handle = FileOpen(FileName + ".bd", FILE_WRITE | FILE_BIN | FILE_COMMON);
if(handle < 0)
return false;
if(FileWriteInteger(handle, total) < INT_VALUE)
{
FileClose(handle);
return false;
}
for(int i = 0; i < total; i++)
if(!Buffer[i].Save(handle))
{
FileClose(handle);
return false;
}
FileFlush(handle);
FileClose(handle);
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool LoadTotalBase(void)
{
int handle = FileOpen(FileName + ".bd", FILE_READ | FILE_BIN | FILE_COMMON | FILE_SHARE_READ);
if(handle < 0)