Inverse Bollinger Bands - Mike Need simple Help !

 

Dear All,

I am a newbie of MT4 and I am practice with this language.

I would like to develop a EA that open a SELL position when value go over Up BB band (and viceversa).

EA will close this position when we will have 2 pips of profits.

I have written thsi code but at the moment only with SL and TP and not with closing procedure.

EA will not open any position ... someone can help me?

//+------------------------------------------------------------------+
//| Michele Ainardi |
//| Inverse bollinger trend IBT V001
//+------------------------------------------------------------------+

extern double Lots = 0.1;
extern double TakeProfit = 10;
extern double StopLoss =40;


int start()
{

double BandTop, BandBot;
double ValueAsk, ValueBid;

ValueAsk = Ask;
ValueBid = Bid;
BandTop=iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_UPPER,0);
BandBot=iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_LOWER,0);


if (OrdersTotal() < 1)
{
if ( ValueAsk>BandTop )
{
OrderSend(NULL,OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point);
return;
}
return;
if ( ValueBid<BandBot )
{
OrderSend(NULL,OP_BUY,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point);
return;
}
return;
}


}
// the end.

 

Hi Michele

Your slippage is zero and your order send command is incomplete it is missing parameters.


int OrderSend(

string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color

arrow_color=CLR_NONE)

 
Ruptor:

Your slippage is zero and your order send command is incomplete it is missing parameters.

The comment etc are optional parameters, and don't have to be supplied. The point about the slippage is a good one, though a market order can theoretically succeed - sometimes - with zero slippage.


There are at least two other possible problems with the code:


  • The buy command (OP_BUY) is trying to buy at the bid, not the ask.
  • The take-profit (10 pips) may be too close to the current price if using a 5-digit broker. This may be trying to put the t/p 0.0001 rather than 0.0010 from the entry price. (Potentially ditto on the s/l, bearing in mind that the minimum distance on a market order is in addition to the spread.)
 
jjc:

The comment etc are optional parameters, and don't have to be supplied.


Just for my information where does it say the parameters are optional? You are right about the buy at bid that will stop the buys.

 

Ruptor wrote >>

Just for my information where does it say the parameters are optional

It's the =NULL, =0 etc bits of the function declaration, defining the values which are used if these parameters are omitted. Standard syntax inherited from C++ (or Javascript) - see https://docs.mql4.com/basis/functions.

 

Hello Ruptor

Is the formals syntax... eg: (..., string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)

All 4 can be omitted. But IF want to supply actual for say magic you have to then also supply actuals for ALL leftwards optionals/defaulted formal parameters

Is a organisational issue when allowing optional/defaulted values when scanning left -> right.

IE, if physically leave out 'comment' cuz you have no 'comment' value and so accept NULL default value, the 'position' of all parameters to the right are no effectively moved leftwards by one.

This is harmfull to lower level code and decision making processes regards what item in the call list/stack is what... Of course compiler would shout but if did not, maybe this help you to see what could happen if no build time checks were made...

Course this is interpreter but must I'd assume have similar requirements in what a called function 'expects' to see regards passed in actuals and what was supplied with...

fwiw...

https://docs.mql4.com/basis/variables/formal
https://docs.mql4.com/basis/functions/call
https://docs.mql4.com/basis/functions

 
jjc:

There are at least two other possible problems with the code: [...]

Oh, and also... the buy code (OP_BUY) has obviously just been copied and pasted from the sell code. Not only is it trying to buy at the bid but also, like the sell code, it's trying to use an s/l above the entry price and a t/p below it. Not possible.

 

Thanks Guys

I am a low level programmer by trade, nothing much above embedded C so I shall have a read.

 
michele_ainardi wrote >>

Dear All,

I am a newbie of MT4 and I am practice with this language.

I would like to develop a EA that open a SELL position when value go over Up BB band (and viceversa).

EA will close this position when we will have 2 pips of profits.

I have written thsi code but at the moment only with SL and TP and not with closing procedure.

EA will not open any position ... someone can help me?

//+------------------------------------------------------------------+
//| Michele Ainardi |
//| Inverse bollinger trend IBT V001
//+------------------------------------------------------------------+

extern double Lots = 0.1;
extern double TakeProfit = 10;
extern double StopLoss =40;


int start()
{

double BandTop, BandBot;
double ValueAsk, ValueBid;

ValueAsk = Ask;
ValueBid = Bid;
BandTop=iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_UPPER,0);
BandBot=iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_LOWER,0);


if (OrdersTotal() < 1)
{
if ( ValueAsk>BandTop )
{
OrderSend(NULL,OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point);
return;
}
return;
if ( ValueBid<BandBot )
{
OrderSend(NULL,OP_BUY,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point);
return;
}
return;
}


}
// the end.

Instead of if( OrdersTotal() < 1),

write

if( ExistOpenOrders() )

and add the function:

bool ExistOpenOrders( )
{
for( int i = 0; i < OrdersTotal(); i++ )
if( OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() &&
OrderMagicNumber() == MagicNumber && OrderCloseTime() == 0 ) return (true);
return (false);
}

Also add a int MagicNumber variable .

[]´s,

Gero

 
michele_ainardi wrote >>

Dear All,

I am a newbie of MT4 and I am practice with this language.

I would like to develop a EA that open a SELL position when value go over Up BB band (and viceversa).

EA will close this position when we will have 2 pips of profits.

I have written thsi code but at the moment only with SL and TP and not with closing procedure.

EA will not open any position ... someone can help me?

//+------------------------------------------------------------------+
//| Michele Ainardi |
//| Inverse bollinger trend IBT V001
//+------------------------------------------------------------------+

extern double Lots = 0.1;
extern double TakeProfit = 10;
extern double StopLoss =40;


int start()
{

double BandTop, BandBot;
double ValueAsk, ValueBid;

ValueAsk = Ask;
ValueBid = Bid;
BandTop=iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_UPPER,0);
BandBot=iBands(NULL,0,20,2,0,PRICE_OPEN,MODE_LOWER,0);


if (OrdersTotal() < 1)
{
if ( ValueAsk>BandTop )
{
OrderSend(NULL,OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point);
return;
}
return;
if ( ValueBid<BandBot )
{
OrderSend(NULL,OP_BUY,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point);
return;
}
return;
}


}
// the end.

Hello,

Use this code to buy/ sell (change for your needs):

First Add the variable double RealPoint in your header declaraton:

double RealPoint = 0.0;

then add this to init() function:

RealPoint = Point;

if( Digits == 5 || Digits == 3 ) RealPoint *= 10;

void Buy()
{
RefreshRates();


SendOrder( OP_BUY, Ask, Ask+(RealPoint*TakeProfit), Bid-(RealPoint*StopLoss) );
}

void Sell()
{
RefreshRates();
SendOrder( OP_SELL, Bid, Bid-(RealPoint*TakeProfit), Ask+(RealPoint*StopLoss) );
}

void SendOrder( int type, double price, double takeprofit, double stoploss )
{


OrderSend( Symbol(), type, vol, price, 3, stoploss, takeprofit, "", MagicNumber, 0 );
Sleep(500);
}

 
Gero.Gero:

Instead of if( OrdersTotal() < 1), write if( ExistOpenOrders() ) [...]

You're probably right, but there's a huge amount of code on this forum which uses OrdersTotal() in this sort of context - see 'Help with Max Trades' for one of the almost countless examples. It's so common that I assume lots of people really do only want to enter a new position if there are no existing open or pending orders from any source, rather than just from their own EA (implicitly running on only one chart).

Reason: