last works on XTEST EA and Compile and Packed for Test purposes ...
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,572 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Expert Advisor
|
||||
// ----------------------------------------
|
||||
// Name: XAUUSD
|
||||
// Description: this is an expert controller
|
||||
// advisors which provides a mechanism for use them ...
|
||||
//
|
||||
//
|
||||
// Maintainer:
|
||||
// ------------
|
||||
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
||||
//
|
||||
//////////////////////////////////////////////////////
|
||||
//
|
||||
// Global Properties ...
|
||||
#property copyright "Copyright 2023, SaherElm IT Center"
|
||||
#property link "https://saherelm.ir"
|
||||
#property version "1.00"
|
||||
#property description "SaherElm XTEST EA"
|
||||
#property strict
|
||||
|
||||
//
|
||||
#include <Trade/PositionInfo.mqh>
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
input group "Commons";
|
||||
|
||||
//
|
||||
input double staticVolume = 0.01; // Static Volume
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Including Providers ...
|
||||
//
|
||||
|
||||
//
|
||||
// Logger Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5"
|
||||
|
||||
//
|
||||
// Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||
|
||||
//
|
||||
// Alert Library ...
|
||||
#include "../Libraries/x-saherelm.alert.lib.mq5"
|
||||
|
||||
//
|
||||
// Draw Library ...
|
||||
#include "../Libraries/x-saherelm.draw.lib.mq5"
|
||||
|
||||
//
|
||||
// END Including Providers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
ulong magicNumber = 169405612;
|
||||
|
||||
//
|
||||
int xMAFastHandler = INVALID_HANDLE;
|
||||
double xMAFastBuffer[];
|
||||
|
||||
//
|
||||
int xMASlowHandler = INVALID_HANDLE;
|
||||
double xMASlowBuffer[];
|
||||
|
||||
//
|
||||
int ticksCount;
|
||||
int signalTicksCount;
|
||||
|
||||
//
|
||||
CPositionInfo mPositionInfo;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
logTag = "XTESTEA";
|
||||
drawPrefix = logTag;
|
||||
alertPrefix = logTag;
|
||||
|
||||
//
|
||||
// Apply default Chart Style for EA ...
|
||||
ApplyChartStyle();
|
||||
|
||||
//
|
||||
// Reset Timings ...
|
||||
ResetTiming();
|
||||
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Handlers ...
|
||||
if (!DefineHandlers())
|
||||
{
|
||||
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
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
ReleaseHandlers();
|
||||
}
|
||||
|
||||
//
|
||||
// On Tick Handler ...
|
||||
void OnTick()
|
||||
{
|
||||
//
|
||||
if (!IsNewCandle())
|
||||
{
|
||||
ticksCount++;
|
||||
}
|
||||
|
||||
//
|
||||
HandleTick();
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Trade Events ...
|
||||
void OnTrade()
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
|
||||
//
|
||||
// Validate XTEST Provider Inputs ...
|
||||
result = true;
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// Logging State ...
|
||||
string message = "Validation of (" + logTag + ") Inputs " + (result ? "Succeeded" : "Failed") + " ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
bool DefineHandlers()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
// FAST ...
|
||||
xMAFastHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
fastLength,
|
||||
0,
|
||||
fastMethod,
|
||||
fastAppliedTo);
|
||||
|
||||
//
|
||||
if (xMAFastHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
result = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// SLOW ...
|
||||
xMASlowHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
slowLength,
|
||||
0,
|
||||
fastMethod,
|
||||
fastAppliedTo);
|
||||
|
||||
//
|
||||
if (xMASlowHandler == 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 ...
|
||||
//
|
||||
@@ -0,0 +1,200 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Alert Library
|
||||
// --------------------------------------
|
||||
// Name: XAlert
|
||||
// Description: provides Alerts abilities ...
|
||||
//
|
||||
//
|
||||
// Maintainer:
|
||||
// ------------
|
||||
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
||||
//
|
||||
//////////////////////////////////////////////////////
|
||||
//
|
||||
// Global Properties ...
|
||||
#property library
|
||||
#property copyright "Copyright 2023, SaherElm IT Center"
|
||||
#property link "https://www.saherelm.ir"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input group "Alerts";
|
||||
input bool enableAlerts = true; // Enable Alerts
|
||||
input bool logAlerts = true; // Log Alerts
|
||||
input bool terminalAlerts = false; // Terminal Alerts
|
||||
input bool mailAlerts = false; // Mail Alerts
|
||||
input bool pushAlerts = false; // Push Alerts
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
#include "x-saherelm.log.lib.mq5"
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
string alertPrefix = "X-Alert";
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Provided Functions ...
|
||||
//
|
||||
//
|
||||
// Initial Library if required ...
|
||||
bool OnInInitAlertLibrary()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// DeInitial Library if required ...
|
||||
void OnDeinitAlertLibrary()
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Attach Alert Prefix to Message ...
|
||||
string PrepareAlertMessage(string message)
|
||||
{
|
||||
//
|
||||
StringReplace(message, "\n", " | ");
|
||||
|
||||
//
|
||||
StringConcatenate(message, alertPrefix, " > ", message);
|
||||
|
||||
//
|
||||
return message;
|
||||
}
|
||||
|
||||
//
|
||||
// Logging an Alert ...
|
||||
void LogAlert(string message)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (!enableAlerts || !logAlerts)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
LogMessage(alertPrefix, message);
|
||||
}
|
||||
|
||||
//
|
||||
// Terminal Alert ...
|
||||
void TerminalAlert(string message)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (!enableAlerts || !terminalAlerts)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Message ...
|
||||
message = PrepareAlertMessage(message);
|
||||
|
||||
// Send Terminal Alert ...
|
||||
Alert(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Mail Alert ...
|
||||
void MailAlert(string message)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (!enableAlerts || !mailAlerts)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Send Mail Alert ...
|
||||
SendMail(alertPrefix, message);
|
||||
}
|
||||
|
||||
//
|
||||
// Send Push Notification ...
|
||||
void PushAlert(string message)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (!enableAlerts || !pushAlerts)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Message ...
|
||||
message = PrepareAlertMessage(message);
|
||||
|
||||
//
|
||||
// Send Push Notification Alert ...
|
||||
SendNotification(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Alert Sending ...
|
||||
void SendAlert(string message)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (!enableAlerts)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Log ...
|
||||
if (logAlerts)
|
||||
{
|
||||
LogAlert(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Terminal ...
|
||||
if (terminalAlerts)
|
||||
{
|
||||
TerminalAlert(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Mail ...
|
||||
if (mailAlerts)
|
||||
{
|
||||
MailAlert(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Push ...
|
||||
if (pushAlerts)
|
||||
{
|
||||
PushAlert(message);
|
||||
}
|
||||
}
|
||||
void SendAlert(
|
||||
XSignal &signal, // the Signal Object which requred to Alert
|
||||
bool asExecuted = true // determines Alert Signal as Executed Signal or not
|
||||
)
|
||||
{
|
||||
//
|
||||
// Prepare Message ...
|
||||
string message = asExecuted ? ExecutedSignalToString(signal) : SignalToString(signal);
|
||||
|
||||
//
|
||||
// Do Alerting ...
|
||||
SendAlert(message);
|
||||
}
|
||||
//
|
||||
// END Provided Functions ...
|
||||
//
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,177 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Http Library
|
||||
// ------------------------------------------
|
||||
// Name: XHttp
|
||||
// Description: provide http communication
|
||||
// abilities ...
|
||||
//
|
||||
//
|
||||
// Maintainer:
|
||||
// ------------
|
||||
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
||||
//
|
||||
//////////////////////////////////////////////////////
|
||||
//
|
||||
// Global Properties ...
|
||||
#property library
|
||||
#property copyright "Copyright 2023, SaherElm IT Center"
|
||||
#property link "https://www.saherelm.ir"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Model Definition ...
|
||||
//
|
||||
enum X_HTTP_METHOD
|
||||
{
|
||||
X_HTTP_GET,
|
||||
X_HTTP_POST
|
||||
};
|
||||
//
|
||||
// END Model Definition ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Provided Functions ...
|
||||
//
|
||||
//
|
||||
// Initial Library if required ...
|
||||
bool OnInInitHttpLibrary()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// DeInitial Library if required ...
|
||||
void OnDeinitHttpLibrary()
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Convert enum to String ...
|
||||
string ToString(X_HTTP_METHOD method)
|
||||
{
|
||||
//
|
||||
string result = "";
|
||||
|
||||
//
|
||||
switch (method)
|
||||
{
|
||||
//
|
||||
case X_HTTP_GET:
|
||||
result = "GET";
|
||||
break;
|
||||
|
||||
//
|
||||
case X_HTTP_POST:
|
||||
result = "POST";
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Send Global Request ...
|
||||
int SendRequest(
|
||||
X_HTTP_METHOD method, // Httm Request Method
|
||||
const string url, // Server Address
|
||||
const string headers, // Headers providing
|
||||
const char &payload[], // the Data which needs to Send
|
||||
char &response[], // Response of request
|
||||
string responseHeaders, // Response Headers
|
||||
int timeout = 500 // Timeout for response default is 500
|
||||
)
|
||||
{
|
||||
//
|
||||
int result = -1;
|
||||
|
||||
//
|
||||
string strMethod = ToString(method);
|
||||
if (StringLen(strMethod) == 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = WebRequest(
|
||||
strMethod,
|
||||
url,
|
||||
headers,
|
||||
timeout,
|
||||
payload,
|
||||
response,
|
||||
responseHeaders
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Get Request ...
|
||||
int GetRequest(
|
||||
const string url, // Server Address
|
||||
const string headers, // Headers providing
|
||||
const char &payload[], // the Data which needs to Send
|
||||
char &response[], // Response of request
|
||||
string responseHeaders, // Response Headers
|
||||
int timeout = 500 // Timeout for response default is 500
|
||||
) {
|
||||
//
|
||||
int result = SendRequest(
|
||||
X_HTTP_GET,
|
||||
url,
|
||||
headers,
|
||||
payload,
|
||||
response,
|
||||
responseHeaders,
|
||||
timeout
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Post Request ...
|
||||
int PostRequest(
|
||||
const string url, // Server Address
|
||||
const string headers, // Headers providing
|
||||
const char &payload[], // the Data which needs to Send
|
||||
char &response[], // Response of request
|
||||
string responseHeaders, // Response Headers
|
||||
int timeout = 500 // Timeout for response default is 500
|
||||
) {
|
||||
//
|
||||
int result = SendRequest(
|
||||
X_HTTP_POST,
|
||||
url,
|
||||
headers,
|
||||
payload,
|
||||
response,
|
||||
responseHeaders,
|
||||
timeout
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
//
|
||||
// END Provided Functions ...
|
||||
//
|
||||
@@ -0,0 +1,261 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Logger Library
|
||||
// ---------------------------------------
|
||||
// Name: XLogger
|
||||
// Description: provides Logging abilities ...
|
||||
//
|
||||
//
|
||||
// Maintainer:
|
||||
// ------------
|
||||
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
||||
//
|
||||
//////////////////////////////////////////////////////
|
||||
//
|
||||
// Global Properties ...
|
||||
#property library
|
||||
#property copyright "Copyright 2023, SaherElm IT Center"
|
||||
#property link "https://www.saherelm.ir"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input group "Logging";
|
||||
input bool enableLogging = true; // Enable Logging
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Including Models ...
|
||||
#include "x-saherelm.models.lib.mq5"
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
string logTag = "X-Logger"; // LogTag
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Provided Functions ...
|
||||
//
|
||||
//
|
||||
// Logging a Message ...
|
||||
void LogMessage(string message)
|
||||
{
|
||||
//
|
||||
if (!enableLogging)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
Print(logTag, " > ", message);
|
||||
}
|
||||
void LogMessage(string tag, string message)
|
||||
{
|
||||
//
|
||||
if (!enableLogging)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
Print(tag, " > ", message);
|
||||
}
|
||||
|
||||
//
|
||||
// Logging an Array ...
|
||||
void LogArray(int &array[])
|
||||
{
|
||||
//
|
||||
if (!enableLogging)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
Print(logTag, ">");
|
||||
ArrayPrint(array);
|
||||
}
|
||||
void LogArray(double &array[])
|
||||
{
|
||||
//
|
||||
if (!enableLogging)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
Print(logTag, ">");
|
||||
ArrayPrint(array);
|
||||
}
|
||||
void LogArray(string &array[])
|
||||
{
|
||||
//
|
||||
if (!enableLogging)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
Print(logTag, ">");
|
||||
ArrayPrint(array);
|
||||
}
|
||||
|
||||
//
|
||||
// Convert a Signal to String Message ...
|
||||
string SignalToString(const XSignal &signal)
|
||||
{
|
||||
//
|
||||
string result = "\n";
|
||||
|
||||
//
|
||||
result += "Signal: " + "\n";
|
||||
result += "id: " + (string)signal.id + "\n";
|
||||
result += "symbol: " + signal.symbol + "\n";
|
||||
result += "type: " + EnumToString(signal.type) + "\n";
|
||||
result += "magicNumber: " + (string)signal.magicNumber + "\n";
|
||||
result += "ticket: " + (string)signal.ticket + "\n";
|
||||
result += "entry: " + (string)signal.entry + "\n";
|
||||
result += "tp: " + (string)signal.tp + "\n";
|
||||
result += "sl: " + (string)signal.sl + "\n";
|
||||
result += "riskFreeStep: " + (string)signal.riskFreeStep + "\n";
|
||||
result += "riskFreeRate: " + (string)signal.riskFreeRate + "\n";
|
||||
result += "profit: " + (string)signal.profit + "\n";
|
||||
result += "time: " + (string)signal.time + "\n";
|
||||
result += "volume: " + (string)signal.volume + "\n";
|
||||
result += "comment: " + signal.comment + "\n";
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Convert an Executed Signal to String Message ...
|
||||
string ExecutedSignalToString(const XSignal &signal)
|
||||
{
|
||||
//
|
||||
string result = "";
|
||||
|
||||
//
|
||||
result += (signal.type == X_SIGNAL_LONG ? "Buy" : "Sell") + " Signal Executed provided on (" + signal.symbol + " _ " + EnumToString(_Period) + ") by this comment: " + signal.comment;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Logging Signal ...
|
||||
void LogSignal(const XSignal &signal)
|
||||
{
|
||||
//
|
||||
string message = SignalToString(signal);
|
||||
|
||||
//
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Log Signal Execution ...
|
||||
void LogExecutedSignal(const XSignal &signal)
|
||||
{
|
||||
//
|
||||
string message = ExecutedSignalToString(signal);
|
||||
|
||||
//
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Convert a Deal to String ...
|
||||
string DealToString(const XDeal &deal)
|
||||
{
|
||||
//
|
||||
string result = "\n";
|
||||
|
||||
//
|
||||
result += "Deal:" + "\n";
|
||||
result += " " + "\n";
|
||||
result += "time: " + (string)deal.time + "\n";
|
||||
result += "ticket: " + (string)deal.ticket + "\n";
|
||||
result += "order: " + (string)deal.order + "\n";
|
||||
result += "positionId: " + (string)deal.positionId + "\n";
|
||||
result += "type: " + EnumToString(deal.type) + "\n";
|
||||
result += "entry: " + EnumToString(deal.entry) + "\n";
|
||||
result += "reason: " + EnumToString(deal.reason) + "\n";
|
||||
result += "symbol: " + deal.symbol + "\n";
|
||||
result += "price: " + (string)deal.price + "\n";
|
||||
result += "profit: " + (string)deal.profit + "\n";
|
||||
result += "swap: " + (string)deal.swap + "\n";
|
||||
result += "commission: " + (string)deal.commission + "\n";
|
||||
result += "volume: " + (string)deal.volume + "\n";
|
||||
result += "comment: " + deal.comment + "\n";
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Log Specific Deal ...
|
||||
void LogDeal(const XDeal &deal)
|
||||
{
|
||||
//
|
||||
string message = DealToString(deal);
|
||||
|
||||
//
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
//
|
||||
// Convert an Order to String ...
|
||||
string OrderToString(const XOrder &order)
|
||||
{
|
||||
//
|
||||
string result = "\n";
|
||||
|
||||
//
|
||||
result += "Order: " + "\n";
|
||||
result += "magic: " + (string)order.magic + "\n";
|
||||
result += "positionId: " + (string)order.positionId + "\n";
|
||||
result += "symbol: " + (string)order.symbol + "\n";
|
||||
result += "ticket: " + (string)order.ticket + "\n";
|
||||
result += "timeSetup: " + (string)order.timeSetup + "\n";
|
||||
result += "timeDone: " + (string)order.timeDone + "\n";
|
||||
result += "timeExpiration: " + (string)order.timeExpiration + "\n";
|
||||
result += "priceOpen: " + (string)order.priceOpen + "\n";
|
||||
result += "priceCurrent: " + (string)order.priceCurrent + "\n";
|
||||
result += "priceStopLimit: " + (string)order.priceStopLimit + "\n";
|
||||
result += "stopLoss: " + (string)order.stopLoss + "\n";
|
||||
result += "takeProfit: " + (string)order.takeProfit + "\n";
|
||||
result += "volumeInitial: " + (string)order.volumeInitial + "\n";
|
||||
result += "volumeCurrent: " + (string)order.volumeCurrent + "\n";
|
||||
result += "comment: " + order.comment + "\n";
|
||||
result += "type: " + EnumToString(order.type) + "\n";
|
||||
result += "state: " + EnumToString(order.state) + "\n";
|
||||
result += "typeFilling: " + EnumToString(order.typeFilling) + "\n";
|
||||
result += "typeTime: " + EnumToString(order.typeTime) + "\n";
|
||||
result += "reason: " + EnumToString(order.reason) + "\n";
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Log Specific Order ...
|
||||
void LogOrder(const XOrder &order)
|
||||
{
|
||||
//
|
||||
string message = OrderToString(order);
|
||||
|
||||
//
|
||||
LogMessage(message);
|
||||
}
|
||||
|
||||
//
|
||||
// END Provided Functions ...
|
||||
//
|
||||
@@ -0,0 +1,438 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Models Library
|
||||
// ---------------------------------------
|
||||
// Name: XModels
|
||||
// Description: provides all required models ...
|
||||
//
|
||||
//
|
||||
// Maintainer:
|
||||
// ------------
|
||||
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
||||
//
|
||||
//////////////////////////////////////////////////////
|
||||
//
|
||||
// Global Properties ...
|
||||
#property library
|
||||
#property copyright "Copyright 2023, SaherElm IT Center"
|
||||
#property link "https://www.saherelm.ir"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Provided Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// How To Calculate Static Volume ...
|
||||
enum ENUM_STATIC_VOLUME_METHODS
|
||||
{
|
||||
//
|
||||
// Based On Deposit ...
|
||||
X_STATIC_VOLUME_BASED_ON_DEPOSIT,
|
||||
//
|
||||
// Based On Balance ...
|
||||
X_STATIC_VOLUME_BASED_ON_BALANCE,
|
||||
};
|
||||
|
||||
//
|
||||
// START Model Definitions ...
|
||||
//
|
||||
//
|
||||
// OHCL Candel Model ...
|
||||
enum ENUM_X_PRICE
|
||||
{
|
||||
X_PRICE_HIGH,
|
||||
X_PRICE_OPEN,
|
||||
X_PRICE_CLOSE,
|
||||
X_PRICE_LOW
|
||||
};
|
||||
|
||||
//
|
||||
// Describe an Specific Candle ...
|
||||
struct XOHCL
|
||||
{
|
||||
double high;
|
||||
double open;
|
||||
double close;
|
||||
double low;
|
||||
};
|
||||
|
||||
//
|
||||
// Swing Types Enum ...
|
||||
enum ENUM_X_SWING_TYPE
|
||||
{
|
||||
X_NO_SWING = 0,
|
||||
X_SWING_HIGH = 1,
|
||||
X_SWING_LOW = -1,
|
||||
};
|
||||
|
||||
//
|
||||
// XTrader Execution Signal Result ...
|
||||
enum ENUM_EXECUTION_SIGNAL_RESULT
|
||||
{
|
||||
//
|
||||
X_TRADER_UNKNOWN_ERROR,
|
||||
X_TRADER_SUCCEED_EXECUTION,
|
||||
X_TRADER_INVALID_SIGNAL_ERROR,
|
||||
X_TRADER_NOT_ENOUGH_EQUITY_ERROR,
|
||||
X_TRADER_MAX_TRADES_REACHED_ERROR,
|
||||
X_TRADER_NOT_ENOUGH_BALANCE_ERROR,
|
||||
X_TRADER_NOT_ENOUGH_MARIGIN_ERROR
|
||||
//
|
||||
};
|
||||
|
||||
//
|
||||
// Signal Types ...
|
||||
enum ENUM_X_SIGNAL_TYPE
|
||||
{
|
||||
X_SIGNAL_LONG,
|
||||
X_SIGNAL_SHORT,
|
||||
X_SIGNAL_UNKNOWN,
|
||||
};
|
||||
|
||||
//
|
||||
// Signal Structure ...
|
||||
struct XSignal
|
||||
{
|
||||
//
|
||||
// Identifier ...
|
||||
ulong id;
|
||||
|
||||
//
|
||||
// Which Symbol used for Signal ...
|
||||
string symbol;
|
||||
|
||||
//
|
||||
// Which Type of operations ...
|
||||
ENUM_X_SIGNAL_TYPE type;
|
||||
|
||||
//
|
||||
// Magic Number ...
|
||||
ulong magicNumber;
|
||||
|
||||
//
|
||||
// Ticket ...
|
||||
ulong ticket;
|
||||
|
||||
//
|
||||
// Entry/Open Price ...
|
||||
double entry;
|
||||
|
||||
//
|
||||
// TP ...
|
||||
double tp;
|
||||
|
||||
//
|
||||
// SL ...
|
||||
double sl;
|
||||
|
||||
//
|
||||
// Make Risk Free Trade on this Value if provided ...
|
||||
double riskFreeStep;
|
||||
|
||||
//
|
||||
// a Multiplier for determines how much trade volume exit on risk free time ...
|
||||
double riskFreeRate;
|
||||
|
||||
//
|
||||
// Trade Profit ...
|
||||
double profit;
|
||||
|
||||
//
|
||||
// Time of Operation ...
|
||||
datetime time;
|
||||
|
||||
//
|
||||
// Volume ...
|
||||
double volume;
|
||||
|
||||
//
|
||||
// Comment ...
|
||||
string comment;
|
||||
};
|
||||
|
||||
//
|
||||
// Signal Additional Info ...
|
||||
struct XProvidedSignal
|
||||
{
|
||||
//
|
||||
// Suggested TP ...
|
||||
double tp;
|
||||
|
||||
//
|
||||
// Suggested SL ...
|
||||
double sl;
|
||||
|
||||
//
|
||||
// Signallers ...
|
||||
string signallers[];
|
||||
};
|
||||
|
||||
//
|
||||
// Model Specific Deal ...
|
||||
struct XDeal
|
||||
{
|
||||
//
|
||||
// the ID of the Expert Advisor, that executed the deal ...
|
||||
ulong magic;
|
||||
|
||||
//
|
||||
// the ID of position, in which the deal was involved ...
|
||||
ulong positionId;
|
||||
|
||||
//
|
||||
// Ticket ...
|
||||
ulong ticket;
|
||||
|
||||
//
|
||||
// the name of the deal symbol ...
|
||||
string symbol;
|
||||
|
||||
//
|
||||
// order by which the deal is executed ...
|
||||
ulong order;
|
||||
|
||||
//
|
||||
// the time of deal execution ...
|
||||
datetime time;
|
||||
|
||||
//
|
||||
// Deal price ...
|
||||
double price;
|
||||
|
||||
//
|
||||
// the financial result of the deal (in deposit currency) ...
|
||||
double profit;
|
||||
|
||||
//
|
||||
// the amount of swap when position is closed ...
|
||||
double swap;
|
||||
|
||||
//
|
||||
// the amount of commission of the deal ...
|
||||
double commission;
|
||||
|
||||
//
|
||||
// the volume of deal ...
|
||||
double volume;
|
||||
|
||||
//
|
||||
// the deal comment ...
|
||||
string comment;
|
||||
|
||||
//
|
||||
// the deal type ...
|
||||
// ------------------------------------------------------------------
|
||||
// DEAL_TYPE_BUY => Buy ...
|
||||
// DEAL_TYPE_SELL => Sell ...
|
||||
// DEAL_TYPE_BUY_CANCELED => Canceled buy deal ...
|
||||
// There can be a situation when a previously executed buy deal is canceled. In this case,
|
||||
// the type of the previously executed deal (DEAL_TYPE_BUY) is changed to DEAL_TYPE_BUY_CANCELED,
|
||||
// and its profit/loss is zeroized. Previously obtained profit/loss is charged/withdrawn using a separated balance operation ...
|
||||
//
|
||||
// DEAL_TYPE_SELL_CANCELED => Canceled sell deal ...
|
||||
// There can be a situation when a previously executed sell deal is canceled. In this case,
|
||||
// the type of the previously executed deal (DEAL_TYPE_SELL) is changed to DEAL_TYPE_SELL_CANCELED,
|
||||
// and its profit/loss is zeroized. Previously obtained profit/loss is charged/withdrawn using a separated balance operation
|
||||
//
|
||||
// DEAL_TYPE_BALANCE => Balance ...
|
||||
// DEAL_TYPE_CREDIT => Credit ...
|
||||
// DEAL_TYPE_CHARGE => Additional charge ...
|
||||
// DEAL_TYPE_CORRECTION => Correction ...
|
||||
// DEAL_TAX => Tax charges ...
|
||||
// DEAL_TYPE_BONUS => Bonus ...
|
||||
// DEAL_TYPE_INTEREST => Interest rate ...
|
||||
// DEAL_DIVIDEND => Dividend operations...
|
||||
// DEAL_DIVIDEND_FRANKED => Franked (non-taxable) dividend operations ...
|
||||
// DEAL_TYPE_COMMISSION => Additional commission ...
|
||||
// DEAL_TYPE_COMMISSION_DAILY => Daily commission ...
|
||||
// DEAL_TYPE_COMMISSION_MONTHLY => Monthly commission ...
|
||||
// DEAL_TYPE_COMMISSION_AGENT_DAILY => Daily agent commission ...
|
||||
// DEAL_TYPE_COMMISSION_AGENT_MONTHLY => Monthly agent commission
|
||||
ENUM_DEAL_TYPE type;
|
||||
|
||||
//
|
||||
// the deal direction ...
|
||||
// ----------------------------------
|
||||
// DEAL_ENTRY_IN => Entry in ...
|
||||
// DEAL_ENTRY_OUT => Entry out ...
|
||||
// DEAL_ENTRY_INOUT => Reverse ...
|
||||
// DEAL_ENTRY_OUT_BY => Close a position by an opposite one ...
|
||||
ENUM_DEAL_ENTRY entry;
|
||||
|
||||
//
|
||||
// deal reson ...
|
||||
// ---------------------
|
||||
// DEAL_REASON_SL => The deal was executed as a result of Stop Loss activation ...
|
||||
// DEAL_REASON_TP => The deal was executed as a result of Take Profit activation ...
|
||||
// DEAL_REASON_SO => The deal was executed as a result of the Stop Out event ...
|
||||
// DEAL_REASON_WEB => The deal was executed as a result of activation of an order placed from the web platform ...
|
||||
// DEAL_REASON_SPLIT => The deal was executed after the split (price reduction) of an instrument, which had an open position during split announcement ...
|
||||
// DEAL_REASON_CLIENT => The deal was executed as a result of activation of an order placed from a desktop terminal ...
|
||||
// DEAL_REASON_MOBILE => The deal was executed as a result of activation of an order placed from a mobile application ...
|
||||
// DEAL_REASON_EXPERT => The deal was executed as a result of activation of an order placed from an MQL5 program, i.e. an Expert Advisor or a script ...
|
||||
// DEAL_REASON_VMARGIN => The deal was executed after charging the variation margin ...
|
||||
// DEAL_REASON_ROLLOVER => The deal was executed due to a rollover ...
|
||||
ENUM_DEAL_REASON reason;
|
||||
};
|
||||
|
||||
//
|
||||
// Model Specific Order ...
|
||||
struct XOrder
|
||||
{
|
||||
//
|
||||
// the ID of expert that placed the order ...
|
||||
ulong magic;
|
||||
|
||||
//
|
||||
// the ID of position ...
|
||||
ulong positionId;
|
||||
|
||||
//
|
||||
// the name of order symbol ...
|
||||
string symbol;
|
||||
|
||||
//
|
||||
// the ticket of an order, previously selected for access ...
|
||||
ulong ticket;
|
||||
|
||||
//
|
||||
// the time of order placement ...
|
||||
datetime timeSetup;
|
||||
|
||||
//
|
||||
// the time of order execution or cancellation ...
|
||||
datetime timeDone;
|
||||
|
||||
//
|
||||
// the order expiration time ...
|
||||
datetime timeExpiration;
|
||||
|
||||
//
|
||||
// the order price ...
|
||||
double priceOpen;
|
||||
|
||||
//
|
||||
// the current price by order symbol ...
|
||||
double priceCurrent;
|
||||
|
||||
//
|
||||
// the price of a pending order ...
|
||||
double priceStopLimit;
|
||||
|
||||
//
|
||||
// the order's Stop Loss ...
|
||||
double stopLoss;
|
||||
|
||||
//
|
||||
// the order's Take Profit ...
|
||||
double takeProfit;
|
||||
|
||||
//
|
||||
// the initial volume of order ...
|
||||
double volumeInitial;
|
||||
|
||||
//
|
||||
// the unfilled volume of order ...
|
||||
double volumeCurrent;
|
||||
|
||||
//
|
||||
// the order comment ...
|
||||
string comment;
|
||||
|
||||
//
|
||||
// the order type ...
|
||||
// --------------------
|
||||
// ORDER_TYPE_BUY => Market Buy order ...
|
||||
// ORDER_TYPE_SELL => Market Sell order ...
|
||||
// ORDER_TYPE_BUY_LIMIT => Buy Limit pending order ...
|
||||
// ORDER_TYPE_SELL_LIMIT => Sell Limit pending order ...
|
||||
// ORDER_TYPE_BUY_STOP => Buy Stop pending order ...
|
||||
// ORDER_TYPE_SELL_STOP => Sell Stop pending order ...
|
||||
// ORDER_TYPE_BUY_STOP_LIMIT => Upon reaching the order price, a pending Buy Limit order is placed at the StopLimit price ...
|
||||
// ORDER_TYPE_SELL_STOP_LIMIT => Upon reaching the order price, a pending Sell Limit order is placed at the StopLimit price ...
|
||||
// ORDER_TYPE_CLOSE_BY => Order to close a position by an opposite one ...
|
||||
ENUM_ORDER_TYPE type;
|
||||
|
||||
//
|
||||
// the order state ...
|
||||
// --------------------------
|
||||
// ORDER_STATE_STARTED => Order checked, but not yet accepted by broker ...
|
||||
// ORDER_STATE_PLACED => Order accepted ...
|
||||
// ORDER_STATE_CANCELED => Order canceled by client ...
|
||||
// ORDER_STATE_PARTIAL => Order partially executed ...
|
||||
// ORDER_STATE_FILLED => Order fully executed ...
|
||||
// ORDER_STATE_REJECTED => Order rejected ...
|
||||
// ORDER_STATE_EXPIRED => Order expired ...
|
||||
// ORDER_STATE_REQUEST_ADD => Order is being registered (placing to the trading system)
|
||||
// ORDER_STATE_REQUEST_MODIFY => Order is being modified (changing its parameters) ...
|
||||
// ORDER_STATE_REQUEST_CANCEL => Order is being deleted (deleting from the trading system) ...
|
||||
ENUM_ORDER_STATE state;
|
||||
|
||||
//
|
||||
// the order filling type ...
|
||||
// ----------------------------
|
||||
// ORDER_FILLING_FOK => Fill or Kill
|
||||
// An order can be executed in the specified volume only.
|
||||
// If the necessary amount of a financial instrument is currently unavailable in the market,
|
||||
// the order will not be executed.
|
||||
// The desired volume can be made up of several available offers.
|
||||
// The possibility of using FOK orders is determined at the trade server.
|
||||
// ORDER_FILLING_IOC => Immediate or Cancel
|
||||
// A trader agrees to execute a deal with the volume maximally available in the market within that indicated in the order.
|
||||
// If the request cannot be filled completely, an order with the available volume will be executed,
|
||||
// and the remaining volume will be canceled.
|
||||
// The possibility of using IOC orders is determined at the trade server.
|
||||
// ORDER_FILLING_BOC => Passive (Book or Cancel)
|
||||
// The BoC order assumes that the order can only be placed in the Depth of Market and cannot be immediately executed.
|
||||
// If the order can be executed immediately when placed, then it is canceled.
|
||||
// In fact, the BOC policy guarantees that the price of the placed order will be worse than
|
||||
// the current market. BoC orders are used to implement passive trading,
|
||||
// so that the order is not executed immediately when placed and does not affect current liquidity.
|
||||
// Only limit and stop limit orders are supported (ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_BUY_STOP_LIMIT, ORDER_TYPE_SELL_STOP_LIMIT).
|
||||
// ORDER_FILLING_RETURN => Return
|
||||
// In case of partial filling, an order with remaining volume is not canceled but processed further.
|
||||
// Return orders are not allowed in the Market Execution
|
||||
// mode (market execution — SYMBOL_TRADE_EXECUTION_MARKET).
|
||||
ENUM_ORDER_TYPE_FILLING typeFilling;
|
||||
|
||||
//
|
||||
// the type of order at the time of the expiration ...
|
||||
// --------------------------------------------------------
|
||||
// ORDER_TIME_GTC => Good till cancel order ...
|
||||
// ORDER_TIME_DAY => Good till current trade day order ...
|
||||
// ORDER_TIME_SPECIFIED => Good till expired order ...
|
||||
// ORDER_TIME_SPECIFIED_DAY => The order will be effective till 23:59:59 of the specified day.
|
||||
// If this time is outside a trading session, the order expires in the nearest trading time ...
|
||||
ENUM_ORDER_TYPE_TIME typeTime;
|
||||
|
||||
//
|
||||
// The reason for order placing ...
|
||||
// --------------------------------------
|
||||
// ORDER_REASON_SL => The order was placed as a result of Stop Loss activation ...
|
||||
// ORDER_REASON_TP => The order was placed as a result of Take Profit activation ...
|
||||
// ORDER_REASON_SO => The order was placed as a result of the Stop Out event ...
|
||||
// ORDER_REASON_WEB => The order was placed from a web platform ...
|
||||
// ORDER_REASON_CLIENT => The order was placed from a desktop terminal ...
|
||||
// ORDER_REASON_MOBILE => The order was placed from a mobile application ...
|
||||
// ORDER_REASON_EXPERT => The order was placed from an MQL5-program, i.e. by an Expert Advisor or a script ...
|
||||
ENUM_ORDER_REASON reason;
|
||||
};
|
||||
//
|
||||
// END Provided Functions ...
|
||||
//
|
||||
@@ -20,17 +20,34 @@
|
||||
#property description "SaherElm XTEST EA"
|
||||
#property strict
|
||||
|
||||
//
|
||||
#include <Trade/PositionInfo.mqh>
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
input group "Commons";
|
||||
|
||||
//
|
||||
// END Inputs ...
|
||||
input double staticVolume = 0.01; // Static Volume
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
//
|
||||
|
||||
//
|
||||
//
|
||||
// START Including Providers ...
|
||||
//
|
||||
|
||||
@@ -58,13 +75,22 @@ input group "Commons";
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
bool isXTESTNewHour;
|
||||
bool isXTESTNewDay;
|
||||
bool isXTESTNewWeek;
|
||||
bool isXTESTNewMonth;
|
||||
ulong magicNumber = 169405612;
|
||||
|
||||
//
|
||||
int xMAHandler = INVALID_HANDLE;
|
||||
int xMAFastHandler = INVALID_HANDLE;
|
||||
double xMAFastBuffer[];
|
||||
|
||||
//
|
||||
int xMASlowHandler = INVALID_HANDLE;
|
||||
double xMASlowBuffer[];
|
||||
|
||||
//
|
||||
int ticksCount;
|
||||
int signalTicksCount;
|
||||
|
||||
//
|
||||
CPositionInfo mPositionInfo;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
@@ -97,6 +123,13 @@ int OnInit()
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Handlers ...
|
||||
if (!DefineHandlers())
|
||||
{
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// Init Succeed ...
|
||||
return INIT_SUCCEEDED;
|
||||
@@ -120,6 +153,7 @@ void OnDeinit(const int reason)
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
ReleaseHandlers();
|
||||
}
|
||||
|
||||
//
|
||||
@@ -129,8 +163,11 @@ void OnTick()
|
||||
//
|
||||
if (!IsNewCandle())
|
||||
{
|
||||
return;
|
||||
ticksCount++;
|
||||
}
|
||||
|
||||
//
|
||||
HandleTick();
|
||||
}
|
||||
|
||||
//
|
||||
@@ -174,6 +211,362 @@ bool ValidateInputs()
|
||||
return result;
|
||||
}
|
||||
|
||||
bool DefineHandlers()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
// FAST ...
|
||||
xMAFastHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
fastLength,
|
||||
0,
|
||||
fastMethod,
|
||||
fastAppliedTo);
|
||||
|
||||
//
|
||||
if (xMAFastHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
result = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// SLOW ...
|
||||
xMASlowHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
slowLength,
|
||||
0,
|
||||
fastMethod,
|
||||
fastAppliedTo);
|
||||
|
||||
//
|
||||
if (xMASlowHandler == 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 ...
|
||||
//
|
||||
|
||||
@@ -20,17 +20,34 @@
|
||||
#property description "SaherElm XTEST EA"
|
||||
#property strict
|
||||
|
||||
//
|
||||
#include <Trade/PositionInfo.mqh>
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
input group "Commons";
|
||||
|
||||
//
|
||||
// END Inputs ...
|
||||
input double staticVolume = 0.01; // Static Volume
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
//
|
||||
|
||||
//
|
||||
//
|
||||
// START Including Providers ...
|
||||
//
|
||||
|
||||
@@ -57,10 +74,23 @@ input group "Commons";
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
bool isXTESTNewHour;
|
||||
bool isXTESTNewDay;
|
||||
bool isXTESTNewWeek;
|
||||
bool isXTESTNewMonth;
|
||||
//
|
||||
ulong magicNumber = 169405612;
|
||||
|
||||
//
|
||||
int xMAFastHandler = INVALID_HANDLE;
|
||||
double xMAFastBuffer[];
|
||||
|
||||
//
|
||||
int xMASlowHandler = INVALID_HANDLE;
|
||||
double xMASlowBuffer[];
|
||||
|
||||
//
|
||||
int ticksCount;
|
||||
int signalTicksCount;
|
||||
|
||||
//
|
||||
CPositionInfo mPositionInfo;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
@@ -93,6 +123,13 @@ int OnInit()
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Handlers ...
|
||||
if (!DefineHandlers())
|
||||
{
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// Init Succeed ...
|
||||
return INIT_SUCCEEDED;
|
||||
@@ -116,6 +153,7 @@ void OnDeinit(const int reason)
|
||||
|
||||
//
|
||||
// Here we can handle De Initialization Reasons ...
|
||||
ReleaseHandlers();
|
||||
}
|
||||
|
||||
//
|
||||
@@ -125,8 +163,11 @@ void OnTick()
|
||||
//
|
||||
if (!IsNewCandle())
|
||||
{
|
||||
return;
|
||||
ticksCount++;
|
||||
}
|
||||
|
||||
//
|
||||
HandleTick();
|
||||
}
|
||||
|
||||
//
|
||||
@@ -170,6 +211,362 @@ bool ValidateInputs()
|
||||
return result;
|
||||
}
|
||||
|
||||
bool DefineHandlers()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
// FAST ...
|
||||
xMAFastHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
fastLength,
|
||||
0,
|
||||
fastMethod,
|
||||
fastAppliedTo);
|
||||
|
||||
//
|
||||
if (xMAFastHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
result = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// SLOW ...
|
||||
xMASlowHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
slowLength,
|
||||
0,
|
||||
fastMethod,
|
||||
fastAppliedTo);
|
||||
|
||||
//
|
||||
if (xMASlowHandler == 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