Warrior_EA/references/MQL5/Experts/Evalutionary/Evolution.mq5

251 lines
19 KiB
MQL5

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Genetic.mq5 |
//| Copyright 2022, DNG |
//| https://www.mql5.com/en/users/dng |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, DNG"
#property link "https://www.mql5.com/en/users/dng"
#property version "1.00"
//+------------------------------------------------------------------+
//| Includes |
//+------------------------------------------------------------------+
#include "NetEvolution.mqh"
#include <Trade\SymbolInfo.mqh>
#include <Indicators\Oscilators.mqh>
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define MODEL Symb.Name()+"_"+EnumToString((ENUM_TIMEFRAMES)Period())+"_Evolution"
#define NeuronsToBar 12
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
input int StudyPeriod = 2; //Study period, years
uint HistoryBars = 20; //Depth of history
ENUM_TIMEFRAMES TimeFrame = PERIOD_H1;
input int PopulationSize = 50;
input int Generations = 1000;
input float Mutation = 0.01f;
int Actions = 3;
//---
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
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CSymbolInfo Symb;
MqlRates Rates[];
CNetEvolution Models;
CBufferFloat *TempData;
CiRSI RSI;
CiCCI CCI;
CiATR ATR;
CiMACD MACD;
//---
float dError;
datetime dtStudied;
bool bEventStudy;
MqlDateTime sTime;
float BestLoss;
//+------------------------------------------------------------------+
//| 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(!Models.Load(MODEL + ".nnw", PopulationSize, false))
return INIT_FAILED;
//---
if(!Models.GetLayerOutput(0, TempData))
return INIT_FAILED;
HistoryBars = TempData.Total() / NeuronsToBar;
Models.getResults(TempData);
if(TempData.Total() != Actions)
return INIT_PARAMETERS_INCORRECT;
//---
bEventStudy = EventChartCustom(ChartID(), 1, 0, 0, "Init");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(!!TempData)
delete TempData;
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
if(id == 1001)
Train();