Initial Commit ...
This commit is contained in:
@@ -0,0 +1,558 @@
|
||||
///////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 XTS Oscillator
|
||||
// ---------------------------------------------
|
||||
// Name: XTS
|
||||
// Description: Time Sections Oscillator ...
|
||||
//
|
||||
//
|
||||
// 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 XTS Oscillator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
|
||||
#define ShortName "XTS"
|
||||
|
||||
enum ENUM_X_PERIOD_METHOD
|
||||
{
|
||||
//
|
||||
X_PERIOD_NOTHING,
|
||||
//
|
||||
X_PERIOD_AUTO,
|
||||
//
|
||||
X_PERIOD_MANUALLY
|
||||
};
|
||||
|
||||
//
|
||||
// XTS Oscillator States ...
|
||||
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input group "Current Period";
|
||||
input ENUM_APPLIED_PRICE currentAppliedTo = PRICE_CLOSE; // Current Applied To
|
||||
//
|
||||
input group "Nearest Period";
|
||||
input ENUM_X_PERIOD_METHOD nearestMethod = X_PERIOD_AUTO; // How to Find Nearest Period
|
||||
input ENUM_TIMEFRAMES nearestPeriod = NULL; // Nearest Time Frame
|
||||
input ENUM_APPLIED_PRICE nearestAppliedTo = PRICE_CLOSE; // Nearest Applied To
|
||||
//
|
||||
input group "Mediest Period";
|
||||
input ENUM_X_PERIOD_METHOD mediestMethod = X_PERIOD_AUTO; // How to Find Mediest Period
|
||||
input ENUM_TIMEFRAMES mediestPeriod = NULL; // Mediest Time Frame
|
||||
input ENUM_APPLIED_PRICE mediestAppliedTo = PRICE_CLOSE; // Mediest Applied To
|
||||
//
|
||||
input group "Farest Period";
|
||||
input ENUM_X_PERIOD_METHOD farestMethod = X_PERIOD_AUTO; // How to Find Farest Period
|
||||
input ENUM_TIMEFRAMES farestPeriod = NULL; // Farest Time Frame
|
||||
input ENUM_APPLIED_PRICE farestAppliedTo = PRICE_CLOSE; // Farest Applied To
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5"
|
||||
|
||||
//
|
||||
// Includes Draw Library ...
|
||||
#include "../Libraries/x-saherelm.draw.lib.mq5"
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5"
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 4
|
||||
#property indicator_plots 4
|
||||
|
||||
//
|
||||
// Current ...
|
||||
#define currentBufferIndex 0
|
||||
|
||||
double currentBuffer[];
|
||||
|
||||
#property indicator_label1 "XTS C"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrBrown
|
||||
#property indicator_style1 STYLE_SOLID
|
||||
#property indicator_width1 1
|
||||
|
||||
//
|
||||
// Nearest ...
|
||||
#define nearestBufferIndex 1
|
||||
|
||||
double nearestBuffer[];
|
||||
|
||||
#property indicator_label2 "XTS N"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrAqua
|
||||
#property indicator_style2 STYLE_SOLID
|
||||
#property indicator_width2 1
|
||||
|
||||
//
|
||||
// Mediest ...
|
||||
#define mediestBufferIndex 2
|
||||
|
||||
double mediestBuffer[];
|
||||
|
||||
#property indicator_label3 "XTS M"
|
||||
#property indicator_type3 DRAW_LINE
|
||||
#property indicator_color3 clrLime
|
||||
#property indicator_style3 STYLE_SOLID
|
||||
#property indicator_width3 1
|
||||
|
||||
//
|
||||
// Farest ...
|
||||
#define farestBufferIndex 3
|
||||
|
||||
double farestBuffer[];
|
||||
|
||||
#property indicator_label4 "XTS F"
|
||||
#property indicator_type4 DRAW_LINE
|
||||
#property indicator_color4 clrLightBlue
|
||||
#property indicator_style4 STYLE_SOLID
|
||||
#property indicator_width4 1
|
||||
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
ENUM_TIMEFRAMES mNearestPeriod = NULL;
|
||||
ENUM_TIMEFRAMES mMediestPeriod = NULL;
|
||||
ENUM_TIMEFRAMES mFarestPeriod = NULL;
|
||||
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
drawPrefix = ShortName;
|
||||
|
||||
//
|
||||
// Define Index Buffers ...
|
||||
DefineBuffers();
|
||||
|
||||
//
|
||||
// Set Oscillator ShortName ...
|
||||
SetOscillatorName();
|
||||
|
||||
//
|
||||
// Init Succeed ...
|
||||
return INIT_SUCCEEDED;
|
||||
}
|
||||
|
||||
//
|
||||
// DeInitialization ...
|
||||
void OnDeinit(const int reason)
|
||||
{
|
||||
//
|
||||
OnDeinitDrawLibrary();
|
||||
|
||||
//
|
||||
Comment("");
|
||||
}
|
||||
|
||||
//
|
||||
// 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 = 30;
|
||||
|
||||
//
|
||||
limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1;
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = 0; i < limit && !IsStopped(); i++)
|
||||
{
|
||||
//
|
||||
// Do Buffers Calculation ...
|
||||
CalculateBuffers(i);
|
||||
}
|
||||
|
||||
//
|
||||
return rates_total;
|
||||
}
|
||||
|
||||
//
|
||||
// END Event Handlers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Functions ...
|
||||
//
|
||||
//
|
||||
// Validate Input Args for Initialization ...
|
||||
bool ValidateInputs()
|
||||
{
|
||||
//
|
||||
bool result = true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
int maxLength = 30;
|
||||
|
||||
//
|
||||
// Current ...
|
||||
ArraySetAsSeries(currentBuffer, true);
|
||||
SetIndexBuffer(currentBufferIndex, currentBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(currentBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||
|
||||
//
|
||||
// Nearest ...
|
||||
ArraySetAsSeries(nearestBuffer, true);
|
||||
SetIndexBuffer(nearestBufferIndex, nearestBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(nearestBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||
|
||||
//
|
||||
// Mediest ...
|
||||
ArraySetAsSeries(mediestBuffer, true);
|
||||
SetIndexBuffer(mediestBufferIndex, mediestBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(mediestBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||
|
||||
//
|
||||
// Farest ...
|
||||
ArraySetAsSeries(farestBuffer, true);
|
||||
SetIndexBuffer(farestBufferIndex, farestBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(farestBufferIndex, PLOT_DRAW_BEGIN, maxLength);
|
||||
}
|
||||
|
||||
//
|
||||
// Set Oscillator Short Name and also we can define Buffers Labels ...
|
||||
void SetOscillatorName()
|
||||
{
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, ShortName);
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Buffers ...
|
||||
void CalculateBuffers(int bar_index)
|
||||
{
|
||||
//
|
||||
// Current ...
|
||||
CalculateCurrent(bar_index);
|
||||
|
||||
//
|
||||
// Nearest ...
|
||||
CalculateNearest(bar_index);
|
||||
|
||||
//
|
||||
// Mediest ...
|
||||
CalculateMediest(bar_index);
|
||||
|
||||
//
|
||||
// Farest ...
|
||||
CalculateFarest(bar_index);
|
||||
|
||||
//
|
||||
// Commenting ...
|
||||
string comment = GenerateDescriptionComment();
|
||||
Comment(comment);
|
||||
}
|
||||
|
||||
//
|
||||
// Current ...
|
||||
void CalculateCurrent(int bar_index)
|
||||
{
|
||||
//
|
||||
datetime barTime = iTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
bar_index);
|
||||
datetime startTime = GetPeriodStartTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
barTime);
|
||||
int startBarIndex = iBarShift(
|
||||
_Symbol,
|
||||
_Period,
|
||||
startTime);
|
||||
|
||||
//
|
||||
XOHCL c = GetCandle(
|
||||
_Symbol,
|
||||
_Period,
|
||||
startBarIndex);
|
||||
|
||||
//
|
||||
double value = GetAppliedPrice(
|
||||
c,
|
||||
currentAppliedTo);
|
||||
|
||||
//
|
||||
currentBuffer[bar_index] = value;
|
||||
}
|
||||
|
||||
//
|
||||
// Nearest ...
|
||||
void CalculateNearest(int bar_index)
|
||||
{
|
||||
//
|
||||
if (mNearestPeriod == NULL)
|
||||
{
|
||||
//
|
||||
switch (nearestMethod)
|
||||
{
|
||||
//
|
||||
case X_PERIOD_AUTO:
|
||||
mNearestPeriod = GetNearestTimeFrame(_Period);
|
||||
break;
|
||||
|
||||
//
|
||||
case X_PERIOD_NOTHING:
|
||||
case X_PERIOD_MANUALLY:
|
||||
mNearestPeriod = nearestPeriod;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
datetime barTime = iTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
bar_index);
|
||||
datetime startTime = GetPeriodStartTime(
|
||||
_Symbol,
|
||||
mNearestPeriod,
|
||||
barTime);
|
||||
int startBarIndex = iBarShift(
|
||||
_Symbol,
|
||||
_Period,
|
||||
startTime);
|
||||
|
||||
//
|
||||
XOHCL c = GetCandle(
|
||||
_Symbol,
|
||||
_Period,
|
||||
startBarIndex);
|
||||
|
||||
//
|
||||
double value = GetAppliedPrice(
|
||||
c,
|
||||
nearestAppliedTo);
|
||||
|
||||
//
|
||||
nearestBuffer[bar_index] = value;
|
||||
}
|
||||
|
||||
//
|
||||
// Mediest ...
|
||||
void CalculateMediest(int bar_index)
|
||||
{
|
||||
//
|
||||
if (mMediestPeriod == NULL)
|
||||
{
|
||||
//
|
||||
switch (mediestMethod)
|
||||
{
|
||||
//
|
||||
case X_PERIOD_AUTO:
|
||||
mMediestPeriod = GetMediestTimeFrame(_Period);
|
||||
break;
|
||||
|
||||
//
|
||||
case X_PERIOD_NOTHING:
|
||||
case X_PERIOD_MANUALLY:
|
||||
mMediestPeriod = mediestPeriod;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
datetime barTime = iTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
bar_index);
|
||||
datetime startTime = GetPeriodStartTime(
|
||||
_Symbol,
|
||||
mMediestPeriod,
|
||||
barTime);
|
||||
int startBarIndex = iBarShift(
|
||||
_Symbol,
|
||||
_Period,
|
||||
startTime);
|
||||
|
||||
//
|
||||
XOHCL c = GetCandle(
|
||||
_Symbol,
|
||||
_Period,
|
||||
startBarIndex);
|
||||
|
||||
//
|
||||
double value = GetAppliedPrice(
|
||||
c,
|
||||
mediestAppliedTo);
|
||||
|
||||
//
|
||||
mediestBuffer[bar_index] = value;
|
||||
}
|
||||
|
||||
//
|
||||
// Farest ...
|
||||
void CalculateFarest(int bar_index)
|
||||
{
|
||||
//
|
||||
if (mFarestPeriod == NULL)
|
||||
{
|
||||
//
|
||||
switch (farestMethod)
|
||||
{
|
||||
//
|
||||
case X_PERIOD_AUTO:
|
||||
mFarestPeriod = GetFarestTimeFrame(_Period);
|
||||
break;
|
||||
|
||||
//
|
||||
case X_PERIOD_NOTHING:
|
||||
case X_PERIOD_MANUALLY:
|
||||
mFarestPeriod = farestPeriod;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
datetime barTime = iTime(
|
||||
_Symbol,
|
||||
_Period,
|
||||
bar_index);
|
||||
datetime startTime = GetPeriodStartTime(
|
||||
_Symbol,
|
||||
mFarestPeriod,
|
||||
barTime);
|
||||
int startBarIndex = iBarShift(
|
||||
_Symbol,
|
||||
_Period,
|
||||
startTime);
|
||||
|
||||
//
|
||||
XOHCL c = GetCandle(
|
||||
_Symbol,
|
||||
_Period,
|
||||
startBarIndex);
|
||||
|
||||
//
|
||||
double value = GetAppliedPrice(
|
||||
c,
|
||||
farestAppliedTo);
|
||||
|
||||
//
|
||||
farestBuffer[bar_index] = value;
|
||||
}
|
||||
|
||||
//
|
||||
// Generate Comment ...
|
||||
string GenerateDescriptionComment()
|
||||
{
|
||||
//
|
||||
string result = "";
|
||||
|
||||
//
|
||||
result += "Nearest : " + EnumToString(mNearestPeriod) + "\n";
|
||||
result += "Mediest : " + EnumToString(mMediestPeriod) + "\n";
|
||||
result += "Farest : " + EnumToString(mFarestPeriod) + "\n";
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
Reference in New Issue
Block a user