Files
MQL4Data/Bkp/Used/14020419/x-saherelm.signal.lib.mq4
T
2024-01-25 04:05:58 +03:30

2523 lines
68 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL4 Signal Draw Global Library
// ---------------------------------------------------
// saherelm useful tools and definitions ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
#property library
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://www.saherelm.ir"
#property version "1.00"
#property strict
//
// Includes Logger library ...
#include "../Libraries/x-saherelm.lib.mq4"
//
// Includes Models library ...
#include "../Libraries/x-saherelm.models.lib.mq4"
//
// Includes Drawing library ...
#include "../Libraries/x-saherelm.draw.lib.mq4"
//
// START Global Requirement Functions ...
//
//
static int totalSignals = 0;
static int totalLongSignals = 0;
static int totalShortSignals = 0;
//
static double initialBalance = 0;
//
static int countedBars = 0;
static bool isNewBar = false;
static bool isNewDay = false;
//
static XMAState xmaStates[];
static XOSCState oscStates[];
static XMKTState xmktStates[];
static XSHPDState shpdStates[];
//
// Retrieve State ...
XOSCState GetOSCState(
const int bar_index,
const int foLen,
const double foMul,
const int slLen,
const double slMul
) {
//
// Define Trend Direction Related Values ...
//
XOSCState result = {};
//
// START Extract Data from Oscillator ...
//
//
// Fast OSC ...
double fastOSC = iCustom(
_Symbol,
_Period,
oscIndicatorName,
foLen,
foMul,
slLen,
slMul,
0, // Fast OSC Buffer Index ...
bar_index
);
result.fastOSC = fastOSC;
//
// Slow OSC ...
double slowOSC = iCustom(
_Symbol,
_Period,
oscIndicatorName,
foLen,
foMul,
slLen,
slMul,
1, // Slow OSC Buffer Index ...
bar_index
);
result.slowOSC = slowOSC;
//
// Power OSC ...
double powerOSC = iCustom(
_Symbol,
_Period,
oscIndicatorName,
foLen,
foMul,
slLen,
slMul,
2, // Power OSC Buffer Index ...
bar_index
);
result.powerOSC = powerOSC;
//
// Var OSC ...
double varOSC = iCustom(
_Symbol,
_Period,
oscIndicatorName,
foLen,
foMul,
slLen,
slMul,
3, // Var OSC Buffer Index ...
bar_index
);
result.varOSC = varOSC;
//
// Tan FS OSC ...
double tanFSOSC = iCustom(
_Symbol,
_Period,
oscIndicatorName,
foLen,
foMul,
slLen,
slMul,
4, // Tan F/S OSC Buffer Index ...
bar_index
);
result.tanFSOSC = tanFSOSC;
//
// END Extract Data from Oscillator ...
//
//
return result;
}
//
// Check Market Ranging or not based on OSC ...
bool IsOSCMarketRanging(
//
const int bar_index, // Bar Index ...
const int marketLen, // Market Length ...
//
const double rangingThreshold, // Detect Ranging based On Power OSC ...
//
// OSC Specifications ...
const int foLen, // Fast OSC Length ...
const double foMul, // Fast OSC Multiplier ...
const int slLen, // Slow OSC Length ...
const double slMul // Slow OSC Multiplier ...
) {
//
bool result = false;
//
// Detect Buffer Size ...
const int bufferSize = marketLen + bar_index;
double powerOSCBuffer[];
ArraySetAsSeries(
powerOSCBuffer,
true
);
ArrayResize(
powerOSCBuffer,
bufferSize
);
//
// Loop through Market for Extract PowerOSC from OSC Indicator ...
for (int i = bufferSize - 1; i >= 0; i--) {
//
// Retrieve OSC State at Specific Bar Index ...
XOSCState state = GetOSCState(
i,
foLen,
foMul,
slLen,
slMul
);
//
powerOSCBuffer[i] = state.powerOSC;
}
//
// now we have to extract min and max value of Splitted Buffer ...
XMinMax minMax = GetBufferMinMax(powerOSCBuffer);
//
// now Check Min and Max Direction and Compare them with Ranging Value ...
result = MathAbs(minMax.min) > rangingThreshold
|| MathAbs(minMax.max) > rangingThreshold;
//
return result;
}
//
// Retrieve and Calculate XMA State ...
XMAState GetXMAState(
//
// Market Specification Inpouts ...
const int bar_index, // Bar Index ...
const int marketLen, // Market Length ...
const double stp, // SAR Step ...
const double maxm, // SAR Maximum ...
//
// XMA Inputs ...
const int scFastMul, // Short Cycle Fast Multiplier ...
const int scSlowMul, // Short Cycle Slow Multiplier ...
const int mcFastMul, // Medium Cycle Fast Multiplier ...
const int mcSlowMul, // Medium Cycle Slow Multiplier ...
const int lcFastMul, // Long Cycle Fast Multiplier ...
const int lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
const bool showPSr, // Show Parabolic Sar
const bool showMMiddle, // Show Market Middle ...
const bool showSC, // Show Short Cycle
const bool showSCCrossLines, // Show Short Cycle Vertical Lines
const bool showMC, // Show Medium Cycle
const bool showMCCrossLines, // Show Medium Cycle Vertical Lines
const bool showLC, // Show Long Cycle
const bool showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
const color pSrColor, // Parabolic Sar Color
const color scFastColor, // Short Cycle Fast Color ...
const color scSlowColor, // Short Cycle Slow Color ...
const color mcFastColor, // Medium Cycle Fast Color ...
const color mcSlowColor, // Medium Cycle Slow Color ...
const color lcFastColor, // Medium Cycle Fast Color ...
const color lcSlowColor // Medium Cycle Slow Color ...
) {
//
// Temp Result ...
XMAState result = {};
//
// START Reading Buffers ...
//
//
// Define BufferIndexes ...
int scFastBufIndex = 0;
int scSlowBufIndex = 1;
int mcFastBufIndex = 2;
int mcSlowBufIndex = 3;
int lcFastBufIndex = 4;
int lcSlowBufIndex = 5;
int midBufIndex = 6;
int pSarBufIndex = 7;
//
// START Short Cycle ...
//
//
// Short Cycle Fast ...
double scFast = iCustom(
_Symbol,
_Period,
xmaIndicatorName,
//
// XMA Inputs ...
//
// Market Specification Inpouts ...
marketLen, // Market Length ...
stp, // PSAR Step ...
maxm, // PSAR Maximum ...
scFastMul, // Short Cycle Fast Multiplier ...
scSlowMul, // Short Cycle Slow Multiplier ...
mcFastMul, // Medium Cycle Fast Multiplier ...
mcSlowMul, // Medium Cycle Slow Multiplier ...
lcFastMul, // Long Cycle Fast Multiplier ...
lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
showPSr, // Show Parabolic SAR ...
showMMiddle, // Show Market Middle ...
showSC, // Show Short Cycle
showSCCrossLines, // Show Short Cycle Vertical Lines
showMC, // Show Medium Cycle
showMCCrossLines, // Show Medium Cycle Vertical Lines
showLC, // Show Long Cycle
showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
pSrColor, // Parabolic Sar Color ...
scFastColor, // Short Cycle Fast Color ...
scSlowColor, // Short Cycle Slow Color ...
mcFastColor, // Medium Cycle Fast Color ...
mcSlowColor, // Medium Cycle Slow Color ...
lcFastColor, // Medium Cycle Fast Color ...
lcSlowColor, // Medium Cycle Slow Color ...
//
scFastBufIndex, // Buffer Index ...
bar_index
);
result.scFast = scFast;
//
// Short Cycle Slow ...
double scSlow = iCustom(
_Symbol,
_Period,
xmaIndicatorName,
//
// XMA Inputs ...
//
// Market Specification Inpouts ...
marketLen, // Market Length ...
stp, // PSAR Step ...
maxm, // PSAR Maximum ...
scFastMul, // Short Cycle Fast Multiplier ...
scSlowMul, // Short Cycle Slow Multiplier ...
mcFastMul, // Medium Cycle Fast Multiplier ...
mcSlowMul, // Medium Cycle Slow Multiplier ...
lcFastMul, // Long Cycle Fast Multiplier ...
lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
showPSr, // Show Parabolic SAR ...
showMMiddle, // Show Market Middle ...
showSC, // Show Short Cycle
showSCCrossLines, // Show Short Cycle Vertical Lines
showMC, // Show Medium Cycle
showMCCrossLines, // Show Medium Cycle Vertical Lines
showLC, // Show Long Cycle
showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
pSrColor, // Parabolic Sar Color ...
scFastColor, // Short Cycle Fast Color ...
scSlowColor, // Short Cycle Slow Color ...
mcFastColor, // Medium Cycle Fast Color ...
mcSlowColor, // Medium Cycle Slow Color ...
lcFastColor, // Medium Cycle Fast Color ...
lcSlowColor, // Medium Cycle Slow Color ...
//
scSlowBufIndex, // Buffer Index ...
bar_index
);
result.scSlow = scSlow;
//
// END Short Cycle ...
//
//
// START Medium Cycle ...
//
//
// Medium Cycle Fast ...
double mcFast = iCustom(
_Symbol,
_Period,
xmaIndicatorName,
//
// XMA Inputs ...
//
// Market Specification Inpouts ...
marketLen, // Market Length ...
stp, // PSAR Step ...
maxm, // PSAR Maximum ...
scFastMul, // Short Cycle Fast Multiplier ...
scSlowMul, // Short Cycle Slow Multiplier ...
mcFastMul, // Medium Cycle Fast Multiplier ...
mcSlowMul, // Medium Cycle Slow Multiplier ...
lcFastMul, // Long Cycle Fast Multiplier ...
lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
showPSr, // Show Parabolic SAR ...
showMMiddle, // Show Market Middle ...
showSC, // Show Short Cycle
showSCCrossLines, // Show Short Cycle Vertical Lines
showMC, // Show Medium Cycle
showMCCrossLines, // Show Medium Cycle Vertical Lines
showLC, // Show Long Cycle
showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
pSrColor, // Parabolic Sar Color ...
scFastColor, // Short Cycle Fast Color ...
scSlowColor, // Short Cycle Slow Color ...
mcFastColor, // Medium Cycle Fast Color ...
mcSlowColor, // Medium Cycle Slow Color ...
lcFastColor, // Medium Cycle Fast Color ...
lcSlowColor, // Medium Cycle Slow Color ...
//
mcFastBufIndex, // Buffer Index ...
bar_index
);
result.mcFast = mcFast;
//
// Medium Cycle Slow ...
double mcSlow = iCustom(
_Symbol,
_Period,
xmaIndicatorName,
//
// XMA Inputs ...
//
// Market Specification Inpouts ...
marketLen, // Market Length ...
stp, // PSAR Step ...
maxm, // PSAR Maximum ...
scFastMul, // Short Cycle Fast Multiplier ...
scSlowMul, // Short Cycle Slow Multiplier ...
mcFastMul, // Medium Cycle Fast Multiplier ...
mcSlowMul, // Medium Cycle Slow Multiplier ...
lcFastMul, // Long Cycle Fast Multiplier ...
lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
showPSr, // Show Parabolic SAR ...
showMMiddle, // Show Market Middle ...
showSC, // Show Short Cycle
showSCCrossLines, // Show Short Cycle Vertical Lines
showMC, // Show Medium Cycle
showMCCrossLines, // Show Medium Cycle Vertical Lines
showLC, // Show Long Cycle
showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
pSrColor, // Parabolic Sar Color ...
scFastColor, // Short Cycle Fast Color ...
scSlowColor, // Short Cycle Slow Color ...
mcFastColor, // Medium Cycle Fast Color ...
mcSlowColor, // Medium Cycle Slow Color ...
lcFastColor, // Medium Cycle Fast Color ...
lcSlowColor, // Medium Cycle Slow Color ...
//
mcSlowBufIndex, // Buffer Index ...
bar_index
);
result.mcSlow = mcSlow;
//
// END Medium Cycle ...
//
//
// START Long Cycle ...
//
//
// Long Cycle Fast ...
double lcFast = iCustom(
_Symbol,
_Period,
xmaIndicatorName,
//
// XMA Inputs ...
//
// Market Specification Inpouts ...
marketLen, // Market Length ...
stp, // PSAR Step ...
maxm, // PSAR Maximum ...
scFastMul, // Short Cycle Fast Multiplier ...
scSlowMul, // Short Cycle Slow Multiplier ...
mcFastMul, // Medium Cycle Fast Multiplier ...
mcSlowMul, // Medium Cycle Slow Multiplier ...
lcFastMul, // Long Cycle Fast Multiplier ...
lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
showPSr, // Show Parabolic SAR ...
showMMiddle, // Show Market Middle ...
showSC, // Show Short Cycle
showSCCrossLines, // Show Short Cycle Vertical Lines
showMC, // Show Medium Cycle
showMCCrossLines, // Show Medium Cycle Vertical Lines
showLC, // Show Long Cycle
showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
pSrColor, // Parabolic Sar Color ...
scFastColor, // Short Cycle Fast Color ...
scSlowColor, // Short Cycle Slow Color ...
mcFastColor, // Medium Cycle Fast Color ...
mcSlowColor, // Medium Cycle Slow Color ...
lcFastColor, // Medium Cycle Fast Color ...
lcSlowColor, // Medium Cycle Slow Color ...
//
lcFastBufIndex, // Buffer Index ...
bar_index
);
result.lcFast = lcFast;
//
// Long Cycle Slow ...
double lcSlow = iCustom(
_Symbol,
_Period,
xmaIndicatorName,
//
// XMA Inputs ...
//
// Market Specification Inpouts ...
marketLen, // Market Length ...
stp, // PSAR Step ...
maxm, // PSAR Maximum ...
scFastMul, // Short Cycle Fast Multiplier ...
scSlowMul, // Short Cycle Slow Multiplier ...
mcFastMul, // Medium Cycle Fast Multiplier ...
mcSlowMul, // Medium Cycle Slow Multiplier ...
lcFastMul, // Long Cycle Fast Multiplier ...
lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
showPSr, // Show Parabolic SAR ...
showMMiddle, // Show Market Middle ...
showSC, // Show Short Cycle
showSCCrossLines, // Show Short Cycle Vertical Lines
showMC, // Show Medium Cycle
showMCCrossLines, // Show Medium Cycle Vertical Lines
showLC, // Show Long Cycle
showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
pSrColor, // Parabolic Sar Color ...
scFastColor, // Short Cycle Fast Color ...
scSlowColor, // Short Cycle Slow Color ...
mcFastColor, // Medium Cycle Fast Color ...
mcSlowColor, // Medium Cycle Slow Color ...
lcFastColor, // Medium Cycle Fast Color ...
lcSlowColor, // Medium Cycle Slow Color ...
//
lcSlowBufIndex, // Buffer Index ...
bar_index
);
result.lcSlow = lcSlow;
//
// END Long Cycle ...
//
//
// START Makret Middleage ...
//
//
// Market Middleage ...
double marketMiddleage = iCustom(
_Symbol,
_Period,
xmaIndicatorName,
//
// XMA Inputs ...
//
// Market Specification Inpouts ...
marketLen, // Market Length ...
stp, // PSAR Step ...
maxm, // PSAR Maximum ...
scFastMul, // Short Cycle Fast Multiplier ...
scSlowMul, // Short Cycle Slow Multiplier ...
mcFastMul, // Medium Cycle Fast Multiplier ...
mcSlowMul, // Medium Cycle Slow Multiplier ...
lcFastMul, // Long Cycle Fast Multiplier ...
lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
showPSr, // Show Parabolic SAR ...
showMMiddle, // Show Market Middle ...
showSC, // Show Short Cycle
showSCCrossLines, // Show Short Cycle Vertical Lines
showMC, // Show Medium Cycle
showMCCrossLines, // Show Medium Cycle Vertical Lines
showLC, // Show Long Cycle
showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
pSrColor, // Parabolic Sar Color ...
scFastColor, // Short Cycle Fast Color ...
scSlowColor, // Short Cycle Slow Color ...
mcFastColor, // Medium Cycle Fast Color ...
mcSlowColor, // Medium Cycle Slow Color ...
lcFastColor, // Medium Cycle Fast Color ...
lcSlowColor, // Medium Cycle Slow Color ...
//
midBufIndex, // Buffer Index ...
bar_index
);
result.marketMiddleage = marketMiddleage;
//
// END Makret Middleage ...
//
//
// START Parabolic SAR ...
//
//
// Parabolic ...
double parabolicSAR = iCustom(
_Symbol,
_Period,
xmaIndicatorName,
//
// XMA Inputs ...
//
// Market Specification Inpouts ...
marketLen, // Market Length ...
stp, // PSAR Step ...
maxm, // PSAR Maximum ...
scFastMul, // Short Cycle Fast Multiplier ...
scSlowMul, // Short Cycle Slow Multiplier ...
mcFastMul, // Medium Cycle Fast Multiplier ...
mcSlowMul, // Medium Cycle Slow Multiplier ...
lcFastMul, // Long Cycle Fast Multiplier ...
lcSlowMul, // Long Cycle Slow Multiplier ...
//
// Show/Hide Inputs ...
showPSr, // Show Parabolic SAR ...
showMMiddle, // Show Market Middle ...
showSC, // Show Short Cycle
showSCCrossLines, // Show Short Cycle Vertical Lines
showMC, // Show Medium Cycle
showMCCrossLines, // Show Medium Cycle Vertical Lines
showLC, // Show Long Cycle
showLCCrossLines, // Show Long Cycle Vertical Lines
//
// Color Inputs ...
pSrColor, // Parabolic Sar Color ...
scFastColor, // Short Cycle Fast Color ...
scSlowColor, // Short Cycle Slow Color ...
mcFastColor, // Medium Cycle Fast Color ...
mcSlowColor, // Medium Cycle Slow Color ...
lcFastColor, // Medium Cycle Fast Color ...
lcSlowColor, // Medium Cycle Slow Color ...
//
pSarBufIndex, // Buffer Index ...
bar_index
);
result.parabolicSAR = parabolicSAR;
//
// END Makret Middleage ...
//
//
// END Reading Buffers ...
//
//
return result;
}
//
// Retrieve Market State ...
XMKTState GetMarketState(
//
const int bar_index,
//
const int marketLen,
//
const bool showHH, // Show Highest High ...
const color hhColor, // Show Highest High ...
const ENUM_LINE_STYLE hhStyle, // Show Highest High ...
//
const bool showHL, // Show Highest Low ...
const color hlColor, // Show Highest Low ...
const ENUM_LINE_STYLE hlStyle, // Show Highest Low ...
//
const bool showHO, // Show Highest Open ...
const color hoColor, // Show Highest Open ...
const ENUM_LINE_STYLE hoStyle, // Show Highest Open ...
//
const bool showHC, // Show Highest Close ...
const color hcColor, // Show Highest Close ...
const ENUM_LINE_STYLE hcStyle, // Show Highest Close ...
//
const bool showLH, // Show Lowest High ...
const color lhColor, // Show Lowest High ...
const ENUM_LINE_STYLE lhStyle, // Show Lowest High ...
//
const bool showLL, // Show Lowest Low ...
const color llColor, // Show Lowest Low ...
const ENUM_LINE_STYLE llStyle, // Show Lowest Low ...
//
const bool showLO, // Show Lowest Open ...
const color loColor, // Show Lowest Open ...
const ENUM_LINE_STYLE loStyle, // Show Lowest Open ...
//
const bool showLC, // Show Lowest Close ...
const color lcColor, // Show Lowest Close ...
const ENUM_LINE_STYLE lcStyle // Show Lowest Close ...
) {
//
XMKTState result = {};
//
int hHBufIndex = 0;
int hLBufIndex = 1;
int hOBufIndex = 2;
int hCBufIndex = 3;
int lHBufIndex = 4;
int lLBufIndex = 5;
int lOBufIndex = 6;
int lCBufIndex = 7;
//
// START Reading Buffers ...
//
//
// Highest High ...
double hhValue = iCustom(
_Symbol,
_Period,
xmarketIndicatorName,
//
// Inputs ...
marketLen, // Market Length ...
//
// Show/Hide ...
showHH, // Show Highest High ...
showHL, // Show Highest Low ...
showHO, // Show Highest Open ...
showHC, // Show Highest Close ...
showLH, // Show Lowest High ...
showLL, // Show Lowest Low ...
showLO, // Show Lowest Open ...
showLC, // Show Lowest Close ...
//
// Colors ...
hhColor, // Show Highest High ...
hlColor, // Show Highest Low ...
hoColor, // Show Highest Open ...
hcColor, // Show Highest Close ...
lhColor, // Show Lowest High ...
llColor, // Show Lowest Low ...
loColor, // Show Lowest Open ...
lcColor, // Show Lowest Close ...
//
// Styles ...
hhStyle, // Show Highest High ...
hlStyle, // Show Highest Low ...
hoStyle, // Show Highest Open ...
hcStyle, // Show Highest Close ...
lhStyle, // Show Lowest High ...
llStyle, // Show Lowest Low ...
loStyle, // Show Lowest Open ...
lcStyle, // Show Lowest Close ...
//
hHBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.highestHigh = hhValue;
//
// Highest Low ...
double hlValue = iCustom(
_Symbol,
_Period,
xmarketIndicatorName,
//
// Inputs ...
marketLen, // Market Length ...
//
// Show/Hide ...
showHH, // Show Highest High ...
showHL, // Show Highest Low ...
showHO, // Show Highest Open ...
showHC, // Show Highest Close ...
showLH, // Show Lowest High ...
showLL, // Show Lowest Low ...
showLO, // Show Lowest Open ...
showLC, // Show Lowest Close ...
//
// Colors ...
hhColor, // Show Highest High ...
hlColor, // Show Highest Low ...
hoColor, // Show Highest Open ...
hcColor, // Show Highest Close ...
lhColor, // Show Lowest High ...
llColor, // Show Lowest Low ...
loColor, // Show Lowest Open ...
lcColor, // Show Lowest Close ...
//
// Styles ...
hhStyle, // Show Highest High ...
hlStyle, // Show Highest Low ...
hoStyle, // Show Highest Open ...
hcStyle, // Show Highest Close ...
lhStyle, // Show Lowest High ...
llStyle, // Show Lowest Low ...
loStyle, // Show Lowest Open ...
lcStyle, // Show Lowest Close ...
//
hLBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.highestLow = hlValue;
//
// Highest Open ...
double hoValue = iCustom(
_Symbol,
_Period,
xmarketIndicatorName,
//
// Inputs ...
marketLen, // Market Length ...
//
// Show/Hide ...
showHH, // Show Highest High ...
showHL, // Show Highest Low ...
showHO, // Show Highest Open ...
showHC, // Show Highest Close ...
showLH, // Show Lowest High ...
showLL, // Show Lowest Low ...
showLO, // Show Lowest Open ...
showLC, // Show Lowest Close ...
//
// Colors ...
hhColor, // Show Highest High ...
hlColor, // Show Highest Low ...
hoColor, // Show Highest Open ...
hcColor, // Show Highest Close ...
lhColor, // Show Lowest High ...
llColor, // Show Lowest Low ...
loColor, // Show Lowest Open ...
lcColor, // Show Lowest Close ...
//
// Styles ...
hhStyle, // Show Highest High ...
hlStyle, // Show Highest Low ...
hoStyle, // Show Highest Open ...
hcStyle, // Show Highest Close ...
lhStyle, // Show Lowest High ...
llStyle, // Show Lowest Low ...
loStyle, // Show Lowest Open ...
lcStyle, // Show Lowest Close ...
//
hOBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.highestOpen = hoValue;
//
// Highest Close ...
double hcValue = iCustom(
_Symbol,
_Period,
xmarketIndicatorName,
//
// Inputs ...
marketLen, // Market Length ...
//
// Show/Hide ...
showHH, // Show Highest High ...
showHL, // Show Highest Low ...
showHO, // Show Highest Open ...
showHC, // Show Highest Close ...
showLH, // Show Lowest High ...
showLL, // Show Lowest Low ...
showLO, // Show Lowest Open ...
showLC, // Show Lowest Close ...
//
// Colors ...
hhColor, // Show Highest High ...
hlColor, // Show Highest Low ...
hoColor, // Show Highest Open ...
hcColor, // Show Highest Close ...
lhColor, // Show Lowest High ...
llColor, // Show Lowest Low ...
loColor, // Show Lowest Open ...
lcColor, // Show Lowest Close ...
//
// Styles ...
hhStyle, // Show Highest High ...
hlStyle, // Show Highest Low ...
hoStyle, // Show Highest Open ...
hcStyle, // Show Highest Close ...
lhStyle, // Show Lowest High ...
llStyle, // Show Lowest Low ...
loStyle, // Show Lowest Open ...
lcStyle, // Show Lowest Close ...
//
hCBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.highestClose = hcValue;
//
// Lowest High ...
double lhValue = iCustom(
_Symbol,
_Period,
xmarketIndicatorName,
//
// Inputs ...
marketLen, // Market Length ...
//
// Show/Hide ...
showHH, // Show Highest High ...
showHL, // Show Highest Low ...
showHO, // Show Highest Open ...
showHC, // Show Highest Close ...
showLH, // Show Lowest High ...
showLL, // Show Lowest Low ...
showLO, // Show Lowest Open ...
showLC, // Show Lowest Close ...
//
// Colors ...
hhColor, // Show Highest High ...
hlColor, // Show Highest Low ...
hoColor, // Show Highest Open ...
hcColor, // Show Highest Close ...
lhColor, // Show Lowest High ...
llColor, // Show Lowest Low ...
loColor, // Show Lowest Open ...
lcColor, // Show Lowest Close ...
//
// Styles ...
hhStyle, // Show Highest High ...
hlStyle, // Show Highest Low ...
hoStyle, // Show Highest Open ...
hcStyle, // Show Highest Close ...
lhStyle, // Show Lowest High ...
llStyle, // Show Lowest Low ...
loStyle, // Show Lowest Open ...
lcStyle, // Show Lowest Close ...
//
lHBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.lowestHigh = lhValue;
//
// Lowest Low ...
double llValue = iCustom(
_Symbol,
_Period,
xmarketIndicatorName,
//
// Inputs ...
marketLen, // Market Length ...
//
// Show/Hide ...
showHH, // Show Highest High ...
showHL, // Show Highest Low ...
showHO, // Show Highest Open ...
showHC, // Show Highest Close ...
showLH, // Show Lowest High ...
showLL, // Show Lowest Low ...
showLO, // Show Lowest Open ...
showLC, // Show Lowest Close ...
//
// Colors ...
hhColor, // Show Highest High ...
hlColor, // Show Highest Low ...
hoColor, // Show Highest Open ...
hcColor, // Show Highest Close ...
lhColor, // Show Lowest High ...
llColor, // Show Lowest Low ...
loColor, // Show Lowest Open ...
lcColor, // Show Lowest Close ...
//
// Styles ...
hhStyle, // Show Highest High ...
hlStyle, // Show Highest Low ...
hoStyle, // Show Highest Open ...
hcStyle, // Show Highest Close ...
lhStyle, // Show Lowest High ...
llStyle, // Show Lowest Low ...
loStyle, // Show Lowest Open ...
lcStyle, // Show Lowest Close ...
//
lLBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.lowestLow = llValue;
//
// Lowest Open ...
double loValue = iCustom(
_Symbol,
_Period,
xmarketIndicatorName,
//
// Inputs ...
marketLen, // Market Length ...
//
// Show/Hide ...
showHH, // Show Highest High ...
showHL, // Show Highest Low ...
showHO, // Show Highest Open ...
showHC, // Show Highest Close ...
showLH, // Show Lowest High ...
showLL, // Show Lowest Low ...
showLO, // Show Lowest Open ...
showLC, // Show Lowest Close ...
//
// Colors ...
hhColor, // Show Highest High ...
hlColor, // Show Highest Low ...
hoColor, // Show Highest Open ...
hcColor, // Show Highest Close ...
lhColor, // Show Lowest High ...
llColor, // Show Lowest Low ...
loColor, // Show Lowest Open ...
lcColor, // Show Lowest Close ...
//
// Styles ...
hhStyle, // Show Highest High ...
hlStyle, // Show Highest Low ...
hoStyle, // Show Highest Open ...
hcStyle, // Show Highest Close ...
lhStyle, // Show Lowest High ...
llStyle, // Show Lowest Low ...
loStyle, // Show Lowest Open ...
lcStyle, // Show Lowest Close ...
//
lOBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.lowestOpen = loValue;
//
// Lowest Close ...
double lcValue = iCustom(
_Symbol,
_Period,
xmarketIndicatorName,
//
// Inputs ...
marketLen, // Market Length ...
//
// Show/Hide ...
showHH, // Show Highest High ...
showHL, // Show Highest Low ...
showHO, // Show Highest Open ...
showHC, // Show Highest Close ...
showLH, // Show Lowest High ...
showLL, // Show Lowest Low ...
showLO, // Show Lowest Open ...
showLC, // Show Lowest Close ...
//
// Colors ...
hhColor, // Show Highest High ...
hlColor, // Show Highest Low ...
hoColor, // Show Highest Open ...
hcColor, // Show Highest Close ...
lhColor, // Show Lowest High ...
llColor, // Show Lowest Low ...
loColor, // Show Lowest Open ...
lcColor, // Show Lowest Close ...
//
// Styles ...
hhStyle, // Show Highest High ...
hlStyle, // Show Highest Low ...
hoStyle, // Show Highest Open ...
hcStyle, // Show Highest Close ...
lhStyle, // Show Lowest High ...
llStyle, // Show Lowest Low ...
loStyle, // Show Lowest Open ...
lcStyle, // Show Lowest Close ...
//
lCBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.lowestClose = lcValue;
//
// END Reading Buffers ...
//
//
return result;
}
//
// Retrieve XSHPD State ...
XSHPDState GetXSHPDState(
//
const int bar_index,
//
const int marketLen,
//
const double shpDetectMul, // Sharp Detect Multiplier ...
const bool drwLbls, // Draw Labels on Founded Sharps ...
//
const string shpBullLabel, // Sharp Bullish Label ...
const color shpBullColor, // Sharp Bullish color ...
//
const string shpBearLabel, // Sharp Bearish Label ...
const color shpBearColor // Sharp Bearish color ...
) {
//
// Temp result ...
XSHPDState result = {};
//
int sharpBullishBufIndex = 2;
int sharpBearishBufIndex = 3;
//
double sharpBullishValue = iCustom(
_Symbol,
_Period,
shpdIndicatorName,
//
// Inputs ...
marketLen,
shpDetectMul,
drwLbls,
shpBullLabel,
shpBullColor,
shpBearLabel,
shpBearColor,
//
sharpBullishBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.isSharpBullishDetected =
(int) sharpBullishValue == 1
? true
: false;
//
double sharpBearishValue = iCustom(
_Symbol,
_Period,
shpdIndicatorName,
//
// Inputs ...
marketLen,
shpDetectMul,
drwLbls,
shpBullLabel,
shpBullColor,
shpBearLabel,
shpBearColor,
//
sharpBearishBufIndex, // Buffer Index ...
bar_index // Bar Index ...
);
result.isSharpBearishDetected =
(int) sharpBearishValue == 1
? true
: false;
//
return result;
}
//
// Calculate Fib Level ...
double GetFibonacciLevel(
double upPrice,
double downPrice,
double level,
X_DIRECTION direction
) {
//
double ling = upPrice - downPrice;
double pLevel = (ling / 100) * (level * 100);
//
double result = direction == X_UP ?
upPrice - pLevel :
downPrice + pLevel;
result = NormalizeDouble(result, _Digits);
//
return result;
}
//
// END Global Requirement Functions ...
//
//
// START Signal Verification Functions ...
//
//
// END Signal Verification Functions ...
//
//
// START Market State Functions ...
//
//
// END Market State Functions ...
//
//
// Retrieve Signal Bar Index ...
int GetSignalBarIndex(XSignal &signal) {
//
// Temp Result ...
int result = -1;
//
// Retriev Signal Bar Index ...
result = iBarShift(
_Symbol,
_Period,
signal.time
);
//
return result;
}
//
// Generate a Unique ID for Signal ...
string GenerateSignalTAG(
const XSignal &signal,
const string tag,
const string longSignalLabel,
const string shortSignalLabel
) {
//
// Generate Propper Label for Signal ...
string label = signal.type == X_LONG ? longSignalLabel : signal.type == X_SHORT ? shortSignalLabel : "UN_KNOWN";
//
// Generate Unique String Identifier for Signal ...
string result = tag +
"_[ID:" + IntegerToString(signal.id) + "]" +
"_[SG:" + signal.comment + "]" +
"_[" + label + "]" +
"_[" + signal.symbol + "]" +
"_[Time:" + TimeToStr(signal.time) + "]" +
"_[TP:" + DoubleToString(signal.tp) + "]" +
"_[SL:" + DoubleToString(signal.sl) + "]";
//
return result;
}
//
// Draw Signal Shapes on Chart ...
bool DrawSignal(
const XSignal &signal,
long chartID = 0,
int subWindowID = 0,
const double positionThresholdFactor = 10,
string signalTag = "X-Signal",
string longSignalLabel = "X-Long",
color longSignalColor = clrAqua,
string shortSignalLabel = "X-Short",
color shortSignalColor = clrFuchsia
) {
//
// Generate isLongSignal Var ...
bool isLongSignal = signal.type == X_LONG;
//
// Signal Bar Index ...
int bar_index = iBarShift(
_Symbol,
_Period,
signal.time
);
//
// START Draw Signal Arrow ...
//
//
// Arrow Shape Name ...
string arrowName = StringConcatenate(signalTag, "_Arrow_", signal.id);
//
// Arrow Price ...
double arrowPrice = 0;
if (isLongSignal) {
//
arrowPrice = iLow(
_Symbol,
_Period,
bar_index
);
//
arrowPrice = arrowPrice - (positionThresholdFactor * _Point);
} else {
//
arrowPrice = iHigh(
_Symbol,
_Period,
bar_index
);
//
arrowPrice = arrowPrice + (positionThresholdFactor * _Point);
}
//
// Arrow Code ...
uchar arrowCode = isLongSignal ? SYMBOL_ARROWUP : SYMBOL_ARROWDOWN;
//
// Arrow Anchor Point ...
ENUM_ARROW_ANCHOR arrowAnchorPoint = isLongSignal ? ANCHOR_BOTTOM : ANCHOR_TOP;
//
// Arrow Color ...
color arrowColor = isLongSignal ? longSignalColor : shortSignalColor;
//
// Draw Signal Arrow Shape ...
bool result = DrawArrow(
chartID,
arrowName,
subWindowID,
signal.time,
arrowPrice,
arrowCode,
arrowAnchorPoint,
arrowColor,
STYLE_SOLID,
3,
false,
false,
false,
0
);
//
// END Draw Signal Arrow ...
//
//
// START Draw Signal SL/TP ...
//
string slLineName = StringConcatenate(signalTag, "_SL_", signal.id);
string tpLineName = StringConcatenate(signalTag, "_TP_", signal.id);
string entryLineName = StringConcatenate(signalTag, "_ENTRY_", signal.id);
//
color slColor = shortSignalColor; // isLongSignal ? shortSignalColor : longSignalColor;
color tpColor = longSignalColor; // isLongSignal ? shortSignalColor : longSignalColor;
color entryColor = clrGreen;
//
datetime time1 = iTime(
_Symbol,
_Period,
bar_index + 1
);
//
// Draw Enrty Price ...
result = DrawTrendLine(
chartID,
entryLineName,
subWindowID,
time1,
signal.entryPrice,
signal.time,
signal.entryPrice,
entryColor,
STYLE_SOLID,
2
);
//
// Draw Signal SL ...
if (signal.sl > 0) {
//
result = DrawTrendLine(
chartID,
slLineName,
subWindowID,
time1,
signal.sl,
signal.time,
signal.sl,
slColor,
STYLE_SOLID,
2
);
}
//
// Draw Signal TP ...
if (signal.tp > 0) {
//
result = DrawTrendLine(
chartID,
tpLineName,
subWindowID,
time1,
signal.tp,
signal.time,
signal.tp,
tpColor,
STYLE_SOLID,
2
);
}
//
// END Draw Signal SL/TP ...
//
//
// Start Draw Signal Label ...
//
//
string labelName = StringConcatenate(signalTag, "_Label_", signal.id);
//
double labelPrice = isLongSignal ? arrowPrice - (positionThresholdFactor * _Point) : arrowPrice + (positionThresholdFactor * _Point);
//
string labelText = isLongSignal ? longSignalLabel : shortSignalLabel;
labelText = StringConcatenate(
labelText,
"\n",
signal.comment
);
//
result = DrawText(
chartID,
labelName,
subWindowID,
signal.time,
labelPrice,
labelText,
"Arial",
8,
arrowColor,
0,
ANCHOR_CENTER
);
//
// End Draw Signal Label ...
//
//
return result;
}
//
// Trade on Specific Signal ...
bool TradeSignal(
XSignal &signal,
//
string prefix,
//
double volumePercent,
//
// Long ...
const string longSignalLabel,
const color longSignalColor,
//
// Short ...
const string shortSignalLabel,
const color shortSignalColor,
//
// Define Signal Identifiers ...
const string oscSignalId, // Signal Identifier ...
const string xmaSignalId, // Signal Identifier ...
const string xshpSignalId, // Signal Identifier ...
const string xmntmSignalId, // Signal Identifier ...
const string xrsiSignalId // Signal Identifier ...
) {
//
// Temp Result ...
bool result = false;
//
if (initialBalance == 0) {
initialBalance = AccountInfoDouble(ACCOUNT_BALANCE);
}
//
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
int openPositions = CountOpenTrades();
double volume = (volumePercent * initialBalance);
//
if (
signal.type != X_LONG &&
signal.type != X_SHORT
) {
return result;
}
//
bool hasLongSignal = signal.type == X_LONG;
//
// Check TP and Entry Price ...
if (
(
hasLongSignal
&& signal.entryPrice > signal.tp
) ||
(
!hasLongSignal
&& signal.entryPrice < signal.tp
)
) {
return result;
}
//
// Order Operation ...
int operation = hasLongSignal ?
OP_BUY :
OP_SELL;
//
// Detect Trade Color ...
color clr = hasLongSignal ?
longSignalColor :
shortSignalColor;
//
// Generate Signal Comment ...
string label = hasLongSignal ?
longSignalLabel :
shortSignalLabel;
//
// Generate Signal Comment ...
string comment = StringConcatenate(
prefix,
"_", signal.id,
"_", label,
"_", signal.comment
);
//
// Placing Order ...
int orderTicket = OrderSend(
_Symbol,
operation,
volume,
signal.entryPrice,
3,
signal.sl,
signal.tp,
comment,
signal.id,
0,
clr
);
//
// Draw Signal Shape if Order Opened Successfully ...
if (orderTicket >= 0) {
//
signal.ticket = orderTicket;
//
// Here i must to Draw Signal Shapes ...
DrawSignal(
signal,
0,
0,
50,
prefix, // Signal Tag ...
longSignalLabel, // Long Signal Label ...
longSignalColor,
shortSignalLabel, // Short Signal Label ...
shortSignalColor
);
//
// Logging Signal Provider ...
LogMessage(
StringConcatenate(
"Order [", orderTicket ,"]: Provided by: ", signal.comment
)
);
//
// TODO: Add Exit Strategy ...
// //
// if (signal.comment == oscSignalId) {
// //
// ArrayResize(
// oscSignals,
// ArraySize(oscSignals) + 1
// );
// //
// oscSignals[ArraySize(oscSignals) - 1] = signal;
// } else if (signal.comment == xmaSignalId) {
// //
// ArrayResize(
// xmaSignals,
// ArraySize(xmaSignals) + 1
// );
// //
// xmaSignals[ArraySize(xmaSignals) - 1] = signal;
// } else if (signal.comment == xshpSignalId) {
// //
// ArrayResize(
// xshpSignals,
// ArraySize(xshpSignals) + 1
// );
// //
// xshpSignals[ArraySize(xshpSignals) - 1] = signal;
// }
//
// TODO: here we have to check if there are another
// open positions which their entry price is bigger
// than current signal entry price, for long trades and
// their tp is high than current tp, ...
// close in profit trades or modify orders tp to current ...
// //
// if (CountOpenTrades() > 0) {
// //
// // Here we have to
// // ModifyOpenTradesBasedOnCurrentTrade(signal);
// }
//
result = true;
}
//
return result;
}
//
// END Signal Related Functions ...
//
//
// START Orders Functions ...
//
//
// Convert To XSignal Model ...
XSignal OrderToSignal() {
//
// Temp Result ...
XSignal result = {};
result.type = X_NONE;
//
// Here we can get some usefull info based on selected order ...
double swap = OrderSwap();
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
double stopLoss = OrderStopLoss();
double openPrice = OrderOpenPrice();
datetime openTime = OrderOpenTime();
int magicNumber = OrderMagicNumber();
double takeProfit = OrderTakeProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// Conver Position to Signal ...
result.sl = stopLoss;
result.tp = takeProfit;
result.time = openTime;
result.symbol = _Symbol;
result.id = magicNumber;
result.entryPrice = openPrice;
result.type = isLongOrder ? X_LONG : X_SHORT;
//
return result;
}
//
// START Count Orders ...
//
//
// Count Open Positions ...
int CountOpenTrades() {
//
int result = 0;
//
// Loop through Orders ...
for (int i = 0; i < OrdersTotal(); i++) {
//
// Select indexed Order ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order ...
if (OrderSymbol() == _Symbol) {
result++;
}
}
}
//
// Return Result ...
return result;
}
//
// Count Open Long Positions ...
int CountOpenLongTrades() {
//
int result = 0;
//
// Loop through Orders ...
for (int i = 0; i < OrdersTotal(); i++) {
//
// Select indexed Order ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order ...
if (
OrderSymbol() == _Symbol
&& OrderType() == OP_BUY
) {
result++;
}
}
}
//
// Return Result ...
return result;
}
//
// Count Open Short Positions ...
int CountOpenShortTrades() {
//
int result = 0;
//
// Loop through Orders ...
for (int i = 0; i < OrdersTotal(); i++) {
//
// Select indexed Order ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order ...
if (
OrderSymbol() == _Symbol
&& OrderType() == OP_SELL
) {
result++;
}
}
}
//
// Return Result ...
return result;
}
//
// END Count Orders ...
//
//
// START Close Orders ...
//
//
// Close All Open Trades ...
bool CloseAllTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
//
return result;
}
//
// Close All Open Long Trades ...
bool CloseAllLongTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
if (isLongOrder) {
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Long Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All Open Short Trades ...
bool CloseAllShortTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
if (!isLongOrder) {
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of All Short Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All In Profit Trades ...
bool CloseAllInProfitTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
if (profit > 0) {
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of InProfit Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All In DrawDown Trades ...
bool CloseAllInDrawDownTrades() {
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
if (profit < 0) {
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of InDrawDown Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close Biggest In DrawDown Trades ...
bool CloseBiggestDrawDownTrade() {
//
// Loop through all Positions ...
bool result = false;
double drawDown = 0;
//
// Find Biggest In DrawDown ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
if (
profit < 0
&& MathAbs(profit) > drawDown
) {
drawDown = profit;
}
}
}
}
//
// Close Biggest In DrawDown ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
if (
profit < 0
&& MathAbs(profit) >= drawDown
) {
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of Max InDrawDown Trade Closing issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All Long Time Trades ...
bool CloseAllLongTimeTrades(
const int passedCandles
) {
//
bool result = false;
//
if (passedCandles <= 0) {
return result;
}
//
int currentBarIndex = iBarShift(
_Symbol,
_Period,
TimeCurrent()
);
//
int openTrades = CountOpenTrades();
//
// Loop through all Positions ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
datetime openTime = OrderOpenTime();
int openBarIndex = iBarShift(
_Symbol,
_Period,
openTime
);
//
if (
openBarIndex - currentBarIndex >= passedCandles
|| (
profit > 0.5
&& openBarIndex - currentBarIndex >= passedCandles - (passedCandles / 3)
)
) {
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of Long Time issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
} else if (
openTrades > 0
&& openBarIndex - currentBarIndex >= passedCandles - (passedCandles / 2)
) {
result = CloseAllInProfitTrades();
// && CloseBiggestDrawDownTrade();
}
}
}
}
//
return result;
}
//
// Close all trades which passed Maximum Drawdown ...
bool CloseAllMaximumDrawDownPassedTrades(
const double maxDrDownPercentPerPTrade
) {
//
if (
initialBalance <= 0
|| maxDrDownPercentPerPTrade <= 0
) {
return false;
}
//
// Retrieve Account Balance ...
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
double maxAllowedDrawDown = -1 * (initialBalance * maxDrDownPercentPerPTrade);
//
// Loop through all Positions ...
bool result = false;
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
//
if (
profit < 0
&& MathAbs(profit) >= MathAbs(maxAllowedDrawDown)
) {
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of Maximum DarwDown issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
}
}
}
}
//
return result;
}
//
// Close All Trades Which Opens before Specified Length ...
bool CloseAllTradesWhichOpenInPastMarketLength(
const int marketLen
) {
//
bool result = false;
//
if (marketLen <= 0) {
return result;
}
//
int currentBarIndex = iBarShift(
_Symbol,
_Period,
TimeCurrent()
);
//
int openTrades = CountOpenTrades();
//
// Loop through all Positions ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
datetime openTime = OrderOpenTime();
int openBarIndex = iBarShift(
_Symbol,
_Period,
openTime
);
//
if (openBarIndex <= currentBarIndex + marketLen) {
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// Since because of equity ...
// we have to ckose allpositions ...
bool isOrderClosed = OrderClose(
ticket,
lots,
closePrice,
clrYellow
);
//
if (isOrderClosed) {
//
LogMessage(
StringConcatenate(
"Order [", ticket, "] Closed because of Market Reverse issue ..."
)
);
}
//
result =
result
|| isOrderClosed;
} else if (openTrades > 0) {
//
// Close all InProfit Trdes and Biggest InDrawDown Trades ...
result = CloseAllInProfitTrades();
// && CloseBiggestDrawDownTrade();
}
}
}
}
//
return result;
}
//
// END Close Orders ...
//
//
// START Modify Orders ...
//
//
void ModifyAllPriceLesserOpenTrades(
const XSignal &signal
) {
//
// Loop through all Open Trades ...
for (int i = OrdersTotal(); i >= 0; i--) {
//
// Check Order Selected or not ...
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
//
// Check Order Symbol is same as Current Symbol ...
if (OrderSymbol() == _Symbol) {
//
double lots = OrderLots();
int ticket = OrderTicket();
double profit = OrderProfit();
double stopLoss = OrderStopLoss();
double openPrice = OrderOpenPrice();
datetime openTime = OrderOpenTime();
double takeProfit = OrderTakeProfit();
//
// find order type ...
bool isLongOrder = OrderType() == OP_BUY;
//
// find close price ...
double closePrice = isLongOrder ?
SymbolInfoDouble(_Symbol, SYMBOL_BID) :
SymbolInfoDouble(_Symbol, SYMBOL_ASK);
//
// When new Signal Comming with price less than
// currently open trade and tp less than currently
// open trade, this means a market change happens ...
// here we can Close InProfit Trades or Modify Trades TP ...
if (
openPrice < signal.entryPrice
&& takeProfit > signal.tp
) {
bool result = OrderModify(
ticket,
openPrice,
signal.sl,
signal.tp,
clrYellow
);
}
}
}
}
}
//
void ModifyOpenTradesBasedOnCurrentTrade(
const XSignal &signal
) {
//
ModifyAllPriceLesserOpenTrades(signal);
}
//
// END Modify Orders ...
//
//
// END Orders Functions ...
//