441 lines
9.0 KiB
Plaintext
441 lines
9.0 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 Indicator
|
|
// ----------------------------------------------------
|
|
// Name: XSSLC
|
|
// Description: SSL Channel 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 XSSLC Indicator"
|
|
#property strict
|
|
|
|
//
|
|
// START Constants ...
|
|
//
|
|
|
|
//
|
|
// Indicator Short Name ...
|
|
#define ShortName "XSSLC"
|
|
|
|
//
|
|
// END Constants ...
|
|
//
|
|
|
|
//
|
|
// START Inputs ...
|
|
//
|
|
|
|
//
|
|
input group "Market";
|
|
input int length = 36; // Length
|
|
|
|
//
|
|
input group "Calculation";
|
|
input ENUM_MA_METHOD method = MODE_SMA; // Method
|
|
input ENUM_APPLIED_PRICE upAppliedTo = PRICE_HIGH; // Up Applied To
|
|
input ENUM_APPLIED_PRICE downAppliedTo = PRICE_LOW; // Down Applied To
|
|
|
|
//
|
|
input group "Presentation";
|
|
input bool showUp = true; // Show Up
|
|
input bool showDown = true; // Show Down
|
|
|
|
//
|
|
// END Inputs ...
|
|
//
|
|
|
|
//
|
|
// START Includes and Imports ...
|
|
//
|
|
|
|
//
|
|
// Includes Common Library ...
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// END Includes and Imports ...
|
|
//
|
|
|
|
//
|
|
// START Buffers ...
|
|
//
|
|
#property indicator_chart_window
|
|
|
|
//
|
|
#property indicator_buffers 5
|
|
#property indicator_plots 2
|
|
|
|
//
|
|
// PLOTTED Buffers ...
|
|
|
|
//
|
|
#define upBufferIndex 0
|
|
double upBuffer[];
|
|
|
|
//
|
|
#property indicator_label1 "XSSLC Up"
|
|
#property indicator_type1 DRAW_LINE
|
|
#property indicator_color1 clrLime
|
|
#property indicator_style1 STYLE_SOLID
|
|
#property indicator_width1 1
|
|
|
|
//
|
|
#define downBufferIndex 1
|
|
double downBuffer[];
|
|
|
|
//
|
|
#property indicator_label2 "XSSLC Down"
|
|
#property indicator_type2 DRAW_LINE
|
|
#property indicator_color2 clrRed
|
|
#property indicator_style2 STYLE_SOLID
|
|
#property indicator_width2 1
|
|
|
|
//
|
|
// DATA Buffers ...
|
|
|
|
//
|
|
#define upMaBufferIndex 2
|
|
double upMaBuffer[];
|
|
|
|
//
|
|
#define downMaBufferIndex 3
|
|
double downMaBuffer[];
|
|
|
|
//
|
|
#define kpiBufferIndex 4
|
|
double kpiBuffer[];
|
|
|
|
//
|
|
// END Buffers ...
|
|
//
|
|
|
|
//
|
|
// START Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
int maxLength;
|
|
|
|
//
|
|
int upHandler = INVALID_HANDLE;
|
|
int downHandler = INVALID_HANDLE;
|
|
|
|
//
|
|
// END Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
// START Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// Initialization ...
|
|
int OnInit()
|
|
{
|
|
//
|
|
// Validate Inputs ...
|
|
if (!ValidateInputs())
|
|
{
|
|
return INIT_PARAMETERS_INCORRECT;
|
|
}
|
|
|
|
//
|
|
// 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 ...
|
|
maxLength = ExtractMaxLengthOfInputs();
|
|
|
|
//
|
|
// Define Index Buffers ...
|
|
DefineBuffers();
|
|
|
|
//
|
|
// Set Indicator ShortName ...
|
|
SetIndicatorName();
|
|
|
|
//
|
|
// Initialize Requierd Handlers ...
|
|
upHandler = iMA(
|
|
_Symbol,
|
|
_Period,
|
|
length,
|
|
0,
|
|
method,
|
|
upAppliedTo);
|
|
downHandler = iMA(
|
|
_Symbol,
|
|
_Period,
|
|
length,
|
|
0,
|
|
method,
|
|
downAppliedTo);
|
|
if (upHandler == INVALID_HANDLE || downHandler == INVALID_HANDLE)
|
|
{
|
|
//
|
|
string msg = "Error in Initilizing Indicator ...";
|
|
Print(msg);
|
|
|
|
//
|
|
return INIT_FAILED;
|
|
}
|
|
|
|
//
|
|
// Init Succeed ...
|
|
return INIT_SUCCEEDED;
|
|
}
|
|
|
|
//
|
|
// DeInitialization ...
|
|
void OnDeinit(const int reason)
|
|
{
|
|
//
|
|
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
|
|
// REASON_REMOVE 1 Program removed from a chart
|
|
// REASON_RECOMPILE 2 Program recompiled
|
|
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
|
|
// REASON_CHARTCLOSE 4 Chart closed
|
|
// REASON_PARAMETERS 5 Inputs changed by a user
|
|
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
|
|
// REASON_TEMPLATE 7 Another chart template applied
|
|
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
|
|
// REASON_CLOSE 9 Terminal closed
|
|
|
|
//
|
|
// Release Indicators ...
|
|
IndicatorRelease(upHandler);
|
|
IndicatorRelease(downHandler);
|
|
}
|
|
|
|
//
|
|
// 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;
|
|
|
|
//
|
|
// Check Calculated Bars ...
|
|
int upCalculatedBars = BarsCalculated(upHandler);
|
|
int downCalculatedBars = BarsCalculated(downHandler);
|
|
if (upCalculatedBars < maxLength ||
|
|
downCalculatedBars < maxLength)
|
|
{
|
|
return prev_calculated;
|
|
}
|
|
|
|
//
|
|
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
|
|
|
|
//
|
|
// Buffers Copy ...
|
|
int copiedUps = CopyBuffer(upHandler, 0, 0, limit, upMaBuffer);
|
|
int copiedDowns = CopyBuffer(downHandler, 0, 0, limit, downMaBuffer);
|
|
if (copiedUps <= 0 ||
|
|
copiedDowns <= 0)
|
|
{
|
|
return prev_calculated;
|
|
}
|
|
|
|
//
|
|
// Main Loop ...
|
|
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
|
|
{
|
|
CalculateBuffers(i, limit, rates_total, prev_calculated, close);
|
|
}
|
|
|
|
//
|
|
return rates_total;
|
|
}
|
|
|
|
//
|
|
// END Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// START Functions ...
|
|
//
|
|
|
|
//
|
|
// Validate Input Args for Initialization ...
|
|
bool ValidateInputs()
|
|
{
|
|
//
|
|
bool result =
|
|
//
|
|
(length > 2)
|
|
//
|
|
;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Retrieve all Exists Input Max Length ...
|
|
// use for Start Of Drawing ...
|
|
int ExtractMaxLengthOfInputs()
|
|
{
|
|
//
|
|
int result = 0;
|
|
|
|
//
|
|
result = MathMax(result, length);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Define Indexes and Styles ...
|
|
void DefineBuffers()
|
|
{
|
|
//
|
|
// UP ...
|
|
|
|
//
|
|
ENUM_DRAW_TYPE upDrawType = showUp ? DRAW_LINE : DRAW_NONE;
|
|
|
|
ArraySetAsSeries(upBuffer, true);
|
|
SetIndexBuffer(upBufferIndex, upBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(upBufferIndex, PLOT_SHOW_DATA, showUp);
|
|
PlotIndexSetInteger(upBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
PlotIndexSetInteger(upBufferIndex, PLOT_DRAW_TYPE, upDrawType);
|
|
|
|
//
|
|
// DOWN ...
|
|
|
|
//
|
|
ENUM_DRAW_TYPE downDrawType = showDown ? DRAW_LINE : DRAW_NONE;
|
|
|
|
ArraySetAsSeries(downBuffer, true);
|
|
SetIndexBuffer(downBufferIndex, downBuffer, INDICATOR_DATA);
|
|
PlotIndexSetInteger(downBufferIndex, PLOT_SHOW_DATA, showDown);
|
|
PlotIndexSetInteger(downBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
|
PlotIndexSetInteger(downBufferIndex, PLOT_DRAW_TYPE, downDrawType);
|
|
|
|
//
|
|
// DATA Buffers ...
|
|
|
|
//
|
|
// UP MA ...
|
|
ArraySetAsSeries(upMaBuffer, true);
|
|
SetIndexBuffer(upMaBufferIndex, upMaBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
//
|
|
// DOWN MA ...
|
|
ArraySetAsSeries(downMaBuffer, true);
|
|
SetIndexBuffer(downMaBufferIndex, downMaBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
//
|
|
// KPI ...
|
|
ArraySetAsSeries(kpiBuffer, true);
|
|
SetIndexBuffer(kpiBufferIndex, kpiBuffer, INDICATOR_CALCULATIONS);
|
|
}
|
|
|
|
//
|
|
// Set Indicator Short Name and also we can define Buffers Labels ...
|
|
void SetIndicatorName()
|
|
{
|
|
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
|
}
|
|
|
|
//
|
|
// Calculate Buffers ...
|
|
void CalculateBuffers(
|
|
int bar_index, // Selected Bar Index
|
|
int limit,
|
|
int ratesTotal,
|
|
int prevCalculated,
|
|
const double &close[])
|
|
{
|
|
//
|
|
double upMaValue = upMaBuffer[bar_index];
|
|
double downMaValue = downMaBuffer[bar_index];
|
|
|
|
//
|
|
double closeValue = close[bar_index];
|
|
|
|
//
|
|
double kpiValue;
|
|
if (closeValue > upMaValue)
|
|
{
|
|
kpiValue = 1;
|
|
}
|
|
else if (closeValue < downMaValue)
|
|
{
|
|
kpiValue = -1;
|
|
}
|
|
else
|
|
{
|
|
kpiValue = kpiBuffer[bar_index + 1];
|
|
}
|
|
|
|
//
|
|
kpiBuffer[bar_index] = kpiValue;
|
|
|
|
//
|
|
double upValue = kpiValue < 0 ? downMaValue : upMaValue;
|
|
upBuffer[bar_index] = upValue;
|
|
|
|
//
|
|
double downValue = kpiValue < 0 ? upMaValue : downMaValue;
|
|
downBuffer[bar_index] = downValue;
|
|
}
|
|
|
|
//
|
|
// END Functions ...
|
|
//
|