Author: Copyright 2018, Guilherme Santos.
Indicators Used
Indicator of the average true rangeMoving average indicator
Miscellaneous
It opens Message Boxes to the user
0 Views
0 Downloads
0 Favorites
renko2atr
ÿþ//+------------------------------------------------------------------+

//|                                                    Renko2ATR.mq5 |

//|                                Copyright 2018, Guilherme Santos. |

//|                                               fishguil@gmail.com |

//|                                                    Renko 2.0 ATR |

//|                                                                  |

//|2018-04-10:                                                       |

//| Add tick event and remove timer event for tester                 |

//|2018-04-30:                                                       |

//| Correct volume on renko bars, wicks, performance, and parameters |

//|2018-05-10:                                                       |

//| Now with timer event                                             |

//|2018-05-16:                                                       |

//| New methods and MiniChart display by Marcelo Hoepfner            |

//|2018-06-21:                                                       |

//| New library with custom tick, performance and other improvements |

//|2018-09-27:                                                       |

//| Asymetric reversals, corrections on wick size and initialization |

//+------------------------------------------------------------------+

#property copyright "Copyright 2018, Guilherme Santos."

#property link      "fishguil@gmail.com"

#property version   "2.0"

#property description "Renko 2.0 ATR"

#include <RenkoCharts.mqh>

//+------------------------------------------------------------------+

//| Inputs                                                           |

//+------------------------------------------------------------------+

// Inputs

input string            RenkoSymbol = "";                              //Symbol

input ENUM_TIMEFRAMES   ATRTimeFrame = PERIOD_D1;             //ATR Time frame

input int               ATRPeriod = 14;                                   //ATR Period

input int               MAPeriod = 1;                                     //MA Period (1 = Off)

input ENUM_MA_METHOD    MAMethod = MODE_SMA;                   //MA Method

input double            ATRPercentage = 100;                           //% of ATR

input bool              RenkoWicks     = true;                 // Show Wicks

input bool              RenkoTime      = true;                 // Brick Open Time

input bool              RenkoAsymetricReversal = false;        // Asymetric Reversals

input ENUM_RENKO_WINDOW RenkoWindow    = RENKO_CURRENT_WINDOW; // Chart Mode

input int               RenkoTimer     = 1000;                 // Timer in milliseconds (0 = Off)

input bool              RenkoBook      = true;                 // Watch Market Book





//+------------------------------------------------------------------+

//| Internal variables and objects                                   |

//+------------------------------------------------------------------+

RenkoCharts *RenkoOffline;

string original_symbol, custom_symbol;

int hATR, hMA;

