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

299 linhas
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 = 1e4;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet Autobot;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat State;
CBufferFloat *Result;
CBufferFloat Ones;
CBufferFloat Gradient;
vector<float> STE;
vector<float> STE_Noise;
//---
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(!Autobot.Load(FileName + "Traj.nnw", temp, temp, temp, dtStudied, true))
{
Print("Init new models");
CArrayObj *autobot = new CArrayObj();
if(!CreateTrajNetDescriptions(autobot))
{
delete autobot;
return INIT_FAILED;
}
if(!Autobot.Create(autobot))
{
delete autobot;
return INIT_FAILED;
}
delete autobot;
//---
}
//---
Autobot.getResults(Result);
if(Result.Total() != PrecoderBars * 3)
{
PrintFormat("The scope of the Autobot does not match the precoder bars (%d <> %d)", PrecoderBars * 3, Result.Total());
return INIT_FAILED;
}
//---
Autobot.GetLayerOutput(0, Result);
if(Result.Total() != (HistoryBars * BarDescr))
{
PrintFormat("Input size of Autobot doesn't match state description (%d <> %d)", Result.Total(), (HistoryBars * BarDescr));
return INIT_FAILED;
}
//---
OpenCL = Autobot.GetOpenCL();
if(!Ones.BufferInit(EmbeddingSize, 1) ||
!Gradient.BufferInit(EmbeddingSize, 0) ||
!Ones.BufferCreate(OpenCL) ||
!Gradient.BufferCreate(OpenCL))
{
PrintFormat("Error of create buffers: %d", GetLastError());
return INIT_FAILED;
}
State.BufferInit(HistoryBars * BarDescr, 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))
Autobot.Save(FileName + "Traj.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> result, target, inp;
matrix<float> targets;
matrix<float> delta;
STE = vector<float>::Zeros(PrecoderBars * 3);
int std_count = 0;
int batch = GPTBars + 50;
bool Stop = false;
uint ticks = GetTickCount();
ulong size = HistoryBars * BarDescr;
//---
for(int iter = 0; (iter < Iterations && !IsStopped() && !Stop); iter ++)
{
int tr = SampleTrajectory(probability);
int state = (int)((MathRand() * MathRand() / MathPow(32767, 2)) * (Buffer[tr].Total - 3 - PrecoderBars - batch));
if(state < 0)
{
iter--;
continue;
}
int end = MathMin(state + batch, Buffer[tr].Total - PrecoderBars);