AntRobot/indicators/Amplitude.mq5

217 lines
7.9 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 14:40:14 +02:00
//+------------------------------------------------------------------+
//| Amplitude.mq5 |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_plots 7
//--- plot amplitude
#property indicator_label1 "Amplitude factor"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- plot High wick
#property indicator_label2 "High wick"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrGreen
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- plot Low Wick
#property indicator_label3 "Low Wick"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- plot Load balance
#property indicator_label4 "Load balance"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrBrown
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//--- plot Load balance of wicks
#property indicator_label5 "Load balance of Wicks"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrBrown
#property indicator_style5 STYLE_SOLID
#property indicator_width5 1
//--- plot candle direction
#property indicator_label6 "Candle direction"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrBrown
#property indicator_style6 STYLE_SOLID
#property indicator_width6 1
//--- plot position
#property indicator_label7 "Position"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrBrown
#property indicator_style7 STYLE_SOLID
#property indicator_width7 1
//inputs
input double amplitudeUmbral = 0.5;
input double wickUmbral = 0.20;
input int loadBalanceBars = 2;
//--- indicator buffers
double highWickBuffer[];
double lowWickBuffer[];
double amplitudeBuffer[];
double loadBalanceBuffer[];
double wickLoadBalanceBuffer[];
double candleDirectionBuffer[];
double positionBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit() {
//chart settings
ChartSetInteger(0,CHART_COLOR_BACKGROUND,0);
ChartSetInteger(0, CHART_FOREGROUND,16777215);
ChartSetInteger(0,CHART_COLOR_CHART_UP,7451452);
ChartSetInteger(0,CHART_COLOR_CHART_DOWN,255);
ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,7451452);
ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,255);
ChartSetInteger(0,CHART_COLOR_CHART_LINE,65280);
ChartSetInteger(0,CHART_COLOR_GRID,2697513);
ChartSetInteger(0,CHART_SHIFT,1);
ChartSetInteger(0,CHART_SHOW_ASK_LINE,1);
ChartSetInteger(0,CHART_SHOW_BID_LINE,1);
ChartSetInteger(0,CHART_SHOW_LAST_LINE,1);
ChartSetInteger(0,CHART_SHOW_PERIOD_SEP,1);
ArraySetAsSeries(amplitudeBuffer,true);
ArraySetAsSeries(highWickBuffer,true);
ArraySetAsSeries(lowWickBuffer,true);
ArraySetAsSeries(loadBalanceBuffer,true);
ArraySetAsSeries(wickLoadBalanceBuffer,true);
ArraySetAsSeries(candleDirectionBuffer,true);
ArraySetAsSeries(positionBuffer,true);
//--- indicator buffers mapping
SetIndexBuffer(0,amplitudeBuffer,INDICATOR_DATA);
SetIndexBuffer(1,highWickBuffer,INDICATOR_DATA);
SetIndexBuffer(2,lowWickBuffer,INDICATOR_DATA);
SetIndexBuffer(3,loadBalanceBuffer,INDICATOR_DATA);
SetIndexBuffer(4,wickLoadBalanceBuffer,INDICATOR_DATA);
SetIndexBuffer(5,candleDirectionBuffer,INDICATOR_DATA);
SetIndexBuffer(6,positionBuffer,INDICATOR_DATA);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[]) {
//---
//--- convert to series
ArraySetAsSeries(open,true);
ArraySetAsSeries(close,true);
ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
if(rates_total<loadBalanceBars)
return(0);
int forLength = rates_total - loadBalanceBars;
for(int i=0; i<forLength && !IsStopped(); i++) {
double amplitude = absDiffInPercent(open[i], close[i]);
double highWick;
double lowWick;
if(open[i] > close[i]) {
highWick = absDiffInPercent(open[i], high[i]);
lowWick = absDiffInPercent(close[i], low[i]);
} else {
highWick = absDiffInPercent(close[i], high[i]);
lowWick = absDiffInPercent(open[i], low[i]);
}
amplitudeBuffer[i] = amplitude;
highWickBuffer[i] = highWick;
lowWickBuffer[i] = lowWick;
double loadBalanceTmp = 0;
double wickLoadBalanceTmp = 0;
double init = open[i];
for(int j=i + 1 ; j <= i + loadBalanceBars; j++) {
double absDiffInPercentTmp;
if(high[j] > init) {
loadBalanceTmp = loadBalanceTmp + absDiffInPercent(init, high[j]);
}
if(low[j] < init) {
loadBalanceTmp = loadBalanceTmp - absDiffInPercent(init, low[j]);
}
if(open[j] < close[j]) {
wickLoadBalanceTmp = wickLoadBalanceTmp + (absDiffInPercent(close[j], high[j]) - absDiffInPercent(open[j], low[j]));
} else {
wickLoadBalanceTmp = wickLoadBalanceTmp + (absDiffInPercent(open[j], high[j]) - absDiffInPercent(close[j], low[j]));
}
}
loadBalanceBuffer[i] = loadBalanceTmp;
wickLoadBalanceBuffer[i] = wickLoadBalanceTmp;
candleDirectionBuffer[i] = (open[i] < close[i]) ? 1 : -1;
positionBuffer[i] = getPosition(i);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double absDiffInPercent(const double initExtreme, const double endExtreme) {
return MathAbs(((endExtreme - initExtreme) * 100) / initExtreme);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int getPosition(int i) {
if(amplitudeBuffer[i] <= amplitudeUmbral) {
return 0;
}
if(candleDirectionBuffer[i] == 1) {
if(highWickBuffer[i] >= wickUmbral) {
return -1;
} else {
return 1;
}
}
if(candleDirectionBuffer[i] == -1 ) {
if(lowWickBuffer[i] >= wickUmbral) {
return 1;
} else {
return -1;
}
}
return 0;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+