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

495 linhas
35 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"
//+------------------------------------------------------------------+
//| Includes |
//+------------------------------------------------------------------+
#include "Trajectory.mqh"
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Indicators\Oscilators.mqh>
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
input ENUM_TIMEFRAMES TimeFrame = PERIOD_H1;
input double MinProfit = 10;
//---
input group "---- RSI ----"
input int RSIPeriod = 14; //Period
input ENUM_APPLIED_PRICE RSIPrice = PRICE_CLOSE; //Applied price
//---
input group "---- CCI ----"
input int CCIPeriod = 14; //Period
input ENUM_APPLIED_PRICE CCIPrice = PRICE_TYPICAL; //Applied price
//---
input group "---- ATR ----"
input int ATRPeriod = 14; //Period
//---
input group "---- MACD ----"
input int FastPeriod = 12; //Fast
input int SlowPeriod = 26; //Slow
input int SignalPeriod = 9; //Signal
input ENUM_APPLIED_PRICE MACDPrice = PRICE_CLOSE; //Applied price
input int Agent = 1;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
SState sState;
STrajectory Base;
STrajectory Buffer[];
STrajectory Frame[1];
CNet Autobot;
CNet StateEncoder;
CNet Actor;
CNet Goal;
//---
float dError;
datetime dtStudied;
//---
CSymbolInfo Symb;
CTrade Trade;
//---
MqlRates Rates[];
CiRSI RSI;
CiCCI CCI;
CiATR ATR;
CiMACD MACD;
//---
CBufferFloat bState;
CBufferFloat bOnes;
CBufferFloat bAccount;
CBufferFloat *Result;
vector<float> check;
double PrevBalance = 0;
double PrevEquity = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
if(!Symb.Name(_Symbol))
return INIT_FAILED;
Symb.Refresh();
//---
if(!RSI.Create(Symb.Name(), TimeFrame, RSIPeriod, RSIPrice))
return INIT_FAILED;
//---
if(!CCI.Create(Symb.Name(), TimeFrame, CCIPeriod, CCIPrice))
return INIT_FAILED;
//---
if(!ATR.Create(Symb.Name(), TimeFrame, ATRPeriod))
return INIT_FAILED;
//---
if(!MACD.Create(Symb.Name(), TimeFrame, FastPeriod, SlowPeriod, SignalPeriod, MACDPrice))
return INIT_FAILED;
if(!RSI.BufferResize(HistoryBars) || !CCI.BufferResize(HistoryBars) ||
!ATR.BufferResize(HistoryBars) || !MACD.BufferResize(HistoryBars))
{
PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
return INIT_FAILED;
}
//---
if(!Trade.SetTypeFillingBySymbol(Symb.Name()))
return INIT_FAILED;
//--- load models
float temp;
if(!Autobot.Load(FileName + "Traj.nnw", temp, temp, temp, dtStudied, true))
{
CArrayObj *autobot = new CArrayObj();
if(!CreateTrajNetDescriptions(autobot))
{
delete autobot;
return INIT_FAILED;
}
if(!Autobot.Create(autobot))
{
delete autobot;
return INIT_FAILED;
}
delete autobot;
//---
}
if(!StateEncoder.Load(FileName + "StEnc.nnw", temp, temp, temp, dtStudied, true) ||
!Goal.Load(FileName + "Goal.nnw", temp, temp, temp, dtStudied, true) ||
!Actor.Load(FileName + "Act.nnw", temp, temp, temp, dtStudied, true))
{
CArrayObj *actor = new CArrayObj();
CArrayObj *goal = new CArrayObj();
CArrayObj *encoder = new CArrayObj();
if(!CreateDescriptions(actor, goal, encoder))
{
delete actor;
delete goal;
delete encoder;
return INIT_FAILED;
}
if(!Actor.Create(actor) || !StateEncoder.Create(encoder) || !Goal.Create(goal))
{
delete actor;
delete goal;
delete encoder;
return INIT_FAILED;
}
delete actor;
delete goal;
delete encoder;
//---
}
//---
StateEncoder.SetOpenCL(Actor.GetOpenCL());
Autobot.SetOpenCL(Actor.GetOpenCL());
Goal.SetOpenCL(Actor.GetOpenCL());
Autobot.TrainMode(false);
//---
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;
}
//---
Autobot.getResults(Result);
if(Result.Total() != PrecoderBars * 3)
{
PrintFormat("The scope of the Autobot does not match 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;
}
//---
StateEncoder.GetLayerOutput(0, Result);
if(Result.Total() != PrecoderBars * 3)
{
PrintFormat("Input size of State Encoder doesn't match precoder bars (%d <> %d)", Result.Total(), PrecoderBars * 3);
return INIT_FAILED;
}
//---
StateEncoder.getResults(Result);
int latent_state = Result.Total();
Actor.GetLayerOutput(0, Result);
if(Result.Total() != latent_state)
{
PrintFormat("Input size of Actor doesn't match output State Encoder (%d <> %d)", Result.Total(), latent_state);
return INIT_FAILED;
}
//---
Goal.GetLayerOutput(0, Result);
latent_state = Result.Total();
Autobot.getResults(Result);
if(Result.Total() != latent_state)
{
PrintFormat("Input size of Goal doesn't match output Autobot (%d <> %d)", Result.Total(), latent_state);
return INIT_FAILED;
}
//---
Goal.getResults(Result);
if(Result.Total() != NRewards)
{
PrintFormat("The scope of Goal doesn't match rewards count (%d <> %d)", Result.Total(), NRewards);
return INIT_FAILED;
}
//---
if(!bOnes.BufferInit(EmbeddingSize, 1) ||
!bOnes.BufferCreate(Autobot.GetOpenCL()))
{
PrintFormat("Error of create buffers: %d", GetLastError());
return INIT_FAILED;
}
//---
PrevBalance = AccountInfoDouble(ACCOUNT_BALANCE);
PrevEquity = AccountInfoDouble(ACCOUNT_EQUITY);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
delete Result;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(!IsNewBar())
return;
//---
int bars = CopyRates(Symb.Name(), TimeFrame, iTime(Symb.Name(), TimeFrame, 1), HistoryBars, Rates);
if(!ArraySetAsSeries(Rates, true))
return;
//---
RSI.Refresh();
CCI.Refresh();
ATR.Refresh();
MACD.Refresh();
Symb.Refresh();
Symb.RefreshRates();
//---
float atr = 0;
for(int b = 0; b < (int)HistoryBars; b++)
{
float open = (float)Rates[b].open;
float rsi = (float)RSI.Main(b);
float cci = (float)CCI.Main(b);