Set fixed (main)chart scale on main chart to draw custom candles (for us dollar index)

 

Hi everyone, 

My data provider recently had a conflict with the CME about usdx data feed so I tried to be smart and quickly merge two pieces of mql4 code together to make up for the lack of data. 

Basically the code does:  

1) calculate the USD Dollar Index values 

2) draw custom candles in the main window

Both seem to be working fine for except 1 thing: Setting the main chart scale between 0 and 100. It scales to the original instrument on the main chart.

What would normally be the work around for this ?

Thanks  for the answers !

Cynthia. 

//+------------------------------------------------------------------+
//| Taken from:                                                      |
//| http://www.forexfactory.com/showthread.php?p=4739080             |
//| http://www.forexfactory.com/showthread.php?t=216362              |
//+------------------------------------------------------------------+
#property link "http://www.forexfactory.com/showthread.php?p=4739080"
#property link "http://www.forexfactory.com/showthread.php?t=216362"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Crimson
#property indicator_color3 Green
#property indicator_color4 Crimson
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 5
#property indicator_width4 5

extern int bar_width = 1;
extern int candle_width = 5;

double Bar1[], Candle1[], Bar2[], Candle2[];

string usdxpairs[] = { "EURUSD", "USDJPY", "GBPUSD", "USDCAD", "USDSEK", "USDCHF" };
double usdx_weight[] = { -0.576, 0.136, -0.119, 0.091, 0.042, 0.036 };
double factor = 50.14348112;

int init() {
 IndicatorShortName("USD Dollar Index, "+Period());
 SetIndexBuffer(0, Bar1);
 SetIndexBuffer(1, Bar2);
 SetIndexBuffer(2, Candle1);
 SetIndexBuffer(3, Candle2);
 SetIndexStyle(0, DRAW_HISTOGRAM, 0, bar_width);
 SetIndexStyle(1, DRAW_HISTOGRAM, 0, bar_width);
 SetIndexStyle(2, DRAW_HISTOGRAM, 0, candle_width);
 SetIndexStyle(3, DRAW_HISTOGRAM, 0, candle_width);

 return(0);
}

int start() {
 int timeframe = Period();
 double high, low, open, close, bodyHigh, bodyLow;
 int shift1, shift2, time1;
 int i, j;
  
 for(i = Bars - 1 - IndicatorCounted(); i >= 0; i--) {
  shift1 = iBarShift(NULL, timeframe, Time[i]);
  time1  = iTime(NULL, timeframe, shift1);
  shift2 = iBarShift(NULL, 0, time1);

  for(j = 0; j < ArraySize(usdxpairs); j++) {
   high = factor * MathPow(iHigh(usdxpairs[j], timeframe, shift1), usdx_weight[j]);
   low  = factor * MathPow(iLow(usdxpairs[j], timeframe, shift1), usdx_weight[j]);
   open = factor * MathPow(iOpen(usdxpairs[j], timeframe, shift1), usdx_weight[j]);
   close = factor * MathPow(iClose(usdxpairs[j], timeframe, shift1), usdx_weight[j]);
   bodyHigh = MathMax(open, close);
   bodyLow = MathMin(open, close);
   printf("ohlc %f %f %f %f",open,high,low,close);
  }

  if(open <= close) {
   Bar1[shift2] = high;         
   Candle1[shift2] = bodyHigh;
        Bar2[shift2] = low;             
        Candle2[shift2] = bodyLow;
  } else {
        Bar1[shift2] = low;
        Candle1[shift2] = bodyLow;
        Bar2[shift2] = high;
        Candle2[shift2] = bodyHigh;
  }
 }

 return(0);
}
 

I got it fixed, by setting it by hand. 

The candles clearly are visible, now however it's a pain setting it by hand and automatically setting is not working yet.

Here's the fixed code.

How are the pro's solving this auto scaling issue for custom indicators on the main chart ? 

 

//+------------------------------------------------------------------+
//| Taken from:                                                                    |
//| http://www.forexfactory.com/showthread.php?p=4739080                    |
//| http://www.forexfactory.com/showthread.php?t=216362              |
//+------------------------------------------------------------------+
#property link "https://forum.mql4.com/68158"
#property description "US Dollar Index candles in main chart window"
#property description " "
#property description "Note: Set scaling appropiately with F8"
#property description " "
#property description "Thanks to the original authors, code mixed from: "
#property description "http://www.forexfactory.com/showthread.php?p=4739080"
#property description "http://www.forexfactory.com/showthread.php?t=216362"
#property description "https://www.theice.com/publicdocs/futures_us/ICE_Dollar_Index_FAQ.pdf"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Crimson
#property indicator_color3 Green
#property indicator_color4 Crimson
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 5
#property indicator_width4 5

