# LineArray without loop (x position problem)

I draw kind of signal lines on charts using loop with Plot and Line Array. It looks like this:

``````for ( i = 5; i < BarCount; i++ )
{
}
``````

I know that Plot inside for-loop is not optimal, that is why I would like to work on arrays. I'm trying to use code like this below, however I've got problems with defining X co-ordinates:

``````x0Buy=???; //(previous bar);
y0Buy=H[i-1]; //(y value for previous bar)
``````

I would like to draw LinaArray not only on the last bar, but on the whole chart. I think I should define somehow X position of the bar for LineArray purposes. I did (almost) the same with PlotShapes on arrays (because it doesn't need to define X coordinates, it is enough when showBuy==1), but LineArray is visually much better that is why I would like to solve this issue.

There is proper code on how to use LineArray with Plot by T.J. at User KB.
See @Tomasz's version (2nd code) there.

``````/// modified from T.J. code of User KB at:
bi = Barindex();
Sig = Ref(ROC(H,1)<0 AND ROC(L,1)>0,-1);// every n-bar

y0 = 0;
y1 = L;
FirstVisibleBar = FirstVisibleValue(bi);
Lastvisiblebar = LastVisibleValue(bi);
CombinedLine = Null;
for ( b = Firstvisiblebar; b <= Lastvisiblebar; b++ )
{
if ( Sig[b] )
{
TPrice[b] = L[b];

x0 = y0;
x1 = y1;
y0 = b;
y1 = TPrice[b];
La = LineArray( x0, x1, y0, y1 );

CombinedLine = IIf( IsNull( la ), CombinedLine, la );
}
}

Plot( C, "Price", colorDefault, styleBar );
Plot( CombinedLine, "", colorYellow);
PlotShapes(sig*shapeSmallCircle, colorYellow, 0, L, -12);
`````` 2 Likes

Yes, I know this solution (however, I had some problems with using it), but there is still for-loop and works for visible part of the chart (firstvisible-lastvisible) and it's pretty... complicated Is there any possibility to do it like in my first post (2nd code)? The problem is to define/set X co-ordinates.

Where is that mentioned in first post??

There is no problem with 2nd post's code (I didn't face problems).

If there would be loop-less way then I would have posted it! I am not posting for sake of posting.

You want to plot on chart. Chart is visible area.
Quoting movie character "Biff Tannen":

"Hello? Hello? Anybody home, McFly?"

If you want to output Linearray in analysis window (which you did not say in first post at all) then simply replace

``````FirstVisibleBar = FirstVisibleValue(bi);
Lastvisiblebar = LastVisibleValue(bi);
``````

by

``````FirstVisibleBar = 0;
Lastvisiblebar = BarCount-1;
``````

And Sig may be modified to

``````Sig = Sig AND Status("BarInRange");
``````

But as far as plot is concerned... there is other way using Gfx (but it wlll not output values between each signal point). It will just draw line (connecting signal points).

``````Sig = Ref(ROC(H,1)<0 AND ROC(L,1)>0,-1);

bi = Barindex();
fvb= FirstVisibleValue(bi);
lvb = LastVisibleValue(bi);

GfxSetCoordsMode(1);
GfxSelectPen(colorYellow, 1, 0);
GfxMoveTo(fvb, L[fvb]);
for ( i = fvb; i <= lvb; i++ ) {
if ( Sig[i] ) GfxLineTo(i, L[i]);
}

Plot( C, "Price", colorDefault, styleBar );
PlotShapes(sig*shapeSmallCircle, colorYellow, 0, L, -12);
`````` 2 Likes

OK, now it's clear for me, thanks!!