Code: Select all
//+------------------------------------------------------------------+
//| CustomRenko.mq5 |
//| Copyright 2023, Your Name |
//| https://www.example.com|
//+------------------------------------------------------------------+
#property copyright "2023, Your Name"
#property link "https://www.example.com"
#property version "1.00"
#property strict
#property indicator_chart_window
Plot(0, "Brick Size", clrGray);
Plot(1, "Total Trades", clrRed);
Plot(2, "Buy Trades", clrOrange);
Plot(3, "Sell Trades", clrYellow);
Plot(4, "Total Volume", clrGreen);
Plot(5, "Buy Volume", clrBlue);
Plot(6, "Sell Volume", clrViolet);
Plot(7, "Time Per Brick", clrLightBlue);
// Indicator input parameters
input double BrickSize = 10.0; // Brick size multiplier based on spread
// Global variables
double BufferBrickSize[];
double BufferTotalTrades[];
double BufferBuyTrades[];
double BufferSellTrades[];
double BufferTotalVolume[];
double BufferBuyVolume[];
double BufferSellVolume[];
double BufferTimePerBrick[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Set the indicator buffers
SetIndexBuffer(0, BufferBrickSize, INDICATOR_CALCULATIONS);
SetIndexBuffer(1, BufferTotalTrades, INDICATOR_CALCULATIONS);
SetIndexBuffer(2, BufferBuyTrades, INDICATOR_CALCULATIONS);
SetIndexBuffer(3, BufferSellTrades, INDICATOR_CALCULATIONS);
SetIndexBuffer(4, BufferTotalVolume, INDICATOR_CALCULATIONS);
SetIndexBuffer(5, BufferBuyVolume, INDICATOR_CALCULATIONS);
SetIndexBuffer(6, BufferSellVolume, INDICATOR_CALCULATIONS);
SetIndexBuffer(7, BufferTimePerBrick, INDICATOR_CALCULATIONS);
// Set the indicator labels
PlotIndexSetString(0, PLOT_LABEL, "Brick Size");
PlotIndexSetString(1, PLOT_LABEL, "Total Trades");
PlotIndexSetString(2, PLOT_LABEL, "Buy Trades");
PlotIndexSetString(3, PLOT_LABEL, "Sell Trades");
PlotIndexSetString(4, PLOT_LABEL, "Total Volume");
PlotIndexSetString(5, PLOT_LABEL, "Buy Volume");
PlotIndexSetString(6, PLOT_LABEL, "Sell Volume");
PlotIndexSetString(7, PLOT_LABEL, "Time Per Brick");
// Set the indicator styles
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexStyle(2, DRAW_LINE);
SetIndexStyle(3, DRAW_LINE);
SetIndexStyle(4, DRAW_LINE);
SetIndexStyle(5, DRAW_LINE);
SetIndexStyle(6, DRAW_LINE);
SetIndexStyle(7, DRAW_LINE);
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
int limit = rates_total - prev_calculated;
// Loop through the price data to determine when to generate new Renko bricks
for (int i = rates_total - 1; i >= limit; i--)
{
// Calculate the brick size based on the spread and the BrickSize multiplier
double brickSize = spread[i] * BrickSize;
// Calculate the number of bricks based on price change and brick size
int brickCount = MathAbs(close[i] - close[i + 1]) / brickSize;
// Generate new Renko bricks
for (int j = 0; j < brickCount; j++)
{
// Set the values for each Renko brick
int brickIndex = i + j;
BufferBrickSize[brickIndex] = brickSize;
BufferTotalTrades[brickIndex] = CalculateTotalTrades(brickIndex);
BufferBuyTrades[brickIndex] = CalculateBuyTrades(brickIndex);
BufferSellTrades[brickIndex] = CalculateSellTrades(brickIndex);
BufferTotalVolume[brickIndex] = CalculateTotalVolume(brickIndex);
BufferBuyVolume[brickIndex] = CalculateBuyVolume(brickIndex);
BufferSellVolume[brickIndex] = CalculateSellVolume(brickIndex);
BufferTimePerBrick[brickIndex] = CalculateTimePerBrick(brickIndex);
}
}
return rates_total;
}
// Function to calculate the total trades for a Renko brick
int CalculateTotalTrades(int index)
{
int totalTrades = 0;
// Define the range of trades for the current brick
int startTradeIndex = bufferStart[index];
int endTradeIndex = bufferStart[index + 1];
// Count the number of trades within the range
for (int i = startTradeIndex; i < endTradeIndex; i++)
{
// Check the direction of the trade (buy or sell)
if (bufferTradeType[i] == TRADE_TYPE_BUY || bufferTradeType[i] == TRADE_TYPE_SELL)
{
totalTrades++;
}
}
return totalTrades;
}
// Function to calculate the buy trades for a Renko brick
int CalculateBuyTrades(int index)
{
int buyTrades = 0;
// Define the range of trades for the current brick
int startTradeIndex = bufferStart[index];
int endTradeIndex = bufferStart[index + 1];
// Count the number of buy trades within the range
for (int i = startTradeIndex; i < endTradeIndex; i++)
{
if (bufferTradeType[i] == TRADE_TYPE_BUY)
{
buyTrades++;
}
}
return buyTrades;
}
// Function to calculate the sell trades for a Renko brick
int CalculateSellTrades(int index)
{
int sellTrades = 0;
// Define the range of trades for the current brick
int startTradeIndex = bufferStart[index];
int endTradeIndex = bufferStart[index + 1];
// Count the number of sell trades within the range
for (int i = startTradeIndex; i < endTradeIndex; i++)
{
if (bufferTradeType[i] == TRADE_TYPE_SELL)
{
sellTrades++;
}
}
return sellTrades;
}
// Function to calculate the total volume for a Renko brick
double CalculateTotalVolume(int index)
{
double totalVolume = 0.0;
// Define the range of trades for the current brick
int startTradeIndex = bufferStart[index];
int endTradeIndex = bufferStart[index + 1];
// Sum up the volumes of trades within the range
for (int i = startTradeIndex; i < endTradeIndex; i++)
{
totalVolume += bufferVolume[i];
}
return totalVolume;
}
// Function to calculate the buy volume for a Renko brick
double CalculateBuyVolume(int index)
{
double buyVolume = 0.0;
// Define the range of trades for the current brick
int startTradeIndex = bufferStart[index];
int endTradeIndex = bufferStart[index + 1];
// Sum up the volumes of buy trades within the range
for (int i = startTradeIndex; i < endTradeIndex; i++)
{
if (bufferTradeType[i] == TRADE_TYPE_BUY)
{
buyVolume += bufferVolume[i];
}
}
return buyVolume;
}
// Function to calculate the sell volume for a Renko brick
double CalculateSellVolume(int index)
{
double sellVolume = 0.0;
// Define the range of trades for the current brick
int startTradeIndex = bufferStart[index];
int endTradeIndex = bufferStart[index + 1];
// Sum up the volumes of sell trades within the range
for (int i = startTradeIndex; i < endTradeIndex; i++)
{
if (bufferTradeType[i] == TRADE_TYPE_SELL)
{
sellVolume += bufferVolume[i];
}
}
return sellVolume;
}
// Function to calculate the time per brick for a Renko brick
int CalculateTimePerBrick(int index)
{
int timePerBrick = 0;
if (index > 0)
{
// Calculate the time difference between the current brick and the previous brick
int timeDifference = time[index - 1] - time[index];
timePerBrick = MathAbs(timeDifference);
}
return timePerBrick;
}
Also this:
Code: Select all
//+------------------------------------------------------------------+
//| YourIndicator |
//| Copyright © 2023, YourName |
//| https://www.yourwebsite.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 11
#property indicator_plots 11
// Define the indicator buffers
double BufferBrickSize[];
double BufferTrendType[];
double BufferTrendStrength[];
double BufferTrendLength[];
double BufferMomentum[];
double BufferConsolidation[];
double BufferConsolidationLength[];
double BufferReversalType[];
double BufferReversalLength[];
double BufferRange[];
double BufferRangeIntervals[];
// Global Variables
double BrickSize; // Renko brick size
double AverageSpread; // Average spread value
double CurrentSpread; // Current spread value
double SpreadMultiplierAverage; // Multiplier for average spread
double SpreadMultiplierCurrent; // Multiplier for current spread
double ATRPeriod; // Period for calculating ATR
double FixedBrickSize; // Fixed brick size
// Define other global variables for tracking associated values
int TrendType;
double TrendStrength;
int TrendLength;
double Momentum;
bool Consolidation;
int ConsolidationLength;
int ReversalType;
int ReversalLength;
double Range;
double RangeIntervals;
// Add any additional global variables you need
// Custom Indicator Initialization
int OnInit()
{
// Calculate the average spread
double sumSpread = 0.0;
for (int i = 0; i < rates_total; i++)
{
sumSpread += spread[i];
}
AverageSpread = sumSpread / rates_total;
// Set the brick size based on your chosen calculation method
// You can use any of the methods mentioned earlier (average spread, current spread, ATR, etc.)
// Assign the calculated value to the BrickSize variable
BrickSize = AverageSpread * SpreadMultiplierAverage;
// Initialize the other global variables
CurrentSpread = 0.0;
SpreadMultiplierAverage = 1.0;
SpreadMultiplierCurrent = 1.0;
ATRPeriod = 14;
FixedBrickSize = 10.0; // Set your desired fixed brick size
// Initialize the other global variables for tracking associated values
TrendType = 0;
TrendStrength = 0.0;
TrendLength = 0;
Momentum = 0.0;
Consolidation = false;
ConsolidationLength = 0;
ReversalType = 0;
ReversalLength = 0;
Range = 0.0;
RangeIntervals = 0.0;
// Initialize any additional global variables
// Define indicator properties
IndicatorSetString(INDICATOR_SHORTNAME, "Your Indicator Name"); // Replace with your desired indicator name
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
// Assign buffer(s) to the indicator
SetIndexBuffer(0, BufferBrickSize, INDICATOR_DATA);
SetIndexBuffer(1, BufferTrendType, INDICATOR_DATA);
SetIndexBuffer(2, BufferTrendStrength, INDICATOR_DATA);
SetIndexBuffer(3, BufferTrendLength, INDICATOR_DATA);
SetIndexBuffer(4, BufferMomentum, INDICATOR_DATA);
SetIndexBuffer(5, BufferConsolidation, INDICATOR_DATA);
SetIndexBuffer(6, BufferConsolidationLength, INDICATOR_DATA);
SetIndexBuffer(7, BufferReversalType, INDICATOR_DATA);
SetIndexBuffer(8, BufferReversalLength, INDICATOR_DATA);
SetIndexBuffer(9, BufferRange, INDICATOR_DATA);
SetIndexBuffer(10, BufferRangeIntervals, INDICATOR_DATA);
// Configure indicator plot settings
PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_TYPE_NONE);
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(4, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(5, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
PlotIndexSetInteger(6, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(7, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
PlotIndexSetInteger(8, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(9, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(10, PLOT_DRAW_TYPE, DRAW_LINE);
// Add any additional buffer configuration if required
// Add any additional initialization code you need
return(INIT_SUCCEEDED);
}
// Custom Indicator Calculation
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[])
{
// Add your Renko brick calculation logic here
// Loop through the price data to determine when to generate new Renko bricks
for (int i = prev_calculated; i < rates_total; i++)
{
// Implement your Renko brick generation logic using the BrickSize variable
// Compare price values to decide if a new brick should be formed
// Track associated values such as trend type, trend strength, momentum, etc.
// based on your desired calculations and conditions
// Assign the calculated values to the respective indicator buffers
BufferBrickSize[i] = // Calculate the brick size value
BufferTrendType[i] = // Calculate the trend type value
BufferTrendStrength[i] = // Calculate the trend strength value
BufferTrendLength[i] = // Calculate the trend length value
BufferMomentum[i] = // Calculate the momentum value
BufferConsolidation[i] = // Calculate the consolidation value
BufferConsolidationLength[i] = // Calculate the consolidation length value
BufferReversalType[i] = // Calculate the reversal type value
BufferReversalLength[i] = // Calculate the reversal length value
BufferRange[i] = // Calculate the range value
BufferRangeIntervals[i] = // Calculate the range intervals value
// Update the values of the custom indicator buffers
// based on the generated Renko bricks and associated values
Buffer[i] = // Assign the calculated value to the main buffer
}
return(rates_total);
}
Sincerely (If not inspired to humor)
RauHer