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

432 lines
31 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 OptimizationAgents = 1;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
SState sState;
STrajectory Base;
STrajectory Buffer[];
STrajectory Frame[1];
CNet Agent;
CNet RTG;
//---
float dError;
datetime dtStudied;
//---
CSymbolInfo Symb;
CTrade Trade;
//---
MqlRates Rates[];
CiRSI RSI;
CiCCI CCI;
CiATR ATR;
CiMACD MACD;
//---
CBufferFloat bState;
CBufferFloat *Result;
vector<float> check;
vector<float> AgentResult;
double PrevBalance = 0;
double PrevEquity = 0;
int History = 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;
//---
History = MathMax(ValueBars, NBarInPattern);
if(!RSI.BufferResize(History) || !CCI.BufferResize(History) ||
!ATR.BufferResize(History) || !MACD.BufferResize(History))
{
PrintFormat("%s -> %d", __FUNCTION__, __LINE__);
return INIT_FAILED;
}
//---
if(!Trade.SetTypeFillingBySymbol(Symb.Name()))
return INIT_FAILED;
//--- load models
float temp;
if(!Agent.Load(FileName + "Act.nnw", temp, temp, temp, dtStudied, true))
{
CArrayObj *agent = new CArrayObj();
if(!CreateDescriptions(agent))
{
delete agent;
return INIT_FAILED;
}
if(!Agent.Create(agent))
{
delete agent;
return INIT_FAILED;
}
delete agent;
}
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.SetOpenCL(Agent.GetOpenCL());
//---
Agent.getResults(Result);
if(Result.Total() != NActions)
{
PrintFormat("The scope of the Agent does not match the actions count (%d <> %d)", NActions, Result.Total());
return INIT_FAILED;
}
AgentResult = vector<float>::Zeros(NActions);
//---
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;
}
//---
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), History, Rates);
if(!ArraySetAsSeries(Rates, true))
return;
//---
RSI.Refresh();
CCI.Refresh();
ATR.Refresh();
MACD.Refresh();
Symb.Refresh();
Symb.RefreshRates();
//--- History data
float atr = 0;
bState.Clear();
for(int b = ValueBars - 1; b >= 0; b--)
{
float open = (float)Rates[b].open;
float rsi = (float)RSI.Main(b);
float cci = (float)CCI.Main(b);
atr = (float)ATR.Main(b);
float macd = (float)MACD.Main(b);
float sign = (float)MACD.Signal(b);
if(rsi == EMPTY_VALUE || cci == EMPTY_VALUE || atr == EMPTY_VALUE || macd == EMPTY_VALUE || sign == EMPTY_VALUE)
continue;
//---
bState.Add((float)(Rates[b].close - open));
bState.Add((float)(Rates[b].high - open));
bState.Add((float)(Rates[b].low - open));
bState.Add((float)(Rates[b].tick_volume / 1000.0f));
bState.Add(rsi);
bState.Add(cci);
bState.Add(atr);
bState.Add(macd);
bState.Add(sign);
}
if(!RTG.feedForward((CArrayFloat*)GetPointer(bState), 1, false,(CBufferFloat*)NULL))
return;
//---
for(int b = 0; b < (int)NBarInPattern; b++)
{
float open = (float)Rates[b].open;
float rsi = (float)RSI.Main(b);
float cci = (float)CCI.Main(b);
atr = (float)ATR.Main(b);
float macd = (float)MACD.Main(b);
float sign = (float)MACD.Signal(b);