hi,
I tried Howards code for a function MA( H, period ) + Offset and it works.
First thing is that in my case I try to anticipate the price at which the function MA( H, period ) + Offset is crossed. Therefor I still need to do some rewritting to be able to fill the function ZeroToFind. I used:
function ZeroToFind( P )
{
FTZ = P - ( ( period - 1 ) * ( Ref( MA( H, period - 1 ), -1 ) + offset ) + ( P + offset ) ) / period;
return FTZ;
}
However, if I try to do this for a Bollinger band this is probably not so easy to do (have to think about it a little)
Second thing is that the code only calculates a value for the last bar. When you want to calculate it for the entire array for backtesting purposes then you will need to add additional code. The question is if this will be fast enough. WIll try that later.
Code I used is below. The mathematical price is shown with the orange dots and is inside the array pph
period = Param( "Period", 2, 2, 30, 1 );
slipf = Param( "Slippage factor", 0, 0, 20, 1 );
offsetf = Param( "Offset factor", 0, 0, 20, 1 );
offset = TickSize * offsetf;
mah = MA( H, period ) + offset;
//mal = MA( L, period ) - offset;
pph = Ref( MA( H, period - 1 ), -1 ) + Offset * ( period / ( period - 1 ) );
//pph = ( ceil( ( Ref( MA( H, period - 1 ), -1 ) + Offset * ( period / ( period - 1 ) ) ) / TickSize ) * TickSize );
//ppl = ( floor( ( Ref( MA( L, period - 1 ), -1 ) - Offset * ( period / ( period - 1 ) ) ) / TickSize ) * TickSize );
GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 0, chartShowDates );
SetBarFillColor( IIf( C > O, colorGreen, IIf( C <= O, colorRed, colorLightGrey ) ) );
Plot( C, "Last", IIf( C > O, colorDarkGreen, IIf( C <= O, colorDarkRed, colorLightGrey ) ), 64, null, null, 0, 0, 1 );
Plot( mah, "", colorWhite, 1, null, null, 0, 1, 1 );
//Plot( mal, "", colorWhite, 1, null, null, 0, 1, 1 );
Plot( pph, "", colorOrange, styleDots | styleNoRescale | styleNoline, null, null, 0, 1, 4 );
//Plot( ppl, "", colorOrange, styleDots | styleNoRescale | styleNoline, null, null, 0, 1, 4 );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
// adapted from H.B.Bandy Quatitative Trading Systems (2007) p. 184-185
AccuracyTarget = TickSize / 4;
function ZeroToFind( P )
{
FTZ = P - ( ( period - 1 ) * ( Ref( MA( H, period - 1 ), -1 ) + offset ) + ( P + offset ) ) / period;
return FTZ;
}
BC = LastValue( BarIndex() );
TF = Ref( H, 1 );
TF[BC] = HGuess = TF[BC - 1] * 10;
HSign = IIf( LastValue( ZeroToFind( TF ) ) > 0, 1, -1 );
TF[BC] = LGuess = TF[BC - 1] * 0.1;
LSign = IIf( LastValue( ZeroToFind( TF ) ) > 0, 1, -1 );
it = 0;
if( HSign == LSign )
{
HGuess = 0.0;
}
else
{
while( abs( HGuess - LGuess ) > AccuracyTarget )
{
MGuess = ( HGuess + LGuess ) / 2;
TF[BC] = MGUess;
MSign = IIf( LastValue( ZeroToFind( TF ) ) > 0, 1, -1 );
HGuess = IIf( HSign == MSign, MGuess, HGuess );
LGuess = IIf( LSign == MSign, MGuess, LGuess );
it = it + 1;
}
}
"HGuess: " + WriteVal( HGuess );
"LGuess: " + WriteVal( LGuess );
"MGuess: " + WriteVal( MGuess );
"Mathematical value: " + WriteVal( LastValue(pph) );
"Iterations needed: " + WriteVal( it );
Sent: Tuesday, May 03, 2016 11:14 AM
Subject: Re: [amibroker] cross price calculations
I think it is GenericBinarySearch.afl or "Binary search" in the index. Probably I have one of the earlier versions and therefor it is on page 182-185 in the volume I have,
regards, Ed
Sent: Tuesday, May 03, 2016 11:03 AM
Subject: Re: [amibroker] cross price calculations
Hi Howard,
Please let me know the "Figure #" of the AFL code you're talking about.
Regards, Ton.
----- Original Message -----Sent: Monday, May 02, 2016 7:10 PMSubject: Re: [amibroker] cross price calculations
Hi Ed --
I believe you have a copy of my "Quantitative Trading Systems" book.
There is a listing (pages 190-193) of an AmiBroker program that implements a binary search. It will find the price at which any indicator will have a specific value. There is no need to know the formula of the indicator. It works for indicators as simple as bands based on simple moving averages. Or as complex as almost anything you imagine, including indicators whose parameters change bar-by-bar.
Best regards,
HowardOn Tue, Apr 26, 2016 at 1:14 AM, 'Edward Pottasch' empottasch@skynet.be [amibroker] <amibroker@yahoogroups.com> wrote:
hi,Dimitris posted algorithms to calculate MA cross prices in the files section back in 2003 (file Cross Predictions.txt).Anyone made an algorithm to calculate the cross price of a Bollinger Band? I should be able to do it but would be nice if somebody did this already,thanks
__._,_.___
Posted by: "Edward Pottasch" <empottasch@skynet.be>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (28) |
Upgrade your account with the latest Yahoo Mail app
Get organized with the fast and easy-to-use Yahoo Mail app. Upgrade today!
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
.
__,_._,___
EmoticonEmoticon