Initial Commit ...
This commit is contained in:
@@ -0,0 +1,351 @@
|
||||
/////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SaherElm IT Center MQL5 Trend Power Detector Oscillator
|
||||
// ---------------------------------------------------------
|
||||
// Name: XTPD
|
||||
// Description: trend detecting based on volumes in both sides ...
|
||||
//
|
||||
//
|
||||
// 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 XTPD Oscillator"
|
||||
#property strict
|
||||
|
||||
//
|
||||
// START Constants ...
|
||||
//
|
||||
#define ShortName "XTPD"
|
||||
//
|
||||
// END Constants ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Inputs ...
|
||||
//
|
||||
input int length = 14; // Market Length
|
||||
//
|
||||
// END Inputs ...
|
||||
//
|
||||
|
||||
//
|
||||
// Includes Logging Library ...
|
||||
#include "../Libraries/x-saherelm.log.lib.mq5";
|
||||
|
||||
//
|
||||
// Includes Common Library ...
|
||||
#include "../Libraries/x-saherelm.common.lib.mq5";
|
||||
|
||||
//
|
||||
// START Buffers ...
|
||||
//
|
||||
#property indicator_separate_window
|
||||
|
||||
//
|
||||
#property indicator_buffers 3
|
||||
#property indicator_plots 2
|
||||
|
||||
//
|
||||
#define bullishPowerBufferIndex 0
|
||||
#define bearishPowerBufferIndex 1
|
||||
#define stateBufferIndex 2
|
||||
|
||||
//
|
||||
double bullishPowerBuffer[];
|
||||
double bearishPowerBuffer[];
|
||||
double stateBuffer[];
|
||||
|
||||
#property indicator_label1 "XBullPower"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrLime
|
||||
#property indicator_style1 STYLE_DOT
|
||||
#property indicator_width1 1
|
||||
|
||||
#property indicator_label2 "XBearPower"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrRed
|
||||
#property indicator_style2 STYLE_DOT
|
||||
#property indicator_width2 1
|
||||
//
|
||||
// END Buffers ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
//
|
||||
// END Global Definitions: Variables, Properties and etc ...
|
||||
//
|
||||
|
||||
//
|
||||
// START Event Handlers ...
|
||||
//
|
||||
//
|
||||
// Initialization ...
|
||||
int OnInit()
|
||||
{
|
||||
//
|
||||
// Validate Inputs ...
|
||||
if (!ValidateInputs())
|
||||
{
|
||||
return INIT_PARAMETERS_INCORRECT;
|
||||
}
|
||||
|
||||
//
|
||||
logTag = ShortName;
|
||||
|
||||
//
|
||||
// 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 ...
|
||||
}
|
||||
|
||||
//
|
||||
// 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);
|
||||
|
||||
//
|
||||
limit = prev_calculated < maxLength ? rates_total - (maxLength + 1) : rates_total - prev_calculated;
|
||||
|
||||
//
|
||||
// Main Loop ...
|
||||
for (int i = limit; 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 >= 5)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Define Indexes and Styles ...
|
||||
void DefineBuffers()
|
||||
{
|
||||
//
|
||||
string bullishPowerBufferLabel = "BullPow " + "(" + (string)length + ")";
|
||||
string bearishPowerBufferLabel = "BearPow " + "(" + (string)length + ")";
|
||||
|
||||
//
|
||||
// Bullish Volume Buffer ...
|
||||
ArraySetAsSeries(bullishPowerBuffer, true);
|
||||
SetIndexBuffer(bullishPowerBufferIndex, bullishPowerBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bullishPowerBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bullishPowerBufferIndex, PLOT_LABEL, bullishPowerBufferLabel);
|
||||
|
||||
//
|
||||
// Bearish Volume Buffer ...
|
||||
ArraySetAsSeries(bearishPowerBuffer, true);
|
||||
SetIndexBuffer(bearishPowerBufferIndex, bearishPowerBuffer, INDICATOR_DATA);
|
||||
PlotIndexSetInteger(bearishPowerBufferIndex, PLOT_DRAW_BEGIN, length * 2);
|
||||
PlotIndexSetString(bearishPowerBufferIndex, PLOT_LABEL, bearishPowerBufferLabel);
|
||||
|
||||
//
|
||||
// State Buffer ...
|
||||
ArraySetAsSeries(stateBuffer, true);
|
||||
SetIndexBuffer(stateBufferIndex, stateBuffer, INDICATOR_CALCULATIONS);
|
||||
}
|
||||
|
||||
//
|
||||
// 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 xBullishPowerValue = 0;
|
||||
double xBearishPowerValue = 1;
|
||||
|
||||
//
|
||||
// Loop through bar index and Market Length ...
|
||||
for (int i = bar_index + length + 1; i > bar_index && !IsStopped(); i--)
|
||||
{
|
||||
//
|
||||
XOHCL candle = GetCandle(i);
|
||||
|
||||
//
|
||||
bool isBullish = IsBullishCandle(candle);
|
||||
bool isBearish = IsBearishCandle(candle);
|
||||
|
||||
//
|
||||
if (isBullish)
|
||||
{
|
||||
xBullishPowerValue++;
|
||||
}
|
||||
else if (isBearish)
|
||||
{
|
||||
xBearishPowerValue++;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
bullishPowerBuffer[bar_index] = xBullishPowerValue;
|
||||
bearishPowerBuffer[bar_index] = xBearishPowerValue;
|
||||
|
||||
//
|
||||
// Calculate State ...
|
||||
bool isBullishCrossOverBearish = IsCrossOver(
|
||||
bullishPowerBuffer,
|
||||
bearishPowerBuffer,
|
||||
bar_index);
|
||||
bool isBullishOverBearish = IsOver(
|
||||
bullishPowerBuffer,
|
||||
bearishPowerBuffer,
|
||||
bar_index);
|
||||
bool isBullishCrossUnderBearish = IsCrossUnder(
|
||||
bullishPowerBuffer,
|
||||
bearishPowerBuffer,
|
||||
bar_index);
|
||||
bool isBullishUnderBearish = IsUnder(
|
||||
bullishPowerBuffer,
|
||||
bearishPowerBuffer,
|
||||
bar_index);
|
||||
|
||||
//
|
||||
double stateValue = X_XTPD_NEUTURAL;
|
||||
if (isBullishCrossOverBearish)
|
||||
{
|
||||
stateValue = X_XTPD_BULLISH_CROSSED_OVER_BEARISH;
|
||||
}
|
||||
else if (isBullishCrossUnderBearish)
|
||||
{
|
||||
stateValue = X_XTPD_BULLISH_CROSSED_UNDER_BEARISH;
|
||||
}
|
||||
else if (isBullishOverBearish)
|
||||
{
|
||||
stateValue = X_XTPD_BULLISH_OVER_BEARISH;
|
||||
}
|
||||
else if (isBullishUnderBearish)
|
||||
{
|
||||
stateValue = X_XTPD_BULLISH_UNDER_BEARISH;
|
||||
}
|
||||
|
||||
//
|
||||
stateBuffer[bar_index] = stateValue;
|
||||
}
|
||||
//
|
||||
// END Functions ...
|
||||
//
|
||||
Reference in New Issue
Block a user