Programming Help - Bollinger Bands and Prices

 
Hi, I'm trying to write an Expert Advisor, tho I am not very far along. Could someone help me with some programming?

What I am trying to do is have a trade open when the price closes between the 1 and 2 standard deviation bollinger bands. I thought I had figured it out, but when I went and backtested this, it CONSTANTLY opened trades that had closed above or below the 2 standard deviation bands, whereas, I want the price close to be in between the two.

Can anyone shed some light on this?

Right now, I have my Price Close defined as =iClose(NULL,0,1)

I am assuming there is something wrong there. Can someone clear this up?

thanks,
Gold.
 

iClose(NULL,0,1) refers to the closing price of the most recently completed bar.

What next?

 
So if I said in my programming that I wanted iClose(NULL,0,1)>BandTop1 && iClose(NULL,0,1)<BandTop2 (assuming those are the defined terms for the upper 1 Standard Deviation and the upper 2 Standard Deviation bands), then, presumably, those trades would only open right when the bar completes and is still within that channel between the two upper bands, correct?

For some reason, my trades have been opening when they are FAR above the upper 2 SD bollinger band. Can you help me fix this?
 

Post your code.

I try not to presume anything.

 
Here's the beginning with the definitions and the BUY language:

extern double TrailingStop=30;
extern double StopLoss=20;
extern double TakeProfit=150;
extern double Lots=3;

int start()
{
double PriceClose, PriceClosePrev, BandTop1;
double BandBot1, BandTop2, BandBot2;
double BandTop1Prev, BandBot1Prev, BandTop2Prev;
double BandBot2Prev, MACD, MACDPrev;
int cnt, ticket, total;

if(Bars<100)
{
Print ("Bars less than 100");
return(0);
}
//to simplify the coding and speed up access
//data are put into internal variables

PriceClose=iClose(NULL,0,1);
PriceClosePrev=iClose(NULL,0,2);
BandTop1=iBands(NULL,0,26,1,0,PRICE_CLOSE,MODE_UPPER,0);
BandTop2=iBands(NULL,0,26,2,0,PRICE_CLOSE,MODE_UPPER,0);
BandBot1=iBands(NULL,0,26,1,0,PRICE_CLOSE,MODE_LOWER,0);
BandBot2=iBands(NULL,0,26,2,0,PRICE_CLOSE,MODE_LOWER,0);
BandTop1Prev=iBands(NULL,0,26,1,0,PRICE_CLOSE,MODE_UPPER,1);
BandTop2Prev=iBands(NULL,0,26,2,0,PRICE_CLOSE,MODE_UPPER,1);
BandBot1Prev=iBands(NULL,0,26,1,0,PRICE_CLOSE,MODE_LOWER,1);
BandBot2Prev=iBands(NULL,0,26,2,0,PRICE_CLOSE,MODE_LOWER,1);
MACD=iMACD(NULL,0,26,0,9,PRICE_CLOSE,MODE_EMA,0);
MACDPrev=iMACD(NULL,0,26,0,9,PRICE_CLOSE,MODE_EMA,1);

total=OrdersTotal();
while(total<1)
{
//any orders identified
//check for long position (BUY) possibility

if(PriceClose>BandTop1 && PriceClose<BandTop2 && PriceClosePrev<BandTop1Prev)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point, "BBChannel",17394,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("BUY order opened :", OrderOpenPrice());
}
else Print ("Error opening BUY order : ",GetLastError());
return(0);
}


Does anything in there look out of place? My closeout language works, it's really this stuff up here that's giving me problems.
I should explain also that I want the previous bar to have closed out BELOW the upper 1 SD band. Any thoughts?
 

You are comparing PriceClose of bar 1 and 2 with Band levels of the current bar and bar 1....

You get trapped in the while loop, probably want to use if instead

You mention "speed up processing". This code will not take even 1 millisecond to run.

Put in some print statements and see what all the values are when the trade opens.

"my trades have been opening when they are FAR above the upper 2 SD bollinger band"

How far is "far" ?

 
Ok, based on the code I posted before, I want to know how to open a trade immediately, once last bar has closed inside that channel. I don't want to wait for the next bar to close, I want it to happen immediately. So, if I'm looking at a 30 minute chart, once the bar for the past 30 minutes has posted, I want the trade to open within the next few ticks. Is that possible? How does the language for opening the trade have to read compared to what I have?
 

Your reply above does not address any of the points I raised.

 
golden188:

BandTop1=iBands(NULL,0,26,1,0,PRICE_CLOSE,MODE_UPPER,0);
BandTop2=iBands(NULL,0,26,2,0,PRICE_CLOSE,MODE_UPPER,0);
BandBot1=iBands(NULL,0,26,1,0,PRICE_CLOSE,MODE_LOWER,0);
BandBot2=iBands(NULL,0,26,2,0,PRICE_CLOSE,MODE_LOWER,0);
During bar development, closing price keeps changing from lowest to highest price of that bar. Try something like below to make the values more monotonic:
BandTop1=iBands(NULL,0,26,1,0,PRICE_HIGH,MODE_UPPER,0);
BandTop2=iBands(NULL,0,26,2,0,PRICE_HIGH,MODE_UPPER,0);
BandBot1=iBands(NULL,0,26,1,0,PRICE_LOW,MODE_LOWER,0);
BandBot2=iBands(NULL,0,26,2,0,PRICE_LOW,MODE_LOWER,0);
And as phy said, print the values of those variables (PriceClose, BandTop, etc. ) and show us the log. It may help the discussion.
 

disagree.

He wants to look at the band values for bars 1 and 2 since they are completed. Band values are calculated off the closing price and will be stable for bars 1 and 2

He wants to compare closing price in bar 1 and 2 to see if matches his criteria for band crossing.

Bar 0 should not be part of the band calculation or price to band comparison.

Using PRICE_HIGH/PRICE_LOW vs PRICE_CLOSE does not affect the stability of the result for closed bars 1 and 2

But, that is not what he has coded.

 
What he does is comparing previous bar close, CLOSE[1], against current band MA[0]+STD[0] and MA[0]+2*STD[0]
and comparing prev prev bar close, CLOSE[2], against MA[1]+STD[1].

Here MA[0] and STD[0] are not yet stable.
Reason: