Author: Copyright ?2008, MQL Service
20piprenko

Overview of Script Logic

This script is designed to process and convert standard price movements into a specialized charting format known as Renko bars. Unlike standard candles that represent fixed periods of time (e.g., every 1 minute or 5 minutes), Renko bars focus exclusively on price movement, ignoring the passage of time until a specific price change occurs.

The core objective of this script is to analyze historical data, identify these price-based "blocks," and save them into a custom history file for use in trading analysis.

Key Functional Components

1. Environment and Configuration Checks

Upon startup, the script performs several safety checks to ensure it can operate correctly:

  • Permissions: It verifies that the platform allows the use of External Libraries (DLLs), which are necessary for some of its internal communication tasks.
  • Timeframe Validation: It specifically checks if the user is running the script on a 1-Minute (M1) chart, as it requires high-frequency data to accurately calculate the smaller price movements.
  • Dynamic Scaling: The script detects if the broker uses "5-digit" pricing (common in modern forex markets). If detected, it automatically adjusts its internal calculations to ensure the price sensitivity remains accurate regardless of the decimal places.

2. Historical Data Processing

The script scans back through a user-defined range of historical bars. It iterates through these prices to construct a continuous chain of Renko bars:

  • Price Movement Tracking: It looks for specific "jumps" in price. If the price moves enough to meet the required criteria, it creates a new bar.
  • Directional Logic: The script keeps track of whether the trend is moving upward or downward. It ensures that each new bar is formed based on the preceding bar's high or low point to maintain the integrity of the Renko sequence.
  • Data Recording: As it processes this history, it writes every calculated bar into a .hst file. This includes the timestamp, open price, high, low, close, and a custom volume calculation.

3. Real-Time Monitoring

Once the historical data is processed, the script enters a continuous "Live" mode:

  • Continuous Observation: It monitors the current market price tick-by-tick.
  • Dynamic Construction: As new live prices arrive, it compares them against the last known Renko bar. If the price movement exceeds the defined threshold, it generates a new Renko bar instantly.
  • Real-Time File Updates: Each new bar formed in real-time is immediately written to the history file and flushed to the disk, ensuring that the recorded history stays up-to-date with current market action.

4. System Communication

The script includes a specific mechanism to interact with the trading platform's user interface. It identifies the active chart window and sends internal signals (messages) to notify the system that data is being updated or processed, ensuring smooth integration between the script's calculations and the visual display of the charts.

Summary for the User

In plain terms, this script acts as a converter. It takes "messy" minute-by-minute price data and cleans it into organized "bricks" (Renko bars) that only show meaningful price changes. It then builds a custom history file of these bricks so you can see a cleaner, more trend-focused view of the market movement, free from the "noise" of time-based fluctuations.

Price Data Components
Series array that contains tick volumes of each bar Series array that contains open prices of each barSeries array that contains close prices for each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Miscellaneous
It issuies visual alerts to the screenIt writes information to fileIt writes information to fileIt writes information to fileIt opens Message Boxes to the userIt writes information to file
12 Views
0 Downloads
0 Favorites
20piprenko
/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright ?2008, MQL Service"
#property link      "http://www.mqlservice.net"
#property show_inputs

#include <WinUser32.mqh>

#import "user32.dll"
   int RegisterWindowMessageA(string a0);
#import

extern int BarsRange = 20;
extern int TimeFrame = 2;
extern int MaxBars = 46000;
int g_file_88 = -1;

double LocalPoint;
int LotDigit;


double volume(int ai_0) {
   return (iVolume(Symbol(), PERIOD_M1, ai_0));
}

double open(int ai_0) {
   return (iOpen(Symbol(), PERIOD_M1, ai_0));
}

double close(int ai_0) {
   return (iClose(Symbol(), PERIOD_M1, ai_0));
}

double high(int ai_0) {
   return (iHigh(Symbol(), PERIOD_M1, ai_0));
}

double low(int ai_0) {
   return (iLow(Symbol(), PERIOD_M1, ai_0));
}

int time(int ai_0) {
   return (ai_0);
}


int init() {

   
  LocalPoint = Point*MathPow(10,Digits%2);
  LotDigit=MarketInfo( Symbol(), MODE_DIGITS );
  
  return (0);
}

