last works on XTEST EA and Compile and Packed for Test purposes ...
This commit is contained in:
@@ -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 ...
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user