extern int bar_width = 1;
extern int candle_width = 5;

double Bar1[], Candle1[], Bar2[], Candle2[];

static double factor = 50.14348112;

int init() {
 IndicatorShortName("USD Dollar Index, "+Period());
 SetIndexBuffer(0, Bar1);
 SetIndexBuffer(1, Bar2);
 SetIndexBuffer(2, Candle1);
 SetIndexBuffer(3, Candle2);
 SetIndexStyle(0, DRAW_HISTOGRAM, 0, bar_width);
 SetIndexStyle(1, DRAW_HISTOGRAM, 0, bar_width);
 SetIndexStyle(2, DRAW_HISTOGRAM, 0, candle_width);
 SetIndexStyle(3, DRAW_HISTOGRAM, 0, candle_width);

 return(0);
}

int start() {
 int timeframe = Period();
 double high, low, open, close, bodyHigh, bodyLow;
 int shift1, shift2, time1;
 int i;
 
 for(i = Bars - 1 - IndicatorCounted(); i >= 0; i--) {
  shift1 = iBarShift(NULL, timeframe, Time[i]);
  time1  = iTime(NULL, timeframe, shift1);
  shift2 = iBarShift(NULL, 0, time1);

  high = factor * MathPow(iHigh("EURUSD", timeframe, shift1), -0.576) *
                  MathPow(iHigh("USDJPY", timeframe, shift1),  0.136) *
                  MathPow(iHigh("GBPUSD", timeframe, shift1), -0.119) *
                  MathPow(iHigh("USDCAD", timeframe, shift1),  0.091) *
                  MathPow(iHigh("USDSEK", timeframe, shift1),  0.042) *
                  MathPow(iHigh("USDCHF", timeframe, shift1),  0.036);
  low =  factor * MathPow(iLow("EURUSD", timeframe, shift1), -0.576) *
                  MathPow(iLow("USDJPY", timeframe, shift1),  0.136) *
                  MathPow(iLow("GBPUSD", timeframe, shift1), -0.119) *
                  MathPow(iLow("USDCAD", timeframe, shift1),  0.091) *
                  MathPow(iLow("USDSEK", timeframe, shift1),  0.042) *
                  MathPow(iLow("USDCHF", timeframe, shift1),  0.036);
  open = factor * MathPow(iOpen("EURUSD", timeframe, shift1), -0.576) *
                  MathPow(iOpen("USDJPY", timeframe, shift1),  0.136) *
                  MathPow(iOpen("GBPUSD", timeframe, shift1), -0.119) *
                  MathPow(iOpen("USDCAD", timeframe, shift1),  0.091) *
                  MathPow(iOpen("USDSEK", timeframe, shift1),  0.042) *
                  MathPow(iOpen("USDCHF", timeframe, shift1),  0.036);              
  close =factor * MathPow(iClose("EURUSD", timeframe, shift1), -0.576) *
                  MathPow(iClose("USDJPY", timeframe, shift1),  0.136) *
                  MathPow(iClose("GBPUSD", timeframe, shift1), -0.119) *
                  MathPow(iClose("USDCAD", timeframe, shift1),  0.091) *
                  MathPow(iClose("USDSEK", timeframe, shift1),  0.042) *
                  MathPow(iClose("USDCHF", timeframe, shift1),  0.036);
                         
  bodyHigh = MathMax(open, close);
  bodyLow = MathMin(open, close);
  
   
  // debug
  // printf("ohlc %f %f %f %f",open,high,low,close);

  if(open <= close) {
   Bar1[shift2] = high;         
   Candle1[shift2] = bodyHigh;
        Bar2[shift2] = low;             
        Candle2[shift2] = bodyLow;
  } else {
        Bar1[shift2] = low;
        Candle1[shift2] = bodyLow;
        Bar2[shift2] = high;
        Candle2[shift2] = bodyHigh;
  }
  
  // todo add proper comment string with ohlc data
  // change chart id name ?
  Comment("USD Dollar Index "+NormalizeDouble(Period(), 0));
  
  // todo chart ratio and see if this can be made to work.
  // https://docs.mql4.com/constants/chartconstants/charts_samples#chart_scale
  ChartSetDouble(0, CHART_PRICE_MAX, 100.0);
  ChartSetDouble(0, CHART_PRICE_MIN, 50.0);
 }

 return(0);
}

 

Thanks,

CynthiaL 

Reason: