[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] How to calculate a slope % with Rsquared without a loop?



PureBytes Links

Trading Reference Links

Hi Louis,
 
What will show the smoothness of the pullback is r-squared. It tells you how well a set of data points fits a straight line by returning a number from 0 to 1. 1 is a perfect fit. r-squared ins't built into AmiBroker, but it's simple to add:
 
function RSquared(array, periods) {
 return Correlation(BarIndex(), array, periods) ^ 2;
}
 
The array would be your price -- I use the highs, but you could use (H + O + C)/3, etc. The argument periods has to be a single-valued variable. That makes it tricky in this scenario, because the number of bars back to the highest high of the past n bars will vary bar-by-bar. I suppose you could pass an array, and add a loop to the function itself, and return a custom-built array. I kind of like that idea. Even better would be the addition of the formula to AB. I'll see if that's in the suggestion database.
 
Using a simple scan for pullbacks to a rising EMA20 that is in turn above a rising EMA40 (classic Pristine setup, but it's been around longer than that) showed CRI, BRKR and GEOY all on the same evening a little over a week ago among a total of only about 75 charts returned from the scan. r-squared helps filter and keep the list you manually scan through small.
 
By the way, perhaps someone can see why the following code doesn't work as I expect it to. Here's a section of code I use in the scan. It's not pretty, as you'll see. exploreStartIdx and exploreEndIdx both return a single value, and work fine in the for loop that follows. LastValue returns the desired number since only one bar satisifes the ValueWhen condition (otherwise LastValue in exploration code literally returns that last value in the array passed).
 
What confuses me is that the line of code in the loop
 
per = LastValue(ValueWhen(i==BarIndex(), BarsSince(H==HHV(H, BarsToLookBackForHHV))));
 
seems to assign an array to per -- or I should say, passing per as the "periods" argument to the r-squared function results in an error, and it is the error I see when I accidentally pass an array where a single value is required. I have work-around I'd love to do away with. It would eliminate a loop and speed up the code. Realizing while writing this that the loop should go inside the r-squared function will help a lot. Would probably want to use logic in the function to test to see if it is being called in an exploration or backtest to reduce the number of iterations of the loop. I'll write that function off-line and post separately.
 
// ************ start snippet *************
 
exploreStartIdx = LastValue(ValueWhen(DateNum()==Status("rangefromdate"), BarIndex()));
exploreEndIdx = LastValue(ValueWhen(DateNum()==Status("rangetodate"), BarIndex()));
 
BarsToLookBackForHHV = 30;
BarsBackToHigh = BarsSince(H==HHV(H, BarsToLookBackForHHV));
 
// -- Initialize arrays -- not sure this is needed
r2Arr = H-H;
BarsBackToNTHigh = H-H;
DT = DateTime();
for (i=exploreStartIdx; i<=exploreEndIdx; i++) {
 
  // -- would like this to work
  per = LastValue(ValueWhen(i==BarIndex(), BarsSince(H==HHV(H, BarsToLookBackForHHV))));
 
  // -- this is the workaround -- not very nice.
  highestHigh = 0;
  if (i > (BarsToLookBackForHHV + 1)) {
 
    for (k=1; k<=BarsToLookBackForHHV; k++) {
      curHigh = H[i-k];
      if (curHigh >= highestHigh) {
        highestHigh = curHigh;
        BarsBackToNTHigh[i] = k;
      }
 
    }
  
    per = BarsBackToNTHigh[i];
    r2 = RSquared(H, per) * 100;
    // -- array of r-squared values to use in exploration, etc.
    r2arr[i] = r2[i];
 
  }
 
}
 
// ************ end snippet *************
 
 
I emailed this to support, but didn't get a response on this one. That's not a knock on support -- Tomasz and Marcin provide excellent responses and usually quite quickly -- I think this one just slipped between the cracks, or even just got lost in the ether -- or a junk mail filter -- somewhere. But I would like to get to the bottom of it. It at least *seems* to me it should work, but I may just not be seeing an error I've made in the code.
 
Totally off-topic -- it's interesting to see that RIMM and GOOG and others are right on support after Friday's close.
 
Regards,
 
Gordon
 
----- Original Message -----
From: Louis P.
Sent: Friday, September 05, 2008 8:23 PM
Subject: Re: [amibroker] How to calculate a slope % with Rsquared without a loop?

Hi again,

Here is what I've done so far:

higher= HHV(H,20);
RL= LinearReg (higher,20);
slope= ((RL - Ref(RL,-1)) /  Ref(RL,-1))*100;

Ok so with this I first get the value of the highest high of the last 20 periods.  Then I do a Linear regression of this High (is it correct or should I do a LR from the date/time of that High?).  Then I calculate the slope.  Are you sure about the -1 value?  Cause the result is not really meaningful.  I can have a very negative value and then 3-4 bars later (when we are out of the latest HHV bar that was in the last 20 bars) then the result is much higher.  Do you think this formula looks good?

Also, how do you do to calculate the minbars<barspast?

Thanks,

Louis


2008/9/5 Ara Kaloustian <ara1@xxxxxxxcom>

I think the best solution would come from linear regression ... along with some help from smoothing perhaps.
 
The concept would be that you first qualify a setup.
Exapmle:
You want 10% drop from HHV in a particular amount of time ... maximum number of bars. (MinBars to MaxBars)
 
1. Close < 0.9 * HHV(High,Maxbars);       // 10 % drop in less then Numbars bars
2. BarsPast = HHVBars(High,Maxbars);  // Bars since HHV
3. MinBars < BarsPast;                          // Insure that a minimum number of bars since HHV
4. Draw regression line - Plot(LinearReg(array,period) .... ) 
5. Evaluate slope of regression line
    slope = RL - Ref(RL,-1) /  Ref(RL,-1);   //  Slope at any point on the line
6. You can decide how to alternately evaluate to slope .... max or min slope value ...etc
 
You may also use a smoothed price array instead of actual prices by using price = (H+L+C+O) / 4 or (H+L) / 2 .
 
Hope this gets you closer to your objective.
 
----- Original Message -----
From: Louis P.
Sent: Friday, September 05, 2008 11:00 AM
Subject: Re: [amibroker] How to calculate a slope % with Rsquared without a loop?

Hi,

@Joe Landry: Thanks for the tips.  I'm not sure how the K-ratio can help on this, but if it can help...

@Ara: I'm sorry if what I said was not clear.  What I want to do is to calculate a % pullback from a HHV of a stock but I want the slope to be "smooth", that is, I am not happy to see a 5 or 10% pullback but would like to see a 5 or 10% pullback with a particular slope inclination.   That is: if the stock dropped 10% in a day is not the same as if it quietly lost 0.5% for 20 days (would not be exactly 10%, but this is an image).  You understand what I mean?  I want the slope to be as smooth as possible with a particular inclation %.  Is this possible?

Thanks,

Louis



2008/9/5 Ara Kaloustian <ara1@xxxxxxxcom>

Louis,
 
I am not quite sure what you mean by "a smooth slope".
 
Do you want to have a plot of prices that are smoothed ... as you would gt with a filter??
 
OR do you want a computed value of slope at some particular point? ... if so at what point ... or do you want a plot of the slope ... etc
 
If you can be moe specific that would be halpful!
 
A
 
 
 
----- Original Message -----
From: Louis P.
Sent: Friday, September 05, 2008 8:41 AM
Subject: Re: [amibroker] How to calculate a slope % with Rsquared without a loop?

Hi,

Linear function is my big problem.  I'm so weak using that.  What is the difference between using this and doing it the other way?

BTW, how would you set the number of bars in the current:

HHVBars(Var,period); // Find number of bars - distance from HHV to current bar
%slope = ((Highest high - currentclose) / Highest high) / number of bars * 100;

Thanks,

Louis


2008/9/4 Ara Kaloustian <ara1@xxxxxxxcom>

You can also use linearray to draw a stright line from HHV to current point.
 
Look up LinearReg function
----- Original Message -----
Sent: Thursday, September 04, 2008 10:37 AM
Subject: Re: [amibroker] How to calculate a slope % with Rsquared without a loop?

Try this:
 
HHV(Var,period);   // find value of highest high
HHVBars(Var,period); // Find number of bars - distance from HHV to current bar
%slope = ((Highest high - currentclose) / Highest high) / number of bars * 100;
 
You may choose to define slope in a different way, but this is the basic structure
 
A
----- Original Message -----
From: Louis P.
Sent: Thursday, September 04, 2008 9:34 AM
Subject: Re: [amibroker] How to calculate a slope % with Rsquared without a loop?

Hi,

Thanks for your response.  I should have said I want to calculate a slope from a high point; I'd like to get a smooth slope from a HHV... How would you do that?

Thanks a lot!
Louis

2008/9/4 Ara Kaloustian <ara1@xxxxxxxcom>

%slope = (variable - Ref(Variable,-x) ) / Ref(Variable,-x) * 100;
----- Original Message -----
From: Louis P.
Sent: Thursday, September 04, 2008 9:20 AM
Subject: [amibroker] How to calculate a slope % with Rsquared without a loop?

Hi,

I was wondering how to calculate the % of a slope without doing any loop.  Anybody has any idea?

Thanks,

Louis




__._,_.___

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html




Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___