Renko_Chart__From_Ticks

Download script
Author: Binoy Raphael
Renko_Chart__From_Ticks

Overview of the Renko Chart Generator Logic

This script is designed to transform standard market price movements into a specialized "Renko" chart format. Unlike traditional charts that display every price fluctuation over time, Renko charts focus exclusively on price movement, filtering out minor "noise" to show you the clear trend of the market.

Here is a breakdown of how the logic works:

Core Concept: Movement Over Time

The primary goal of this script is to generate a sequence of "boxes." A new box is only created when the price moves by a specific, predefined amount (the "Renko Size"). If the price fluctuates slightly without reaching that threshold, the chart remains unchanged. This allows traders to see the actual trajectory of the market more clearly.

Key Functional Steps:

  • Initialization and Setup: When the script starts, it establishes your custom settings, such as how large each Renko box should be. It also prepares a dedicated history file on your computer. This ensures that every new movement generated by the script is recorded systematically for later review.

  • Real-Time Price Monitoring: The script constantly watches the "live" market price (the Bid). Every time the price moves, the script compares it against the boundaries of the most recent Renko box.

  • Dynamic Box Generation: The logic handles two specific types of movement:

    1. Upward Movement: If the current market price climbs above the top boundary of the previous box by the required amount, the script "builds" a new upward box.
    2. Downward Movement: If the current market price drops below the bottom boundary of the previous box by the required amount, the script "builds" a new downward box.
  • Continuous Calculation (Looping): One of the smarter features of this logic is its ability to handle rapid price swings. If the market moves very quickly and jumps past multiple "box levels" in a single second, the script uses a loop to instantly generate all the necessary intermediate boxes. This ensures that no data points are missed, even during periods of high volatility.

  • Data Logging: Every time a new Renko box is formed—whether it is moving up or down—the script automatically logs the specific details (Time, Open Price, High, Low, and Close Price) into your history file. This provides a clean, structured record of the price action.

Summary for the User

In short, this script acts as a filter. It ignores the constant "jitter" of the market and only records significant movements. By converting these movements into uniform boxes, it helps you visualize whether the market is trending up, down, or moving sideways, providing a much cleaner perspective on price action than a standard candle chart.

Miscellaneous
Uses files from the file systemIt writes information to fileIt writes information to fileIt writes information to fileIt writes information to file
5 Views
0 Downloads
0 Favorites
Renko_Chart__From_Ticks
//+------------------------------------------------------------------+
//|                                      Renko_Chart__From_Ticks.mq4 |
//|                                                    Binoy Raphael |
//+------------------------------------------------------------------+
// Rev0: Initial revision

#property copyright "Binoy Raphael"
#property link      ""
#property version   "1.00"
#property strict
input int RenkoSize        = 10; // Renko BoxSize
input int RenkoTimeFrame   = 2;  // Renko New Timeframe
#include <stdlib.mqh>

// Newbar Variable
datetime stored_bartime;

//Renko calculation variables
double points=Point;
double BoxPoints;
double prevRenkoOpen,prevRenkoClose;
double prevRenkoHi,prevRenkoLo;
static double volume;

// Variables for History write
int HstHandle;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
// 1. Renko initialization
   if(Digits==3 || Digits==5){points*=10;}
   BoxPoints      = NormalizeDouble(RenkoSize * points,Digits);
   prevRenkoOpen  = NormalizeDouble(MathFloor(Close[1]/BoxPoints)*BoxPoints,Digits);
   prevRenkoClose = prevRenkoOpen + BoxPoints;
   prevRenkoHi    = High[1];
   prevRenkoLo    = Low[1];
   volume         =(double)Volume[1];
// Not used in the history file

// 2. History file initialization
// Create History file : Will be located in "Tester/Files/". Copy it to the "History" directory
   string filename=Symbol()+IntegerToString(RenkoTimeFrame)+".hst";
   HstHandle=FileOpen(filename,FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);
   if(HstHandle<0)
     {
      Print("Error: can\'t create history file: "+ErrorDescription(GetLastError())+": "+filename);
      return(0);
     }

// 3. Write the history header
   int HstUnused[13];
   FileWriteInteger(HstHandle, 400, LONG_VALUE);            // Version
   FileWriteString(HstHandle, "", 64);                      // Copyright
   FileWriteString(HstHandle, Symbol(), 12);                // Symbol
   FileWriteInteger(HstHandle, RenkoTimeFrame, LONG_VALUE); // Period
   FileWriteInteger(HstHandle, Digits, LONG_VALUE);         // Digits
   FileWriteInteger(HstHandle, 0, LONG_VALUE);              // Time Sign
   FileWriteInteger(HstHandle, 0, LONG_VALUE);              // Last Sync
   FileWriteArray(HstHandle, HstUnused, 0, 13);             // Unused

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(HstHandle>=0)
     {
      FileClose(HstHandle);
      HstHandle=-1;
     }
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double bidPrice=Bid;

   datetime bar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
/// NEW BAR GENERATED ?
   if(stored_bartime!=bar_time)
     {
      // A new bar is created...
      // Reset to defaults
      stored_bartime=bar_time;
      volume+=(double)Volume[1];
     }

// Calculation of renko arrays
   prevRenkoHi = MathMax(prevRenkoHi, Bid);
   prevRenkoLo = MathMin(prevRenkoLo, Bid);
/// 1. Check for the LOW renko
   while(bidPrice<(prevRenkoOpen-BoxPoints) || CompareDoubles(bidPrice,prevRenkoOpen-BoxPoints))
     {
      prevRenkoOpen    -= BoxPoints;
      prevRenkoClose   -= BoxPoints;
      FileRenkoWrite(Time[0],prevRenkoClose,prevRenkoLo,prevRenkoHi,prevRenkoOpen,0);
      // time, open, low, high, close, vol

      volume=(double)Volume[1]; // Volume not used for file-writing
      prevRenkoHi = prevRenkoOpen;
      prevRenkoLo = prevRenkoOpen;
     }
/// 2. Check for the HIGH renko
   while(bidPrice>(prevRenkoClose+BoxPoints) || CompareDoubles(bidPrice,prevRenkoClose+BoxPoints))
     {
      prevRenkoOpen    += BoxPoints;
      prevRenkoClose   += BoxPoints;
      FileRenkoWrite(Time[0],prevRenkoOpen,prevRenkoLo,prevRenkoHi,prevRenkoClose,0);
      // time, open, low, high, close, vol

      volume=(double)Volume[1]; // Volume not used for file-writing
      prevRenkoHi = prevRenkoClose;
      prevRenkoLo = prevRenkoClose;
     }
  }
//+------------------------------------------------------------------+

void FileRenkoWrite(datetime time,double open,double low,double high,double close,double vol)
  {
   FileWriteInteger(HstHandle,(int)time,LONG_VALUE);
   FileWriteDouble(HstHandle,open,DOUBLE_VALUE);
   FileWriteDouble(HstHandle,low,DOUBLE_VALUE);
   FileWriteDouble(HstHandle,high,DOUBLE_VALUE);
   FileWriteDouble(HstHandle,close,DOUBLE_VALUE);
   FileWriteDouble(HstHandle,vol,DOUBLE_VALUE);
  }
//+------------------------------------------------------------------+

Comments