maku77/maku77.github.io

convert mql4 to mql5

Opened this issue · 0 comments

I want to convert this mt4 indicator to mt5 and I have done it to an extend but got hook, if anyone can assist

SEE THE MT4 BELOW
#property link "RSI-3TF_ALERT_25"

#property indicator_separate_window
#property indicator_minimum 0.0
#property indicator_maximum 100.0
#property indicator_levelcolor DarkGray
#property indicator_levelwidth 0
#property indicator_buffers 8
#property indicator_color1 Fuchsia
#property indicator_color2 Aqua
#property indicator_color3 Red
#property indicator_color4 Yellow
#property indicator_color5 White
#property indicator_color6 White
#property indicator_color7 GreenYellow
#property indicator_color8 Yellow
#property indicator_level1 93.0
#property indicator_level2 7.0
#property indicator_level3 50.0

extern string RSI_INPUT_1 = "=== RSI A ===";
extern int RSI_Period_1 = 2;
extern int RSI_Period_2 = 2;
extern int RSI_Period_3 = 2;
extern string RSI_INPUT_2 = "=== RSI B ===";
extern int RSI_Period_B1 = 2;
extern int RSI_Period_B2 = 2;
extern int RSI_Period_B3 = 2;
extern int TF1 = 0;
extern int TF2 = 0;
extern int TF3 = 0;
bool re_unused_128 = TRUE;
bool re_unused_132 = TRUE;
bool re_unused_136 = TRUE;
extern int LineSize1 = 1;
extern int LineSize2 = 1;
extern int LineSize3 = 2;
extern int LineSize4 = 1;
extern int DotSizeRSI = 1;
extern int DotSizeARROW = 1;
extern int DotSizeZERO = 1;
extern int DotSizeRSI5 = 4;
extern int NumberOfBars = 500;
extern bool AlertOn = TRUE;
extern bool AlertHighProbabilityOnly = FALSE;
extern bool Email_Alerts = true;
double re_176;
double re_184;
double re_192;
double re_200;
double re_208;
double re_216;
double re_224;
double re_232;
double re_240;
double re_248;
double re_256;
double re_264;
double re_272;
double re_280;
double re_288;
double re_296;
double re_ibuf_304[];
double re_ibuf_308[];
double re_ibuf_312[];
double re_ibuf_316[];
double re_ibuf_320[];
double re_ibuf_324[];
double re_ibuf_328[];
double re_ibuf_332[];
int re_336 = 0;
datetime re_dtPrevSignal;

void init() {
string ls_unused_12;

SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, LineSize1);
SetIndexBuffer(0, re_ibuf_304);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, LineSize2);
SetIndexBuffer(1, re_ibuf_308);
SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, LineSize3);
SetIndexBuffer(2, re_ibuf_312);
SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, DotSizeRSI);
SetIndexArrow(3, 159);
SetIndexBuffer(3, re_ibuf_316);
SetIndexStyle(4, DRAW_ARROW, STYLE_SOLID, DotSizeARROW);
SetIndexArrow(4, 233);
SetIndexBuffer(4, re_ibuf_320);
SetIndexStyle(5, DRAW_ARROW, STYLE_SOLID, DotSizeARROW);
SetIndexArrow(5, 234);
SetIndexBuffer(5, re_ibuf_324);
SetIndexStyle(6, DRAW_ARROW, STYLE_SOLID, DotSizeZERO);
SetIndexArrow(6, 159);
SetIndexBuffer(6, re_ibuf_328);
SetIndexStyle(7, DRAW_ARROW, STYLE_SOLID, DotSizeRSI5);
SetIndexArrow(7, 159);
SetIndexBuffer(7, re_ibuf_332);
re_224 = 0;
re_232 = 0;
re_288 = 0;
re_296 = 0;
switch (TF1) {
case 1:
ls_unused_12 = "Period_M1";
break;
case 5:
ls_unused_12 = "Period_M5";
break;
case 15:
ls_unused_12 = "Period_M15";
break;
case 30:
ls_unused_12 = "Period_M30";
break;
case 60:
ls_unused_12 = "Period_H1";
break;
case 240:
ls_unused_12 = "Period_H4";
break;
case 1440:
ls_unused_12 = "Period_D1";
break;
case 10080:
ls_unused_12 = "Period_W1";
break;
case 43200:
ls_unused_12 = "Period_MN1";
break;
default:
ls_unused_12 = "Current Timeframe";
}
SetStepTF1_Up();
}

