Align two scales in zero

Hi,
I plot 2 arrays with different scales in same pane.
In image below the histogram (area) ZERO is below the ZERO of first scale.
Is it possible to align both at zero ?

bi = BarIndex();
fBar = FirstVisibleValue(bi);
lBar = LastVisibleValue(bi) + 1;

MxScale1 = MxFromString( "[20;0;0;19;31;42;35;-27;-20;-16;-1;21;30;27;49;43;54;67;63;60;56;-20;-4;8;25;20;24;-43;-19;-10;-50;4;-80;-22;-66;29;47;38;51;56;42;30;33;31;9;12;7;14;15;21;26;61;24;31;38;29;-8;-24;5;6;-2;-29;-8;6;-85;58;72;57;-26;-12;-7;-70;-58;-39;-36;-57;13;52;-5;-14;-8;39;16;-61;-24;-25;-24;4;-9;-7;-17;-19;9;-6;5;10;17;20;22;39;39;42;-26;-28;-49;-18;-15;22;-1;-59;-58;-45;3;21] ");
MxScale2 = MxFromString( "[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;25883;31127;50594;51077;42124;35698;19155;31073;46118;51311;47775;43818;17111;22494;29396;6251;10170;-4102;-16863;-32064;-7269;5359;13817;10287;13188;-1439;-5576;-5834;11190;6274;2169;10015;12746;21981;26756;44275;24763;4534;9993;6130;-4268;-7697;-8227;-1640;-547;-7502;-11645;-7088;-9417;-19253;8481;22931;12255;8461;13842;14638;1208;14269;25265;18000;12770;17422;27399;14853;11698;22145;240;-21092;-27978;-5499;-13048;-15150;7538;-124;-3754;-8459;-11023;-11066;-22351;-7506;-3151;-842;-7890;-2672;28080;20003;24390;4279;-4494;-11656;-14106;-11678;923;-7036;-20606;-24675;-40506;-29730;-21761]" );

bars1 = MxGetSize( MxScale1, 0 ); 
bars2 = MxGetSize( MxScale2, 0 ); 
if( bars1 != bars2 )
	Error("Matrixs should be same size.");

if( BarCount < bars1 )
	Error("Choose a Symbol with at least " + bars + " bars.");

Scale1 = 0;
Scale2 = 0;

for( m = 0, i = fBar; m < bars1 AND i < lBar; i++, m++ )
{
	Scale1[i] = MxScale1[m][0];
	Scale2[i] = MxScale2[m][0];
}
Plot( Scale1, "SCALE 1", colorOrange, styleLine | styleThick);
Plot( Scale2, "SCALE 2", colorLime, styleThick | styleArea | styleOwnScale);
PlotGrid(0);


You can remove your last 3 Plot() and try this code.
It is a bit crude but something I had put together to align my scales.

// modified code from here

// This function returns Highest absolute value of an Array
function getRange( arr ) {
	a1 = abs( LastValue( Highest(arr) ));
	a2 = abs( LastValue( Lowest (arr) ));
	return IIf(a1>a2,a1,a2);
}

s1 = getRange(Scale1);	// get max of 1st array
s2 = getRange(Scale2);	// get max of 2nd array
sc = IIf(s1>s2,s1,s2);	// get max of both arrays, which can be used in left scale numbers.

// Make a dummy Array. This is essentially for the Left Scale that is plot
dummy[BarCount - 1] = sc;
dummy[BarCount - 2] = -sc;

Plot(dummy,"",colorDefault,styleNoDraw);	// To display scale
// dummy can be s1 or s2 as well.

//PlotGrid(0);
// using styleOwnScale and its custom MIN,MAX values.
Plot( Scale1, "SCALE 1", colorOrange, styleLine | styleThick | styleOwnScale,-s1,s1);
Plot( Scale2, "SCALE 2", colorLime, styleThick | styleArea | styleOwnScale,-s2,s2);

I dont have price or another Array in this pane which is picked up by Plot() to draw the scale. Thats why we use the dummy Array.

Another way could be:
I think AB Remap function will rescale the values which you may not want.
Back then i was a noob, there could be a better way to do it from the experienced members.

3 Likes

If there are plain Arrays whose higher value is required, then Max() is ideal instead of IIF(). I had some longer stuff in there that i removed.

return Max(a1,a2);

// and same with s1,s2. can nest multiple arrays with max s1,s2,s3...
1 Like

Has been covered her already

So just add this below

y_min1 = LowestVisibleValue(Scale1); 
y_max1 = HighestVisibleValue(Scale1); 

y_min2 = LowestVisibleValue(Scale2); 
y_max2 = HighestVisibleValue(Scale2); 

min_dest = Min(y_min1,y_min2); 
max_dest = Max(y_max1,y_max2); 

price1 = Remap(Scale1, y_min1, y_max1, min_dest, max_dest); 
price2 = Remap(Scale2, y_min2, y_max2, min_dest, max_dest);
 
Plot( price1, "SCALE 1", colorOrange, styleLine | styleThick);
Plot( price2, "SCALE 2", colorLime, styleThick | styleArea);
PlotGrid(0);

2 Likes
Plot( price1-LastValue(price1), "SCALE 1", colorOrange, styleLine | styleThick);

Thank you both!

In my case, solution provided by @nsm51 fits better as values are not changed

2 Likes

Just add Title!


y_min1 = LowestVisibleValue(Scale1); 
y_max1 = HighestVisibleValue(Scale1); 

y_min2 = LowestVisibleValue(Scale2); 
y_max2 = HighestVisibleValue(Scale2); 

min_dest = Min(y_min1,y_min2); 
max_dest = Max(y_max1,y_max2); 

price1 = Remap(Scale1, y_min1, y_max1, min_dest, max_dest);
price1 -= LastValue(price1); 
price2 = Remap(Scale2, y_min2, y_max2, min_dest, max_dest);
 
Plot( price1, "SCALE 1", colorOrange, styleLine | styleThick | styleNoTitle | styleNoLabel);
Plot( price2, "SCALE 2", colorLime, styleThick | styleArea | styleNoTitle | styleNoLabel  );
Title = StrFormat("Scale1: %g, Scale2: %g", Scale1, Scale2);
PlotGrid(0);

Besides with this code you can scroll while with one of post #2 you can't.

1 Like

Ok now I got it.
Thanks very much @fxshrat .

Thank you too @nsm51

If you know beforehand that dataset1 is always has smallest highest lowest values then you need just one time remap.

y_min1 = LowestVisibleValue(Scale1); 
y_max1 = HighestVisibleValue(Scale1); 

y_min2 = LowestVisibleValue(Scale2); 
y_max2 = HighestVisibleValue(Scale2); 

price1 = Remap(Scale1, y_min1, y_max1, y_min2, y_max2);
price1 -= LastValue(price1); 
  
Plot( price1, "SCALE 1", colorOrange, styleLine | styleThick | styleNoTitle | styleNoLabel);
Plot( Scale2, "SCALE 2", colorLime, styleThick | styleArea | styleNoTitle | styleNoLabel);
Title = StrFormat("Scale1: %g, Scale2: %g", Scale1, Scale2);
PlotGrid(0);


1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.