int start() {
   int li_16;
   int li_28;
   double ld_80;
   int lia_124[13];
   double ld_144;
   if (_invalid(BarsRange, 0, "BarsRange")) return (0);
   if (_invalid(TimeFrame, 1, "TimeFrame")) return (0);
   if (!IsDllsAllowed()) {
      Alert(WindowExpertName(), ": Please Allow DLL imports to run this script");
      Comment(WindowExpertName(), " aborted due to disabled DLL calls");
      return (-1);
   }
   int li_0 = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   string ls_4 = Symbol();
   int li_96 = 0;
   int l_count_100 = 0;
   int li_104 = 400;
   int li_116 = TimeFrame;
   int l_digits_120 = Digits;
   for (int li_12 = StringFind(ls_4, ".", 0); li_12 > 0; li_12 = StringFind(ls_4, ".", 0)) ls_4 = StringSubstr(ls_4, 0, li_12) + StringSubstr(ls_4, li_12 + 1);
   g_file_88 = FileOpenHistory(ls_4 + li_116 + ".hst", FILE_BIN|FILE_WRITE);
   if (g_file_88 < 0) {
      Alert(WindowExpertName(), ": Cannot create ", ls_4 + TimeFrame + ".hst");
      Comment(WindowExpertName(), " aborted: Cannot create ", ls_4 + TimeFrame + ".hst");
      return (-1);
   }
   Print("$Id: //mqlservice/mt4files/experts/scripts/s-Renko bars.mq4#7 $");
   string ls_108 = "(C)opyright 2008, MQL Service";
   FileWriteInteger(g_file_88, li_104, LONG_VALUE);
   FileWriteString(g_file_88, ls_108, 64);
   FileWriteString(g_file_88, ls_4, 12);
   FileWriteInteger(g_file_88, li_116, LONG_VALUE);
   FileWriteInteger(g_file_88, l_digits_120, LONG_VALUE);
   FileWriteInteger(g_file_88, 0, LONG_VALUE);
   FileWriteInteger(g_file_88, 0, LONG_VALUE);
   FileWriteArray(g_file_88, lia_124, 0, 13);
   FileFlush(g_file_88);
   int li_36 = 60 * li_116;
   int li_20 = MathMin(MaxBars, iBars(Symbol(), PERIOD_M1) - 1);
   if (li_20 <= 0)
      if (MessageBox("No 1M bars available, there will be no history bars!\n\nContinue?", WindowExpertName(), MB_YESNO|MB_ICONQUESTION) == IDNO) return (0);
   if (Period() != PERIOD_M1)
      if (MessageBox("It should be run on 1M chart!\n\nContinue?", WindowExpertName(), MB_YESNO|MB_ICONQUESTION) == IDNO) return (0);
   int li_128 = 1;
   if (Digits == 3 || Digits > 4) {
      if (MessageBox("5 digits broker detected!\n\nRescale BarsRange?", WindowExpertName(), MB_YESNO|MB_ICONQUESTION) == IDYES) {
         BarsRange = 10 * BarsRange;
         li_128 = 10;
      }
   }
   int li_24 = time(iTime(Symbol(), PERIOD_M1, li_20));
   Print("Starting from ", TimeToStr(li_24, TIME_DATE|TIME_MINUTES|TIME_SECONDS));
   int li_32 = FileTell(g_file_88);
   double ld_48 = close(li_20);
   double ld_56 = ld_48;
   double ld_72 = 1.0;
   double ld_64 = ld_48;
   double ld_40 = ld_48;
   double ld_88 = ld_48;
   int li_132 = 1;
   int li_136 = 1;
   int li_140 = 1;
   for (li_12 = li_20 - 1; li_12 >= 0; li_12--) {
      li_28 = time(iTime(Symbol(), PERIOD_M1, li_12));
      if (Volume[li_12] > 0.0) li_140 = MathMax(li_128, MathCeil((high(li_12) - low(li_12)) / Point / Volume[li_12]));
      else li_140 = li_128;
      li_140 = MathRound(li_140 / li_128) * li_128;
      if (open(li_12) > close(li_12)) {
         li_16 = high(li_12) / Point;
         while (li_16 >= low(li_12) / Point) {
            ld_144 = NormalizeDouble(li_16 * Point, Digits);
            ld_48 = MathMin(ld_48, ld_144);
            ld_56 = MathMax(ld_56, ld_144);
            ld_72 += MathRound(MathAbs(ld_64 - ld_144) / Point);
            ld_64 = ld_144;
            while (MathRound((ld_56 - ld_88) / Point) >= li_132 * BarsRange || MathRound((ld_88 - ld_48) / Point) >= li_136 * BarsRange) {
               li_32 = FileTell(g_file_88);
               if (MathRound((ld_88 - ld_48) / Point) >= li_136 * BarsRange) {
                  if (li_136 == 2) ld_88 -= BarsRange * Point;
                  ld_64 = ld_88 - BarsRange * Point;
                  ld_48 = ld_64;
                  ld_40 = ld_88;
                  ld_56 = ld_88;
                  li_136 = 1;
                  li_132 = 2;
               }
               if (MathRound((ld_56 - ld_88) / Point) >= li_132 * BarsRange) {
                  if (li_132 == 2) ld_88 += BarsRange * Point;
                  ld_64 = ld_88 + BarsRange * Point;
                  ld_56 = ld_64;
                  ld_40 = ld_88;
                  ld_48 = ld_88;
                  li_132 = 1;
                  li_136 = 2;
               }
               FileWriteInteger(g_file_88, li_24, LONG_VALUE);
               FileWriteDouble(g_file_88, ld_40, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_48, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_56, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_64, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_72, DOUBLE_VALUE);
               li_24++;
               FileFlush(g_file_88);
               l_count_100++;
               ld_88 = ld_64;
               li_24 = MathMax(li_28, li_24);
               ld_40 = ld_64;
               ld_48 = MathMin(ld_144, ld_64);
               ld_56 = MathMax(ld_144, ld_64);
               ld_64 = ld_144;
               ld_72 = 1.0;
            }
            li_16 -= li_140;
         }
      } else {
         li_16 = low(li_12) / Point;
         while (li_16 <= high(li_12) / Point) {
            ld_144 = NormalizeDouble(li_16 * Point, Digits);
            ld_48 = MathMin(ld_48, ld_144);
            ld_56 = MathMax(ld_56, ld_144);
            ld_72 += MathRound(MathAbs(ld_64 - ld_144) / Point);
            ld_64 = ld_144;
            while (MathRound((ld_56 - ld_88) / Point) >= li_132 * BarsRange || MathRound((ld_88 - ld_48) / Point) >= li_136 * BarsRange) {
               li_32 = FileTell(g_file_88);
               if (MathRound((ld_88 - ld_48) / Point) >= li_136 * BarsRange) {
                  if (li_136 == 2) ld_88 -= BarsRange * Point;
                  ld_64 = ld_88 - BarsRange * Point;
                  ld_48 = ld_64;
                  ld_40 = ld_88;
                  ld_56 = ld_88;
                  li_136 = 1;
                  li_132 = 2;
               }
               if (MathRound((ld_56 - ld_88) / Point) >= li_132 * BarsRange) {
                  if (li_132 == 2) ld_88 += BarsRange * Point;
                  ld_64 = ld_88 + BarsRange * Point;
                  ld_56 = ld_64;
                  ld_40 = ld_88;
                  ld_48 = ld_88;
                  li_132 = 1;
                  li_136 = 2;
               }
               FileWriteInteger(g_file_88, li_24, LONG_VALUE);
               FileWriteDouble(g_file_88, ld_40, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_48, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_56, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_64, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_72, DOUBLE_VALUE);
               li_24++;
               FileFlush(g_file_88);
               l_count_100++;
               ld_88 = ld_64;
               li_24 = MathMax(li_28, li_24);
               ld_40 = ld_64;
               ld_48 = MathMin(ld_144, ld_64);
               ld_56 = MathMax(ld_144, ld_64);
               ld_64 = ld_144;
               ld_72 = 1.0;
            }
            li_16 += li_140;
         }
      }
   }
   li_32 = FileTell(g_file_88);
   ld_64 = Close[0];
   FileWriteInteger(g_file_88, TimeCurrent(), LONG_VALUE);
   FileWriteDouble(g_file_88, ld_40, DOUBLE_VALUE);
   FileWriteDouble(g_file_88, ld_48, DOUBLE_VALUE);
   FileWriteDouble(g_file_88, ld_56, DOUBLE_VALUE);
   FileWriteDouble(g_file_88, ld_64, DOUBLE_VALUE);
   FileWriteDouble(g_file_88, ld_72, DOUBLE_VALUE);
   FileFlush(g_file_88);
   Print(l_count_100, " record(s) written");
   if (li_96 == 0) {
      li_96 = WindowHandle(ls_4, li_116);
      if (li_96 != 0) Print("Chart window detected");
   }
   _postMessage(li_96, li_0);
   li_24 = MathMax(TimeCurrent(), li_24);
   while (IsStopped() == FALSE) {
      if (RefreshRates()) {
         FileSeek(g_file_88, li_32, SEEK_SET);
         ld_48 = MathMin(ld_48, Close[0]);
         ld_56 = MathMax(ld_56, Close[0]);
         ld_72 += MathRound(MathAbs(ld_64 - close(0)) / Point);
         if (MathRound((ld_56 - ld_88) / Point) < li_132 * BarsRange && MathRound((ld_88 - ld_48) / Point) < li_136 * BarsRange) {
            ld_80 = volume(0);
            ld_64 = close(0);
         } else {
            while (MathRound((ld_56 - ld_88) / Point) >= li_132 * BarsRange || MathRound((ld_88 - ld_48) / Point) >= li_136 * BarsRange) {
               if (MathRound((ld_88 - ld_48) / Point) >= li_136 * BarsRange) {
                  if (li_136 == 2) ld_88 -= BarsRange * Point;
                  ld_64 = ld_88 - BarsRange * Point;
                  ld_48 = ld_64;
                  ld_40 = ld_88;
                  ld_56 = ld_88;
                  li_136 = 1;
                  li_132 = 2;
               }
               if (MathRound((ld_56 - ld_88) / Point) >= li_132 * BarsRange) {
                  if (li_132 == 2) ld_88 += BarsRange * Point;
                  ld_64 = ld_88 + BarsRange * Point;
                  ld_56 = ld_64;
                  ld_40 = ld_88;
                  ld_48 = ld_88;
                  li_132 = 1;
                  li_136 = 2;
               }
               FileWriteInteger(g_file_88, li_24, LONG_VALUE);
               FileWriteDouble(g_file_88, ld_40, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_48, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_56, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_64, DOUBLE_VALUE);
               FileWriteDouble(g_file_88, ld_72, DOUBLE_VALUE);
               li_32 = FileTell(g_file_88);
               li_24++;
               li_24 = MathMax(li_24, TimeCurrent());
               ld_88 = ld_64;
               li_24 = MathMax(li_28, li_24);
               ld_40 = ld_64;
               ld_48 = MathMin(Close[0], ld_64);
               ld_56 = MathMax(Close[0], ld_64);
               ld_64 = Close[0];
               ld_72 = 1.0;
            }
         }
         FileWriteInteger(g_file_88, li_24, LONG_VALUE);
         FileWriteDouble(g_file_88, ld_40, DOUBLE_VALUE);
         FileWriteDouble(g_file_88, ld_48, DOUBLE_VALUE);
         FileWriteDouble(g_file_88, ld_56, DOUBLE_VALUE);
         FileWriteDouble(g_file_88, ld_64, DOUBLE_VALUE);
         FileWriteDouble(g_file_88, ld_72, DOUBLE_VALUE);
         FileFlush(g_file_88);
         if (li_96 == 0) {
            li_96 = WindowHandle(ls_4, li_116);
            if (li_96 != 0) Print("Chart window detected");
         }
         _postMessage(li_96, li_0);
      } else Sleep(100);
   }
   return (0);
}

void _postMessage(int ai_0, int ai_4) {
   if (ai_0 != 0) {
      PostMessageA(ai_0, WM_COMMAND, 33324, 1);
      PostMessageA(ai_0, ai_4, 2, 1);
   }
}

void deinit() {
   if (g_file_88 >= 0) {
      FileClose(g_file_88);
      g_file_88 = -1;
   }
}

bool _invalid(int ai_0, int ai_4, string as_8) {
   if (ai_0 <= ai_4) {
      Alert(WindowExpertName() + " " + Symbol() + Period() + " invalid parameter ", as_8, ": ", ai_0, ". Must be grater than ", ai_4);
      return (TRUE);
   }
   return (FALSE);
}

Comments