void SetStepTF1_Up() {
switch (Period()) {
case PERIOD_M1:
SetValues(30);
return;
case PERIOD_M5:
SetValues(60);
return;
case PERIOD_M15:
SetValues(240);
return;
case PERIOD_M30:
SetValues(1440);
return;
case PERIOD_H1:
SetValues(10080);
return;
case PERIOD_H4:
SetValues(43200);
return;
case PERIOD_D1:
SetValues(43200);
return;
case PERIOD_W1:
SetValues(43200);
return;
case PERIOD_MN1:
SetValues(43200);
return;
}
}

void SetValues(int ai_0) {
TF1 = ai_0;
}

void SetStepTF2_Up() {
switch (Period()) {
case PERIOD_M1:
SetValues2(15);
return;
case PERIOD_M5:
SetValues2(30);
return;
case PERIOD_M15:
SetValues2(60);
return;
case PERIOD_M30:
SetValues2(240);
return;
case PERIOD_H1:
SetValues2(1440);
return;
case PERIOD_H4:
SetValues2(10080);
return;
case PERIOD_D1:
SetValues2(43200);
return;
case PERIOD_W1:
SetValues2(43200);
return;
case PERIOD_MN1:
SetValues2(43200);
return;
}
}

void SetValues2(int ai_0) {
TF2 = ai_0;
}

void SetStepTF3_Up() {
switch (Period()) {
case PERIOD_M1:
SetValues3(5);
return;
case PERIOD_M5:
SetValues3(15);
return;
case PERIOD_M15:
SetValues3(30);
return;
case PERIOD_M30:
SetValues3(60);
return;
case PERIOD_H1:
SetValues3(240);
return;
case PERIOD_H4:
SetValues3(1440);
return;
case PERIOD_D1:
SetValues3(10080);
return;
case PERIOD_W1:
SetValues3(43200);
return;
case PERIOD_MN1:
SetValues3(43200);
return;
}
}

void SetValues3(int ai_0) {
TF3 = ai_0;
}

int start() {
static datetime lastRefreshTime = 0;

// Check if 10 minutes (600 seconds) have passed since the last refresh
if (TimeCurrent() - lastRefreshTime >= 600) {
    lastRefreshTime = TimeCurrent(); // Update the last refresh time

    int li_4;
    int l_shift_12;
    int l_shift_16;
    int l_shift_20;
    double l_irsi_24;
    double l_irsi_32;
    double l_irsi_40;
    int li_0 = Bars - NumberOfBars;

    if (NumberOfBars == 0) li_4 = Bars - 1;
    else li_4 = NumberOfBars - 1;
    
    for (int li_8 = li_4; li_8 >= 0; li_8--) {
        l_shift_20 = iBarShift(NULL, TF1, Time[li_8]);
        l_shift_16 = iBarShift(NULL, TF2, Time[li_8]);
        l_shift_12 = iBarShift(NULL, TF3, Time[li_8]);

        l_irsi_40 = iRSI(NULL, TF1, RSI_Period_1, PRICE_CLOSE, l_shift_20);
        l_irsi_32 = iRSI(NULL, TF2, RSI_Period_2, PRICE_CLOSE, l_shift_16);
        l_irsi_24 = iRSI(NULL, TF3, RSI_Period_3, PRICE_CLOSE, l_shift_12);

        re_ibuf_304[li_8] = l_irsi_40;
        re_ibuf_308[li_8] = l_irsi_32;
        re_ibuf_312[li_8] = l_irsi_24;

        if (AlertOn && li_8 == 0) {
            string message = "";
            if (l_irsi_40 > 93.0 && l_irsi_32 > 93.0 && l_irsi_24 > 93.0) {
                message = "RSI above 93.0 on all timeframes!";
            } else if (l_irsi_40 < 7.0 && l_irsi_32 < 7.0 && l_irsi_24 < 7.0) {
                message = "RSI below 7.0 on all timeframes!";
            }

            if (StringLen(message) > 0 && TimeCurrent() != re_dtPrevSignal) {
                Alert(message);
                re_dtPrevSignal = TimeCurrent();

                if (Email_Alerts) {
                    SendMail("RSI Alert", message);
                }
            }
        }
    }
}
return 0;

}
AND SEE MY MY5 BELOW
//+------------------------------------------------------------------+
//| RSI COPILOT.mq5 |
//| Copyright 2024, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict

