Files
xMQL5/MQLTestWorkspace/BKPS/14030228/Documents/BKP/x-sample.ea copy.mq5
T
2024-05-17 04:54:01 +03:30

728 lines
16 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center XSAMPLE MQL5 Expert Advisor
// -------------------------------------------------
// Name: XSAMPLE
// Description: an Exper Advisor which used RSI and MA
// to Analyse Market ...
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////x
//
// Global Properties ...
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm XSAMPLE"
#property strict
//
#define ShortName "XSAMPLE"
//
#include "../Libraries/x-saherelm.log.lib.mq5"
#include "../Libraries/x-saherelm.draw.lib.mq5"
#include "../Libraries/x-saherelm.common.lib.mq5"
#include "../Classes/x-saherelm.xtrade.class.mq5"
//
#include "../Helpers/x-saherelm.x121.xmc.helper.mq5"
#include "../Helpers/x-saherelm.x121.xstr.helper.mq5"
#include "../Helpers/x-saherelm.x121.xche.helper.mq5"
#include "../Helpers/x-saherelm.x121.xosc.helper.mq5"
//
#include "../Classes/x-saherelm.x121.xmcycle.class.mq5"
//
// Inputs ...
long xSampleEAMagicNumber = 78692110; // Magic Number
int xSampleEASlippage = 10; // Slippgae
//
double xSampleEAVolume = 0.01;
//
// Vars ...
string xSampleEASymbol;
ENUM_TIMEFRAMES xSampleEAPeriod;
//
int maxBuys = 1;
int maxSells = 1;
int buyCount = 0;
int sellCount = 0;
//
double _tp = 0;
double _sl = 0;
double _entry = 0;
ENUM_POSITION_TYPE _type = NULL;
//
MqlTick xSampleEATick;
XSCTrade *xSampleEATrader;
// //
// // XCHE ...
// X121CHEInputs xSampleEACHEInputs;
// XSCX121CHEHelper xSampleEACHEHelper;
// //
// // XMC ...
// X121MCInputs xSampleEAMCInputs;
// XSCX121MCHelper xSampleEAMCHelper;
// //
// // XSTR ...
// X121STRInputs xSampleEASTRInputs;
// XSCX121STRHelper xSampleEASTRHelper;
// //
// // XOSC ...
// X121OSCInputs xSampleEAOSCInputs;
// XSCX121OSCHelper xSampleEAOSCHelper;
// //
// // XICH ...
// X121ICHInputs xSampleEAICHInputs;
// XSCX121ICHHelper xSampleEAICHHelper;
//
// X121MCycle ...
X121MCycleInputs xSampleEA121MCInputs;
XSC121Market xSampleEA121MC;
//
// Event Handlers ...
//
// Initialization ...
int OnInit()
{
//
if (xSampleEASymbol == NULL || StringLen(xSampleEASymbol) <= 0)
{
xSampleEASymbol = _Symbol;
}
//
if (xSampleEAPeriod == NULL)
{
xSampleEAPeriod = _Period;
}
//
// Validate Inputs ...
if (!XSampleEAValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
logTag = ShortName;
drawPrefix = ShortName;
// //
// // XCHE ...
// //
// xSampleEACHEInputs.Default();
// if (!xSampleEACHEInputs.IsValid())
// {
// return INIT_PARAMETERS_INCORRECT;
// }
// //
// if (!xSampleEACHEHelper.Init(
// xSampleEASymbol,
// xSampleEAPeriod,
// xSampleEACHEInputs))
// {
// return INIT_PARAMETERS_INCORRECT;
// }
// //
// // XMC ...
// //
// xSampleEAMCInputs.Default();
// if (!xSampleEAMCInputs.IsValid())
// {
// return INIT_PARAMETERS_INCORRECT;
// }
// //
// if (!xSampleEAMCHelper.Init(
// xSampleEASymbol,
// xSampleEAPeriod,
// xSampleEAMCInputs))
// {
// return INIT_PARAMETERS_INCORRECT;
// }
// //
// // XSTR ...
// //
// xSampleEASTRInputs.Default();
// if (!xSampleEASTRInputs.IsValid())
// {
// return INIT_PARAMETERS_INCORRECT;
// }
// //
// if (!xSampleEASTRHelper.Init(
// xSampleEASymbol,
// xSampleEAPeriod,
// xSampleEASTRInputs))
// {
// return INIT_PARAMETERS_INCORRECT;
// }
// //
// // XOSC ...
// //
// xSampleEAOSCInputs.Default();
// if (!xSampleEAOSCInputs.IsValid())
// {
// return INIT_PARAMETERS_INCORRECT;
// }
// //
// if (!xSampleEAOSCHelper.Init(
// xSampleEASymbol,
// xSampleEAPeriod,
// xSampleEAOSCInputs))
// {
// return INIT_PARAMETERS_INCORRECT;
// }
//
// XICH ...
// //
// xSampleEAICHInputs.Default();
// if (!xSampleEAICHInputs.IsValid())
// {
// return INIT_PARAMETERS_INCORRECT;
// }
// //
// if (!xSampleEAICHHelper.Init(
// xSampleEASymbol,
// xSampleEAPeriod,
// xSampleEAICHInputs))
// {
// return INIT_PARAMETERS_INCORRECT;
// }
//
// X121Market Cycle ...
bool result = xSampleEA121MCInputs.Init(
xSampleEASymbol,
xSampleEAPeriod,
X_MARKET_CYCLE_SHORT,
PERIOD_H1,
X_PERIOD_MANUALLY //
);
//
// after Inputs Initialize Successfully we must prepare Styling for Draw ...
ApplyMCDrawStyles();
//
result = xSampleEA121MC.Init(
xSampleEA121MCInputs
//
);
if (!result)
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize XTrader ...
xSampleEATrader = new XSCTrade(
xSampleEASlippage,
xSampleEAMagicNumber);
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
//
// DeInitialization ...
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
//
// De Initialize XSampleEA Providers ...
delete xSampleEATrader;
}
//
// On Tick Handler ...
void OnTick()
{
//
if (!IsNewCandle(
xSampleEASymbol,
xSampleEAPeriod))
{
return;
}
//
// Reading Tick ...
if (!SymbolInfoTick(xSampleEASymbol, xSampleEATick))
{
return;
}
//
// Counting Currently Open Positions ...
buyCount = xSampleEATrader.CountPositions(xSampleEASymbol, POSITION_TYPE_BUY);
sellCount = xSampleEATrader.CountPositions(xSampleEASymbol, POSITION_TYPE_SELL);
//
// TestMC();
// TestCHE();
// TestSTR();
// TestOSC();
// TestICH();
//
// TestMCycle();
//
// Draw Market Cycle Bars On Chart ...
int offset = 0;
xSampleEA121MC.Draw(
ChartID(),
0,
offset
//
);
//
if (buyCount < maxBuys)
{
//
// Check For Buy Signals ...
}
//
if (sellCount < maxSells)
{
//
// Check for Sell Signals ...
}
}
//
// Custom Functions ...
//
// Validate Variables nd Inputs ...
bool XSampleEAValidateInputs()
{
//
bool result = false;
//
result =
//
xSampleEASlippage > 0 &&
xSampleEAMagicNumber > 0
//
;
//
return result;
}
//
// Tools ...
// //
// void TestCHE()
// {
// //
// double longExit1 = xSampleEACHEHelper.GetLongExit1(0);
// if (NotEmpty(longExit1))
// {
// Print("LE1: " + ToString(longExit1));
// }
// //
// double lEs[];
// int lEsCount = xSampleEACHEHelper.CopyLongExit1(
// 0,
// 2,
// lEs);
// if (lEsCount > 0)
// {
// //
// for (int i = 0; i < lEsCount; i++)
// {
// //
// double iVal = lEs[i];
// //
// if (NotEmpty(iVal))
// {
// //
// Print("LE " + ToString(i) + ": " + ToString(iVal));
// }
// }
// //
// Print(" ");
// }
// }
// //
// void TestMC()
// {
// //
// double fast = xSampleEAMCHelper.GetFast(0);
// if (NotEmpty(fast))
// {
// Print("Fast: " + ToString(fast));
// }
// //
// double slow = xSampleEAMCHelper.GetSlow(0);
// if (NotEmpty(slow))
// {
// Print("Slow: " + ToString(slow));
// }
// }
// //
// void TestSTR()
// {
// //
// double trend = xSampleEASTRHelper.GetTrend(0);
// if (NotEmpty(trend))
// {
// Print("Trend: " + ToString(trend));
// }
// //
// double state = xSampleEASTRHelper.GetState(0);
// if (NotEmpty(state))
// {
// Print("State: " + EnumToString(xSampleEASTRHelper.ToState(state)));
// }
// }
// //
// void TestOSC()
// {
// //
// double rsi = xSampleEAOSCHelper.GetRSI(0);
// if (NotEmpty(rsi))
// {
// Print("RSI: " + ToString(rsi));
// }
// //
// double cci = xSampleEAOSCHelper.GetCCI(0);
// if (NotEmpty(cci))
// {
// Print("CCI: " + ToString(cci));
// }
// }
// //
// void TestICH()
// {
// //
// double tenkanSen = xSampleEAICHHelper.GetTenkanSen(1);
// double tenkanSenLast = xSampleEAICHHelper.GetTenkanSen(2);
// //
// double kijunSen = xSampleEAICHHelper.GetKijunSen(1);
// double kijunSenLast = xSampleEAICHHelper.GetKijunSen(2);
// //
// double chikouSpan = xSampleEAICHHelper.GetChikouSpan(1);
// //
// double senkouSpanA = xSampleEAICHHelper.GetSenkouSpanA(1);
// double senkouSpanALast = xSampleEAICHHelper.GetSenkouSpanA(2);
// //
// double senkouSpanB = xSampleEAICHHelper.GetSenkouSpanB(1);
// double senkouSpanBLast = xSampleEAICHHelper.GetSenkouSpanB(2);
// //
// double futureSenkouSpanA = xSampleEAICHHelper.GetFutureSenkouSpanA(1);
// double futureSenkouSpanALast = xSampleEAICHHelper.GetFutureSenkouSpanA(2);
// //
// double futureSenkouSpanB = xSampleEAICHHelper.GetFutureSenkouSpanB(1);
// double futureSenkouSpanBLast = xSampleEAICHHelper.GetFutureSenkouSpanB(2);
// //
// bool isTenkanSenCrossedOverKijunSen =
// //
// tenkanSen > kijunSen &&
// tenkanSenLast <= kijunSenLast
// //
// ;
// if (isTenkanSenCrossedOverKijunSen)
// {
// Print("isTenkanSenCrossedOverKijunSen");
// }
// //
// bool isTenkanSenCrossedUnderKijunSen =
// //
// tenkanSen < kijunSen &&
// tenkanSenLast >= kijunSenLast
// //
// ;
// if (isTenkanSenCrossedUnderKijunSen)
// {
// Print("isTenkanSenCrossedUnderKijunSen");
// }
// //
// bool isKumoSwitchedToBullish =
// //
// senkouSpanA > senkouSpanB &&
// senkouSpanALast <= senkouSpanBLast
// //
// ;
// if (isKumoSwitchedToBullish)
// {
// Print("isKumoSwitchedToBullish");
// }
// //
// bool isKumoSwitchedToBearish =
// //
// senkouSpanA < senkouSpanB &&
// senkouSpanALast >= senkouSpanBLast
// //
// ;
// if (isKumoSwitchedToBearish)
// {
// Print("isKumoSwitchedToBearish");
// }
// //
// bool isFutureKumoSwitchedToBullish =
// //
// futureSenkouSpanA > futureSenkouSpanB &&
// futureSenkouSpanALast <= futureSenkouSpanBLast
// //
// ;
// if (isFutureKumoSwitchedToBullish)
// {
// Print("isFutureKumoSwitchedToBullish");
// }
// //
// bool isFutureKumoSwitchedToBearish =
// //
// futureSenkouSpanA < futureSenkouSpanB &&
// futureSenkouSpanALast >= futureSenkouSpanBLast
// //
// ;
// if (isFutureKumoSwitchedToBearish)
// {
// Print("isFutureKumoSwitchedToBearish");
// }
// }
// //
// void TestMCycle()
// {
// //
// // double fast0 = xSampleEA121MC.mc.GetFast(0);
// // double fast1 = xSampleEA121MC.mc.GetFast(1);
// // //
// // double slow0 = xSampleEA121MC.mc.GetSlow(0);
// // double slow1 = xSampleEA121MC.mc.GetSlow(1);
// // //
// // double tenkanSen = xSampleEA121MC.ich.GetTenkanSen(0);
// // //
// // Print("Fast0: " + ToString(fast0));
// // Print("Slow0: " + ToString(slow0));
// // //
// // Print("Fast1: " + ToString(fast1));
// // Print("Slow1: " + ToString(slow1));
// // //
// // Print("TenkanSen: " + ToString(tenkanSen));
// // //
// // bool isFastCrossedOverSlow =
// // //
// // fast0 > slow0 &&
// // fast1 <= slow1
// // //
// // ;
// // if (isFastCrossedOverSlow)
// // {
// // //
// // Print("Fast Crossed Over Slow");
// // }
// //
// X121MCycleConditions conditions = xSampleEA121MC.GetMarketConditions(0);
// string mcSummary = conditions.GenerateSummary();
// Print(mcSummary);
// //
// Print(" ");
// }
void ApplyMCDrawStyles()
{
//
// Here we Apply All Draw Styles on Input of Market Cycle ...
xSampleEA121MCInputs.drawCBar = true;
xSampleEA121MCInputs.drawPBar = true;
xSampleEA121MCInputs.drawLabels = true;
xSampleEA121MCInputs.drawCBarMid = true;
xSampleEA121MCInputs.drawPBarMid = true;
//
// Current ...
//
// MidLine ...
xSampleEA121MCInputs.cBarMidDrawSpecs.width = 1;
xSampleEA121MCInputs.cBarMidDrawSpecs.style = STYLE_DOT;
xSampleEA121MCInputs.cBarMidDrawSpecs.clr = clrSpringGreen;
xSampleEA121MCInputs.cBarMidDrawSpecs.Label(
xSampleEA121MCInputs.cycle.prefix + "_C MID",
xSampleEA121MCInputs.cBarMidDrawSpecs.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
//
// XOHCL ...
xSampleEA121MCInputs.cBarDrawSpecs.Default();
xSampleEA121MCInputs.cBarDrawSpecs.Width(1);
xSampleEA121MCInputs.cBarDrawSpecs.Style(STYLE_DOT);
//
xSampleEA121MCInputs.cBarDrawSpecs.openStyle.Label(
xSampleEA121MCInputs.cycle.prefix + "_C O",
xSampleEA121MCInputs.cBarDrawSpecs.openStyle.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
xSampleEA121MCInputs.cBarDrawSpecs.highStyle.Label(
xSampleEA121MCInputs.cycle.prefix + "_C H",
xSampleEA121MCInputs.cBarDrawSpecs.highStyle.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
xSampleEA121MCInputs.cBarDrawSpecs.closeStyle.Label(
xSampleEA121MCInputs.cycle.prefix + "_C C",
xSampleEA121MCInputs.cBarDrawSpecs.closeStyle.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
xSampleEA121MCInputs.cBarDrawSpecs.lowStyle.Label(
xSampleEA121MCInputs.cycle.prefix + "_C L",
xSampleEA121MCInputs.cBarDrawSpecs.lowStyle.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
//
// Prev ...
//
// MidLine ...
xSampleEA121MCInputs.pBarMidDrawSpecs.width = 2;
xSampleEA121MCInputs.pBarMidDrawSpecs.style = STYLE_SOLID;
xSampleEA121MCInputs.pBarMidDrawSpecs.clr = clrLimeGreen;
xSampleEA121MCInputs.pBarMidDrawSpecs.Label(
xSampleEA121MCInputs.cycle.prefix + "_P MID",
xSampleEA121MCInputs.pBarMidDrawSpecs.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
//
// XOHCL ...
xSampleEA121MCInputs.pBarDrawSpecs.Default();
xSampleEA121MCInputs.pBarDrawSpecs.Width(2);
//
xSampleEA121MCInputs.pBarDrawSpecs.openStyle.Label(
xSampleEA121MCInputs.cycle.prefix + "_P O",
xSampleEA121MCInputs.pBarDrawSpecs.openStyle.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
xSampleEA121MCInputs.pBarDrawSpecs.highStyle.Label(
xSampleEA121MCInputs.cycle.prefix + "_P H",
xSampleEA121MCInputs.pBarDrawSpecs.highStyle.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
xSampleEA121MCInputs.pBarDrawSpecs.closeStyle.Label(
xSampleEA121MCInputs.cycle.prefix + "_P C",
xSampleEA121MCInputs.pBarDrawSpecs.closeStyle.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
xSampleEA121MCInputs.pBarDrawSpecs.lowStyle.Label(
xSampleEA121MCInputs.cycle.prefix + "_P L",
xSampleEA121MCInputs.pBarDrawSpecs.lowStyle.clr,
"Arial",
8,
ANCHOR_LEFT_UPPER,
90
//
);
}