# Order send based on bollinger bands works partially

20

I made an Expert Advisor that opens an order based on price compared to the Bollinger Bands, it sends the order correctly, but sometimes it ignores some bars and opens, or ignores all of them and waits for another trigger.

Here is the part of code

``` void NewOrder(){

int OrderSended;

double BMiddle = iBands ( NULL , 0 , 20 , 2 , 0 , PRICE_CLOSE ,MODE_MAIN, 0 );

if ( iLow ( NULL , 0 , 0 ) < iBands ( NULL , 0 , 20 , 2 , 0 , PRICE_CLOSE ,MODE_LOWER, 0 ) && AvailableB == true ){

OrderSended = OrderSend ( NULL ,OP_BUY,CalcSize(Risk),Ask, 50 ,StopLoss( 0 ), 0 , NULL ,MagicN, 0 , clrGreen );

if (OrderSended > 0 ){

AvailableB = false ;
ticketB = OrderSended;

}
else
Print ( "error" );
}

if ( iHigh ( NULL , 0 , 0 ) > iBands ( NULL , 0 , 20 , 2 , 0 , PRICE_CLOSE ,MODE_UPPER, 0 ) && AvailableS == true ){

OrderSended = OrderSend ( NULL ,OP_SELL,CalcolaLotti(Rischio),Bid, 50 ,StopLoss( 1 ), 0 , NULL ,MagicN, 0 , clrGreen );

if (OrderSended > 0 ){

AvailableS = false ;
ticketS = OrderSended;

}
else
Print ( "error" );
}

}```
19170

1. Once your code sets AvailableB or AvailableS to false, it can never trade that direction again. Your problem is elsewhere.

2. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence. As is "if available B".

3. ```       if ( iHigh ( NULL , 0 , 0 ) > iBands ( NULL , 0 , 20 , 2 , 0 , PRICE_CLOSE ,MODE_UPPER, 0 ) && AvailableS == true ){

OrderSended = OrderSend ( NULL ,OP_SELL,CalcolaLotti(Rischio),Bid, 50 ,StopLoss( 1 ), 0 , NULL ,MagicN, 0 , clrGreen );
```
Don't use NULL.
• You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
• Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
• Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
• MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].

20

William Roeder:
1. Once your code sets AvailableB or AvailableS to false, it can never trade that direction again. Your problem is elsewhere.

2. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence. As is "if available B".

3. Don't use NULL.
• You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
• Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
• Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
• MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].

Thanks for the advices, to set that variables back to false i have another part of code that i forgot to post sorry.

Here it is

```void CloseOrder(){

bool ClosedB;

if(Bid > iMA(NULL,PERIOD_CURRENT,20,1,MODE_SMA,PRICE_CLOSE,1) && !AvailableB){

AvailableB = true;

}
}

bool ClosedS;

ClosedS = OrderClose(OrderTicket(),OrderLots(),Bid,2);
AvailableS = true;

}
}
}```
19170

2. ```   if(OrderSelect(ticketB,SELECT_BY_TICKET,MODE_TRADES)){