//--- Indicator properties
#property indicator_separate_window
#property indicator_minimum 0.0
#property indicator_maximum 100.0
#property indicator_buffers 5
#property indicator_plots 5

//--- Plot Line
#property indicator_label1 "StepLine"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2

#property indicator_label2 "StepLine"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1

#property indicator_label3 "StepLine"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrGreen
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1

#property indicator_label4 "AlertSell"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 clrWhite
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1

#property indicator_label5 "AlertBuy"
#property indicator_type5 DRAW_ARROW
#property indicator_color5 clrYellow
#property indicator_style5 STYLE_SOLID
#property indicator_width5 1

//--- input parameters
input int TF1 = 10080; // in minutes
input int TF2 = 1440;
input int TF3 = 240;
input int RSI_Period_B1 = 2;
input int RSI_Period_B2 = 2;
input int RSI_Period_B3 = 2;
input int NumberOfBars = 500;

#property indicator_level1 93.0
#property indicator_level2 50.0
#property indicator_level3 7.0

//--- indicator buffers
double StepLinebuffer1[];
double StepLinebuffer2[];
double StepLinebuffer3[];
double arrow_up[];
double arrow_down[];

//--- global variables for handles
int handle1, handle2, handle3;

//--- OnInit function
int OnInit()
{
// Convert timeframes from minutes to MQL5 timeframes
ENUM_TIMEFRAMES tf1 = ConvertMinutesToTimeframe(TF1);
ENUM_TIMEFRAMES tf2 = ConvertMinutesToTimeframe(TF2);
ENUM_TIMEFRAMES tf3 = ConvertMinutesToTimeframe(TF3);

//--- create RSI handles
handle1 = iRSI(_Symbol, tf1, RSI_Period_B1, PRICE_CLOSE);
handle2 = iRSI(_Symbol, tf2, RSI_Period_B2, PRICE_CLOSE);
handle3 = iRSI(_Symbol, tf3, RSI_Period_B3, PRICE_CLOSE);

if(handle1 == INVALID_HANDLE || handle2 == INVALID_HANDLE || handle3 == INVALID_HANDLE)
{
Print("Error creating RSI handles. Error code: ", GetLastError());
return INIT_FAILED;
}

//--- set buffers and their properties
SetIndexBuffer(0, StepLinebuffer1,INDICATOR_DATA);
SetIndexBuffer(1, StepLinebuffer2,INDICATOR_DATA);
SetIndexBuffer(2, StepLinebuffer3,INDICATOR_DATA);
SetIndexBuffer(3, arrow_up);
SetIndexBuffer(4, arrow_down);

// Set arrow properties (arrows may need different settings depending on the platform and display)
PlotIndexSetInteger(3, PLOT_ARROW, 233); // White arrow up
PlotIndexSetInteger(4, PLOT_ARROW, 234); // Yellow arrow down

return INIT_SUCCEEDED;
}

