Am trying to convert an mt4 indicator to pine script but am having issue understanding the interpretation of this code.
First i dont know why the 240 was used, and why the code below was written in that format, is there another way to write that
Thanks
Am trying to convert an mt4 indicator to pine script but am having issue understanding the interpretation of this code.
First i dont know why the 240 was used, and why the code below was written in that format, is there another way to write that
Thanks
this code snippet is part of a larger program that populates arrays with historical price data, specifically open, high, low, and close prices.
The mode for calculating high and low prices seems to depend on the value of ModeHL .
The code is structured to collect this data for different time periods or bars, as indicated by the loop variable j , and for different elements, as indicated by the variable i
you can upload the full code using the source button above for more accurate results
@Dominik Egert, @Zyad Nhra, here is the code
//------------------------------------------------------------------ #property copyright "Mr. X" #property link "www.metaquotes.net" //------------------------------------------------------------------ #property indicator_separate_window #property indicator_levelcolor White #property indicator_buffers 2 #property indicator_color1 Lime #property indicator_color2 Red #property indicator_level1 25.0 #property indicator_level2 15.0 extern int Length = 150; extern int HistoryBars = 500; extern int TimeFrame1 = 0; extern int TimeFrame2 = 0; extern bool UseHighLowMode = TRUE; extern bool OnlineMode = TRUE; extern bool FileMode = FALSE; extern bool HistoryMode = FALSE; extern bool EnableAlert = false; extern bool EnableSound = FALSE; extern bool EnableEmail = FALSE; extern bool EnableGV = FALSE; extern double SignalThreshold = 25.0; double HighValue1[]; double HighValue2[]; double HighValue3[]; int BarShiftValue; int BarsOnTimeFrame2; int CounterValue; double HighSumValue; double LowSumValue; double HighValue4; double LowValue1; double LowValue2; double HighValue5; double LowValue3; double HighValue6; double LowValue4; int BarShift1Value; int BarShift2Value; int LimitValue; int LoopCounterValue; int LoopLimit1; int LoopLimit2; int FileHandleReadValue; int ChartWindowValue; int FileHandleValue; bool HistoryModeEnabled; bool GlobalBooleanValue; int FileHandleWriteValue; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int init() { ChartWindowValue = WindowOnDropped(); if (FileMode) FileDelete(Symbol() + "-SP-" + Period() + ".ini"); if (TimeFrame2 == 0) TimeFrame2 = (int)Period(); if(TimeFrame2 / Period()!=0) HistoryBars = NormalizeDouble(HistoryBars / (TimeFrame2 / Period()), 0); SetIndexBuffer(0, HighValue1); SetIndexBuffer(1, HighValue2); SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2); SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2); ArrayResize(HighValue3, HistoryBars + Length); ArrayResize(LowValue1, HistoryBars + Length); ArrayResize(LowValue2, HistoryBars + Length); ArrayResize(LowValue3, HistoryBars + Length); ArrayResize(HighValue4, HistoryBars + Length); ArrayResize(LowValue4, HistoryBars + Length); CurrentTimeFrame = Period(); if (FileMode) FileHandleValue = FileOpen(Symbol() + "-SP-" + Period() + ".ini", FILE_WRITE, " "); return (0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { int Count; int BarShift2; int BarShift1; int TimeFromString; int IntFromString1; int IntFromString2; int FileHandleReadValue; if (OnlineMode || FileMode) { if (iTime(NULL, TimeFrame2, 0) == LastCheckedTime) { GlobalVariableSet("PeakRepainter"+Symbol()+(string)Period()+"HighValue1",HighValue1[2]); GlobalVariableSet("PeakRepainter"+Symbol()+(string)Period()+"HighValue2",HighValue2[2]); DrawHorizontalLine("HighValue1[1]",HighValue1[2],indicator_color1); DrawHorizontalLine("HighValue2[1]",HighValue2[2],indicator_color2); return (0); } LastCheckedTime = iTime(NULL, TimeFrame2, 0); for (i = HistoryBars + Length; i > 0; i--) { BarsOnTimeFrame2 = iBarShift(NULL, TimeFrame1, iTime(NULL, TimeFrame2, i)); Count = 0; for (j = BarsOnTimeFrame2; j > BarsOnTimeFrame2 - TimeFrame2; j--) { HighValue3[i][Count] = iClose(NULL, TimeFrame1, j); if (UseHighLowMode) LowValue1[i][Count] = iHigh(NULL, TimeFrame1, j); else LowValue1[i][Count] = MathMax(iOpen(NULL, TimeFrame1, j), iClose(NULL, TimeFrame1, j)); if (UseHighLowMode) LowValue2[i][Count] = iLow(NULL, TimeFrame1, j); else LowValue2[i][Count] = MathMin(iOpen(NULL, TimeFrame1, j), iClose(NULL, TimeFrame1, j)); Count++; } } LimitValue = NormalizeDouble((Bars - IndicatorCounted()) / (TimeFrame2 / Period()), 0); if(LimitValue<0) LimitValue=0; if (OnlineMode && (!IsTesting())) LimitValue = HistoryBars; for (i = LimitValue; i > 0; i--) { LoopCounterValue = 0; HighSumValue = 0; LowSumValue = 0; HighestClose = 0; LowestLow = 1000000; while (LoopCounterValue < Length) { BarShiftValue = i + LoopCounterValue; HighSumValue = 0; LowSumValue = 0; for (CounterValue = 0; CounterValue < TimeFrame2; CounterValue++) { if (HighValue3[BarShiftValue][CounterValue] > 0.0) HighValue4 = HighValue3[BarShiftValue][CounterValue]; if (LowValue1[BarShiftValue][CounterValue] > 0.0) HighValue5 = LowValue1[BarShiftValue][CounterValue]; if (LowValue2[BarShiftValue][CounterValue] > 0.0) HighValue6 = LowValue2[BarShiftValue][CounterValue]; if (HighValue5 > HighValue4) { HighValue4 = HighValue5; HighSumValue += HighValue4; } if (HighValue6 < LowestLow) { LowestLow = HighValue6; LowSumValue += HighValue4; } } if (HighSumValue > 0.0) HighSumValue += HighSumValue; if (LowSumValue > 0.0) LowSumValue += LowSumValue; LoopCounterValue++; } if (HighSumValue > 0.0 && LowSumValue > 0.0) { if (FileMode && HistoryModeEnabled != Time[i]) { HistoryModeEnabled = Time[i]; FileWrite(FileHandleValue, StringConcatenate(TimeToStr(Time[i]), ";", DoubleToStr(HighSumValue / LowSumValue, 0), ";", DoubleToStr(LowSumValue / HighSumValue, 0))); } BarShift2Value = iBarShift(NULL, 0, iTime(NULL, TimeFrame2, i)); for (BarShift1Value = BarShift2Value; BarShift1Value > BarShift2Value - TimeFrame2 / Period(); BarShift1Value--) { HighValue1[BarShift1Value] = HighSumValue / LowSumValue; HighValue2[BarShift1Value] = LowSumValue / HighSumValue; } } } GlobalVariableSet("PeakRepainter"+Symbol()+(string)Period()+"HighValue1",HighValue1[2]); GlobalVariableSet("PeakRepainter"+Symbol()+(string)Period()+"HighValue2",HighValue2[2]); DrawHorizontalLine("HighValue1[1]",HighValue1[2],indicator_color1); DrawHorizontalLine("HighValue2[1]",HighValue2[2],indicator_color2); return (0); }
I'm too lazy/busy to analyse the code myself, so out of curiosity and as an experiment, I gave it to Genie in VS Code. Here is the result. I see some obvious errors in the code like not checking the handle of opened file before writing to it but I did not dive into actual logic of the indicator itself.
//------------------------------------------------------------------ // Copyright information and link to the author's website #property copyright "Mr. X" #property link "www.metaquotes.net" //------------------------------------------------------------------ // Indicator properties for MetaTrader platform #property indicator_separate_window #property indicator_levelcolor White #property indicator_buffers 2 #property indicator_color1 Lime #property indicator_color2 Red #property indicator_level1 25.0 #property indicator_level2 15.0 // External variables that can be modified by the user extern int Length = 150; extern int HistoryBars = 500; extern int TimeFrame1 = 0; extern int TimeFrame2 = 0; extern bool UseHighLowMode = TRUE; extern bool OnlineMode = TRUE; extern bool FileMode = FALSE; extern bool HistoryMode = FALSE; extern bool EnableAlert = false; extern bool EnableSound = FALSE; extern bool EnableEmail = FALSE; extern bool EnableGV = FALSE; extern double SignalThreshold = 25.0; // Internal variables used in the code double HighValue1[]; double HighValue2[]; double HighValue3[]; int BarShiftValue; int BarsOnTimeFrame2; int CounterValue; double HighSumValue; double LowSumValue; double HighValue4; double LowValue1; double LowValue2; double HighValue5; double LowValue3; double HighValue6; double LowValue4; int BarShift1Value; int BarShift2Value; int LimitValue; int LoopCounterValue; int LoopLimit1; int LoopLimit2; int FileHandleReadValue; int ChartWindowValue; int FileHandleValue; bool HistoryModeEnabled; bool GlobalBooleanValue; int FileHandleWriteValue; //+------------------------------------------------------------------+ // Initialization function, executed once when the indicator is added to the chart //+------------------------------------------------------------------+ int init() { // Get the window number where the Expert Advisor was dropped ChartWindowValue = WindowOnDropped(); // If FileMode is true, delete the specified file if (FileMode) FileDelete(Symbol() + "-SP-" + Period() + ".ini"); // If TimeFrame2 is not set, assign it the current chart period if (TimeFrame2 == 0) TimeFrame2 = (int)Period(); // Normalize HistoryBars based on the ratio of TimeFrame2 to the current chart period if(TimeFrame2 / Period()!=0) HistoryBars = NormalizeDouble(HistoryBars / (TimeFrame2 / Period()), 0); // Set HighValue1 and HighValue2 as custom indicator buffers SetIndexBuffer(0, HighValue1); SetIndexBuffer(1, HighValue2); // Set the style of the lines drawn by the custom indicator buffers SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2); SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2); // Resize arrays to accommodate historical data ArrayResize(HighValue3, HistoryBars + Length); ArrayResize(LowValue1, HistoryBars + Length); ArrayResize(LowValue2, HistoryBars + Length); ArrayResize(LowValue3, HistoryBars + Length); ArrayResize(HighValue4, HistoryBars + Length); ArrayResize(LowValue4, HistoryBars + Length); // Get the current chart period CurrentTimeFrame = Period(); // If FileMode is true, open the specified file for writing if (FileMode) FileHandleValue = FileOpen(Symbol() + "-SP-" + Period() + ".ini", FILE_WRITE, " "); // Return 0 to indicate successful initialization return (0); } // Main function executed on every tick int start() { // Declare variables int Count; int BarShift2; int BarShift1; int TimeFromString; int IntFromString1; int IntFromString2; int FileHandleReadValue; // Check if the indicator is running in OnlineMode or FileMode if (OnlineMode || FileMode) { // If the last checked time matches the current bar's opening time on TimeFrame2, update global variables and draw horizontal lines if (iTime(NULL, TimeFrame2, 0) == LastCheckedTime) { GlobalVariableSet("PeakRepainter"+Symbol()+(string)Period()+"HighValue1",HighValue1[2]); GlobalVariableSet("PeakRepainter"+Symbol()+(string)Period()+"HighValue2",HighValue2[2]); DrawHorizontalLine("HighValue1[1]",HighValue1[2],indicator_color1); DrawHorizontalLine("HighValue2[1]",HighValue2[2],indicator_color2); return (0); } // Update LastCheckedTime to the current bar's opening time on TimeFrame2 LastCheckedTime = iTime(NULL, TimeFrame2, 0); // Loop over historical bars and calculate HighValue3 and LowValue1/LowValue2 arrays based on the closing, high, low, and open prices for (i = HistoryBars + Length; i > 0; i--) { BarsOnTimeFrame2 = iBarShift(NULL, TimeFrame1, iTime(NULL, TimeFrame2, i)); Count = 0; for (j = BarsOnTimeFrame2; j > BarsOnTimeFrame2 - TimeFrame2; j--) { HighValue3[i][Count] = iClose(NULL, TimeFrame1, j); if (UseHighLowMode) LowValue1[i][Count] = iHigh(NULL, TimeFrame1, j); else LowValue1[i][Count] = MathMax(iOpen(NULL, TimeFrame1, j), iClose(NULL, TimeFrame1, j)); if (UseHighLowMode) LowValue2[i][Count] = iLow(NULL, TimeFrame1, j); else LowValue2[i][Count] = MathMin(iOpen(NULL, TimeFrame1, j), iClose(NULL, TimeFrame1, j)); Count++; } } // Calculate the limit value for the next loop LimitValue = NormalizeDouble((Bars - IndicatorCounted()) / (TimeFrame2 / Period()), 0); if(LimitValue<0) LimitValue=0; if (OnlineMode && (!IsTesting())) LimitValue = HistoryBars; // Loop over historical bars up to LimitValue and calculate HighSumValue and LowSumValue for (i = LimitValue; i > 0; i--) { LoopCounterValue = 0; HighSumValue = 0; LowSumValue = 0; HighestClose = 0; LowestLow = 1000000; while (LoopCounterValue < Length) { BarShiftValue = i + LoopCounterValue; HighSumValue = 0; LowSumValue = 0; for (CounterValue = 0; CounterValue < TimeFrame2; CounterValue++) { if (HighValue3[BarShiftValue][CounterValue] > 0.0) HighValue4 = HighValue3[BarShiftValue][CounterValue]; if (LowValue1[BarShiftValue][CounterValue] > 0.0) HighValue5 = LowValue1[BarShiftValue][CounterValue]; if (LowValue2[BarShiftValue][CounterValue] > 0.0) HighValue6 = LowValue2[BarShiftValue][CounterValue]; if (HighValue5 > HighValue4) { HighValue4 = HighValue5; HighSumValue += HighValue4; } if (HighValue6 < LowestLow) { LowestLow = HighValue6; LowSumValue += HighValue4; } } if (HighSumValue > 0.0) HighSumValue += HighSumValue; if (LowSumValue > 0.0) LowSumValue += LowSumValue; LoopCounterValue++; } // If both HighSumValue and LowSumValue are greater than 0, write to file and calculate HighValue1 and HighValue2 arrays if (HighSumValue > 0.0 && LowSumValue > 0.0) { if (FileMode && HistoryModeEnabled != Time[i]) { HistoryModeEnabled = Time[i]; FileWrite(FileHandleValue, StringConcatenate(TimeToStr(Time[i]), ";", DoubleToStr(HighSumValue / LowSumValue, 0), ";", DoubleToStr(LowSumValue / HighSumValue, 0))); } BarShift2Value = iBarShift(NULL, 0, iTime(NULL, TimeFrame2, i)); for (BarShift1Value = BarShift2Value; BarShift1Value > BarShift2Value - TimeFrame2 / Period(); BarShift1Value--) { HighValue1[BarShift1Value] = HighSumValue / LowSumValue; HighValue2[BarShift1Value] = LowSumValue / HighSumValue; } } } // Update global variables and draw horizontal lines GlobalVariableSet("PeakRepainter"+Symbol()+(string)Period()+"HighValue1",HighValue1[2]); GlobalVariableSet("PeakRepainter"+Symbol()+(string)Period()+"HighValue2",HighValue2[2]); DrawHorizontalLine("HighValue1[1]",HighValue1[2],indicator_color1); DrawHorizontalLine("HighValue2[1]",HighValue2[2],indicator_color2); // Return 0 to indicate successful execution return (0); }
--EDIT--
I see that when you give a smaller chunk of code to process the comments are more precise. Example.
// Update LastCheckedTime to the current bar's opening time on TimeFrame2 LastCheckedTime = iTime(NULL, TimeFrame2, 0); // Loop over historical bars and calculate HighValue3 and LowValue1/LowValue2 arrays based on the closing, high, low, and open prices for (i = HistoryBars + Length; i > 0; i--) { // Get the shift value for the current bar on TimeFrame1 BarsOnTimeFrame2 = iBarShift(NULL, TimeFrame1, iTime(NULL, TimeFrame2, i)); // Initialize count variable Count = 0; // Loop over the bars on TimeFrame2 for (j = BarsOnTimeFrame2; j > BarsOnTimeFrame2 - TimeFrame2; j--) { // Store the close price of the current bar on TimeFrame1 in HighValue3 array HighValue3[i][Count] = iClose(NULL, TimeFrame1, j); // If UseHighLowMode is true, store the high price of the current bar on TimeFrame1 in LowValue1 array // Else, store the maximum of open and close prices of the current bar on TimeFrame1 in LowValue1 array if (UseHighLowMode) LowValue1[i][Count] = iHigh(NULL, TimeFrame1, j); else LowValue1[i][Count] = MathMax(iOpen(NULL, TimeFrame1, j), iClose(NULL, TimeFrame1, j)); // If UseHighLowMode is true, store the low price of the current bar on TimeFrame1 in LowValue2 array // Else, store the minimum of open and close prices of the current bar on TimeFrame1 in LowValue2 array if (UseHighLowMode) LowValue2[i][Count] = iLow(NULL, TimeFrame1, j); else LowValue2[i][Count] = MathMin(iOpen(NULL, TimeFrame1, j), iClose(NULL, TimeFrame1, j)); // Increment count variable Count++; } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
First i dont know why the 240 was used, and why the code below was written in that format, is there another way to write that
Thanks