One issue I can see is that you choose the start and end bars by
comparing their 'y' values to startvalue and endvalue1. If there are
multiple bars with the same (O+C)/2 value, then the startbar and
endbar formula will find the most recent occurrences, not the ones
used to obtain startvalue and endvalue1.

It seems to me you're going about this backwards. I'd find the bar
numbers first and then their values, not the other way around. You
already have the start bars marked in the "xx" array, so to get the
actual bar numbers (or array indices), just use something like:

x = Cum(1)-1; // Can also use BarIndex();
xx = DateNum() != Ref(DateNum(),-1);
startbar = LastValue(ValueWhen(xx, x, 2)); // Yesterday's start bar
endBar = LastValue(ValueWhen(xx, x, 1))-1; // Bar before today's start bar

The 'y' values are then just:

ocAvg = (O+C)/2;
startOc = ocAvg[startBar];
endOc = ocAvg[endBar];

although you should test both bar values for being in the valid range
0 to BarCount-1 first. You can plot the line the fundamental way using
the formula:

slope = (endOc-startOc)/(endBar-startBar);
trendLine = slope*(x-startBar) + startOc;

or just use the LineArray function:

trendLine = LineArray(startBar, startOc, endBar, endOc);

Note that these plots will only look correct with linear price scale,
unless the price range is small. If you're using semilog, then the
line formula is:

slope = log10(endOc/startOc)/(endBar-startBar);
trendLine = startOc*(10^(slope*(x-startBar)));

Hope this helps, although note that I haven't actually tried any of
this code.

Regards,
GP



--- In [email protected], "convertah" <[EMAIL PROTECTED]> wrote:
>
> Hello and good day,
> I have been trying to write a formula that will draw an automatic
> trendline through the, (O+C)/2, of the first and last five minute
> candles of yesterday. I'm new to AFL and had fun trying to get this far,
> which was only accomplished by searching mail archives,the users guide,
> knowledge bases and, even the "new" googlesearch.
> 
> The trendline works perfectly on some stocks but other stocks will have
> the line through one but not both of the candles; while other stocks
> show no connection with the trendline at all.
> 
> Thoughts on how to make this really work would be greatly appreciated.
> 
> This is the code:
> 
> x = Cum(1);
> 
> xx =  DateNum() !=Ref (DateNum(),-1);
> 
> startvalue = LastValue(ValueWhen( Ref (xx,79), (O+C)/2,1 ));     /*
> (O+C)/2   of first bar of yesterday */
> endvalue1 = LastValue(ValueWhen( Ref (xx,1), (O+C)/2,1 ));        /*
> (O+C)/2   of last bar of yesterday */
> 
> startbar = LastValue( ValueWhen( (O+C)/2 == startvalue, x, 1 ) );
> endbar = LastValue( ValueWhen( (O+C)/2 == endvalue1, x, 1 ) );
> 
> Aa = (endvalue1-startvalue)/(endbar-startbar);
> b = startvalue;
> 
> trendline = Aa * ( x  - startbar ) + b;
> 
> Plot( Close, "Price", colorBlue, styleCandle );
> Plot( trendline, "Trendline", colorRed );
> 
> Thank you,
> Kind regards
> 
> Geary
>


Reply via email to