//--- OnCalculate 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 timeframes from minutes to MQL5 timeframes
ENUM_TIMEFRAMES tf1 = ConvertMinutesToTimeframe(TF1);
ENUM_TIMEFRAMES tf2 = ConvertMinutesToTimeframe(TF2);
ENUM_TIMEFRAMES tf3 = ConvertMinutesToTimeframe(TF3);

// Check available bars for each timeframe
int bars1 = Bars(_Symbol, tf1);
int bars2 = Bars(_Symbol, tf2);
int bars3 = Bars(_Symbol, tf3);

// Print available bars for debugging
Print("Bars available for TF1: ", bars1, " TF2: ", bars2, " TF3: ", bars3);

// Determine the minimum number of bars available across all timeframes
int min_bars = MathMin(MathMin(bars1, bars2), bars3);

// Ensure the number of bars requested does not exceed the minimum available
int calculated_bars = MathMin(rates_total, min_bars);

// Resize buffers based on calculated bars
ArrayResize(StepLinebuffer1, calculated_bars);
ArrayResize(StepLinebuffer2, calculated_bars);
ArrayResize(StepLinebuffer3, calculated_bars);
ArrayResize(arrow_up, calculated_bars);
ArrayResize(arrow_down, calculated_bars);

// Ensure buffers are set as series for proper indexing
ArraySetAsSeries(StepLinebuffer1, true);
ArraySetAsSeries(StepLinebuffer2, true);
ArraySetAsSeries(StepLinebuffer2, true);
ArraySetAsSeries(arrow_up, true);
ArraySetAsSeries(arrow_down, true);

// Initialize arrow buffers to EMPTY_VALUE
ArrayFill(arrow_up, 0, calculated_bars, EMPTY_VALUE);
ArrayFill(arrow_down, 0, calculated_bars, EMPTY_VALUE);

// Copy data into buffers and check if CopyBuffer was successful
int copied1 = CopyBuffer(handle1, 0, 0, calculated_bars, StepLinebuffer1);
int copied2 = CopyBuffer(handle2, 0, 0, calculated_bars, StepLinebuffer2);
int copied3 = CopyBuffer(handle3, 0, 0, calculated_bars, StepLinebuffer3);

if (copied1 <= 0 || copied2 <= 0 || copied3 <= 0)
{
    Print("Error copying buffer data. Error code: ", GetLastError());
    return prev_calculated;
}

// Fill RSI buffers with step line format
for(int i = 1; i < rates_total; i++)
{
    if (StepLinebuffer1[i] == EMPTY_VALUE)
        StepLinebuffer1[i] = close[i - 1];
    if (StepLinebuffer2[i] == EMPTY_VALUE)
        StepLinebuffer2[i] = close[i - 1];
    if (StepLinebuffer3[i] == EMPTY_VALUE)
        StepLinebuffer3[i] = close[i - 1];
}

// Process alerts
for (int i = 0; i < calculated_bars; i++)
{
    if (StepLinebuffer1[i] > 50 && StepLinebuffer2[i] > 50 && StepLinebuffer3[i] > 50)
    {
        arrow_up[i] = 30;
    }
    else if (StepLinebuffer1[i] < 50 && StepLinebuffer2[i] < 50 && StepLinebuffer3[i] < 50)
    {
        arrow_down[i] = 70;
    }
}

return calculated_bars;

}

//--- Convert minutes to MQL5 timeframe
ENUM_TIMEFRAMES ConvertMinutesToTimeframe(int minutes)
{
switch(minutes)
{
case 1:
return PERIOD_M1;
case 5:
return PERIOD_M5;
case 15:
return PERIOD_M15;
case 30:
return PERIOD_M30;
case 60:
return PERIOD_H1;
case 240:
return PERIOD_H4;
case 1440:
return PERIOD_D1;
case 10080:
return PERIOD_W1;
case 43200:
return PERIOD_MN1;
default:
return PERIOD_CURRENT;
}
}
//+------------------------------------------------------------------+

My email simitradingsolutions@gmail.com