Re: [amibroker] cross price calculations

 



Hi Ed,

 

I have tried to get a simpler equation with Bollinger but failed. The only solution I see is simulating the Bollinger Band as a normal lineair band. To show you what I want, see underneath the simplified code. By modifying constantly the Param for the last 3 prices you will get a lineair band that is close to the Bollinger band giving the F factor.

 

Ftr           = Param("Factor", 0, 0, 5, 0.01 );

 

MA3        = (Ref(Close,-2) + Ref(Close,-1) + Close )/3;

Std           = StDev(Close, 3);

UP1         = MA3 + Std;

UP2         = MA3 + Ftr/100 * MA3;

 

Plot(Close,"", colorBlack, styleCandle);

Plot(UP1,"", colorRed );

Plot(UP2,"", colorblue );

 

Having a normal lineair band the solution is simple :

 

P1        = F * ( P3 + P2 + P1 ) / 3

 

Here is F the factor that simulates the Bollinger over de last 3 prices. And the above equation gives the same simple answer as you've already got or :

 

P1        = ( K3 + K2 ) / ( 3/F – 1 )

 

The Bollinger simulation process that gives F should not be difficult.

 

Regards, Ton.

 

----- Original Message -----
Sent: Friday, April 29, 2016 7:26 AM
Subject: Re: [amibroker] cross price calculations

 

hi Ton,
 
yes the point is that your system is based in daily, hourly, 15 minute, whatever, and you allow for an intraday ( intrabar) signal. So when you are trading the data comes in tick by tick and you will get your signal in the real time, so there is no problem here. But when developing the system you might only need to, or are able to use hourly or daily bars. And if your signal is intrabar you need to know the price as precisely as possible. Using the simple MA( H, period ) function as an intrabar signal generator I shown in my code example which price to use.
 
regards, Ed
 
 
Sent: Thursday, April 28, 2016 3:20 PM
Subject: Re: [amibroker] cross price calculations
 
 



Thanks Ed. Just a simple last question. Concerning "But my problem was:  what is the BuyPrice when I want to buy at Cross( H, MA( H, 3 ) )  in the hourly timeframe.", with Mandelbrot in mind, do you have that same problem with EOD timeseries ?

Regards, Ton.

----- Original Message -----
Sent: Thursday, April 28, 2016 12:39 PM
Subject: Re: [amibroker] cross price calculations
 
 

hi Ton,
 
Buying at the bar low works great (in theory)    but this would not be correct. Your backtest would give an inflated result. Turns out if you get a signal at a certain bar you can calculate the price at which this signal occurs ahead of time (at the end of the previous bar).
 
But my problem was:  what is the BuyPrice when I want to buy at Cross( H, MA( H, 3 ) )  in the hourly timeframe.
 
You would think this price would be MA( H, 3) but that is not the case. The price is:  ref( MA( H, 2 ), –1 )
 
However, when I want to calculate the BuyPrice for a buy signal at Cross( H, BBandTop( H, 3, 1 ) )  there is not such a simple solution.  I thought I could do the same as I did with the MA but this is not correct. So I didn't solve this one yet,
 
regards, Ed
 
 
Sent: Thursday, April 28, 2016 11:41 AM
Subject: Re: [amibroker] cross price calculations
 
 



Hi Ed,

Must be me. Getting older J

But I would get the BuyPrice with following instruction :

BuyPrice = IIf(Cross(stl,L),L,Null);

Giving me :

 

Regards, Ton.

----- Original Message -----
Sent: Thursday, April 28, 2016 8:45 AM
Subject: Re: [amibroker] cross price calculations
 
 

all these emails and I am still not clear?   I must work a bit on my writing skills, or perhaps I see things that are not there.
 
think of it this way: say you develop a trading system in the hourly timeframe that uses a simple MA cross, like  Buy = cross( H, MA( H, 3 ) )
 
what is the BuyPrice you use? What is the price (in real time) you could get? What is the price at which the signal will emerge?
 
regards, Ed
 
Sent: Wednesday, April 27, 2016 9:10 PM
Subject: Re: [amibroker] cross price calculations
 
 



Ed I'm getting the same result as with the AB standard instructions :

sth     = bbandtop( H, periods, width );

stl      = bbandbot( L, periods, width );

Still do not understand where you're looking for …

Regards, Ton.

----- Original Message -----
Sent: Wednesday, April 27, 2016 4:30 PM
Subject: Re: [amibroker] cross price calculations
 
 

hi Ton,
 
in my previous post I posted a simple solution, although I am still looking at it, getting some strange things happening so I am not entirely convinced it is correct.
 
that formula was based on me writing the BB bands as follows:
 
 
 
// AFL code E.M.Pottasch 2016

