AntRobot/indicators/PointsWinnerFromPT.mq5
super.admin 9174d587ba convert
2025-05-30 14:40:14 +02:00

180 lines
13 KiB
MQL5

//+------------------------------------------------------------------+
//| PointsWinnerFromPT.mq5 |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include "..\\utils\\Utils.mqh"
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 4
//--- plot winners
#property indicator_label1 "winners"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- plot losser
#property indicator_label2 "losser"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrPeru
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//--- plot targetProfit
#property indicator_label3 "Target profit(%)"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrGreen
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- plot stopLoss
#property indicator_label4 "Stop loss(%)"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrRed
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//inputs
input int countBarsToTest = 24;
//--- indicator buffers
double winnersBuffer[];
double losserBuffer[];
double targetProfitBuffer[];
double stopLossBuffer[];
Utils utils;
int indicatorProbableProfitHandle=0;
//+------------------------------------------------------------------+
//| 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(winnersBuffer,true);
ArraySetAsSeries(losserBuffer,true);
ArraySetAsSeries(targetProfitBuffer,true);
ArraySetAsSeries(stopLossBuffer,true);
//--- indicator buffers mapping
SetIndexBuffer(0,winnersBuffer,INDICATOR_DATA);
SetIndexBuffer(1,losserBuffer,INDICATOR_DATA);
SetIndexBuffer(2,targetProfitBuffer,INDICATOR_DATA);
SetIndexBuffer(3,stopLossBuffer,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
PlotIndexSetInteger(0,PLOT_ARROW,159);
PlotIndexSetInteger(1,PLOT_ARROW,159);
string path = utils.GetRelativeProgramPath();
indicatorProbableProfitHandle=iCustom(Symbol(),PERIOD_CURRENT,path + "\\ProbableTarget",0.10,countBarsToTest,countBarsToTest);
if(indicatorProbableProfitHandle==INVALID_HANDLE) {
Print("Probable profit error inicialization, Code = ",GetLastError());
return -1;
}
//---
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<countBarsToTest)
return(0);
if(CopyBuffer(indicatorProbableProfitHandle,0,0,rates_total,targetProfitBuffer) != rates_total) {
Print("CopyBuffer error de copia 0, Code = ",GetLastError());
return 0;
}
if(CopyBuffer(indicatorProbableProfitHandle,1,0,rates_total,stopLossBuffer) != rates_total) {
Print("CopyBuffer error de copia 1, Code = ",GetLastError());
return 0;
}
for(int i=0; i<rates_total && !IsStopped(); i++) {
double init = open[i];
double targetProfit = targetProfitBuffer[i];
double stopLoss = stopLossBuffer[i];
int above = 0;
int below = 0;
int losser = 0;
if(i >= countBarsToTest) {
for(int j = i; j > i - countBarsToTest && !IsStopped(); j--) {
if(absDiffInPercent(init, low[j]) >= stopLoss || absDiffInPercent(init, high[j]) >= stopLoss) {
losser += 1;
}
if(absDiffInPercent(init, high[j]) >= targetProfit) {
above += 1;
}
if(absDiffInPercent(init, low[j]) >= targetProfit) {
below += 1;
}
if(losser > 0) {
losserBuffer[i] = high[i] + 100;
break;
}
if(above > 0 && below > 0) {
winnersBuffer[i] = high[i] + 100;
break;
}
}
//this code is for the bars that are not winner o losser, if close is bigger than open that bars are winner
//if((above == 0 && below == 0 && losser == 0) || !(above > 0 && below > 0)) {
// if(init > close[i - countBarsToTest]) {
// losserBuffer[i] = high[i] + 100;
// } else {
// winnersBuffer[i] = high[i] + 100;
// }
// }
}
}
//--- 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);
}
//+------------------------------------------------------------------+