Files
xMQL5/MQLTestWorkspace/XBEEA/14030224/Experts/x-rsima.ea.mq5
T
2024-05-13 18:21:02 +03:30

494 lines
8.6 KiB
Plaintext

///////////////////////////////////////////////////////
//
// 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;
}