395 lines
9.4 KiB
Plaintext
395 lines
9.4 KiB
Plaintext
/////////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center MQL5 OrderBlock Detector Oscillator
|
|
// ------------------------------------------------------
|
|
// Name: XRSI
|
|
// Description: detect market conditions based on RSI ...
|
|
//
|
|
//
|
|
// 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 XRSI Oscillator"
|
|
#property strict
|
|
|
|
//
|
|
// START Constants ...
|
|
//
|
|
|
|
#define ShortName "XRSI"
|
|
|
|
//
|
|
// END Constants ...
|
|
//
|
|
|
|
//
|
|
// START Inputs ...
|
|
//
|
|
|
|
//
|
|
input group "Market";
|
|
input int length = 14; // Market Length
|
|
input ENUM_APPLIED_PRICE appliedTo = PRICE_CLOSE; // Applied To
|
|
|
|
//
|
|
input group "Short Entry";
|
|
input double shortEntryValue = 70; // Short Entry Level
|
|
input color shortEntryColor = clrRed; // Short Entry Level Color
|
|
input ENUM_LINE_STYLE shortEntryLineStyle = STYLE_DOT; // Short Entry Style
|
|
|
|
//
|
|
input group "Short Exit";
|
|
input double shortExitValue = 40; // Short Exit Level
|
|
input color shortExitColor = clrRed; // Short Exit Level Color
|
|
input ENUM_LINE_STYLE shortExitLineStyle = STYLE_DOT; // Short Exit Style
|
|
|
|
//
|
|
input group "Long Entry";
|
|
input double longEntryValue = 30; // Long Entry Level
|
|
input color longEntryColor = clrRed; // Long Entry Level Color
|
|
input ENUM_LINE_STYLE longEntryLineStyle = STYLE_DOT; // Long Entry Style
|
|
|
|
//
|
|
input group "Long Exit";
|
|
input double longExitValue = 60; // Long Exit Level
|
|
input color longExitColor = clrRed; // Long Exit Level Color
|
|
input ENUM_LINE_STYLE longExitLineStyle = STYLE_DOT; // Long Exit Style
|
|
|
|
//
|
|
// START Inputs ...
|
|
//
|
|
|
|
//
|
|
// Include Common and Models Library ...
|
|
#include "../Libraries/x-saherelm.common.lib.mq5"
|
|
|
|
//
|
|
// START Buffers ...
|
|
//
|
|
|
|
//
|
|
// #property indicator_separate_window
|
|
#property indicator_chart_window
|
|
#property indicator_buffers 2
|
|
// #property indicator_plots 1
|
|
#property indicator_plots 0
|
|
|
|
//
|
|
#define rsiBufferIndex 0
|
|
#define rsiColorBufferIndex 1
|
|
|
|
//
|
|
double rsiBuffer[];
|
|
double rsiColorBuffer[];
|
|
|
|
//
|
|
// #property indicator_label1 "XRSI"
|
|
// #property indicator_type1 DRAW_COLOR_LINE
|
|
// #property indicator_color1 clrAqua, clrGray, clrFuchsia
|
|
// #property indicator_style1 STYLE_DOT
|
|
// #property indicator_width1 1
|
|
|
|
//
|
|
// END Buffers ...
|
|
//
|
|
|
|
//
|
|
// START Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
int rsiHandler = INVALID_HANDLE;
|
|
|
|
//
|
|
// END Global Definitions: Variables, Properties and etc ...
|
|
//
|
|
|
|
//
|
|
// START Event Handlers ...
|
|
//
|
|
//
|
|
// Initialization ...
|
|
int OnInit()
|
|
{
|
|
//
|
|
// Validate Inputs ...
|
|
if (!ValidateInputs())
|
|
{
|
|
return INIT_PARAMETERS_INCORRECT;
|
|
}
|
|
|
|
//
|
|
rsiHandler = iRSI(
|
|
_Symbol,
|
|
_Period,
|
|
length,
|
|
appliedTo);
|
|
if (rsiHandler == INVALID_HANDLE)
|
|
{
|
|
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 ...
|
|
|
|
//
|
|
IndicatorRelease(rsiHandler);
|
|
}
|
|
|
|
//
|
|
// 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, length);
|
|
|
|
//
|
|
int rsiCalculatedBars = BarsCalculated(rsiHandler);
|
|
if (rsiCalculatedBars < maxLength)
|
|
{
|
|
return prev_calculated;
|
|
}
|
|
|
|
//
|
|
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
|
|
|
//
|
|
int rsiCopiedItems = CopyBuffer(rsiHandler, 0, 0, limit, rsiBuffer);
|
|
if (rsiCopiedItems <= 0)
|
|
{
|
|
return prev_calculated;
|
|
}
|
|
|
|
//
|
|
// Main Loop ...
|
|
for (int i = limit - 1; i >= 0 && !IsStopped(); i--)
|
|
{
|
|
CalculateBuffers(i);
|
|
}
|
|
|
|
//
|
|
return rates_total;
|
|
}
|
|
//
|
|
// END Event Handlers ...
|
|
//
|
|
|
|
//
|
|
// START Functions ...
|
|
//
|
|
//
|
|
// Validate Input Args for Initialization ...
|
|
bool ValidateInputs()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Validate Args ...
|
|
if (length >= 2)
|
|
{
|
|
result = true;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Define Indexes and Styles ...
|
|
void DefineBuffers()
|
|
{
|
|
//
|
|
// RSI ...
|
|
string rsiBufferLabel = ShortName + " (" + (string)length + ")";
|
|
ArraySetAsSeries(rsiBuffer, true);
|
|
SetIndexBuffer(rsiBufferIndex, rsiBuffer, INDICATOR_CALCULATIONS);
|
|
// SetIndexBuffer(rsiBufferIndex, rsiBuffer, INDICATOR_DATA);
|
|
// PlotIndexSetInteger(rsiBufferIndex, PLOT_DRAW_BEGIN, length);
|
|
// PlotIndexSetString(rsiBufferIndex, PLOT_LABEL, rsiBufferLabel);
|
|
|
|
// //
|
|
// // RSI Color Buffer ...
|
|
ArraySetAsSeries(rsiColorBuffer, true);
|
|
SetIndexBuffer(rsiColorBufferIndex, rsiColorBuffer, INDICATOR_CALCULATIONS);
|
|
// SetIndexBuffer(rsiColorBufferIndex, rsiColorBuffer, INDICATOR_COLOR_INDEX);
|
|
|
|
//
|
|
// Set Indicator Levels here ...
|
|
// OB, ExitLong, ExitShort, OS ...
|
|
IndicatorSetInteger(INDICATOR_LEVELS, 4);
|
|
|
|
//
|
|
// SHORTENTRY ...
|
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, shortEntryValue);
|
|
IndicatorSetInteger(INDICATOR_LEVELCOLOR, 0, shortEntryColor);
|
|
IndicatorSetInteger(INDICATOR_LEVELSTYLE, 0, shortEntryLineStyle);
|
|
IndicatorSetString(INDICATOR_LEVELTEXT, 0, "Short Entry");
|
|
|
|
//
|
|
// LONGEXIT ...
|
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 1, longExitValue);
|
|
IndicatorSetInteger(INDICATOR_LEVELCOLOR, 1, longExitColor);
|
|
IndicatorSetInteger(INDICATOR_LEVELSTYLE, 1, shortExitLineStyle);
|
|
IndicatorSetString(INDICATOR_LEVELTEXT, 1, "Long Exit");
|
|
|
|
//
|
|
// SHORTEXIT ...
|
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 2, shortExitValue);
|
|
IndicatorSetInteger(INDICATOR_LEVELCOLOR, 2, shortExitColor);
|
|
IndicatorSetInteger(INDICATOR_LEVELSTYLE, 2, longEntryLineStyle);
|
|
IndicatorSetString(INDICATOR_LEVELTEXT, 2, "Short Exit");
|
|
|
|
//
|
|
// LONGENTRY ...
|
|
IndicatorSetDouble(INDICATOR_LEVELVALUE, 3, longEntryValue);
|
|
IndicatorSetInteger(INDICATOR_LEVELCOLOR, 3, longEntryColor);
|
|
IndicatorSetInteger(INDICATOR_LEVELSTYLE, 3, longExitLineStyle);
|
|
IndicatorSetString(INDICATOR_LEVELTEXT, 3, "Long Entry");
|
|
|
|
//
|
|
// Set Maximum and Minimum for subwindow
|
|
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
|
IndicatorSetDouble(INDICATOR_MINIMUM, 0);
|
|
IndicatorSetDouble(INDICATOR_MAXIMUM, 100);
|
|
}
|
|
|
|
//
|
|
// Set Indicator Short Name and also we can define Buffers Labels ...
|
|
void SetIndicatorName()
|
|
{
|
|
//
|
|
string indicatorShortName = "";
|
|
StringConcatenate(indicatorShortName, ShortName, " ", "( ", length, " )");
|
|
|
|
//
|
|
IndicatorSetString(INDICATOR_SHORTNAME, indicatorShortName);
|
|
}
|
|
|
|
//
|
|
// Calculate Buffers ...
|
|
void CalculateBuffers(int bar_index)
|
|
{
|
|
//
|
|
double iValue = rsiBuffer[bar_index];
|
|
|
|
//
|
|
double iColorIndex = 0;
|
|
|
|
//
|
|
if (
|
|
iValue >= shortEntryValue ||
|
|
iValue <= longEntryValue ||
|
|
(iValue < longExitValue &&
|
|
iValue > shortExitValue))
|
|
{
|
|
iColorIndex = 0;
|
|
}
|
|
else if (
|
|
iValue <= shortEntryValue &&
|
|
iValue >= longExitValue)
|
|
{
|
|
iColorIndex = 2;
|
|
}
|
|
else if (
|
|
iValue < shortExitValue &&
|
|
iValue > longEntryValue)
|
|
{
|
|
iColorIndex = 1;
|
|
}
|
|
|
|
//
|
|
rsiColorBuffer[bar_index] = iColorIndex;
|
|
}
|
|
|
|
//
|
|
// END Functions ...
|
|
//
|