periods = Param( "Periods", 3, 3, 30, 1 );
Width = Param( "Width", 1, 0, 5, 0.1 );

// Bollinger Band Top calculation
function calculateBollingerBandTop( pp, periods, width )
{
    bol = Null;

    for( i = periods; i < BarCount; i++ )
    {
        aa = 0;

        for( j = 0; j < periods; j++ )
        {
            aa = pp[i - j] + aa;
        }

        sdp = 0;

        for( j = 0; j < periods; j++ )
        {
            sdp = sdp + ( pp[i - j] - ( aa / periods ) ) ^ 2;
        }

        bol[i] = ( aa / periods ) + width * sqrt( sdp / periods );
    }

    return bol;
}

// Bollinger Band Bot calculation
function calculateBollingerBandBot( pp, periods, width )
{
    bol = Null;

    for( i = periods; i < BarCount; i++ )
    {
        aa = 0;

        for( j = 0; j < periods; j++ )
        {
            aa = pp[i - j] + aa;
        }

        sdp = 0;

        for( j = 0; j < periods; j++ )
        {
            sdp = sdp + ( pp[i - j] - ( aa / periods ) ) ^ 2;
        }

        bol[i] = ( aa / periods ) - width * sqrt( sdp / periods );
    }

    return bol;
}

sth = calculateBollingerBandTop( H, periods, width );
stl = calculateBollingerBandBot( L, periods, width );

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 );
clr1 = ColorRGB( 0, 46, 184 );
Plot( sth, "", clr1, 1, null, null, 0, 1, 1 );
Plot( stl, "", clr1, 1, null, null, 0, 1, 1 );
 
 
Sent: Wednesday, April 27, 2016 4:12 PM
Subject: Re: [amibroker] cross price calculations
 
 



OK I see where you're looking for. But are you sure your BB formula is correct ? Check in Excel with an example. This is what I got for your formula :

=(A5+A6+A7) + SQRT(((A5-(A5+A6+A7))^2+(A6-(A5+A6+A7))^2+(A7-(A5+A6+A7))^2)/3)

TS

AVG

STDEV

BB

EDBB

10

12

11

11,00

1,00

12,00

55,02

14

12,33

1,53

13,86

61,70

12

12,33

1,53

13,86

61,70

16

14,00

2,00

16,00

70,05

Regards, Ton.

----- Original Message -----
Sent: Wednesday, April 27, 2016 12:15 PM
Subject: Re: [amibroker] cross price calculations
 
 

as I mentioned the cross price of the High with a 3 period simple moving average of the High can be written as:
 
P[i] = ( H[i-2] + H[i-1] + P[i] ) / 3
 
which can be simplified to solve the price P where the cross occurs to: P[i] = 0.5 * ( H[i-2] + H[i-1] )
 
Doing the same for a Bollinger band, finding the cross price of BBANDTOP( H, 3, 1 )
 
can be written as:
 
P[i] = ( H[i-2] + H[i-1] + P[i] ) + SQRT( ( ( H[i-2] - ( H[i-2] + H[i-1] + P[i] ) )^2 + ( H[i-1] - ( H[i-2] + H[i-1] + P[i] ) )^2 + ( P[i] - ( H[i-2] + H[i-1] + P[i] ) )^2 ) / 3 )
 
 
now simplify that one to solve the price P where the cross occurs and then work out a generalized algorithm  
 
 
 
Sent: Wednesday, April 27, 2016 10:17 AM
Subject: Re: [amibroker] cross price calculations
 
 

I added an example GIF movie of this code I posted here (top chart) https://sites.google.com/site/amitradingideas/
 
you see the orange dots. When the high crosses this point then the signal occurs. This is the price one could buy instead of MA(H,3). This orange dot is already known at the end of the previous bar. It can help you doing backtests when you only have for instance 5minute data to pinpoint the exact price of entry.
 
Ed
 
Sent: Wednesday, April 27, 2016 9:51 AM
Subject: Re: [amibroker] cross price calculations
 
 

hi Ton,
 
that is strange. No, it should be visible using this link: https://sites.google.com/site/amitradingideas/  the images I set to be visible for everyone.
 
But I show some code here to show what I am after (see below). Assume you are in the hourly timeframe  and you want to use signals when the price crossed MA(H,3). Because you are in the hourly timeframe the best guess you can make for the price you have to pay is at MA(H,3).
 
In real time when data comes in you will see that this is usually not the price at which the cross occurs. Before the bar starts at which a cross occurs you can exactly calculate that when a cross will occur you already know at which price it will occur. This I show in the chart with the orange dots. This is the price where in real time the cross occurs. Like I explained yesterday it is just simple highschool math. It occurs at the price where the price function and the MA function are equal. Or in this simple example:
 
P[i] = ( H[i-2] + H[i-1] + P[i] ) / 3
 
