apply xTest Project Content from Workspace and Prepare IT ...
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// SaherElm IT Center MQL5 Expert Advisor
|
||||
// ----------------------------------------
|
||||
// Name: XAUUSD
|
||||
// Name: XTEST
|
||||
// Description: this is an expert controller
|
||||
// advisors which provides a mechanism for use them ...
|
||||
//
|
||||
@@ -20,29 +20,18 @@
|
||||
#property description "SaherElm XTEST EA"
|
||||
#property strict
|
||||
|
||||
//
|
||||
#include <Trade/PositionInfo.mqh>
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
|
||||
input group "Commons";
|
||||
|
||||
//
|
||||
input double staticVolume = 0.01; // Static Volume
|
||||
// XCTIMER inputs ...
|
||||
input bool xTESTShowCandleTimer = true; // Show Condle Remaining Time
|
||||
input color xTESTCandleTimerColot = clrYellow; // Candle Timer Color
|
||||
input ENUM_BASE_CORNER xTESTCandleTimerCorner = CORNER_RIGHT_LOWER; // Where to Show Candle Timer
|
||||
|
||||
//
|
||||
// FAST ...
|
||||
input int fastLength = 9; // Fast Ma Length
|
||||
input ENUM_MA_METHOD fastMethod = MODE_SMA; // fast Ma Method
|
||||
input ENUM_APPLIED_PRICE fastAppliedTo = PRICE_CLOSE; // fast Ma Applied To
|
||||
|
||||
//
|
||||
// SLOW ...
|
||||
input int slowLength = 18; // Slow Ma Length
|
||||
input ENUM_MA_METHOD slowMethod = MODE_SMA; // slow Ma Method
|
||||
input ENUM_APPLIED_PRICE slowAppliedTo = PRICE_CLOSE; // slow Ma Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
@@ -67,6 +56,10 @@ input ENUM_APPLIED_PRICE slowAppliedTo = PRICE_CLOSE; // slow Ma Applied To
|
||||
// Draw Library ...
|
||||
#include "../Libraries/x-saherelm.draw.lib.mq5"
|
||||
|
||||
//
|
||||
// XTEST Signal Provider ...
|
||||
#include "../Providers/x-saherelm.xtest.provider.lib.mq5"
|
||||
|
||||
//
|
||||
// END Including Providers ...
|
||||
//
|
||||
@@ -74,23 +67,13 @@ input ENUM_APPLIED_PRICE slowAppliedTo = PRICE_CLOSE; // slow Ma Applied To
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
ulong magicNumber = 169405612;
|
||||
bool isXTESTNewHour;
|
||||
bool isXTESTNewDay;
|
||||
bool isXTESTNewWeek;
|
||||
bool isXTESTNewMonth;
|
||||
|
||||
//
|
||||
int xMAFastHandler = INVALID_HANDLE;
|
||||
double xMAFastBuffer[];
|
||||
|
||||
//
|
||||
int xMASlowHandler = INVALID_HANDLE;
|
||||
double xMASlowBuffer[];
|
||||
|
||||
//
|
||||
int ticksCount;
|
||||
int signalTicksCount;
|
||||
|
||||
//
|
||||
CPositionInfo mPositionInfo;
|
||||
int xTESTCTimerHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
@@ -124,10 +107,20 @@ int OnInit()
|
||||
}
|
||||
|
||||
//
|
||||
// Define Handlers ...
|
||||
if (!DefineHandlers())
|
||||
// Initialize XTEST Provider ...
|
||||
if (!XTESTInitSignalProviderLibrary())
|
||||
{
|
||||
return INIT_FAILED;
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
// Init XCTimer ...
|
||||
if (xTESTShowCandleTimer)
|
||||
{
|
||||
if (!XTESTCTimerInit())
|
||||
{
|
||||
return INIT_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
@@ -153,7 +146,17 @@ void OnDeinit(const int reason)
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
ReleaseHandlers();
|
||||
|
||||
//
|
||||
// De Initialize XTEST Provider ...
|
||||
XTESTDeinitSignalProviderLibrary(reason);
|
||||
|
||||
//
|
||||
// De Init XDraw Library ...
|
||||
// OnDeinitDrawLibrary();
|
||||
|
||||
//
|
||||
IndicatorRelease(xTESTCTimerHandler);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -161,19 +164,175 @@ void OnDeinit(const int reason)
|
||||
void OnTick()
|
||||
{
|
||||
//
|
||||
if (!IsNewCandle())
|
||||
// Handle Monthly Alert ...
|
||||
bool isFirstMonth = xMonth == -1;
|
||||
isXTESTNewMonth = IsNewMonth();
|
||||
bool canMonthlyAlert =
|
||||
isXTESTNewMonth &&
|
||||
xTESTEnableAlerts &&
|
||||
xTESTEnableMonthlyAlerts;
|
||||
bool canMonthlyReport =
|
||||
!isFirstMonth &&
|
||||
canMonthlyAlert &&
|
||||
xTESTEnableMonthlyReports;
|
||||
if (canMonthlyAlert)
|
||||
{
|
||||
ticksCount++;
|
||||
//
|
||||
string message = "New Month ...";
|
||||
|
||||
//
|
||||
// Prepare Monthly Report ...
|
||||
if (canMonthlyReport)
|
||||
{
|
||||
//
|
||||
string report = xTESTReporter.GenerateReportString(XREPORT_MONTHLY);
|
||||
|
||||
//
|
||||
message = "Last Month Report:\n" + report;
|
||||
}
|
||||
|
||||
//
|
||||
// Reset Report ...
|
||||
xTESTReporter.Reset(XREPORT_MONTHLY);
|
||||
|
||||
//
|
||||
SendAlert(message);
|
||||
}
|
||||
|
||||
//
|
||||
HandleTick();
|
||||
// Handle Weekly Alert ...
|
||||
bool isFirstWeek = xDayOfWeek == -1;
|
||||
isXTESTNewWeek = IsNewWeek();
|
||||
bool canWeeklyAlert =
|
||||
isXTESTNewWeek &&
|
||||
xTESTEnableAlerts &&
|
||||
xTESTEnableWeeklyAlerts;
|
||||
bool canWeeklyReport =
|
||||
!isFirstWeek &&
|
||||
canWeeklyAlert &&
|
||||
xTESTEnableWeeklyReports;
|
||||
if (canWeeklyAlert)
|
||||
{
|
||||
//
|
||||
string message = "New Week ...";
|
||||
|
||||
//
|
||||
// Prepare Weekly Report ...
|
||||
if (canWeeklyReport)
|
||||
{
|
||||
//
|
||||
string report = xTESTReporter.GenerateReportString(XREPORT_WEEKLY);
|
||||
|
||||
//
|
||||
message = "Last Week Report:\n" + report;
|
||||
}
|
||||
|
||||
//
|
||||
// Reset Report ...
|
||||
xTESTReporter.Reset(XREPORT_WEEKLY);
|
||||
|
||||
//
|
||||
SendAlert(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Daily Alert ...
|
||||
bool isFirstDay = xDay == -1;
|
||||
isXTESTNewDay = IsNewDay();
|
||||
bool canDailyAlert =
|
||||
isXTESTNewDay &&
|
||||
xTESTEnableAlerts &&
|
||||
xTESTEnableDailyAlerts;
|
||||
bool canDailyReport =
|
||||
!isFirstDay &&
|
||||
canDailyAlert &&
|
||||
xTESTEnableDailyReports;
|
||||
if (canDailyAlert)
|
||||
{
|
||||
//
|
||||
string message = "New Day ...";
|
||||
|
||||
//
|
||||
// Prepare Daily Report ...
|
||||
if (canDailyReport)
|
||||
{
|
||||
//
|
||||
string report = xTESTReporter.GenerateReportString(XREPORT_DAILY);
|
||||
|
||||
//
|
||||
message = "Last Week Report:\n" + report;
|
||||
}
|
||||
|
||||
//
|
||||
// Reset Report ...
|
||||
xTESTReporter.Reset(XREPORT_DAILY);
|
||||
|
||||
//
|
||||
SendAlert(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Hourly Alert ...
|
||||
bool isFirstHour = xHour == -1;
|
||||
isXTESTNewHour = IsNewHour();
|
||||
bool canHourlyAlert =
|
||||
isXTESTNewHour &&
|
||||
xTESTEnableAlerts &&
|
||||
xTESTEnableHourlyAlerts;
|
||||
if (canHourlyAlert)
|
||||
{
|
||||
//
|
||||
string message = "New Hour ...";
|
||||
|
||||
//
|
||||
SendAlert(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Prevent Going Forward if user Disable it ...
|
||||
if (xTESTEnableProvider)
|
||||
{
|
||||
//
|
||||
// Handle Processing on XTEST Signal Provider ...
|
||||
XTESTSignalProviderDoControlState();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Can Ignore Tick or not ..
|
||||
bool canIgnoreTick = xTESTIsTestMode
|
||||
? !IsNewCandle()
|
||||
: !IsNewCandle() && xTESTWaitUntilNextCandle;
|
||||
if (canIgnoreTick)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
xTESTWaitUntilNextCandle = false;
|
||||
|
||||
//
|
||||
// Prevent Going Forward if user Disable it ...
|
||||
if (xTESTEnableProvider)
|
||||
{
|
||||
//
|
||||
// Handle Processing on XTEST Signal Provider ...
|
||||
XTESTSignalProviderHandleTick();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Trade Events ...
|
||||
void OnTrade()
|
||||
{
|
||||
|
||||
//
|
||||
// Prevent Going Forward if user Disable it ...
|
||||
if (xTESTEnableProvider)
|
||||
{
|
||||
//
|
||||
// Handle Processing Trades on XTEST Signal Provider ...
|
||||
XTESTSignalProviderHandleTrade();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
@@ -196,7 +355,7 @@ bool ValidateInputs()
|
||||
|
||||
//
|
||||
// Validate XTEST Provider Inputs ...
|
||||
result = true;
|
||||
result = XTESTValidateInputs();
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
@@ -211,362 +370,30 @@ bool ValidateInputs()
|
||||
return result;
|
||||
}
|
||||
|
||||
bool DefineHandlers()
|
||||
bool XTESTCTimerInit()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
// FAST ...
|
||||
xMAFastHandler = iMA(
|
||||
xTESTCTimerHandler = iCustom(
|
||||
_Symbol,
|
||||
_Period,
|
||||
fastLength,
|
||||
0,
|
||||
fastMethod,
|
||||
fastAppliedTo);
|
||||
|
||||
//
|
||||
if (xMAFastHandler == INVALID_HANDLE)
|
||||
{
|
||||
"x-saherelm.xctimer.indicator",
|
||||
//
|
||||
result = false;
|
||||
return result;
|
||||
}
|
||||
// Inputs ...
|
||||
xTESTCandleTimerColot,
|
||||
xTESTCandleTimerCorner);
|
||||
|
||||
//
|
||||
// SLOW ...
|
||||
xMASlowHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
slowLength,
|
||||
0,
|
||||
fastMethod,
|
||||
fastAppliedTo);
|
||||
|
||||
//
|
||||
if (xMASlowHandler == INVALID_HANDLE)
|
||||
if (xTESTCTimerHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
result = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
void ReadBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// Reading Fast Buffer ...
|
||||
int readedFastItems = CopyBuffer(
|
||||
xMAFastHandler,
|
||||
0,
|
||||
bar_index,
|
||||
5,
|
||||
xMAFastBuffer);
|
||||
|
||||
//
|
||||
// Reading Slow Buffer ...
|
||||
int readedSlowItems = CopyBuffer(
|
||||
xMASlowHandler,
|
||||
0,
|
||||
bar_index,
|
||||
5,
|
||||
xMASlowBuffer);
|
||||
}
|
||||
|
||||
void ReleaseHandlers()
|
||||
{
|
||||
//
|
||||
IndicatorRelease(xMAFastHandler);
|
||||
IndicatorRelease(xMASlowHandler);
|
||||
}
|
||||
|
||||
void HandleTick()
|
||||
{
|
||||
//
|
||||
HandleControlState();
|
||||
|
||||
//
|
||||
ReadBuffers(1);
|
||||
|
||||
//
|
||||
XOHCL cl0 = GetCandle(0);
|
||||
XOHCL cl1 = GetCandle(1);
|
||||
XOHCL cl2 = GetCandle(2);
|
||||
|
||||
//
|
||||
bool hasLong =
|
||||
//
|
||||
// Base Condition ...
|
||||
(
|
||||
//
|
||||
xMAFastBuffer[0] > xMASlowBuffer[0]
|
||||
//
|
||||
&&
|
||||
//
|
||||
xMAFastBuffer[1] > xMASlowBuffer[1]
|
||||
//
|
||||
&&
|
||||
//
|
||||
xMAFastBuffer[2] > xMASlowBuffer[2]
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
cl1.close > cl2.close
|
||||
//
|
||||
&&
|
||||
//
|
||||
cl0.close > cl1.close
|
||||
//
|
||||
;
|
||||
if (hasLong && signalTicksCount < 3)
|
||||
{
|
||||
//
|
||||
signalTicksCount++;
|
||||
|
||||
//
|
||||
if (signalTicksCount == 3)
|
||||
{
|
||||
//
|
||||
signalTicksCount = 0;
|
||||
|
||||
//
|
||||
double entry = GetEntry(X_SIGNAL_LONG);
|
||||
double sl = GetLowestLow(3, 0);
|
||||
double tp = entry + (150 * _Point);
|
||||
double volume = staticVolume;
|
||||
|
||||
//
|
||||
// declare and initialize the trade request and result of trade request
|
||||
MqlTradeRequest request = {};
|
||||
MqlTradeResult result = {};
|
||||
|
||||
//
|
||||
// parameters of request
|
||||
request.tp = tp;
|
||||
request.sl = sl;
|
||||
request.deviation = 5;
|
||||
request.price = entry;
|
||||
request.volume = volume;
|
||||
request.symbol = _Symbol;
|
||||
request.magic = magicNumber;
|
||||
request.type = ORDER_TYPE_BUY;
|
||||
request.action = TRADE_ACTION_DEAL;
|
||||
|
||||
//
|
||||
ResetLastError();
|
||||
string signalType = EnumToString(request.type);
|
||||
|
||||
//
|
||||
bool isExecuted = OrderSend(
|
||||
request,
|
||||
result);
|
||||
if (!isExecuted)
|
||||
{
|
||||
//
|
||||
string errMsg = GetErrorDescription(GetLastError());
|
||||
LogMessage("Signal " + signalType + " Execution Failed: " + errMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogMessage("Signal " + signalType + " Executed Successfully ...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
bool hasShort =
|
||||
//
|
||||
// Base Condition ...
|
||||
(
|
||||
//
|
||||
xMAFastBuffer[0] < xMASlowBuffer[0]
|
||||
//
|
||||
&&
|
||||
//
|
||||
xMAFastBuffer[1] < xMASlowBuffer[1]
|
||||
//
|
||||
&&
|
||||
//
|
||||
xMAFastBuffer[2] < xMASlowBuffer[2]
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
cl1.close < cl2.close
|
||||
//
|
||||
&&
|
||||
//
|
||||
cl0.close < cl1.close
|
||||
//
|
||||
;
|
||||
if (hasShort && signalTicksCount < 3)
|
||||
{
|
||||
//
|
||||
signalTicksCount++;
|
||||
|
||||
//
|
||||
if (signalTicksCount == 3)
|
||||
{
|
||||
//
|
||||
signalTicksCount = 0;
|
||||
|
||||
//
|
||||
double entry = GetEntry(X_SIGNAL_SHORT);
|
||||
double sl = GetHighestHigh(3, 0);
|
||||
double tp = entry - (150 * _Point);
|
||||
double volume = staticVolume;
|
||||
|
||||
//
|
||||
// declare and initialize the trade request and result of trade request
|
||||
MqlTradeRequest request = {};
|
||||
MqlTradeResult result = {};
|
||||
|
||||
//
|
||||
// parameters of request
|
||||
request.tp = tp;
|
||||
request.sl = sl;
|
||||
request.deviation = 5;
|
||||
request.price = entry;
|
||||
request.volume = volume;
|
||||
request.symbol = _Symbol;
|
||||
request.magic = magicNumber;
|
||||
request.type = ORDER_TYPE_SELL;
|
||||
request.action = TRADE_ACTION_DEAL;
|
||||
|
||||
//
|
||||
ResetLastError();
|
||||
string signalType = EnumToString(request.type);
|
||||
|
||||
//
|
||||
bool isExecuted = OrderSend(
|
||||
request,
|
||||
result);
|
||||
if (!isExecuted)
|
||||
{
|
||||
//
|
||||
string errMsg = GetErrorDescription(GetLastError());
|
||||
LogMessage("Signal " + signalType + " Execution Failed: " + errMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogMessage("Signal " + signalType + " Executed Successfully ...");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleControlState()
|
||||
{
|
||||
//
|
||||
int count = PositionsTotal();
|
||||
if (count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
double totalProfit = 0;
|
||||
|
||||
//
|
||||
// Loop through Positions ...
|
||||
for (int index = count - 1; index >= 0; index--)
|
||||
{
|
||||
//
|
||||
int isSelected = mPositionInfo.SelectByIndex(index);
|
||||
if (!isSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
string symbol = mPositionInfo.Symbol();
|
||||
if (symbol != _Symbol)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
ulong pMagic = mPositionInfo.Magic();
|
||||
if (pMagic != magicNumber)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
double pProfit = mPositionInfo.Profit();
|
||||
totalProfit += pProfit;
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Hedging Positions ...
|
||||
if (count > 1 && totalProfit > 0)
|
||||
{
|
||||
//
|
||||
// Loop through Positions and Close All of Them ...
|
||||
for (int index = count - 1; index >= 0; index--)
|
||||
{
|
||||
//
|
||||
int isSelected = mPositionInfo.SelectByIndex(index);
|
||||
if (!isSelected)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
string symbol = mPositionInfo.Symbol();
|
||||
if (symbol != _Symbol)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
ulong pMagic = mPositionInfo.Magic();
|
||||
if (pMagic != magicNumber)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
ulong ticket = mPositionInfo.Ticket();
|
||||
double volume = mPositionInfo.Volume();
|
||||
ENUM_POSITION_TYPE pType = (ENUM_POSITION_TYPE)mPositionInfo.PositionType();
|
||||
|
||||
//
|
||||
double entry = GetEntry(pType == POSITION_TYPE_BUY ? X_SIGNAL_SHORT : X_SIGNAL_LONG);
|
||||
|
||||
//
|
||||
//
|
||||
// declare and initialize the trade request and result of trade request
|
||||
MqlTradeRequest request = {};
|
||||
MqlTradeResult result = {};
|
||||
|
||||
//
|
||||
// parameters of request
|
||||
request.deviation = 5;
|
||||
request.price = entry;
|
||||
request.volume = volume;
|
||||
request.symbol = _Symbol;
|
||||
request.magic = magicNumber;
|
||||
request.type = pType == POSITION_TYPE_BUY ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;
|
||||
request.action = TRADE_ACTION_DEAL;
|
||||
|
||||
//
|
||||
ResetLastError();
|
||||
|
||||
//
|
||||
bool isExecuted = OrderSend(
|
||||
request,
|
||||
result);
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user