bool _DebugMode = (MQL5InfoInteger(MQL5_TESTER) || MQL5InfoInteger(MQL5_DEBUG) || MQL5InfoInteger(MQL5_DEBUGGING) || MQL5InfoInteger(MQL5_OPTIMIZATION) || MQL5InfoInteger(MQL5_VISUAL_MODE) || MQL5InfoInteger(MQL5_PROFILER));

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

   // Check Symbol

   original_symbol = StringAt(_Symbol, "_");

   if(RenkoSymbol!="")

      original_symbol = RenkoSymbol;

   //Check Period

   if(RenkoWindow == RENKO_CURRENT_WINDOW && ChartPeriod(0) != PERIOD_M1)

     {

      MessageBox("Renko must be M1 period!", __FILE__, MB_OK);

      ChartSetSymbolPeriod(0, _Symbol, PERIOD_M1);

      return(INIT_SUCCEEDED);

     }

   // ATR Config

   hATR = iATR(original_symbol, ATRTimeFrame, ATRPeriod);

   hMA = iMA(original_symbol, ATRTimeFrame, MAPeriod, 0, MAMethod, hATR);

   if(hATR==INVALID_HANDLE || hMA==INVALID_HANDLE) 

     { 

      MessageBox("Renko ATR error!", __FILE__, MB_OK);

      return(INIT_FAILED); 

     } 

   // Setup Renko

   double renko_size = ATRBoxSize();

   if (RenkoOffline == NULL) 

      if ((RenkoOffline = new RenkoCharts()) == NULL)

        {

         MessageBox("Renko create class error. Check error log!", __FILE__, MB_OK);

         return(INIT_FAILED);

        }

   if(!RenkoOffline.Setup(original_symbol, RENKO_TYPE_POINTS, renko_size, RenkoWicks))

     {

      MessageBox("Renko setup error. Check error log!", __FILE__, MB_OK);

      return(INIT_FAILED);

     }

   // Create Custom Symbol

   StringConcatenate(custom_symbol

         , original_symbol

         , "_ATR", (string) ATRPeriod

         , StringPeriod(ATRTimeFrame)

         , (MAPeriod!=1) ? StringMethod(MAMethod) : ""

         , (MAPeriod!=1) ? (string) MAPeriod : ""

         , (ATRPercentage!=100) ? StringFormat("_PCT%g", ATRPercentage) : ""

      );

   RenkoOffline.CreateCustomSymbol(custom_symbol);

   RenkoOffline.ClearCustomSymbol();

   // Load History

   RenkoOffline.UpdateRates();

   RenkoOffline.ReplaceCustomSymbol();   

   //Start

   if(_DebugMode) 

      RenkoOffline.Start(RENKO_CURRENT_WINDOW, RenkoTimer, RenkoBook);

   else 

      RenkoOffline.Start(RenkoWindow, RenkoTimer, RenkoBook);

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

   if(RenkoOffline!=NULL)

     {

      RenkoOffline.Stop();

      delete RenkoOffline;

      RenkoOffline=NULL;

     }  

  }

//+------------------------------------------------------------------+

//| Tick Event (for testing purposes only)                           |

//+------------------------------------------------------------------+

void OnTick()

  {

   if(!IsStopped())

      if(RenkoOffline!=NULL)

         CustomRefresh();

  }

//+------------------------------------------------------------------+

//| Book Event                                                       |

//+------------------------------------------------------------------+

void OnBookEvent(const string& symbol)

  {

   if(RenkoBook)

      OnTick();

  }

//+------------------------------------------------------------------+

//| Timer Event                                                      |

//+------------------------------------------------------------------+

void OnTimer()

  {

   if(RenkoTimer>0)

      if(!MQL5InfoInteger(MQL5_TESTER) && !MQL5InfoInteger(MQL5_OPTIMIZATION))

         OnTick();

  }

//+------------------------------------------------------------------+

void CustomRefresh()

  {

   static datetime next_update;

   //Update ATR Boxes

   datetime current = TimeCurrent();

   if(next_update <= current)

     {

      next_update = current - current % PeriodSeconds(ATRTimeFrame) + PeriodSeconds(ATRTimeFrame);

      double renko_size = ATRBoxSize();

      if(!RenkoOffline.Setup(original_symbol, RENKO_TYPE_POINTS, renko_size, RenkoWicks))

        {

         MessageBox("Renko reload error. Check error log!", "Renko 2.0 ATR", MB_OK);

         return;

        }

      //Update history

      RenkoOffline.ClearRates();

      RenkoOffline.UpdateRates();

      //Update custom symbol

      RenkoOffline.ClearCustomSymbol();

      RenkoOffline.ReplaceCustomSymbol();

     }

   RenkoOffline.Refresh();

  }



double ATRBoxSize(int index = 1)

  {

   double buffer[1], size;

   if(MAPeriod!=1) 

      size = CopyBuffer(hMA, 0, index, 1, buffer);

   else 

      size = CopyBuffer(hATR, 0, index, 1, buffer);

   if(size<0)

      return 0;

   size = buffer[0] * ATRPercentage/100;

   return NormalizeDouble(size, _Digits);

  }



string StringPeriod(ENUM_TIMEFRAMES value)

  {

   if(value == PERIOD_CURRENT) value = (ENUM_TIMEFRAMES) _Period;

   string period = EnumToString(value);

   return StringSubstr(period, 6);

  }



string StringMethod(ENUM_MA_METHOD value)

  {

   string method = EnumToString(value);

   return StringSubstr(method, 4);

  }  

  

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---