the same can be done for a Bollinger band, although this is more complicated. The equation   P[i] = ( H[i-2] + H[i-1] + P[i] ) / 3  can be simplified into P[i] = 0.5 * ( H[i-2] + H[i-1] ). However, one should be able to reduce this to a generalized equation which includes the period as a variable. For this simple MA setup I probably could figure out a generalized equation (by trial and error, usually my approach ...). However with the Bollinger bands things become more complicated. How does one get a generalized equation similar to what Dimitris has done ?
 
thanks, ed
 
 
 
ma1 = MA( H, 3 );

function calc()
{
    pp = null;

    for( i = 2; i < BarCount; i++ )
    {
        pp[i] = 0.5 * ( H[i - 2] + H[i - 1] );
    }

    return pp;
}

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( ma1, "", colorwhite, 1, null, null, 0, 1, 1 );

pp = calc();
Plot( pp, "", colorOrange, styleDots | styleNoRescale | styleNoline, null, null, 0, 1, 4 );

Buy = Cross( H, ma1 );
BuyPrice = ma1;

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -20 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
 
Sent: Wednesday, April 27, 2016 9:04 AM
Subject: Re: [amibroker] cross price calculations
 
 



Hi Ed,

When clicking the URL in your email, I am getting a blank screen with a "Snapshot" header. On the left the projects. Which one ? "Real Time Data Fit" or "Polynomial Curve Fit" ? The first is hanging. The second just shows a price range without bands … Something going wrong ?

Regards, Ton
 
----- Original Message -----
Sent: Tuesday, April 26, 2016 5:51 PM
Subject: Re: [amibroker] cross price calculations
 
 

Ton
 
this one I removed, it was actually the other image I pointed at.
 
 
you see that once the signal comes the price is moving higher and therefor also the entry price, since the entry price is at the Bollinger band. This is because the Bollinger band is using the High and Low. In some cases the entry price moves a lot in my favour, sometimes not. In the hourly timeframe it is allowed to move 8 ticks on average for the system still to work.
 
But instead of adding a slippage I can actually exactly calculate when the signal will start as per Dimitris post. You can calculate the cross at the end of the previous bar so one can exactly calculate the entry price. This is what I am trying to do
 
regards, Ed
 
 
Sent: Tuesday, April 26, 2016 5:05 PM
Subject: Re: [amibroker] cross price calculations
 
 



I have two problems Ed. Firstly I do not see the Bollinger in your video. Did you already plot the bands ?

Secondly I do not understand your problem. If you have the average and the stdev than you should have the Bollingers. And the crossing of the price with the bands should be a simple thing to do. Am I missing something ?

Regards, Ton.

----- Original Message -----
Sent: Tuesday, April 26, 2016 3:02 PM
Subject: Re: [amibroker] cross price calculations
 
 

hi Ton,
 
what I am trying to do is related to what Dimitris is doing but my problem is simple and certainly solvable.
 
all I want is to calculate the exact price at which a Bollinger band is crossed, it is basically high school mathematics
 
see the signals in the top chart here https://sites.google.com/site/amitradingideas/
 
it is oil ,1 hour bars. The Bollinger bands use 3 bars and are calculated using the High. For the entry price I use the Bollinger band price, In reality you will almost never get this price. So I want to calculate at which price the price is the same as the Bollinger bands. This is the price in reality I will buy or short.
 
a simpler problem would be for instance to calculate at which price the MA(C,3) will be crossed. The price should be equal to the MA ( in highschool we had to calculate the cross point of 2 functions). So simply put
 
P[i] = (C[i-2] + C[i-1] + PP[i] ) / 3
 
or: P[i] = 0.5*( C[i-2] + C[i-1] )
 
price P will be the exact price where the MA(C,3) is crossed. For a Bollinger band it becomes a bit more complicated and I have difficulties translating this into a general algorithm, like Dimitris has done.
 
I will get back to this tomorrow to explain it a bit in greater detail.
 
But for this Bollinger band system I show in the chart I can calculate the exact price where the buy/short signals will be generated. Then I can more precisely calculate if the system will work or not
 
regards, ed
 
Sent: Tuesday, April 26, 2016 2:24 PM
Subject: Re: [amibroker] cross price calculations
 
 

Hi Edwards,

Assume you're talking about https://groups.yahoo.com/neo/groups/amibroker/conversations/topics/45738

Please check following : It is obvious that there are extended periods for which the DEMAcross is impossible.

Question is how to build the bands without having the average ?

Regards, Ton.

----- Original Message -----
Sent: Tuesday, April 26, 2016 10:14 AM
Subject: [amibroker] cross price calculations
 
 

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: "Ton Sieverding" <ton.sieverding@scarlet.be>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (23)

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/


.

__,_._,___


EmoticonEmoticon