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.
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" ?
Your reply above does not address any of the points I raised.
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);
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.
and comparing prev prev bar close, CLOSE[2], against MA[1]+STD[1].
Here MA[0] and STD[0] are not yet stable.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.