# Help with make a very simple Ea

1942

Hi all,

Open Position on 15M Candle.

If Close[0]>Open[0] then open buy position whit 3% risk.

If Close[0]<Open[0] then open Sell position whit 3% risk and Close the last Buy position.

and Vice versa...

We take 15 Pip as S.L. for avoiding Gaps.. but in Long positions as soon as Close[0]<Open[0] we close buy position and make a reversal Sell and by Short Positions as soon as Close[0]>Open[0] we close Sell position and make a reversal Buy.

Close Order on next candle Opening as T.P.

25838

hmrt135:
No Slaves here, learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
1942

WHRoeder:
No Slaves here, learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.

I have already do that for my self but it does not work like that i said. take a look at this code

```//+------------------------------------------------------------------+
//|                                               hmrt135 EA.mq4 |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA  20050610

extern double Lots               = 0.1;
extern double MaximumRisk        = 0.02;
extern double DecreaseFactor     = 3;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
else       return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int    orders=HistoryTotal();     // history orders total
int    losses=0;                  // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
{
int    res;
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;

//---- sell conditions
if(Close[0]<Open[0])
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+150*Point,0,"",MAGICMA,0,Red);
return;
}
if(Close[0]>Open[0])
{
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
{

//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average

//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//---- check order type
{
if(Close[0]<Open[0])  OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else                                    CheckForClose();
//----
}
//+------------------------------------------------------------------+```
17411

You need to make use of the variable you have created for the return value from OrderSend . . res, check it to see if the OrderSend worked, if it didn't print the error and you will be in a better position to fix your own issues.

Your Orders do not take into account spread . . Buy at Ask, but a Buy is closed with a Sell and a Sell is executed at Bid.

1942

RaptorUK:

You need to make use of the variable you have created for the return value from OrderSend . . res, check it to see if the OrderSend worked, if it didn't print the error and you will be in a better position to fix your own issues.

Your Orders do not take into account spread . . Buy at Ask, but a Buy is closed with a Sell and a Sell is executed at Bid.

thank you, i have tried, but it does not worked !

17411

hmrt135:

thank you, i have tried, but it does not worked !

In what way didn't it work ? what errors did you get ? you need to be more precise about your issue . . . I'm not going to download your code, compile it, run it in the Strategy Tester and make fixes to it . . I have my own code to do.
25838

1. `if(Volume[0]>1) return;`
Volume is unreliable (you can mis ticks.) Bars is unreliable (max bars on chart.) Always use time
`static datetime Time0; if (Time0 == Time[0]) return; Time0 = Time[0];`

2. `   res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+150*Point,0,"",`
Always test return codes so you find out WHY.
```res = ...
if (res < 0) Alert("OpenOrder failed: ", GetLastError());```

3. On 5 digit brokers you must adjust TP, SL, AND SLIPPAGE. On ECN brokers you must open first and THEN set stops.
```//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){
if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
} else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
// OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
/* On ECN brokers you must open first and THEN set stops
int ticket = OrderSend(..., 0,0,...)
if (ticket < 0)
else if (!OrderSelect(ticket, SELECT_BY_TICKET))
else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
*/
```

4. You MUST count down when closing in the presence of multiple order (multiple charts)
5. you don't need the if (order type) close (Bid) else close(Ask)
```if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3*pips2points, White))

7

Hi guys

Can you tell me what is the difference between 4 and 5 digits broker for a MQL programer, please?

I sense that the answer lies in another question: What is the difference between a tick and a point (MarketInfo() function and MODE_TICKSIZE and MODE_POINT flags).

In my opinion as long as a size of tick and a size of point are the same and if your code is well parametrized by one of these sizes a MQL program can work well regardless of a broker digits value for given symbol.

I never seen a broker with different sizes of tick and point. Is there any?

Thanks a lot!

17411