Files
xMQL4/xIndicators/x-saherelm.xmma.mq4
2024-01-25 04:03:36 +03:30

408 lines
8.9 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center XMMA Indicator
// ---------------------------------------------
// this is an indicator which going to analyse
// Market based on Price Movements and provide
// some informations to act based on current market
// state ...
//
// 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 Market Analyser (XMMA) Indicator"
#property strict
//
// START Inputs ...
//
input int marketLength = 7; // Market Length
input int fastShift = 1; // Fast Shift
input int slowShift = 3; // Slow Shift
input int longMarketFastMultiplier = 100; // Long Market Fast Multiplier
input int longMarketSlowMultiplier = 200; // Long Market Slow Multiplier
//
// END Inputs ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
#property indicator_chart_window
//
// define indicator buffers ...
#property indicator_buffers 7
#property indicator_plots 7
//
// Fast ...
#property indicator_label1 "Fast"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_style1 STYLE_DOT
#property indicator_width1 1
//
// Slow ...
#property indicator_label2 "SLow"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
//
// Mid ...
#property indicator_label3 "Mid"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrBisque
#property indicator_style3 STYLE_DASH
#property indicator_width3 1
//
// Long SL ...
#property indicator_label4 "Long SL"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrAqua
#property indicator_style4 STYLE_DASH
#property indicator_width4 1
//
// Short SL ...
#property indicator_label5 "Short SL"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrFuchsia
#property indicator_style5 STYLE_DASH
#property indicator_width5 1
//
// Long Market Fast ...
#property indicator_label6 "Long Fast"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrAqua
#property indicator_style6 STYLE_DOT
#property indicator_width6 1
//
// Long Market Slow ...
#property indicator_label7 "Long Market Slow"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrFuchsia
#property indicator_style7 STYLE_DOT
#property indicator_width7 1
//
// Declare Buffers ...
#define fastBufferIndex 0
#define slowBufferIndex 1
#define midBufferIndex 2
#define longSLBufferIndex 3
#define shortSLBufferIndex 4
#define longMarketFastBufferIndex 5
#define longMarketSlowBufferIndex 6
//
double fastBuffer[];
double slowBuffer[];
double midBuffer[];
double longSLBuffer[];
double shortSLBuffer[];
double longMarketFastBuffer[];
double longMarketSlowBuffer[];
//
int lastCrossOverIndex = 0;
double lastCrossOver = 0.0;
//
int lastCrossUnderIndex = 0;
double lastCrossUnder = 0.0;
//
double lastLongSL = 0.0;
int lastLongSLIndex = 0;
//
double lastShortSL = 0.0;
int lastShortSLIndex = 0;
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// Start Event Handlers ...
//
//
// Initialization ...
int OnInit() {
//
// Initialize what we want ...
//
// Validating Input Parameters ...
if (
fastShift < 0 ||
slowShift < 0 ||
marketLength <= 0 ||
slowShift < fastShift
) {
return INIT_PARAMETERS_INCORRECT;
}
//
// Fast ...
SetIndexBuffer(fastBufferIndex, fastBuffer);
//
// Slow ...
SetIndexBuffer(slowBufferIndex, slowBuffer);
//
// Mid ...
SetIndexBuffer(midBufferIndex, midBuffer);
//
// Long SL ...
SetIndexBuffer(longSLBufferIndex, longSLBuffer);
//
// Short SL ...
SetIndexBuffer(shortSLBufferIndex, shortSLBuffer);
//
// Long Market Fast ...
SetIndexBuffer(longMarketFastBufferIndex, longMarketFastBuffer);
//
// Long Market Slow ...
SetIndexBuffer(longMarketSlowBufferIndex, longMarketSlowBuffer);
//
// initialization done ...
return(INIT_SUCCEEDED);
}
//
// Do Calculation ...
int OnCalculate(
const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[]
) {
//
// 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, marketLength);
//
// input variable, we return 0 means nothing passed ...
if (rates_total < maxLength) {
return 0;
}
//
// found which candles calculated before ...
limit = (prev_calculated == 0) ? rates_total - maxLength - 1 : rates_total - prev_calculated + 1;
//
// this is the main loop of calculations, for each bar index ...
for (int i = limit - 1; i >= 0; i--) {
//
// Start Calculation here ...
// i is bar_index ...
//
// Calculate Flow ...
CalculateFlow(i);
}
//
return rates_total;
}
//
// END Event Handlers ...
//
//
// START Functions ...
//
//
// Calculate Flow ...
void CalculateFlow(
const int &bar_index
) {
//
int fastLength = marketLength + fastShift;
int slowLength = marketLength + fastShift + slowShift;
//
// Calculating Fast ...
double fast = iMA(
_Symbol,
_Period,
fastLength,
0,
MODE_EMA,
PRICE_WEIGHTED,
bar_index
);
fastBuffer[bar_index] = fast;
//
// Calculating Slow ...
double slow = iMA(
_Symbol,
_Period,
slowLength,
0,
MODE_EMA,
PRICE_WEIGHTED,
bar_index
);
slowBuffer[bar_index] = slow;
//
// Calculating XMa Mid ...
double xmaMid = iMA(
_Symbol,
_Period,
marketLength,
0,
MODE_EMA,
PRICE_HIGH,
bar_index
);
midBuffer[bar_index] = xmaMid;
//
// Calculating XMa Long Market Fast ...
double xmaLongMarketFast = iMA(
_Symbol,
_Period,
marketLength * longMarketFastMultiplier,
0,
MODE_SMA,
PRICE_CLOSE,
bar_index
);
longMarketFastBuffer[bar_index] = xmaLongMarketFast;
//
// Calculating XMa Long Market Slow ...
double xmaLongMarketSlow = iMA(
_Symbol,
_Period,
marketLength * longMarketSlowMultiplier,
0,
MODE_SMA,
PRICE_CLOSE,
bar_index
);
longMarketSlowBuffer[bar_index] = xmaLongMarketSlow;
//
// Find Cross Points ...
//
bool isCrossOver = fastBuffer[bar_index] > slowBuffer[bar_index]
&& !(
fastBuffer[bar_index + 1] > slowBuffer[bar_index + 1]
);
//
bool isCrossUnder = fastBuffer[bar_index] < slowBuffer[bar_index]
&& !(
fastBuffer[bar_index + 1] < slowBuffer[bar_index + 1]
);
//
// Find Trending ...
//
bool isTrendUp = fastBuffer[bar_index] > slowBuffer[bar_index]
&& fastBuffer[bar_index] > fastBuffer[bar_index + 1]
&& slowBuffer[bar_index] > slowBuffer[bar_index + 1];
//
bool isTrendDown = fastBuffer[bar_index] < slowBuffer[bar_index]
&& fastBuffer[bar_index] < fastBuffer[bar_index + 1]
&& slowBuffer[bar_index] < slowBuffer[bar_index + 1];
//
// Do Some Calculations ...
if (isCrossOver) {
//
lastCrossOverIndex = bar_index;
lastCrossOver = fastBuffer[bar_index];
} else if (isCrossUnder) {
//
lastCrossUnderIndex = bar_index;
lastCrossUnder = fastBuffer[bar_index];
}
//
// Calculate SLs ...
//
// Long SL ...
if (lastCrossOver > 0) {
//
lastLongSLIndex = iLowest(
_Symbol,
_Period,
MODE_LOW,
marketLength,
lastCrossOverIndex
);
//
lastLongSL = iLow(
_Symbol,
_Period,
lastLongSLIndex
);
}
//
// Short SL ...
if (lastCrossUnder > 0) {
//
lastShortSLIndex = iHighest(
_Symbol,
_Period,
MODE_HIGH,
marketLength,
lastCrossUnderIndex
);
//
lastShortSL = iHigh(
_Symbol,
_Period,
lastShortSLIndex
);
}
//
longSLBuffer[bar_index] = lastLongSL;
shortSLBuffer[bar_index] = lastShortSL;
}
//
// END Functions ...
//