Warrior_EA/references/MQL5/Experts/CWBC/StudyRTG.mq5

229 lines
16 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 = 1e7;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet RTG;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat State;
CBufferFloat *Result;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ResetLastError();
if(!LoadTotalBase())
{
PrintFormat("Error of load study data: %d", GetLastError());
return INIT_FAILED;
}
//--- load models
float temp;
if(!RTG.Load(FileName + "RTG.nnw", temp, temp, temp, dtStudied, true))
{
CArrayObj *rtg = new CArrayObj();
if(!CreateRTGDescriptions(rtg))
{
delete rtg;
return INIT_FAILED;
}
if(!RTG.Create(rtg))
{
delete rtg;
return INIT_FAILED;
}
delete rtg;
//---
}
//---
RTG.getResults(Result);
if(Result.Total() != NRewards)
{
PrintFormat("The scope of the RTG-model does not match the rewards count (%d <> %d)", NRewards, 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)
{
//---
RTG.Save(FileName + "RTG.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();
}
//+------------------------------------------------------------------+
//| Train function |
//+------------------------------------------------------------------+
void Train(void)
{
float max_reward = 0, quanitle = 0;
vector<float> std;
vector<float> probability = GetProbTrajectories(Buffer, max_reward, quanitle, std, 0.95, 0.1f);
uint ticks = GetTickCount();
//---
bool StopFlag = false;
for(int iter = 0; (iter < Iterations && !IsStopped() && !StopFlag); iter ++)
{
int tr = SampleTrajectory(probability);
int i = (int)((MathRand() * MathRand() / MathPow(32767, 2)) * (Buffer[tr].Total - 2 * ValueBars));
if(i < 0)
{
iter--;
continue;
}