bkp workspace ...
This commit is contained in:
@@ -0,0 +1,558 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center RSIMA MQL5 Expert Advisor
|
||||
// -------------------------------------------------
|
||||
// Name: XDONCHAINEA
|
||||
// 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 XDONCHAINEA"
|
||||
#property strict
|
||||
|
||||
//
|
||||
#include <Trade/Trade.mqh>
|
||||
|
||||
//
|
||||
#define ShortName "XDONCHAINEA"
|
||||
|
||||
//
|
||||
// Inputs ...
|
||||
long magicNumber = 78692110;
|
||||
double lotSize = 0.01;
|
||||
|
||||
//
|
||||
double stopLoss = 200;
|
||||
double takeProfit = 400;
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
//
|
||||
int xdonHandler;
|
||||
double xdonUpperBuffer[];
|
||||
double xdonLowerBuffer[];
|
||||
|
||||
//
|
||||
int ma20Handler;
|
||||
double ma20Buffer[];
|
||||
|
||||
//
|
||||
int ma50Handler;
|
||||
double ma50Buffer[];
|
||||
|
||||
//
|
||||
int ma100Handler;
|
||||
double ma100Buffer[];
|
||||
|
||||
//
|
||||
CTrade trade;
|
||||
|
||||
//
|
||||
ENUM_TIMEFRAMES scPeriod = NULL;
|
||||
ENUM_TIMEFRAMES mcPeriod = NULL;
|
||||
ENUM_TIMEFRAMES lcPeriod = NULL;
|
||||
ENUM_TIMEFRAMES hcPeriod = NULL;
|
||||
|
||||
//
|
||||
MqlTick currentTick;
|
||||
MqlRates rates[];
|
||||
MqlRates scRates[];
|
||||
MqlRates mcRates[];
|
||||
MqlRates lcRates[];
|
||||
MqlRates hcRates[];
|
||||
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
trade.SetExpertMagicNumber(magicNumber);
|
||||
|
||||
//
|
||||
xdonHandler = iCustom(
|
||||
//
|
||||
_Symbol,
|
||||
_Period,
|
||||
"x-saherelm.xdon",
|
||||
//
|
||||
// Inputs ...
|
||||
52,
|
||||
0
|
||||
//
|
||||
);
|
||||
|
||||
//
|
||||
ma20Handler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
20,
|
||||
0,
|
||||
MODE_SMA,
|
||||
PRICE_OPEN);
|
||||
|
||||
//
|
||||
ma50Handler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
50,
|
||||
0,
|
||||
MODE_SMA,
|
||||
PRICE_OPEN);
|
||||
|
||||
//
|
||||
ma100Handler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
100,
|
||||
0,
|
||||
MODE_SMA,
|
||||
PRICE_OPEN);
|
||||
|
||||
//
|
||||
if (xdonHandler == INVALID_HANDLE ||
|
||||
ma20Handler == INVALID_HANDLE ||
|
||||
ma50Handler == INVALID_HANDLE ||
|
||||
ma100Handler == INVALID_HANDLE)
|
||||
{
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
ArraySetAsSeries(rates, true);
|
||||
ArraySetAsSeries(ma20Buffer, true);
|
||||
ArraySetAsSeries(ma50Buffer, true);
|
||||
ArraySetAsSeries(ma100Buffer, true);
|
||||
ArraySetAsSeries(xdonUpperBuffer, true);
|
||||
ArraySetAsSeries(xdonLowerBuffer, true);
|
||||
|
||||
//
|
||||
// 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 XEA Providers ...
|
||||
IndicatorRelease(ma20Handler);
|
||||
IndicatorRelease(ma50Handler);
|
||||
IndicatorRelease(ma100Handler);
|
||||
IndicatorRelease(xdonHandler);
|
||||
}
|
||||
|
||||
//
|
||||
// On Tick Handler ...
|
||||
void OnTick()
|
||||
{
|
||||
//
|
||||
if (!IsNewBar())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Reading Tick ...
|
||||
if (!SymbolInfoTick(_Symbol, currentTick))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// if (scPeriod == NULL) {
|
||||
// scPeriod = GetNearest
|
||||
// }
|
||||
|
||||
//
|
||||
// Prepare Rates ...
|
||||
int ratesCopied = CopyRates(
|
||||
_Symbol,
|
||||
_Period,
|
||||
0,
|
||||
15,
|
||||
rates);
|
||||
|
||||
//
|
||||
int xdonUpperCopied = CopyBuffer(
|
||||
xdonHandler,
|
||||
0,
|
||||
0,
|
||||
15,
|
||||
xdonUpperBuffer);
|
||||
int xdonLowerCopied = CopyBuffer(
|
||||
xdonHandler,
|
||||
1,
|
||||
0,
|
||||
15,
|
||||
xdonLowerBuffer);
|
||||
if (xdonUpperCopied <= 0 || xdonLowerCopied <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
int ma20Copied = CopyBuffer(
|
||||
ma20Handler,
|
||||
0,
|
||||
0,
|
||||
15,
|
||||
ma20Buffer);
|
||||
int ma50Copied = CopyBuffer(
|
||||
ma50Handler,
|
||||
0,
|
||||
0,
|
||||
15,
|
||||
ma50Buffer);
|
||||
int ma100Copied = CopyBuffer(
|
||||
ma100Handler,
|
||||
0,
|
||||
0,
|
||||
15,
|
||||
ma100Buffer);
|
||||
if (ma20Copied <= 0 || ma50Copied <= 0 || ma100Copied <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// BUY ...
|
||||
bool buyConditions =
|
||||
//
|
||||
// First Condition ...
|
||||
// ma20Buffer[1] < MathMin(ma50Buffer[1], ma100Buffer[1])
|
||||
// //
|
||||
// &&
|
||||
//
|
||||
// Donchain Condition ...
|
||||
// Candle Must Shadow Hunt ...
|
||||
(rates[1].low < xdonLowerBuffer[1] &&
|
||||
MathMin(rates[1].open, rates[1].close) > xdonLowerBuffer[1])
|
||||
//
|
||||
||
|
||||
//
|
||||
(rates[1].low < xdonLowerBuffer[1] &&
|
||||
rates[2].low < xdonLowerBuffer[2])
|
||||
//
|
||||
;
|
||||
if (buyConditions)
|
||||
{
|
||||
//
|
||||
|
||||
//
|
||||
double sl =
|
||||
stopLoss <= 0
|
||||
? 0
|
||||
: currentTick.bid - (stopLoss * _Point);
|
||||
if (!NormalizePrice(sl))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
double tp =
|
||||
takeProfit <= 0
|
||||
? 0
|
||||
: currentTick.bid + (takeProfit * _Point);
|
||||
if (!NormalizePrice(tp))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
trade.PositionOpen(
|
||||
_Symbol,
|
||||
ORDER_TYPE_BUY,
|
||||
lotSize,
|
||||
currentTick.ask,
|
||||
sl,
|
||||
tp,
|
||||
ShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// SELL ...
|
||||
bool sellConditions =
|
||||
//
|
||||
// First Condition ...
|
||||
// ma20Buffer[1] > MathMax(ma50Buffer[1], ma100Buffer[1])
|
||||
// //
|
||||
// &&
|
||||
//
|
||||
// Donchain Condition ...
|
||||
// Candle Must Shadow Hunt ...
|
||||
(rates[1].high > xdonUpperBuffer[1] &&
|
||||
MathMax(rates[1].open, rates[1].close) < xdonUpperBuffer[1])
|
||||
//
|
||||
||
|
||||
//
|
||||
(rates[1].high > xdonUpperBuffer[1] &&
|
||||
rates[2].high > xdonUpperBuffer[2])
|
||||
//
|
||||
;
|
||||
if (sellConditions)
|
||||
{
|
||||
//
|
||||
double sl =
|
||||
stopLoss <= 0
|
||||
? 0
|
||||
: currentTick.ask - (stopLoss * _Point);
|
||||
if (!NormalizePrice(sl))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
double tp =
|
||||
takeProfit <= 0
|
||||
? 0
|
||||
: currentTick.ask + (takeProfit * _Point);
|
||||
if (!NormalizePrice(tp))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
trade.PositionOpen(
|
||||
_Symbol,
|
||||
ORDER_TYPE_SELL,
|
||||
lotSize,
|
||||
currentTick.bid,
|
||||
sl,
|
||||
tp,
|
||||
ShortName);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// TOOLS ...
|
||||
bool IsNewBar()
|
||||
{
|
||||
//
|
||||
static datetime prevTime = 0;
|
||||
datetime currentTime = iTime(_Symbol, _Period, 0);
|
||||
|
||||
//
|
||||
if (prevTime != currentTime)
|
||||
{
|
||||
//
|
||||
prevTime = currentTime;
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
bool NormalizePrice(
|
||||
double &price // What to Normal
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
double tickSize = 0;
|
||||
result = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE, tickSize);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
price = NormalizeDouble(MathRound(price / tickSize) * tickSize, _Digits);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
bool CountOpenPositions(
|
||||
int &buyCount, // Buys
|
||||
int &sellCount // Sells
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
buyCount = 0;
|
||||
sellCount = 0;
|
||||
|
||||
//
|
||||
int total = PositionsTotal();
|
||||
if (total <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = total - 1; i >= 0; i--)
|
||||
{
|
||||
//
|
||||
ulong ticket = PositionGetTicket(i);
|
||||
if (ticket <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
bool isSelected = PositionSelectByTicket(ticket);
|
||||
if (!isSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
long magic;
|
||||
bool isMagicSelected = PositionGetInteger(POSITION_MAGIC, magic);
|
||||
if (!isMagicSelected || (isMagicSelected && magic != magicNumber))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
long typeInt;
|
||||
ENUM_POSITION_TYPE type;
|
||||
bool isTypeSelected = PositionGetInteger(POSITION_TYPE, typeInt);
|
||||
if (!isTypeSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
type = (ENUM_POSITION_TYPE)typeInt;
|
||||
|
||||
//
|
||||
if (type == POSITION_TYPE_BUY)
|
||||
{
|
||||
buyCount++;
|
||||
}
|
||||
else if (type == POSITION_TYPE_SELL)
|
||||
{
|
||||
sellCount++;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
bool ClosePositions(ENUM_POSITION_TYPE type)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
int total = PositionsTotal();
|
||||
if (total <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = total - 1; i >= 0; i--)
|
||||
{
|
||||
//
|
||||
ulong ticket = PositionGetTicket(i);
|
||||
if (ticket <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
bool isSelected = PositionSelectByTicket(ticket);
|
||||
if (!isSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
long magic;
|
||||
bool isMagicSelected = PositionGetInteger(POSITION_MAGIC, magic);
|
||||
if (!isMagicSelected || (isMagicSelected && magic != magicNumber))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
long typeInt;
|
||||
ENUM_POSITION_TYPE pType;
|
||||
bool isTypeSelected = PositionGetInteger(POSITION_TYPE, typeInt);
|
||||
if (!isTypeSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
pType = (ENUM_POSITION_TYPE)typeInt;
|
||||
|
||||
//
|
||||
if (type != pType)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
trade.PositionClose(ticket);
|
||||
}
|
||||
|
||||
//
|
||||
result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
template <typename T>
|
||||
string ToString(const T value)
|
||||
{
|
||||
//
|
||||
return (string)value;
|
||||
}
|
||||
@@ -0,0 +1,494 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center RSIMA MQL5 Expert Advisor
|
||||
// -------------------------------------------------
|
||||
// Name: XRSIMAEA
|
||||
// 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 XRSIMAEA"
|
||||
#property strict
|
||||
|
||||
//
|
||||
#include <Trade/Trade.mqh>
|
||||
|
||||
//
|
||||
#define ShortName "XRSIMAEA"
|
||||
|
||||
//
|
||||
// Inputs ...
|
||||
long magicNumber = 78692110;
|
||||
double lotSize = 0.01;
|
||||
|
||||
//
|
||||
int rsiLength = 25;
|
||||
int rsiLevel = 70;
|
||||
|
||||
//
|
||||
int maLength = 25;
|
||||
|
||||
//
|
||||
double stopLoss = 200;
|
||||
double takeProfit = 100;
|
||||
|
||||
//
|
||||
bool closeSignal = false;
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
//
|
||||
int rsiHandler;
|
||||
double rsiBuffer[];
|
||||
|
||||
//
|
||||
int maHandler;
|
||||
double maBuffer[];
|
||||
|
||||
//
|
||||
CTrade trade;
|
||||
|
||||
//
|
||||
MqlTick currentTick;
|
||||
|
||||
//
|
||||
datetime openTimeBuy = 0;
|
||||
datetime openTimeSell = 0;
|
||||
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
trade.SetExpertMagicNumber(magicNumber);
|
||||
|
||||
//
|
||||
rsiHandler = iRSI(
|
||||
_Symbol,
|
||||
_Period,
|
||||
rsiLength,
|
||||
PRICE_OPEN);
|
||||
|
||||
//
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
PERIOD_H1,
|
||||
maLength,
|
||||
0,
|
||||
MODE_SMA,
|
||||
PRICE_OPEN);
|
||||
|
||||
//
|
||||
if (rsiHandler == INVALID_HANDLE || maHandler == INVALID_HANDLE)
|
||||
{
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
ArraySetAsSeries(rsiBuffer, true);
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
|
||||
//
|
||||
// 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 XEA Providers ...
|
||||
IndicatorRelease(rsiHandler);
|
||||
IndicatorRelease(maHandler);
|
||||
}
|
||||
|
||||
//
|
||||
// On Tick Handler ...
|
||||
void OnTick()
|
||||
{
|
||||
//
|
||||
if (!IsNewBar())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Reading Tick ...
|
||||
if (!SymbolInfoTick(_Symbol, currentTick))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
int rsiCopied = CopyBuffer(
|
||||
rsiHandler,
|
||||
0,
|
||||
0,
|
||||
2,
|
||||
rsiBuffer);
|
||||
if (rsiCopied < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
int maCopied = CopyBuffer(
|
||||
maHandler,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
maBuffer);
|
||||
if (maCopied < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
Comment(
|
||||
"rsi[0]: " + ToString(rsiBuffer[0]) + "\n" +
|
||||
"rsi[1]: " + ToString(rsiBuffer[1]) + "\n" +
|
||||
"ma[0]: " + ToString(maBuffer[0]));
|
||||
|
||||
//
|
||||
int buyCount;
|
||||
int sellCount;
|
||||
//
|
||||
if (!CountOpenPositions(buyCount, sellCount))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// BUY ...
|
||||
if (
|
||||
buyCount == 0 &&
|
||||
rsiBuffer[1] >= (100 - rsiLevel) &&
|
||||
rsiBuffer[0] < (100 - rsiLevel) &&
|
||||
currentTick.ask > maBuffer[0])
|
||||
{
|
||||
//
|
||||
if (closeSignal)
|
||||
{
|
||||
//
|
||||
bool isClose = ClosePositions(POSITION_TYPE_SELL);
|
||||
if (!isClose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
double sl =
|
||||
stopLoss <= 0
|
||||
? 0
|
||||
: currentTick.bid - (stopLoss * _Point);
|
||||
if (!NormalizePrice(sl))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
double tp =
|
||||
takeProfit <= 0
|
||||
? 0
|
||||
: currentTick.bid + (takeProfit * _Point);
|
||||
if (!NormalizePrice(tp))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
trade.PositionOpen(
|
||||
_Symbol,
|
||||
ORDER_TYPE_BUY,
|
||||
lotSize,
|
||||
currentTick.ask,
|
||||
sl,
|
||||
tp,
|
||||
ShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// SELL ...
|
||||
if (
|
||||
sellCount == 0 &&
|
||||
rsiBuffer[1] <= rsiLevel &&
|
||||
rsiBuffer[0] > rsiLevel &&
|
||||
currentTick.bid < maBuffer[0])
|
||||
{
|
||||
//
|
||||
if (closeSignal)
|
||||
{
|
||||
//
|
||||
bool isClose = ClosePositions(POSITION_TYPE_BUY);
|
||||
if (!isClose)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
double sl =
|
||||
stopLoss <= 0
|
||||
? 0
|
||||
: currentTick.ask - (stopLoss * _Point);
|
||||
if (!NormalizePrice(sl))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
double tp =
|
||||
takeProfit <= 0
|
||||
? 0
|
||||
: currentTick.ask + (takeProfit * _Point);
|
||||
if (!NormalizePrice(tp))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
trade.PositionOpen(
|
||||
_Symbol,
|
||||
ORDER_TYPE_SELL,
|
||||
lotSize,
|
||||
currentTick.bid,
|
||||
sl,
|
||||
tp,
|
||||
ShortName);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// TOOLS ...
|
||||
bool IsNewBar()
|
||||
{
|
||||
//
|
||||
static datetime prevTime = 0;
|
||||
datetime currentTime = iTime(_Symbol, _Period, 0);
|
||||
|
||||
//
|
||||
if (prevTime != currentTime)
|
||||
{
|
||||
//
|
||||
prevTime = currentTime;
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
bool NormalizePrice(
|
||||
double &price // What to Normal
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
double tickSize = 0;
|
||||
result = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE, tickSize);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
price = NormalizeDouble(MathRound(price / tickSize) * tickSize, _Digits);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
bool CountOpenPositions(
|
||||
int &buyCount, // Buys
|
||||
int &sellCount // Sells
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
buyCount = 0;
|
||||
sellCount = 0;
|
||||
|
||||
//
|
||||
int total = PositionsTotal();
|
||||
if (total <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = total - 1; i >= 0; i--)
|
||||
{
|
||||
//
|
||||
ulong ticket = PositionGetTicket(i);
|
||||
if (ticket <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
bool isSelected = PositionSelectByTicket(ticket);
|
||||
if (!isSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
long magic;
|
||||
bool isMagicSelected = PositionGetInteger(POSITION_MAGIC, magic);
|
||||
if (!isMagicSelected || (isMagicSelected && magic != magicNumber))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
long typeInt;
|
||||
ENUM_POSITION_TYPE type;
|
||||
bool isTypeSelected = PositionGetInteger(POSITION_TYPE, typeInt);
|
||||
if (!isTypeSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
type = (ENUM_POSITION_TYPE)typeInt;
|
||||
|
||||
//
|
||||
if (type == POSITION_TYPE_BUY)
|
||||
{
|
||||
buyCount++;
|
||||
}
|
||||
else if (type == POSITION_TYPE_SELL)
|
||||
{
|
||||
sellCount++;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
bool ClosePositions(ENUM_POSITION_TYPE type)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
int total = PositionsTotal();
|
||||
if (total <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = total - 1; i >= 0; i--)
|
||||
{
|
||||
//
|
||||
ulong ticket = PositionGetTicket(i);
|
||||
if (ticket <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
bool isSelected = PositionSelectByTicket(ticket);
|
||||
if (!isSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
long magic;
|
||||
bool isMagicSelected = PositionGetInteger(POSITION_MAGIC, magic);
|
||||
if (!isMagicSelected || (isMagicSelected && magic != magicNumber))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
long typeInt;
|
||||
ENUM_POSITION_TYPE pType;
|
||||
bool isTypeSelected = PositionGetInteger(POSITION_TYPE, typeInt);
|
||||
if (!isTypeSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
pType = (ENUM_POSITION_TYPE)typeInt;
|
||||
|
||||
//
|
||||
if (type != pType)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
trade.PositionClose(ticket);
|
||||
}
|
||||
|
||||
//
|
||||
result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
template <typename T>
|
||||
string ToString(const T value)
|
||||
{
|
||||
//
|
||||
return (string)value;
|
||||
}
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -0,0 +1,377 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Expert Advisor
|
||||
// ----------------------------------------
|
||||
// Name: X121EA
|
||||
// Description: this is an expert controller
|
||||
// advisors which provides a mechanism for use them ...
|
||||
//
|
||||
// NOTE: about provider instancing in multi asset state:
|
||||
// you can provide configurations using this:
|
||||
//
|
||||
// EURUSDb,USDCHFb|M5|0.01|X786,X92|true
|
||||
// EURUSDb,GBPUSDb,USDJPYb,USDCHFb,XAUUSDb|M5,M15|0.01|X786|true
|
||||
// EURUSDb,GBPUSDb,USDJPYb,USDCHFb,XAUUSDb|M5,M15|0.01|X786|true
|
||||
//
|
||||
// or make the instance use this:
|
||||
//
|
||||
// XPV(EURUSDb|M15|0.01|X92|true)XPV(GBPUSDb|M5|0.02|X786|false)
|
||||
//
|
||||
// if leave Configuration string blank this will used default config
|
||||
// and One asset mode enabled ...
|
||||
//
|
||||
// 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 X121EA"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Definitions ...
|
||||
//
|
||||
|
||||
//
|
||||
#define ShortName "X121EA"
|
||||
|
||||
//
|
||||
// END Definitions ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// EA ...
|
||||
input group "Common";
|
||||
input bool xEAEnable = true; // Enable EA
|
||||
input int xEASlippage = 10; // Slippgae
|
||||
input ulong xEAMagicNumber = 1604056; // Magic Number
|
||||
|
||||
//
|
||||
// Providers ...
|
||||
input group "Providers";
|
||||
input string xEAProvidersConfiguration = "EURUSDb,GBPUSDb,USDJPYb,USDCHFb,XAUUSDb|M5,M15|0.01|X786|true"; // Providers Configuration
|
||||
input string xEADefaultSignalProviders = "XTEST"; // Default Allowed Signal Providers
|
||||
|
||||
//
|
||||
// Signals ...
|
||||
input group "Signals";
|
||||
input bool xEAEnableLongs = true; // Enable Long Trades
|
||||
input bool xEAEnableShorts = true; // Enable Short Trades
|
||||
|
||||
//
|
||||
// Risk Management ...
|
||||
input group "Risk Management";
|
||||
input double xEAMaxAllowedSpread = 10; // Max Allowed Spread to Execute Signal
|
||||
input int xEAMaxOpenPositions = 15; // Max Allowed Open Trades
|
||||
input double xEAMinRiskAmountPerTrade = 0.001; // Min Risk Amount Per Trade
|
||||
input double xEAMaxAllowedDrawDownFactor = 0.01; // Max Allowed Drawdown Factor
|
||||
input double xEAMinProfitPerTrade = 0.5; // Min Acceptable Profit Per Trade
|
||||
input double xEAMinProfitPerVolumeFactor = 0.01; // Min Acceptable Profit Volume Factor Per Trade
|
||||
input bool xEAUseRiskAmountAsStaticVolume = true; // Use Provided Risk Amount as Static Volume
|
||||
|
||||
//
|
||||
input group "EQM";
|
||||
input int xEAMinAgeForDoSupport = 35; // Minimum Age For Supporting In Drawdown Positions when there isn't Equity
|
||||
input int xEAMinAgeForUpdateSupports = 35; // Minimum Age For Update Untriggered Support Orders
|
||||
|
||||
//
|
||||
// Alerts ...
|
||||
input group "Alerts";
|
||||
input bool xEAEnableAlerts = true; // Enable Events Alert
|
||||
input bool xEAEnableHourlyAlerts = false; // Send Alert on each new Hour
|
||||
input bool xEAEnableDailyAlerts = false; // Send Alert on each new Day
|
||||
input bool xEAEnableWeeklyAlerts = false; // Send Alert on each new Week
|
||||
input bool xEAEnableMonthlyAlerts = false; // Send Alert on each new Month
|
||||
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Including Providers ...
|
||||
//
|
||||
|
||||
//
|
||||
// Logger Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5"
|
||||
#include "../Classes/x-saherelm.x121.xea.class.mq5"
|
||||
#include "../Libraries/x-saherelm.seriallizer.lib.mq5"
|
||||
|
||||
//
|
||||
// END Including Providers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
XSCX121EA *mX121EA;
|
||||
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
logTag = ShortName;
|
||||
drawPrefix = logTag;
|
||||
alertPrefix = logTag;
|
||||
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
bool isInit = InitEAInstance();
|
||||
if (!isInit)
|
||||
{
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// 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 XEA Providers ...
|
||||
delete mX121EA;
|
||||
}
|
||||
|
||||
//
|
||||
// On Tick Handler ...
|
||||
void OnTick()
|
||||
{
|
||||
//
|
||||
// Prevent Going Forward if user Disable it ...
|
||||
if (xEAEnable)
|
||||
{
|
||||
mX121EA.OnTick();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Trade Events ...
|
||||
void OnTrade()
|
||||
{
|
||||
mX121EA.OnTrade();
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Signal Event ...
|
||||
void OnSignalConditionsRecieved(X121MarketConditions &info)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Handle StopLoss Event ...
|
||||
void OnStopLossTriggered(const XDeal &deal)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Handle TakeProfit Event ...
|
||||
void OnTakeProfitTriggered(const XDeal &deal)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Deals Changed Event ...
|
||||
void OnDealsChangedHandler(int count)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Order Changed Event ...
|
||||
void OnOrdersChangedHandler(int count)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Position Changed Event ...
|
||||
void OnPositionsChangedHandler(int count)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Trade State Changed ...
|
||||
void OnTradeStateChangedHandler(const XOnTradeHandlerState &state)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
result =
|
||||
//
|
||||
xEASlippage > 0 &&
|
||||
xEAMagicNumber > 0 &&
|
||||
xEAMinRiskAmountPerTrade > 0 &&
|
||||
StringLen(xEADefaultSignalProviders) > 0
|
||||
//
|
||||
// TODO: add new validations here ...
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
// Validate Providers ...
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
string providers[];
|
||||
SplitContent(
|
||||
providers,
|
||||
xEADefaultSignalProviders);
|
||||
|
||||
//
|
||||
int providersCount = ArraySize(providers);
|
||||
if (providersCount <= 0)
|
||||
{
|
||||
//
|
||||
result = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
string allProviders[];
|
||||
GetAllProviders(allProviders);
|
||||
|
||||
//
|
||||
bool hasError = false;
|
||||
for (int i = 0; i < providersCount; i++)
|
||||
{
|
||||
//
|
||||
string iProvider = providers[i];
|
||||
if (!Contains(iProvider, allProviders))
|
||||
{
|
||||
//
|
||||
hasError = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
if (hasError)
|
||||
{
|
||||
//
|
||||
result = false;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Logging State ...
|
||||
string message = "Validation of (" + logTag + ") Inputs " + (result ? "Succeeded" : "Failed") + " ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize X121EA Class Instances ...
|
||||
bool InitEAInstance()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
X121SignalProvider providers[];
|
||||
|
||||
//
|
||||
// Try to Extract Configs ...
|
||||
int configsCount = ExtractProviderConfigurations(
|
||||
providers,
|
||||
xEAProvidersConfiguration,
|
||||
xEADefaultSignalProviders,
|
||||
xEAMinRiskAmountPerTrade,
|
||||
xEAUseRiskAmountAsStaticVolume);
|
||||
|
||||
//
|
||||
mX121EA = new XSCX121EA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
xEAMagicNumber,
|
||||
xEASlippage,
|
||||
xEAMaxAllowedSpread,
|
||||
xEAMaxOpenPositions,
|
||||
xEAMinProfitPerTrade,
|
||||
xEAMinProfitPerVolumeFactor,
|
||||
xEAMaxAllowedDrawDownFactor,
|
||||
OnStopLossTriggered,
|
||||
OnTakeProfitTriggered,
|
||||
OnDealsChangedHandler,
|
||||
OnOrdersChangedHandler,
|
||||
OnPositionsChangedHandler,
|
||||
OnTradeStateChangedHandler,
|
||||
//
|
||||
xEAMinAgeForDoSupport,
|
||||
xEAMinAgeForUpdateSupports,
|
||||
OnSignalConditionsRecieved);
|
||||
|
||||
//
|
||||
mX121EA.EnableLongs(xEAEnableLongs);
|
||||
mX121EA.EnableShorts(xEAEnableShorts);
|
||||
|
||||
//
|
||||
// Attach Providers to EA Instance ...
|
||||
int providersCount = ArraySize(providers);
|
||||
int addedProviders = mX121EA.AddProviders(providers);
|
||||
result = addedProviders == providersCount;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
Reference in New Issue
Block a user