Hi, I would like to re-create the candlestick chart in an indicator window, any suggestions about the functions to use/articles to read?
In a separate window, histograms are drawn from the buffer value to zero. They are drawn from the first to the last. If you want one to be the background, it must be the first buffer.
See also
How to Draw Cnadle chart in indicator_separate_window ? (XDXD) - MQL4 programming forum (2016)
Two possibilities:
#property indicator_separate_window #property indicator_buffers 5 #property indicator_plots 1 //--- plot OpenHighLowClose #property indicator_label1 "Open;High;Low;Close" #property indicator_type1 DRAW_COLOR_CANDLES #property indicator_color1 clrDodgerBlue,clrFireBrick,clrYellow #property indicator_style1 STYLE_SOLID //--- indicator buffers double Open []; double High []; double Low []; double Close[]; double Color[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,Open, INDICATOR_DATA); SetIndexBuffer(1,High, INDICATOR_DATA); SetIndexBuffer(2,Low, INDICATOR_DATA); SetIndexBuffer(3,Close,INDICATOR_DATA); SetIndexBuffer(4,Color,INDICATOR_COLOR_INDEX); //--- 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 begin=(rates_total!=prev_calculated)?prev_calculated:prev_calculated-1; //--- for(int i=begin;i<rates_total;i++) { double opn=open[i]; double cls=close[i]; Open [i]=opn; High [i]=high[i]; Low [i]=low[i]; Close[i]=cls; Color[i]=(cls>opn)?0:(cls<opn)?1:2; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
#property indicator_separate_window //--- input parameters input ENUM_TIMEFRAMES TimeFrame=PERIOD_CURRENT; input bool ShowDateScale=true; input bool ShowPriceScale=true; //--- #include <Charts\Chart.mqh> CChart cht; #include <ChartObjects\ChartObjectSubChart.mqh> CChartObjectSubChart subCht; //--- long chartID; int chartWin; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { chartID=ChartID(); chartWin=ChartWindowFind(); cht.Attach(chartID); subCht.Create(chartID,"SubChart",chartWin,0,0,cht.WidthInPixels(),cht.HeightInPixels(chartWin)); subCht.Symbol(_Symbol); subCht.Period(TimeFrame); subCht.Scale(cht.Scale()); subCht.DateScale(ShowDateScale); subCht.PriceScale(ShowPriceScale); cht.Redraw(); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[]) { //--- //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { cht.Detach(); subCht.Detach(); } //+------------------------------------------------------------------+ //| ChartEvent function | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- if(id==CHARTEVENT_CHART_CHANGE) { subCht.X_Size(cht.WidthInPixels()); subCht.Y_Size(cht.HeightInPixels(chartWin)); subCht.Scale(cht.Scale()); cht.Redraw(); } } //+------------------------------------------------------------------+
Files:
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register