//@version=5 indicator("ATR-Levels", overlay=true) // Input for ATR period atrPeriod =…
- User
//@version=5 indicator("ATR-Levels", overlay=true)
// Input for ATR period atrPeriod = input.int(10, title="ATR Period")
// Fetch monthly time frame high, low, and open [monthlyHigh, monthlyLow, monthlyOpen, monthlyTime] = request.security(syminfo.tickerid, "M", [high, low, open, time])
// ATR Calculation H_L = array.new_float() openATR = array.new_float()
for i = 0 to atrPeriod - 1 h = monthlyHigh[i] l = monthlyLow[i] o = monthlyOpen[i] array.push(H_L, h - l) array.push(openATR, o)
atrSum = 0.0
for i = 0 to array.size(H_L) - 2 Sum = (array.get(H_L, i) + array.get(H_L, i+1)) atrSum := atrSum + Sum / 2 / array.get(openATR, i+1) * 100
ATRavg = atrSum / (array.size(H_L) - 1) ATRavg := na(ATRavg) ? 0 : ATRavg // Handle potential NA values
priceHigh = array.get(openATR, 0) * (1 + ATRavg / 100) priceLow = array.get(openATR, 0) * (1 - ATRavg / 100) priceHigh_5 = array.get(openATR, 0) * (1 + ATRavg / 200) priceLow_5 = array.get(openATR, 0) * (1 - ATRavg / 200)
// Текущее время currentTime = timenow start_time = timestamp(year(currentTime), month(currentTime), 1) end_time = timestamp(year(currentTime), month(currentTime) + 1, 1)
// Ensure to adjust the timeframe of lines rendering to a reasonable range of current time and future time for visibility in the script var line priceHighLine = na var line priceLowLine = na var line priceHighHalfLine = na var line priceLowHalfLine = na
if not na(priceHighLine) line.delete(priceHighLine) if not na(priceLowLine) line.delete(priceLowLine) if not na(priceHighHalfLine) line.delete(priceHighHalfLine) if not na(priceLowHalfLine) line.delete(priceLowHalfLine)
// Plot lines using specific time positions with xloc.bar_time priceHighLine := line.new(x1=start_time, y1=priceHigh, x2=end_time, y2=priceHigh, color=color.red, width=2, style=line.style_solid, xloc=xloc.bar_time) priceLowLine := line.new(x1=start_time, y1=priceLow, x2=end_time, y2=priceLow, color=color.red, width=2, style=line.style_solid, xloc=xloc.bar_time) priceHighHalfLine := line.new(x1=start_time, y1=priceHigh_5, x2=end_time, y2=priceHigh_5, color=color.blue, width=2, style=line.style_solid, xloc=xloc.bar_time) priceLowHalfLine := line.new(x1=start_time, y1=priceLow_5, x2=end_time, y2=priceLow_5, color=color.blue, width=2, style=line.style_solid, xloc=xloc.bar_time) есть такая программа на pinescript как мне переделать её что бы значения были как у программы на mql5 //+------------------------------------------------------------------+ //| ProjectName | //| Copyright 2020, CompanyName | //| http://www.companyname.net | //+------------------------------------------------------------------+ #property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0
//— входные параметры input int InpAtrPeriod = 10; // Период ATR
//±-----------------------------------------------------------------+ //| Функция инициализации пользовательского индикатора | //±-----------------------------------------------------------------+ int OnInit() { return (INIT_SUCCEEDED); }
//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectDelete(0,"priceHigh"); ObjectDelete(0,"priceLow"); ObjectDelete(0,"priceHigh_0.5"); ObjectDelete(0,"priceLow_0.5"); }
//±-----------------------------------------------------------------+ //| Функция итерации пользовательского индикатора | //±-----------------------------------------------------------------+ 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[]) { double H_L[10]; double openATR[10]; int m = 0;
// Расчет значений ATR с использованием данных месячного таймфрейма for(int i = 0; i < InpAtrPeriod && i < iBars(_Symbol, PERIOD_MN1); i++) { int shift = i; double maxHigh = iHigh(_Symbol, PERIOD_MN1, shift); double minLow = iLow(_Symbol, PERIOD_MN1, shift); openATR[m] = iOpen(_Symbol, PERIOD_MN1, shift); H_L[m] = NormalizeDouble(maxHigh - minLow, _Digits); m++; }
double ATR = 0; for(int i = 0; i < m - 1; i++) { double sum = H_L[i] + H_L[i + 1]; ATR += sum / 2 / openATR[i + 1] * 100; }
double ATRavg = NormalizeDouble(ATR / (m - 1), 2);
if(m > 0) { double priceHigh = openATR[0] + (openATR[0] * ATRavg / 100); double priceLow = openATR[0] - (openATR[0] * ATRavg / 100); double priceHigh_2 = openATR[0] + (openATR[0] * ATRavg / 200); double priceLow_2 = openATR[0] - (openATR[0] * ATRavg / 200);
datetime time_start = iTime(_Symbol, PERIOD_MN1, 0); datetime time_end = time_start + 31 * 24 * 60 * 60; ObjectCreate(0, "priceHigh", OBJ_TREND, 0, time_start, priceHigh, time_end, priceHigh); ObjectCreate(0, "priceLow", OBJ_TREND, 0, time_start, priceLow, time_end, priceLow); ObjectCreate(0, "priceHigh_0.5", OBJ_TREND, 0, time_start, priceHigh_2, time_end, priceHigh_2); ObjectCreate(0, "priceLow_0.5", OBJ_TREND, 0, time_start, priceLow_2, time_end, priceLow_2); ObjectSetInteger(0, "priceHigh", OBJPROP_COLOR, clrCrimson); ObjectSetInteger(0, "priceLow", OBJPROP_COLOR, clrCrimson); ObjectSetInteger(0, "priceHigh_0.5", OBJPROP_COLOR, clrBlue); ObjectSetInteger(0, "priceLow_0.5", OBJPROP_COLOR, clrBlue); ObjectSetInteger(0, "priceHigh", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceLow", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceHigh_0.5", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceLow_0.5", OBJPROP_WIDTH, 2); }return (rates_total); }
//+------------------------------------------------------------------+
- Assistant
To transform the Pine Script into an equivalent MQL5 script while aligning the calculations, we need to convert several key components like handling time frames, calculating ATR, and rendering lines on the chart. Here is an approach to mimic the Pine script functionalities in MQL5:
-
Time Frame Handling:
- Both scripts are obtaining high, low, and open prices from the monthly time frame. In MQL5, this is done using
iHigh,iLow, andiOpenfunctions.
- Both scripts are obtaining high, low, and open prices from the monthly time frame. In MQL5, this is done using
-
ATR Calculation:
- Calculate the ATR using the same logic for monthly high and low values as seen in Pine Script, adjusted the data access to match MQL5 arrays.
-
Rendering Lines:
- Use
ObjectCreateandObjectSetIntegerto draw lines on the chart in MQL5. Ensure that colors and line widths match the Pine script styles.
- Use
Here is a revised version of your MQL5 script, considering these elements:
//+------------------------------------------------------------------+ //| ATR Levels MQL5 Script | //| Adapted for MQL5 Platform | //+------------------------------------------------------------------+ #property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0 input int InpAtrPeriod = 10; // ATR Period //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectDelete(0, "priceHigh"); ObjectDelete(0, "priceLow"); ObjectDelete(0, "priceHigh_0.5"); ObjectDelete(0, "priceLow_0.5"); } //+------------------------------------------------------------------+ 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[]) { double H_L[10]; // Array for storing monthly high-low differences double openATR[10]; // Array for storing monthly open prices int countBars = iBars(_Symbol, PERIOD_MN1); // Total bars in Monthly chart int m = 0; for(int i = 0; i < InpAtrPeriod && i < countBars; i++) { double maxHigh = iHigh(_Symbol, PERIOD_MN1, i); double minLow = iLow(_Symbol, PERIOD_MN1, i); openATR[m] = iOpen(_Symbol, PERIOD_MN1, i); H_L[m] = maxHigh - minLow; m++; } double ATR = 0; for(int i = 0; i < m - 1; i++) { double sum = H_L[i] + H_L[i + 1]; ATR += sum / 2 / openATR[i + 1] * 100; } double ATRavg = ATR / (m - 1); if(m > 0) { double priceHigh = openATR[0] * (1 + ATRavg / 100); double priceLow = openATR[0] * (1 - ATRavg / 100); double priceHigh_0_5 = openATR[0] * (1 + ATRavg / 200); double priceLow_0_5 = openATR[0] * (1 - ATRavg / 200); datetime timeStart = iTime(_Symbol, PERIOD_MN1, 0); datetime timeEnd = timeStart + 28 * 24 * 60 * 60; // Approximately one month ahead CreateLine("priceHigh", timeStart, priceHigh, timeEnd, priceHigh, clrCrimson); CreateLine("priceLow", timeStart, priceLow, timeEnd, priceLow, clrCrimson); CreateLine("priceHigh_0.5", timeStart, priceHigh_0_5, timeEnd, priceHigh_0_5, clrBlue); CreateLine("priceLow_0.5", timeStart, priceLow_0_5, timeEnd, priceLow_0_5, clrBlue); } return(rates_total); } // Custom function to streamline object creation void CreateLine(string name, datetime time1, double price1, datetime time2, double price2, color clr) { ObjectCreate(0, name, OBJ_TREND, 0, time1, price1, time2, price2); ObjectSetInteger(0, name, OBJPROP_COLOR, clr); ObjectSetInteger(0, name, OBJPROP_WIDTH, 2); } //+------------------------------------------------------------------+Key Adjustments and Considerations:
- Ensure the MQL5 script correctly handles historical data access for monthly candles by capturing both
highandlowusing loops and arrays. - The
CreateLinefunction is used to reduce repetitive code when creating line objects. - Adjust
timeEndto be approximately one month later to simulate the current month using28 * 24 * 60 * 60. - Verify the naming of lines and properties align with the Pine Script, such as colors and line widths.
- Ensure that calculations do not run into array index issues, handle scripts robustly for lesser data points available than ATR periods.
-