2026-09-29 18:12:11 +03:00 | | | //+------------------------------------------------------------------+
|
2026-09-28 01:52:58 +03:00 | | | //| Study.mq5 |
|
| | | //| Copyright 2026, DNG |
|
| | | //| https://www.mql5.com/ru/users/dng |
|
2026-08-13 23:48:47 +03:00 | | | //+------------------------------------------------------------------+
|
2026-09-28 01:52:58 +03:00 | | | #property copyright "Copyright 2026, DNG"
|
2026-06-05 22:00:59 +03:00 | | | #property link "https://www.mql5.com/ru/users/dng"
|
| | | #property version "1.00"
|
2026-09-28 01:52:58 +03:00 | | | #property description "Харнесс обучения MASAAT: формирование и калибровка банков навыков."
|
| | | #property description "Проходит стадии жизненного цикла MASAAT и обучает пару актор-критик."
|
| | | #property description "Только для тестов; ордера не выставляет."
|
2026-06-05 22:00:59 +03:00 | | | #define Study
|
| | | #include "Trajectory.mqh"
|
| | | //+------------------------------------------------------------------+
|
| | | //| Input parameters |
|
| | | //+------------------------------------------------------------------+
|
| | | input int Iterations = 100000;
|
| | | STrajectory Buffer[];
|
| | | CNet Actor;
|
| | | CNet Critic;
|
| | | //---
|
| | | float dError;
|
| | | datetime dtStudied;
|
| | | //---
|
| | | CBufferFloat bState;
|
| | | CBufferFloat bActions;
|
| | | CBufferFloat bAccount;
|
| | | CBufferFloat bGradient;
|
| | | CBufferFloat *Result;
|
| | | vector<float> check;
|
| | | vector<float> STD_Actor;
|
| | | vector<float> STD_Goal;
|
| | | //---
|
| | | 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 = 0;
|
| | | if(!Actor.Load(FileName + "Act.nnw", temp, temp, temp, dtStudied, true) ||
|
| | | !Critic.Load(FileName + "Crt.nnw", temp, temp, temp, dtStudied, true)
|
| | | )
|
| | | {
|
| | | Print("Create new models");
|
| | | CArrayObj *actor = new CArrayObj();
|
| | | CArrayObj *critic = new CArrayObj();
|
| | | if(!CreateDescriptions(actor, critic))
|
| | | {
|
| | | delete actor;
|
| | | delete critic;
|
| | | return INIT_FAILED;
|
| | | }
|
| | | if(!Actor.Create(actor) ||
|
| | | !Critic.Create(critic))
|
| | | {
|
| | | delete actor;
|
| | | delete critic;
|
| | | return INIT_FAILED;
|
| | | }
|
| | | delete actor;
|
| | | delete critic;
|
| | | }
|
| | | //---
|
| | | Actor.TrainMode(true);
|
| | | Critic.TrainMode(true);
|
| | | OpenCL = Actor.GetOpenCL();
|
| | | Critic.SetOpenCL(OpenCL);
|
| | | //---
|
| | | Actor.getResults(Result);
|
| | | if(Result.Total() != NActions)
|
| | | {
|
| | | PrintFormat("The scope of the actor does not match the actions count (%d <> %d)", NActions, Result.Total());
|
| | | return INIT_FAILED;
|
| | | }
|
| | | //---
|
| | | Actor.GetLayerOutput(0, Result);
|
| | | if(Result.Total() != (HistoryBars * BarDescr))
|
| | | {
|
| | | PrintFormat("Input size of Encoder doesn't match state description (%d <> %d)", Result.Total(), (HistoryBars * BarDescr));
|
| | | return INIT_FAILED;
|
| | | }
|
| | | //---
|
| | | if(!bGradient.BufferInit(MathMax(AccountDescr, NActions), 0) ||
|
| | | !bGradient.BufferCreate(OpenCL))
|
| | | {
|
| | | PrintFormat("Error of create buffers: %d", GetLastError());
|
| | | 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)
|
| | | {
|
| | | //---
|
| | | if(!(reason == REASON_INITFAILED || reason == REASON_RECOMPILE))
|
| | | {
|
| | | Actor.Save(FileName + "Act.nnw", 0, 0, 0, TimeCurrent(), true);
|
| | | Critic.Save(FileName + "Crt.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, state;
|
| | | bool Stop = false;
|
| | | //---
|
| | | uint ticks = GetTickCount();
|
| | | //---
|
| | | for(int iter = 0; (iter < Iterations && !IsStopped() && !Stop); iter ++)
|
| | | {
|
| | | int tr = SampleTrajectory(probability);
|
| | | int i = (int)((MathRand() * MathRand() / MathPow(32767, 2)) * (Buffer[tr].Total - 2 - NForecast));
|
| | | if(i <= 0)
|
| | | {
|
| | | iter --;
|
| | | continue;
|
| | | }
|
| | | if(!state.Assign(Buffer[tr].States[i].state) ||
|
2026-09-28 01:52:58 +03:00 | | | //--- Implements MathAbs.
|
2026-06-05 22:00:59 +03:00 | | | MathAbs(state).Sum() == 0 ||
|
| | | !bState.AssignArray(state))
|
| | | {
|
| | | iter --;
|
| | | continue;
|
| | | }
|
| | | //--- Account
|
| | | bAccount.Clear();
|
| | | float PrevBalance = Buffer[tr].States[MathMax(i - 1, 0)].account[0];
|
| | | float PrevEquity = Buffer[tr].States[MathMax(i - 1, 0)].account[1];
|
| | | bAccount.Add((Buffer[tr].States[i].account[0] - PrevBalance) / PrevBalance);
|
| | | bAccount.Add(Buffer[tr].States[i].account[1] / PrevBalance);
|
| | | bAccount.Add((Buffer[tr].States[i].account[1] - PrevEquity) / PrevEquity);
|
| | | bAccount.Add(Buffer[tr].States[i].account[2]);
|
| | | bAccount.Add(Buffer[tr].States[i].account[3]);
|
| | | bAccount.Add(Buffer[tr].States[i].account[4] / PrevBalance);
|
| | | bAccount.Add(Buffer[tr].States[i].account[5] / PrevBalance);
|
| | | bAccount.Add(Buffer[tr].States[i].account[6] / PrevBalance);
|
| | | //---
|
| | | double time = (double)Buffer[tr].States[i].account[7];
|
| | | double x = time / (double)(D'2024.01.01' - D'2023.01.01');
|
| | | bAccount.Add((float)MathSin(x != 0 ? 2.0 * M_PI * x : 0));
|
| | | x = time / (double)PeriodSeconds(PERIOD_MN1);
|
| | | bAccount.Add((float)MathCos(x != 0 ? 2.0 * M_PI * x : 0));
|
| | | x = time / (double)PeriodSeconds(PERIOD_W1);
|
| | | bAccount.Add((float)MathSin(x != 0 ? 2.0 * M_PI * x : 0));
|
| | | x = time / (double)PeriodSeconds(PERIOD_D1);
|
| | | bAccount.Add((float)MathSin(x != 0 ? 2.0 * M_PI * x : 0));
|
| | | if(!!bAccount.GetOpenCL())
|
| | | {
|
| | | if(!bAccount.BufferWrite())
|
| | | {
|
| | | PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
|
| | | Stop = true;
|
| | | break;
|
| | | }
|
| | | }
|
| | | //--- Critic
|
| | | target.Assign(Buffer[tr].States[i].action);
|
| | | target.Clip(0,1);
|
| | | bActions.AssignArray(target);
|
| | | if(!!bActions.GetOpenCL())
|
| | | if(!bActions.BufferWrite())
|
| | | {
|
| | | PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
|
| | | Stop = true;
|
| | | break;
|
| | | }
|
| | | Critic.TrainMode(true);
|
| | | if(!Critic.feedForward((CBufferFloat*)GetPointer(bState), 1, false, (CBufferFloat*)GetPointer(bActions)))
|
| | | {
|
| | | PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
|
| | | Stop = true;
|
| | | break;
|
| | | }
|
| | | result.Assign(Buffer[tr].States[i + 1].rewards);
|
| | | target.Assign(Buffer[tr].States[i + 2].rewards);
|
| | | result = result - target * DiscFactor;
|
| | | Result.AssignArray(result);
|
| | | if(!Critic.backProp(Result, (CBufferFloat *)GetPointer(bActions), (CBufferFloat *)GetPointer(bGradient)))
|
| | | {
|
| | | PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
|
| | | Stop = true;
|
| | | break;
|
| | | }
|
| | | //--- Actor Policy
|
| | | if(!Actor.feedForward((CBufferFloat*)GetPointer(bState), 1, false, (CBufferFloat*)GetPointer(bAccount)))
|
| | | {
|
| | | PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
|
| | | Stop = true;
|
| | | break;
|
| | | }
|
| | | Critic.TrainMode(false);
|
| | | if(!Critic.feedForward((CBufferFloat*)GetPointer(bState), 1, false, (CNet*)GetPointer(Actor), LatentLayer))
|
| | | {
|
| | | PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
|
| | | Stop = true;
|
| | | break;
|
| | | }
|
| | | if(result.Sum() >= 0)
|
| | | if(!Actor.backProp(GetPointer(bActions), (CBufferFloat*)GetPointer(bAccount), GetPointer(bGradient)))
|
| | | {
|
| | | PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
|
| | | Stop = true;
|
| | | break;
|
| | | }
|
| | | Critic.getResults(Result);
|
| | | for(int c = 0; c < Result.Total(); c++)
|
| | | {
|
| | | float value = Result.At(c);
|
| | | if(value >= 0)
|
| | | Result.Update(c, value * 1.01f);
|
| | | else
|
| | | Result.Update(c, value * 0.99f);
|
| | | }
|
| | | if(!Critic.backProp(Result, (CNet *)GetPointer(Actor), LatentLayer) ||
|
| | | !Actor.backPropGradient((CBufferFloat*)GetPointer(bAccount), GetPointer(bGradient)))
|
| | | {
|
| | | PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
|
| | | Stop = true;
|
| | | break;
|
| | | }
|
| | | //---
|
| | | if(GetTickCount() - ticks > 500)
|
| | | {
|
| | | double percent = double(iter) * 100.0 / (Iterations);
|
| | | string str = StringFormat("%-14s %6.2f%% -> Error %15.8f\n", "Actor", percent, Actor.getRecentAverageError());
|
| | | str += StringFormat("%-14s %6.2f%% -> Error %15.8f\n", "Critic", percent, Critic.getRecentAverageError());
|
| | | Comment(str);
|
| | | ticks = GetTickCount();
|
| | | }
|
| | | }
|
| | | Comment("");
|
| | | //---
|
| | | PrintFormat("%s -> %d -> %-15s %10.7f", __FUNCTION__, __LINE__, "Actor", Actor.getRecentAverageError());
|
| | | PrintFormat("%s -> %d -> %-15s %10.7f", __FUNCTION__, __LINE__, "Critic", Critic.getRecentAverageError());
|
| | | ExpertRemove();
|
| | | //---
|
| | | }
|
2026-09-29 18:12:11 +03:00 | | | //+------------------------------------------------------------------+
|