okWarrior_EA/references/MQL5/Experts/EDL/StudyModel.mq5

287 lines
21 KiB
MQL5

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| StudyModel.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 = 1000000;
bool TrainMode = true;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet Scheduler;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat State;
CBufferFloat *Result;
vector<float> SchedulerResult;
//+------------------------------------------------------------------+
//| 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))
{
CArrayObj *actor = new CArrayObj();
CArrayObj *scheduler = new CArrayObj();
if(!CreateDescriptions(actor, scheduler))
{
delete actor;
delete scheduler;
return INIT_FAILED;
}
if(!Scheduler.Create(scheduler))
{
delete actor;
delete scheduler;
return INIT_FAILED;
}
delete actor;
delete scheduler;
//---
}
//---
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;
}
//---
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)
{
//---
Scheduler.Save(FileName + "Sch.nnw", Scheduler.getRecentAverageError(), 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 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;
}
//+------------------------------------------------------------------+
//| Train function |
//+------------------------------------------------------------------+
void Train(void)
{
int total_tr = ArraySize(Buffer);
uint ticks = GetTickCount();
vector<float> account, reward;
int bar, action;
//---
for(int iter = 0; (iter < Iterations && !IsStopped()); iter ++)
{
int tr = (int)(((double)MathRand() / 32767.0) * (total_tr - 1));
int i = (int)((MathRand() * MathRand() / MathPow(32767, 2)) * (Buffer[tr].Total - 2));
State.AssignArray(Buffer[tr].States[i].state);