Hello GP, Thanks for the explanation on the EOD data that makes sense. At some point I would like to code the auto trendline for daily charts too because it acts as a good moving average filter if you run the trendline through the (O+C)/2 of the first and last bar of the moving average period.
I was able to code the parallel trendline, I just added a constant to the ocAvg = (O+C)/2, the 'y' values, following your suggestion and it worked like a charm. The next step is to place the parallel trendlines at the high and low of yesterday with your "trickier" explanation as my guide. Thank you, Geary --- In [email protected], "gp_sydney" <[EMAIL PROTECTED]> wrote: > > Geary, > > Okay, the reason you had to add the extra -1 to the startBar and > endBar formulas is because you didn't put it after the Cum(1) like I did. > > With regard to the EOD data, I didn't explain that fully. To test the > line drawing with EOD data using changes in month rather than day, I > had to also change the definition of "xx" to detect different months > rather than different days. That was just for my testing though. With > your formula, you won't see anything on a daily or longer time frame > because it only draws the line across one day, which on a daily chart > is within a single bar (ie. the start and end bars are the same bar). > > Drawing parallel trendlines is easy, you just offset the start and end > 'y' values by the same amount (for a linear display). The trickier > part though, assuming I understand what you want, is finding the high > and low points to draw them through. If you just want them from the > high and low of yesterday's first bar, then that's easy, but you may > then get other bars cutting through the lines. If you want them > touching a bar but no other bars cutting through (ie. like a tangent), > then that's more difficult if the channel is sloping, as the touch > points may not be the same as the absolute highest and lowest values > during the period (eg. in a down-sloping channel, the high point that > touches the line may actually have a lower 'y' value than an earlier > bar that doesn't touch the line). > > Regards, > GP > > > --- In [email protected], "Geary" convertah@ wrote: > > > > GP, > > > > This is the code that I am using: > > > > x = Cum(1); > > > > xx = DateNum() !=Ref(DateNum(),-1); > > > > startbar = LastValue(ValueWhen(xx, x, 2)-1); // Yesterday's start bar > > endBar = LastValue(ValueWhen(xx, x, 1)-2); // Bar before today's start > > bar > > > > ocAvg = (O+C)/2; > > startOc = ocAvg[startBar]; > > endOc = ocAvg[endBar]; > > > > trendLine = LineArray(startBar, startOc, endBar, endOc,3); > > > > Plot( Close, "Price", colorBlue, styleCandle ); > > Plot( trendline, "Trendline", colorRed ); > > > > The trendline works just as I wanted but when you mentioned EOD data I > > took a look at daily thru monthly charts and the trendline does not plot > > at all, intraday data plots just fine, so I'm a bit mystified. > > > > I use the trendline as a current day filter so I use it extended. > > The next step is to try and produce two more parallel trendlines that > > will be placed at the high and low of yesterday, yielding a regression > > channel that yields additional filtering. > > > > Your help, and AmiBroker has allowed me to begin to analyze and follow > > the patterns that I see in the market. Up till now I have seen these > > repeating patterns as only, after the fact and too late to act upon. Now > > I believe I will be able to detect them as they evolve and grow. > > > > Once again thank you, > > Best regards > > > > Geary > > > > > > --- In [email protected], "sumangalam" <sumangalam@> wrote: > > > > > > GP, > > > > > > I can not stop myself from expressing my gratitude : your code helped > > > me in being introduced to the power of Amibroker arrays : > > > > > > > > ocAvg = (O+C)/2; > > > > > startOc = ocAvg[startBar]; > > > > > endOc = ocAvg[endBar]; > > > > > > With Regards > > > > > > Sanjiv Bansal > > > > > > > > > > > > > > > --- In [email protected], "gp_sydney" gp.investment@ > > > wrote: > > > > > > > > Geary, > > > > > > > > Did you use "Cum(1)-1" for 'x' or just "Cum(1)"? > > > > > > > > Both the LineArray function and manual line formula work fine for me > > > > (although I only have EOD data so have to test it with months rather > > > > than days). > > > > > > > > One oversight with the manual line formula though is that it extends > > > > across the whole chart. To limit it to just between the start and > > > end > > > > bars, mask it like this: > > > > > > > > mask = x >= startBar && x <= endBar; > > > > trendLine = IIf(mask, slope*(x-startBar)+startOc, Null); > > > > > > > > Regards, > > > > GP > > > > > > > > > > > > --- In [email protected], "Geary" <convertah@> wrote: > > > > > > > > > > GP, > > > > > > > > > > Thank you for taking the time to help me with the auto trendline > > > code I > > > > > greatly appreciate it. > > > > > When I first tried your original code for the start and end bars: > > > > > > > > > > startbar = LastValue(ValueWhen(xx, x, 2)); // Yesterday's > > > start bar > > > > > -original > > > > > endBar = LastValue(ValueWhen(xx, x, 1))-1; // Bar before > > > today's start > > > > > bar -original > > > > > > > > > > It gave me a trendline through the (o+c)/2 of yesterdays second > > > bar and > > > > > todays first bar and it was consistent > > > > > with every stock I tried. I finally came up with a modified > > > version that > > > > > produced a trendline through the (o+c)/2 > > > > > of yesterdays first and last bar that works with every stock: > > > > > > > > > > startbar = LastValue(ValueWhen(xx, x, 2)-1); // Yesterday's > > > start bar > > > > > -modified > > > > > endBar = LastValue(ValueWhen(xx, x, 1))-2; // Bar before > > > today's > > > > > start bar -modified > > > > > > > > > > The modified code only works with the LineArray function as when > > > I use > > > > > the slope formula it > > > > > gives me a small offset from the (o+c)/2. > > > > > > > > > > I still need more work on this array stuff because the light has > > > not > > > > > gone on quite yet. > > > > > Once again I am grateful for the help. > > > > > > > > > > Thank you, > > > > > Kind regards > > > > > > > > > > Geary > > > > > > > > > > --- In [email protected], "gp_sydney" <gp.investment@> > > > wrote: > > > > > > > > > > > > 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" convertah@ 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 > > > > > > > > > > > > > > > > > > > > > > > > > > > >
