Initial Commit ...
This commit is contained in:
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -0,0 +1,189 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 ...
|
||||
//
|
||||
|
||||
//
|
||||
// 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_INVALID_SIGNAL_ERROR,
|
||||
X_XTRADER_NOT_ENOUGH_BALANCE_ERROR,
|
||||
X_XTRADER_NOT_ENOUGH_MARIGIN_ERROR,
|
||||
X_XTRADER_MAX_ALLOWED_TRADE_KIND_REACHED_ERROR,
|
||||
X_XTRADER_MAX_ALLOWED_SUPPORT_KIND_REACHED_ERROR,
|
||||
X_UNKNOWN_ERROR,
|
||||
X_SUCCEED_EXECUTION
|
||||
};
|
||||
|
||||
//
|
||||
// Signal Types ...
|
||||
enum ENUM_X_SIGNAL_TYPE
|
||||
{
|
||||
X_SIGNAL_LONG,
|
||||
X_SIGNAL_SHORT,
|
||||
X_SIGNAL_UNKNOWN,
|
||||
};
|
||||
|
||||
//
|
||||
// Represent a Signal issued for Trade or Support a Trade ...
|
||||
enum ENUM_X_SIGNAL_KIND
|
||||
{
|
||||
X_KIND_TRADE,
|
||||
X_KIND_SUPPORT
|
||||
};
|
||||
|
||||
//
|
||||
// Signal Structure ...
|
||||
struct XSignal
|
||||
{
|
||||
//
|
||||
// Identifier ...
|
||||
ulong id;
|
||||
|
||||
//
|
||||
// Which Symbol used for Signal ...
|
||||
string symbol;
|
||||
|
||||
//
|
||||
// Which Type of operations ...
|
||||
ENUM_X_SIGNAL_TYPE type;
|
||||
|
||||
//
|
||||
// Signal Kind ...
|
||||
ENUM_X_SIGNAL_KIND kind;
|
||||
|
||||
//
|
||||
// Parent Ticket ...
|
||||
ulong parent;
|
||||
|
||||
//
|
||||
// Magic Number ...
|
||||
ulong magicNumber;
|
||||
|
||||
//
|
||||
// Ticket ...
|
||||
ulong ticket;
|
||||
|
||||
//
|
||||
// Entry/Open Price ...
|
||||
double entry;
|
||||
|
||||
//
|
||||
// TP ...
|
||||
double tp;
|
||||
|
||||
//
|
||||
double virtualTP;
|
||||
|
||||
//
|
||||
// SL ...
|
||||
double sl;
|
||||
|
||||
//
|
||||
double virtualSL;
|
||||
|
||||
//
|
||||
// Make Risk Free Trade on this Value if provided ...
|
||||
double riskFree;
|
||||
|
||||
//
|
||||
// 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;
|
||||
};
|
||||
|
||||
//
|
||||
// TP/SL Struct ...
|
||||
struct XTPSL
|
||||
{
|
||||
ENUM_X_SIGNAL_TYPE type;
|
||||
double tp;
|
||||
double sl;
|
||||
double entry;
|
||||
double r2r;
|
||||
};
|
||||
//
|
||||
// END Provided Functions ...
|
||||
//
|
||||
@@ -0,0 +1,186 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Expert Advisor
|
||||
// ----------------------------------------
|
||||
// Name: XEA
|
||||
// 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 XEA"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
|
||||
input group "Commons";
|
||||
|
||||
//
|
||||
// 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";
|
||||
|
||||
//
|
||||
// XTEST Signal Provider ...
|
||||
#include "..\Libraries\x-saherelm.xtest.provider.lib.mq5";
|
||||
|
||||
//
|
||||
// END Including Providers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
logTag = "XEA";
|
||||
drawPrefix = logTag;
|
||||
alertPrefix = logTag;
|
||||
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize XTEST Provider ...
|
||||
if (!XTESTInitSignalProviderLibrary())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
// Apply default Chart Style for EA ...
|
||||
ApplyChartStyle();
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
|
||||
//
|
||||
// De Initialize XTEST Provider ...
|
||||
XTESTDeinitSignalProviderLibrary(reason);
|
||||
|
||||
//
|
||||
// De Init XDraw Library ...
|
||||
// OnDeinitDrawLibrary();
|
||||
}
|
||||
|
||||
//
|
||||
// On Tick Handler ...
|
||||
void OnTick()
|
||||
{
|
||||
//
|
||||
if (!IsNewCandle())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Processing on XTEST Signal Provider ...
|
||||
XTESTSignalProviderHandleTick();
|
||||
}
|
||||
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
|
||||
//
|
||||
// Validate XTEST Provider Inputs ...
|
||||
result = XTESTValidateInputs();
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
// Logging State ...
|
||||
string message = "Validation of (" + "XEA" + ") Inputs " + (result ? "Succeeded" : "Failed") + " ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
///////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 XTEST Signal Provider Indicators Library
|
||||
// ----------------------------------------------------------------
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Include Provider Inputs ...
|
||||
#include "x-saherelm.xtest.provider.inputs.lib.mq5";
|
||||
|
||||
//
|
||||
// Imports all Indicators Helper here ...
|
||||
input group "XTEST Provider Indicators";
|
||||
//
|
||||
#include "x-saherelm.xtm.indicator.helper.lib.mq5";
|
||||
|
||||
//
|
||||
int xTESTMaxLengthOfInputs = 0;
|
||||
int xTESTCalculatedBars = 0;
|
||||
|
||||
//
|
||||
// Init Indicators ...
|
||||
bool XTESTInitIndicators()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Check and Calculate Max Length in Inputs ...
|
||||
xTESTMaxLengthOfInputs = MathMax(
|
||||
xTMMaPeriod,
|
||||
xTESTMaxLengthOfInputs);
|
||||
xTESTMaxLengthOfInputs = MathMax(
|
||||
xTESTNumberOfItemsPerTick,
|
||||
xTESTMaxLengthOfInputs);
|
||||
|
||||
//
|
||||
// Initialize Indicator/Oscillator Handlers ...
|
||||
ResetLastError();
|
||||
|
||||
//
|
||||
// XTM Init ...
|
||||
result = XTMInitHandlers();
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Release Indicators ...
|
||||
void XTESTReleaseIndicators()
|
||||
{
|
||||
//
|
||||
// Release Handlers ...
|
||||
XTMReleaseHandlers();
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Reading Buffers ...
|
||||
void XTESTHandleReadingBuffers()
|
||||
{
|
||||
//
|
||||
// Reading Calculated Bars for a Simple Handler ...
|
||||
xTESTCalculatedBars = BarsCalculated(xTMHandler);
|
||||
|
||||
//
|
||||
// Read Indicators Buffers ...
|
||||
XTMReadBuffers(xTESTNumberOfItemsPerTick);
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
// START Combination Functions ...
|
||||
//
|
||||
//
|
||||
|
||||
//
|
||||
// Can Open Long Trade ...
|
||||
// this is a Function will Check all market Conditions and get all
|
||||
// Strategy requirement Verifications for Long Signals ...
|
||||
bool XTESTCanOpenLongTrade()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result = XTMHasLongConditions(xTESTNumberOfItemsPerTick);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Can Close Long Trade ...
|
||||
// this is a Function will Check all market Conditions and get all
|
||||
// Strategy requirement Verifications for Long Signals ...
|
||||
bool XTESTCanCloseLongTrade()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// TODO: Check Market Conditions for Close Long Trades ...
|
||||
// result = XTESTHasLongCloseConditions(xTESTNumberOfItemsPerTick);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Can Open Short Trade ...
|
||||
// this is a Function will Check all market Conditions and get all
|
||||
// Strategy requirement Verifications for Short Signals ...
|
||||
bool XTESTCanOpenShortTrade()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result = XTMHasShortConditions(xTESTNumberOfItemsPerTick);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Can Close Short Trade ...
|
||||
// this is a Function will Check all market Conditions and get all
|
||||
// Strategy requirement Verifications for Short Signals ...
|
||||
bool XTESTCanCloseShortTrade()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// TODO: Check Market Conditions for Close Short Trades ...
|
||||
// result = XTESTHasShortCloseConditions(xTESTNumberOfItemsPerTick);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
// END Combination Functions ...
|
||||
//
|
||||
//
|
||||
Binary file not shown.
@@ -0,0 +1,65 @@
|
||||
///////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 XTEST Signal Provider Library Inputs
|
||||
// -----------------------------------------------------------
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
input group "XTEST Provider";
|
||||
|
||||
//
|
||||
input group "XTEST Common";
|
||||
input bool xTESTEnableProvider = true; // Enable Provider
|
||||
input bool xTESTEnableAlerts = false; // Enable Events Alert
|
||||
input int xTESTNumberOfItemsPerTick = 110; // Number Of items Readed In Each Tick
|
||||
|
||||
//
|
||||
input group "XTEST Trader";
|
||||
input int xTESTMagicNumber = 16940567; // Trader MagicNumber
|
||||
input int xTESTSlippage = 10; // Trader Slippage
|
||||
|
||||
//
|
||||
input group "XTEST Trade Management";
|
||||
input bool xTESTUseVirtualTPSL = true; // Use Virtual TP SL
|
||||
input bool xTESTAllowLongTrades = true; // Allow Long Trades
|
||||
input bool xTESTAllowShortTrades = true; // Allow Short Trades
|
||||
|
||||
//
|
||||
input group "XTEST Risk Management";
|
||||
input int xTESTMaxAllowedTrades = 5; // Max Allowed Trades at Same Time
|
||||
input int xTESTMaxInProfitTradeAge = 0; // Max In Profit Trades Age
|
||||
input int xTESTMaxInDrawDownTradeAge = 576; // Max In DrawDown Trades Age
|
||||
input double xTESTStaticVolume = 0.01; // Static Volume
|
||||
input double xTESTVolumeMultiplier = 2; // Volume Multiplier
|
||||
input double xTESTMinRewardPerTradeInPips = 2; // Min Reward Per Trade In Pips
|
||||
input double xTESTFreeMarginFactorForOpenTrades = 0.5; // Minimum Free Marging for Open Trades
|
||||
input double xTESTBalanceFactorForOpenTrades = 0.5; // Minimum Balance for Open Trades
|
||||
input int xTESTMaxAllowedSupportTrades = 5; // Max Allowed Support Trade
|
||||
input int xTESTSupportTradesPriceDistanceInPips = 5; // Distance Price to Open Support Trade
|
||||
|
||||
//
|
||||
// Validate Inputs before Initialization ...
|
||||
bool XTESTValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// TODO: Fix this ...
|
||||
result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,678 @@
|
||||
////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 XTEST Signal Provider Library
|
||||
// --------------------------------------------------------
|
||||
// Name: XTESTSignalProvider
|
||||
// Description: XTEST based signal provider ...
|
||||
//
|
||||
//
|
||||
// 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
|
||||
|
||||
#define XTESTProviderName "XTEST"
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
|
||||
#include "x-saherelm.xtest.provider.inputs.lib.mq5";
|
||||
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Include Common Library ...
|
||||
#include "x-saherelm.common.lib.mq5"
|
||||
|
||||
//
|
||||
// Include Logger Library ...
|
||||
#include "x-saherelm.log.lib.mq5"
|
||||
|
||||
//
|
||||
// Include Alert Library ...
|
||||
#include "x-saherelm.alert.lib.mq5"
|
||||
|
||||
//
|
||||
// Include Draw Library ...
|
||||
#include "x-saherelm.draw.lib.mq5"
|
||||
|
||||
//
|
||||
// Include Class Libraries ...
|
||||
#include "x-saherelm.class.lib.mq5"
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
XCTrade *xTESTTrader;
|
||||
XCAccountInfo xTESTAccountInfo;
|
||||
|
||||
//
|
||||
#include "x-saherelm.xtest.provider.indicators.lib.mq5"
|
||||
|
||||
//
|
||||
double xTESTDeposit = 0;
|
||||
double xTESTBalance = 0;
|
||||
double xTESTFreeMargin = 0;
|
||||
double xTESTMinRewardPerTrade = 0;
|
||||
double xTESTFreeMarginForOpenTrades = 0;
|
||||
double xTESTBalanceForOpenTrades = 0;
|
||||
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Provided Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// Initial Library if required ...
|
||||
bool XTESTInitSignalProviderLibrary()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Inputs ...
|
||||
result = XTESTValidateInputs();
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Init Indicators ...
|
||||
result = XTESTInitIndicators();
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Account Deposit ...
|
||||
xTESTDeposit = xTESTAccountInfo.GetInitialBalance();
|
||||
|
||||
//
|
||||
// Calculate xTESTMinRewardPerTrade ...
|
||||
if (xTESTMinRewardPerTradeInPips > 0)
|
||||
{
|
||||
xTESTMinRewardPerTrade = PipsToPrice(xTESTMinRewardPerTradeInPips);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Min Balance for Trades ...
|
||||
if (xTESTBalanceFactorForOpenTrades > 0)
|
||||
{
|
||||
xTESTBalanceForOpenTrades = xTESTBalanceFactorForOpenTrades * xTESTDeposit;
|
||||
}
|
||||
|
||||
//
|
||||
// Claculate Min Free Marging for Trades ...
|
||||
if (xTESTFreeMarginFactorForOpenTrades > 0)
|
||||
{
|
||||
xTESTFreeMarginForOpenTrades = xTESTFreeMarginFactorForOpenTrades * xTESTDeposit;
|
||||
}
|
||||
|
||||
//
|
||||
// Make XCTrader instance ...
|
||||
xTESTTrader = new XCTrade(
|
||||
XTESTProviderName,
|
||||
_Symbol,
|
||||
xTESTSlippage,
|
||||
xTESTMagicNumber,
|
||||
xTESTMaxAllowedTrades,
|
||||
xTESTMaxAllowedSupportTrades,
|
||||
xTESTMinRewardPerTradeInPips,
|
||||
xTESTSupportTradesPriceDistanceInPips,
|
||||
xTESTFreeMarginForOpenTrades,
|
||||
xTESTBalanceForOpenTrades);
|
||||
|
||||
//
|
||||
result = true;
|
||||
|
||||
//
|
||||
// Logging State ...
|
||||
XTESTIssueInitializationSucceedAlert();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// DeInitial Library if required ...
|
||||
void XTESTDeinitSignalProviderLibrary(
|
||||
const int reason)
|
||||
{
|
||||
//
|
||||
// Release Handlers ...
|
||||
XTESTReleaseIndicators();
|
||||
|
||||
//
|
||||
// Logging State ...
|
||||
XTESTIssueDeinitializationSucceedAlert();
|
||||
}
|
||||
|
||||
//
|
||||
// this is a Globally Function which do all of
|
||||
// checkings and positions handling ...
|
||||
void XTESTSignalProviderHandleTick()
|
||||
{
|
||||
//
|
||||
// Update account Balance ...
|
||||
xTESTBalance = xTESTAccountInfo.GetBalance();
|
||||
xTESTFreeMargin = xTESTAccountInfo.GetFreeMargin();
|
||||
|
||||
//
|
||||
// Check Provider is Enable or Not ...
|
||||
if (!xTESTEnableProvider)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Reading Indicator Buffers ...
|
||||
XTESTHandleReadingBuffers();
|
||||
if (xTESTCalculatedBars < xTESTMaxLengthOfInputs)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Open Trades ...
|
||||
XTESTHandleOpenTrades();
|
||||
|
||||
//
|
||||
// Handle Close Trades ...
|
||||
XTESTHandleCloseTrades();
|
||||
|
||||
//
|
||||
// Handle Support Trades ...
|
||||
XTESTHandleSupportTrades();
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Open Trades ...
|
||||
void XTESTHandleOpenTrades()
|
||||
{
|
||||
//
|
||||
// Check Can Trade ...
|
||||
bool canTrade = xTESTAllowLongTrades || xTESTAllowShortTrades;
|
||||
if (!canTrade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Signals Exists ...
|
||||
bool hasLongSignal = XTESTCanOpenLongTrade();
|
||||
bool hasShortSignal = XTESTCanOpenShortTrade();
|
||||
if (!hasLongSignal && !hasShortSignal)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Open Long Trades ...
|
||||
if (
|
||||
//
|
||||
hasLongSignal &&
|
||||
xTESTAllowLongTrades)
|
||||
{
|
||||
//
|
||||
bool isOpened = XTESTOpenLongPosition();
|
||||
}
|
||||
|
||||
//
|
||||
// Open Short Trades ...
|
||||
if (
|
||||
//
|
||||
hasShortSignal &&
|
||||
xTESTAllowShortTrades)
|
||||
{
|
||||
//
|
||||
bool isOpened = XTESTOpenShortPosition();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Close Trades ...
|
||||
void XTESTHandleCloseTrades()
|
||||
{
|
||||
//
|
||||
// All Close Mechanism Handles Here ...
|
||||
// - Force Close;
|
||||
// - Partial Close;
|
||||
// - Risk Free Close;
|
||||
// - Virtual TP/SL Close;
|
||||
|
||||
//
|
||||
// Force Close Long Trades ...
|
||||
bool canCloseLongTrades = XTESTCanCloseLongTrade();
|
||||
if (xTESTAllowLongTrades && canCloseLongTrades)
|
||||
{
|
||||
//
|
||||
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_LONG;
|
||||
|
||||
//
|
||||
XSignal closed[];
|
||||
xTESTTrader.ClosePositionsByType(
|
||||
type,
|
||||
closed);
|
||||
|
||||
//
|
||||
bool isClosed = ArraySize(closed) > 0;
|
||||
if (isClosed)
|
||||
{
|
||||
XTESTIssueForceCloseTradesAlert(type);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Force Close Short Trades ...
|
||||
bool canCloseShortTrades = XTESTCanCloseShortTrade();
|
||||
if (xTESTAllowShortTrades && canCloseShortTrades)
|
||||
{
|
||||
//
|
||||
ENUM_X_SIGNAL_TYPE type = X_SIGNAL_SHORT;
|
||||
|
||||
//
|
||||
XSignal closed[];
|
||||
xTESTTrader.ClosePositionsByType(
|
||||
type,
|
||||
closed);
|
||||
|
||||
//
|
||||
bool isClosed = ArraySize(closed) > 0;
|
||||
if (isClosed)
|
||||
{
|
||||
XTESTIssueForceCloseTradesAlert(type);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Close Risk Free Trades ...
|
||||
// XSignal riskFrees[];
|
||||
// xTESTTrader.RiskFreeSignals(riskFrees);
|
||||
// bool isRiskFree = ArraySize(riskFrees) > 0;
|
||||
// if (isRiskFree)
|
||||
// {
|
||||
// XTESTIssueRiskFreeTradesAlert();
|
||||
// }
|
||||
|
||||
//
|
||||
// Close Virtual TP / SL Trades ...
|
||||
// if (xTESTUseVirtualTPSL)
|
||||
// {
|
||||
//
|
||||
XSignal takeProfits[];
|
||||
XSignal stopLosses[];
|
||||
|
||||
//
|
||||
xTESTTrader.HandleVirtualTPSL(
|
||||
takeProfits,
|
||||
stopLosses);
|
||||
|
||||
//
|
||||
// Take Profits ...
|
||||
int takeProfitsCount = ArraySize(takeProfits);
|
||||
if (takeProfitsCount > 0)
|
||||
{
|
||||
//
|
||||
// Do What we Want in Profits Trades ...
|
||||
}
|
||||
|
||||
//
|
||||
// Stop Losses ...
|
||||
int stopLossesCount = ArraySize(stopLosses);
|
||||
if (stopLossesCount > 0)
|
||||
{
|
||||
//
|
||||
// Do What we Want in Stop Losses Trades ...
|
||||
}
|
||||
// }
|
||||
|
||||
//
|
||||
// Close Max DrawDown Trades ...
|
||||
|
||||
//
|
||||
// Close Long Time Trades ...
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Support Trades ...
|
||||
void XTESTHandleSupportTrades()
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
// Open Long Position ...
|
||||
bool XTESTOpenLongPosition()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
double riskFreeRate = 0; // 0.75;
|
||||
double volume = XTESTCalculateVolume();
|
||||
double tp = PipsToPrice(xTESTMinRewardPerTradeInPips);
|
||||
double sl = PipsToPrice(xTESTMinRewardPerTradeInPips);
|
||||
double riskFree = 0; // PipsToPrice(xTESTMinRewardPerTradeInPips);
|
||||
|
||||
//
|
||||
bool useVirtalTPSL = true; // xTESTUseVirtualTPSL;
|
||||
|
||||
//
|
||||
XSignal signal = {};
|
||||
signal = xTESTTrader.GenerateTradeSignal(
|
||||
X_SIGNAL_LONG,
|
||||
tp,
|
||||
sl,
|
||||
volume,
|
||||
riskFree,
|
||||
riskFreeRate,
|
||||
useVirtalTPSL);
|
||||
bool isValidSignal = IsValid(
|
||||
signal,
|
||||
xTESTMagicNumber);
|
||||
if (!isValidSignal)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
int error = -1;
|
||||
result = xTESTTrader.ExecuteSignal(signal, error);
|
||||
if (result)
|
||||
{
|
||||
XTESTIssueSignalExecutionAlert(signal);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Open Short Position ...
|
||||
bool XTESTOpenShortPosition()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
// //
|
||||
// double riskFreeRate = 0; // 0.75;
|
||||
// double volume = XTESTCalculateVolume();
|
||||
// double tp = PipsToPrice(xTESTMinRewardPerTradeInPips * 3);
|
||||
// double riskFree = 0; // PipsToPrice(xTESTMinRewardPerTradeInPips);
|
||||
|
||||
// //
|
||||
// XSignal signal = {};
|
||||
// signal = xTESTTrader.GenerateTradeSignal(
|
||||
// X_SIGNAL_SHORT,
|
||||
// tp,
|
||||
// 0,
|
||||
// volume,
|
||||
// riskFree,
|
||||
// riskFreeRate,
|
||||
// false);
|
||||
// bool isValidSignal = IsValid(
|
||||
// signal,
|
||||
// xTESTMagicNumber);
|
||||
// if (!isValidSignal)
|
||||
// {
|
||||
// return result;
|
||||
// }
|
||||
|
||||
// //
|
||||
// int error = -1;
|
||||
// result = xTESTTrader.ExecuteSignal(signal, error);
|
||||
// if (result)
|
||||
// {
|
||||
// XTESTIssueSignalExecutionAlert(signal);
|
||||
// }
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Open a Support Signal ...
|
||||
bool XTESTOpenSupportTrade(
|
||||
XSignal &signal // Parent Signal
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// END Provided Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Private Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// Calculating Volume for Tradings ...
|
||||
double XTESTCalculateVolume()
|
||||
{
|
||||
//
|
||||
// Assign default Value ...
|
||||
double result = xTESTStaticVolume;
|
||||
|
||||
//
|
||||
// we can count open trades and in draw down trades
|
||||
// then do check volume based on it for gridding ...
|
||||
int openTrades = xTESTTrader.Count();
|
||||
if (openTrades == 0)
|
||||
{
|
||||
openTrades = 1;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Normalized Multiplier ...
|
||||
double multiplier = xTESTVolumeMultiplier <= 0 ? 1 : xTESTVolumeMultiplier;
|
||||
|
||||
//
|
||||
result = multiplier * openTrades * xTESTStaticVolume;
|
||||
|
||||
//
|
||||
// Normalize Volume ...
|
||||
result = NormalizeVolume(result);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Signal TP and SL ...
|
||||
XTPSL XTESTCalculateTPSL(
|
||||
ENUM_X_SIGNAL_TYPE type // Position Type
|
||||
)
|
||||
{
|
||||
//
|
||||
XTPSL result = {};
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (
|
||||
type != X_SIGNAL_LONG &&
|
||||
type != X_SIGNAL_SHORT)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
bool isLong = type == X_SIGNAL_LONG;
|
||||
|
||||
//
|
||||
double ask = GetAsk();
|
||||
double bid = GetBid();
|
||||
double spread = GetSpread();
|
||||
double entry = isLong ? ask : bid;
|
||||
|
||||
//
|
||||
// TODO: Find Risk ..
|
||||
double risk = 0;
|
||||
|
||||
//
|
||||
// Calculate Reward ...
|
||||
double reward = PipsToPrice(xTESTMinRewardPerTradeInPips) + spread;
|
||||
|
||||
//
|
||||
// Calculate TP and SL ...
|
||||
double tp = isLong ? entry + reward : entry - reward;
|
||||
double sl = 0;
|
||||
|
||||
//
|
||||
result.type = type;
|
||||
result.tp = tp;
|
||||
result.sl = sl;
|
||||
result.entry = entry;
|
||||
//
|
||||
// TODO: Calculate R2R ...
|
||||
result.r2r = 0;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
void XTESTIssueInitializationSucceedAlert()
|
||||
{
|
||||
//
|
||||
string message = "Initializion of (" + XTESTProviderName + " Provider" + " _ " + _Symbol + " _ " + EnumToString(_Period) + ") Succeeded ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueDeinitializationSucceedAlert()
|
||||
{
|
||||
//
|
||||
string message = "De Initializion of (" + XTESTProviderName + " Provider" + ") Succeeded ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueForceCloseTradesAlert(ENUM_X_SIGNAL_TYPE type)
|
||||
{
|
||||
//
|
||||
string typeStr = GetSignalType(type);
|
||||
if (StringLen(typeStr) == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
string message = "Force Close " + typeStr + " Trades ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueMaxAllowedTradesReachedAlert()
|
||||
{
|
||||
//
|
||||
string message = "reached Max Allowed Same Time Trades ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueMaxAllowedFreeMarginReachedAlert()
|
||||
{
|
||||
//
|
||||
string message = "reached FreeMarigin For Open Trades ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueMinAllowedBalanceReachedAlert()
|
||||
{
|
||||
//
|
||||
string message = "doesn't have Minimum Balance For Open Trades ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueSignalExecutionAlert(
|
||||
XSignal &signal // Executed Signal
|
||||
)
|
||||
{
|
||||
//
|
||||
XTESTIssueAlert(signal);
|
||||
}
|
||||
|
||||
void XTESTIssueSignalExecutionError(int error)
|
||||
{
|
||||
//
|
||||
string message = GetSignalExecutionError(error);
|
||||
if (StringLen(message) == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueRiskFreeTradesAlert()
|
||||
{
|
||||
//
|
||||
string message = "Make Some Trades Risk Free ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueLongInProfitTradeClosed()
|
||||
{
|
||||
//
|
||||
string message = "Closed Long Time in Profit Trades ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueLongInDrawdownTradeClosed()
|
||||
{
|
||||
//
|
||||
string message = "Closed Long Time in Profit Trades ...";
|
||||
XTESTIssueAlert(message);
|
||||
}
|
||||
|
||||
void XTESTIssueAlert(string message)
|
||||
{
|
||||
//
|
||||
if (xTESTEnableAlerts)
|
||||
{
|
||||
SendAlert(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogMessage(message);
|
||||
}
|
||||
}
|
||||
void XTESTIssueAlert(XSignal &signal)
|
||||
{
|
||||
//
|
||||
string signalStr = xTESTTrader.ToString(signal);
|
||||
XTESTIssueAlert(signalStr);
|
||||
|
||||
// //
|
||||
// if (xTESTEnableAlerts)
|
||||
// {
|
||||
// SendAlert(signal);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// LogSignal(signal);
|
||||
// }
|
||||
}
|
||||
|
||||
//
|
||||
// END Private Functions ...
|
||||
//
|
||||
@@ -0,0 +1,475 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 XTM Indicator Helper for Signal Providers
|
||||
// --------------------------------------------------------------------------
|
||||
//
|
||||
// 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
|
||||
|
||||
//
|
||||
// Includes ...
|
||||
#include "..\Libraries\x-saherelm.log.lib.mq5"
|
||||
#include "..\Libraries\x-saherelm.common.lib.mq5"
|
||||
|
||||
//
|
||||
// START Enum Definitions ...
|
||||
//
|
||||
|
||||
//
|
||||
// XTM Indicator States ...
|
||||
enum ENUM_X_XTM_STATES
|
||||
{
|
||||
X_XTM_BULLISH = 1,
|
||||
X_XTM_BEARISH = -1,
|
||||
X_XTM_NEUTURAL = 0,
|
||||
};
|
||||
|
||||
//
|
||||
// XTM Indicator Buffer Lines ...
|
||||
enum ENUM_X_XTM_BUFFER_LINES
|
||||
{
|
||||
X_XTM_MA_LINE = 0,
|
||||
X_XTM_MC_COLOR_LINE = 1,
|
||||
X_XTM_STATE_LINE = 2,
|
||||
};
|
||||
|
||||
//
|
||||
// END Enum Definitions ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
|
||||
input group "XTM Indicator";
|
||||
input int xTMMaPeriod = 14; // Period
|
||||
input int xTMMaShift = 0; // Shift
|
||||
input ENUM_MA_METHOD xTMMaMethod = MODE_SMA; // Method
|
||||
input ENUM_APPLIED_PRICE xTMMaAppliedTo = PRICE_WEIGHTED; // Applied To
|
||||
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Definitions ...
|
||||
//
|
||||
|
||||
int xTMHandler = INVALID_HANDLE;
|
||||
double xTMMaBuffer[];
|
||||
double xTMStateBuffer[];
|
||||
|
||||
//
|
||||
// END Definitions ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Handler Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// Init Indicators ...
|
||||
bool XTMInitHandlers()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Initialize Indicator/Oscillator Handlers ...
|
||||
ResetLastError();
|
||||
|
||||
//
|
||||
// XTM Handler ...
|
||||
ArraySetAsSeries(xTMMaBuffer, true);
|
||||
ArraySetAsSeries(xTMStateBuffer, true);
|
||||
xTMHandler = iCustom(
|
||||
_Symbol,
|
||||
_Period,
|
||||
"x-saherelm.xtm.indicator",
|
||||
//
|
||||
// Inputs ...
|
||||
xTMMaPeriod,
|
||||
xTMMaShift,
|
||||
xTMMaMethod,
|
||||
xTMMaAppliedTo);
|
||||
if (xTMHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
LogMessage("failed to Initialize XTM Indicator: " + (string)GetLastError());
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// xTMOHandler ...
|
||||
// ArraySetAsSeries(xTMOBuffer, true);
|
||||
// xTMOHandler = iCustom(
|
||||
// _Symbol,
|
||||
// _Period,
|
||||
// "x-saherelm.xma.indicator",
|
||||
// //
|
||||
// // Inputs ...
|
||||
// xTMMaPeriod,
|
||||
// xTMMaShift,
|
||||
// MODE_SMA,
|
||||
// PRICE_OPEN);
|
||||
// if (xTMOHandler == INVALID_HANDLE)
|
||||
// {
|
||||
// //
|
||||
// LogMessage("failed to Initialize XMA Indicator: " + (string)GetLastError());
|
||||
// return result;
|
||||
// }
|
||||
|
||||
//
|
||||
result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Release Indicators ...
|
||||
void XTMReleaseHandlers()
|
||||
{
|
||||
IndicatorRelease(xTMHandler);
|
||||
// IndicatorRelease(xTMOHandler);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Reading Buffers ...
|
||||
void XTMReadBuffers(
|
||||
int xTMItemsPerTick // Number Of Reading Buffer Items Per Tick
|
||||
)
|
||||
{
|
||||
//
|
||||
// Ma Buffer ...
|
||||
CopyBuffer(
|
||||
xTMHandler,
|
||||
X_XTM_MA_LINE,
|
||||
0,
|
||||
xTMItemsPerTick,
|
||||
xTMMaBuffer);
|
||||
|
||||
//
|
||||
// State Buffer ...
|
||||
CopyBuffer(
|
||||
xTMHandler,
|
||||
X_XTM_STATE_LINE,
|
||||
0,
|
||||
xTMItemsPerTick,
|
||||
xTMStateBuffer);
|
||||
|
||||
//
|
||||
// XTMO Buffer ...
|
||||
// CopyBuffer(
|
||||
// xTMOHandler,
|
||||
// 0,
|
||||
// 0,
|
||||
// xTMItemsPerTick,
|
||||
// xTMOBuffer);
|
||||
}
|
||||
|
||||
//
|
||||
// END Handler Funcions ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Signal Related Conditions ...
|
||||
//
|
||||
|
||||
//
|
||||
// XTM Long Conditions ...
|
||||
bool XTMHasLongConditions(
|
||||
int xTMItemsPerTick // Number Of Reading Buffer Items Per Tick
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
bool isPeak = XTMIsPeak();
|
||||
bool isVale = XTMIsVale();
|
||||
|
||||
//
|
||||
double minValue = GetMinValue(
|
||||
xTMItemsPerTick,
|
||||
xTMMaBuffer);
|
||||
double maxValue = GetMaxValue(
|
||||
xTMItemsPerTick,
|
||||
xTMMaBuffer);
|
||||
double currentValue = xTMMaBuffer[1];
|
||||
bool isTMMinMaxPassed =
|
||||
currentValue < maxValue && currentValue > minValue;
|
||||
|
||||
//
|
||||
bool isInPeak = IsInPeak(
|
||||
15,
|
||||
xTMMaBuffer);
|
||||
|
||||
//
|
||||
bool isSharpChanged = IsSharpChanged(
|
||||
15,
|
||||
1,
|
||||
xTMMaBuffer);
|
||||
bool isCandleSharpChanged = IsSharpChanged(
|
||||
15,
|
||||
1,
|
||||
PRICE_CLOSE);
|
||||
|
||||
//
|
||||
double entry = GetEntry(X_SIGNAL_LONG);
|
||||
double maxPrice = GetHighestHigh(
|
||||
7,
|
||||
0);
|
||||
double minPrice = GetLowestLow(
|
||||
7,
|
||||
0);
|
||||
double priceDelta = maxPrice - minPrice;
|
||||
double priceStep = priceDelta / 7;
|
||||
|
||||
//
|
||||
bool isPricePassed =
|
||||
entry < maxPrice &&
|
||||
maxPrice - entry < (priceDelta / 4) * 3;
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
isVale
|
||||
//
|
||||
&&
|
||||
//
|
||||
!isInPeak
|
||||
//
|
||||
&&
|
||||
//
|
||||
!isSharpChanged
|
||||
//
|
||||
&&
|
||||
//
|
||||
isPricePassed
|
||||
//
|
||||
&&
|
||||
//
|
||||
!isCandleSharpChanged
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// XTM Short Conditions ...
|
||||
bool XTMHasShortConditions(
|
||||
int xTMItemsPerTick // Number Of Reading Buffer Items Per Tick
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
bool isPeak = XTMIsPeak();
|
||||
bool isValue = XTMIsVale();
|
||||
|
||||
//
|
||||
double minValue = GetMinValue(
|
||||
xTMItemsPerTick,
|
||||
xTMMaBuffer);
|
||||
double maxValue = GetMaxValue(
|
||||
xTMItemsPerTick,
|
||||
xTMMaBuffer);
|
||||
double currentValue = xTMMaBuffer[1];
|
||||
bool isTMMinMaxPassed =
|
||||
currentValue < maxValue && currentValue > minValue;
|
||||
|
||||
//
|
||||
bool isInPeak = IsInPeak(
|
||||
15,
|
||||
xTMMaBuffer);
|
||||
bool isInVale = IsInVale(
|
||||
15,
|
||||
xTMMaBuffer);
|
||||
|
||||
//
|
||||
bool isSharpChanged = IsSharpChanged(
|
||||
15,
|
||||
1,
|
||||
xTMMaBuffer);
|
||||
bool isCandleSharpChanged = IsSharpChanged(
|
||||
15,
|
||||
1,
|
||||
PRICE_CLOSE);
|
||||
|
||||
//
|
||||
double entry = GetEntry(X_SIGNAL_SHORT);
|
||||
double maxPrice = GetHighestHigh(
|
||||
7,
|
||||
0);
|
||||
double minPrice = GetLowestLow(
|
||||
7,
|
||||
0);
|
||||
double priceDelta = maxPrice - minPrice;
|
||||
double priceStep = priceDelta / 7;
|
||||
|
||||
//
|
||||
bool isPricePassed =
|
||||
entry > minPrice &&
|
||||
minPrice + entry > (priceDelta / 4);
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
isPeak
|
||||
//
|
||||
&&
|
||||
//
|
||||
!isInVale
|
||||
//
|
||||
&&
|
||||
//
|
||||
!isSharpChanged
|
||||
//
|
||||
&&
|
||||
//
|
||||
isPricePassed
|
||||
//
|
||||
&&
|
||||
//
|
||||
!isCandleSharpChanged
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// END Signal Related Conditions ...
|
||||
//
|
||||
|
||||
//
|
||||
// START XTM Custom Functions ...
|
||||
//
|
||||
|
||||
//
|
||||
// Find Peak based On this Indicator ...
|
||||
bool XTMIsPeak()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Check Is Vale ...
|
||||
|
||||
//
|
||||
double currentValue = xTMMaBuffer[1];
|
||||
|
||||
//
|
||||
// Must Decreasing ...
|
||||
bool isIncreasing = IsIncreasing(
|
||||
15,
|
||||
3,
|
||||
xTMMaBuffer);
|
||||
|
||||
//
|
||||
// Must Increasing ...
|
||||
bool isDecreasing = IsDecreasing(
|
||||
3,
|
||||
1,
|
||||
xTMMaBuffer);
|
||||
|
||||
//
|
||||
// Find State of First and Last ...
|
||||
double stateValue1 = xTMStateBuffer[1];
|
||||
double stateValue2 = xTMStateBuffer[2];
|
||||
bool isStatePassed =
|
||||
stateValue2 == X_XTM_NEUTURAL &&
|
||||
stateValue1 == X_XTM_BEARISH;
|
||||
|
||||
//
|
||||
// Calculate Result ...
|
||||
result =
|
||||
//
|
||||
isIncreasing
|
||||
//
|
||||
&&
|
||||
//
|
||||
isDecreasing
|
||||
//
|
||||
&&
|
||||
//
|
||||
isStatePassed
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Find Vale based On this Indicator ...
|
||||
bool XTMIsVale()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Check Is Vale ...
|
||||
|
||||
//
|
||||
double currentValue = xTMMaBuffer[1];
|
||||
|
||||
//
|
||||
// Must Decreasing ...
|
||||
bool isIncreasing = IsIncreasing(
|
||||
3,
|
||||
1,
|
||||
xTMMaBuffer);
|
||||
|
||||
//
|
||||
// Must Increasing ...
|
||||
bool isDecreasing = IsDecreasing(
|
||||
15,
|
||||
3,
|
||||
xTMMaBuffer);
|
||||
|
||||
//
|
||||
// Find State of First and Last ...
|
||||
double stateValue1 = xTMStateBuffer[1];
|
||||
double stateValue2 = xTMStateBuffer[2];
|
||||
bool isStatePassed =
|
||||
stateValue2 == X_XTM_NEUTURAL &&
|
||||
stateValue1 == X_XTM_BULLISH;
|
||||
|
||||
//
|
||||
// Calculate Result ...
|
||||
result =
|
||||
//
|
||||
isIncreasing
|
||||
//
|
||||
&&
|
||||
//
|
||||
isDecreasing
|
||||
//
|
||||
&&
|
||||
//
|
||||
isStatePassed
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// END XTM Custom Functions ...
|
||||
//
|
||||
@@ -0,0 +1,349 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Candle Indicator
|
||||
// ---------------------------------------------
|
||||
// Name: XTM
|
||||
// Description: Trend Magic Indicator ...
|
||||
//
|
||||
//
|
||||
// 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 XTM Indicator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XTM"
|
||||
|
||||
//
|
||||
// XTM Indicator States ...
|
||||
enum ENUM_X_XTM_STATES
|
||||
{
|
||||
X_XTM_BULLISH = 1,
|
||||
X_XTM_BEARISH = -1,
|
||||
X_XTM_NEUTURAL = 0,
|
||||
};
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int maPeriod = 14; // Period
|
||||
input int maShift = 0; // Shift
|
||||
input ENUM_MA_METHOD maMethod = MODE_SMA; // Method
|
||||
input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // Applied To
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_chart_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 3
|
||||
#property indicator_plots 1
|
||||
|
||||
//
|
||||
// maBuffer ...
|
||||
#define maBufferIndex 0
|
||||
#define maColorBufferIndex 1
|
||||
#define stateBufferIndex 2
|
||||
|
||||
double maBuffer[];
|
||||
double maColorBuffer[];
|
||||
double stateBuffer[];
|
||||
|
||||
#property indicator_label1 "XTM"
|
||||
#property indicator_type1 DRAW_COLOR_LINE
|
||||
#property indicator_color1 clrLime, clrGray, clrRed
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
int maHandler = INVALID_HANDLE;
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// Initializing MA Handler ...
|
||||
maHandler = iMA(
|
||||
_Symbol,
|
||||
_Period,
|
||||
maPeriod,
|
||||
maShift,
|
||||
maMethod,
|
||||
maAppliedTo);
|
||||
if (maHandler == INVALID_HANDLE)
|
||||
{
|
||||
//
|
||||
string message = "Error Initializing Ma Handler ...";
|
||||
LogMessage(message);
|
||||
|
||||
//
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Index Buffers ...
|
||||
DefineBuffers();
|
||||
|
||||
//
|
||||
// Set Indicator ShortName ...
|
||||
SetIndicatorName();
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
}
|
||||
|
||||
//
|
||||
// Calculations ...
|
||||
//
|
||||
// Calculating what we want ...
|
||||
int OnCalculate(
|
||||
//
|
||||
// total Candles on chart ...
|
||||
const int rates_total,
|
||||
//
|
||||
// total calculated Candles on charts ...
|
||||
const int prev_calculated,
|
||||
//
|
||||
// history of Candles Open Time ...
|
||||
const datetime &time[],
|
||||
//
|
||||
// history of Candles Open Price ...
|
||||
const double &open[],
|
||||
//
|
||||
// history of Candles High Price ...
|
||||
const double &high[],
|
||||
//
|
||||
// history of Candles Low Price ...
|
||||
const double &low[],
|
||||
//
|
||||
// history of Candles Close Price ...
|
||||
const double &close[],
|
||||
//
|
||||
// history of Tick Volumes on Candle ...
|
||||
const long &tick_volume[],
|
||||
//
|
||||
// history of Trade Volumes ...
|
||||
const long &volume[],
|
||||
//
|
||||
// history of Candles Spread Price ...
|
||||
const int &spread[])
|
||||
{
|
||||
//
|
||||
// Prepare Buffers ...
|
||||
ArraySetAsSeries(time, true);
|
||||
ArraySetAsSeries(open, true);
|
||||
ArraySetAsSeries(high, true);
|
||||
ArraySetAsSeries(low, true);
|
||||
ArraySetAsSeries(close, true);
|
||||
ArraySetAsSeries(tick_volume, true);
|
||||
ArraySetAsSeries(volume, true);
|
||||
ArraySetAsSeries(spread, true);
|
||||
|
||||
//
|
||||
// this counts Available Bars ...
|
||||
int limit;
|
||||
|
||||
//
|
||||
// because in some cases we may have more than one input for
|
||||
// calculation and we must prevent any calculation
|
||||
// untill we pass the biggest input length, here we get max Input length
|
||||
// and then wait until pass it ...
|
||||
int maxLength = MathMax(0, maPeriod);
|
||||
|
||||
//
|
||||
// Check Calculated Bars ...
|
||||
int maCalculatedBars = BarsCalculated(maHandler);
|
||||
if (maCalculatedBars < maxLength)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
|
||||
|
||||
//
|
||||
// Buffers Copy ...
|
||||
int copiedMas = CopyBuffer(maHandler, 0, 0, limit, maBuffer);
|
||||
if (copiedMas <= 0)
|
||||
{
|
||||
return prev_calculated;
|
||||
}
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
// Do Buffers Calculation ...
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (maPeriod >= 2)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string maBufferLabel = "XTM " + "(" + (string)maPeriod + ")";
|
||||
|
||||
//
|
||||
// Ma Buffer ...
|
||||
ArraySetAsSeries(maBuffer, true);
|
||||
SetIndexBuffer(maBufferIndex, maBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetString(maBufferIndex, PLOT_LABEL, maBufferLabel);
|
||||
|
||||
//
|
||||
// Ma Color Buffer ...
|
||||
ArraySetAsSeries(maColorBuffer, true);
|
||||
SetIndexBuffer(maColorBufferIndex, maColorBuffer, INDICATOR_COLOR_INDEX);
|
||||
|
||||
//
|
||||
// State Buffer ...
|
||||
ArraySetAsSeries(stateBuffer, true);
|
||||
SetIndexBuffer(stateBufferIndex, stateBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Indicator Short Name and also we can define Buffers Labels ...
|
||||
void SetIndicatorName()
|
||||
{
|
||||
//
|
||||
string indicatorShortName = "";
|
||||
StringConcatenate(indicatorShortName, ShortName, " ", "(", maPeriod, ")");
|
||||
|
||||
//
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// Requirements ...
|
||||
XOHCL candle = GetCandle(bar_index);
|
||||
|
||||
//
|
||||
double maValue = maBuffer[bar_index];
|
||||
|
||||
//
|
||||
double lineColorIndex =
|
||||
maValue < candle.low ? 0 : maValue > candle.high ? 2
|
||||
: 1;
|
||||
|
||||
//
|
||||
// Define State Value ...
|
||||
// lineColorIndex == 0 => Bullish => 1;
|
||||
// lineColorIndex == 1 => Neutural => 0;
|
||||
// lineColorIndex == 2 => Bearish => -1;
|
||||
double stateValue =
|
||||
lineColorIndex == 0 ? X_XTM_BULLISH : lineColorIndex == 2 ? X_XTM_BEARISH
|
||||
: X_XTM_NEUTURAL;
|
||||
|
||||
//
|
||||
// Set Ma Buffer Color Index ...
|
||||
maColorBuffer[bar_index] = lineColorIndex;
|
||||
|
||||
//
|
||||
// Set State Buffer Value ...
|
||||
stateBuffer[bar_index] = stateValue;
|
||||
}
|
||||
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
Reference in New Issue
Block a user