Help with make a very simple Ea

Hamidreza Taheri
1942

Hi all,

could you please help me to make an Ea with this strategy:

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.

William Roeder
25838
William Roeder  
hmrt135:
could you please help me to make
No Slaves here, learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
Hamidreza Taheri
1942
Hamidreza Taheri  
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 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       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)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   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;
     }
//---- buy conditions
   if(Close[0]>Open[0])  
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-150*Point,0,"",MAGICMA,0,Blue);
      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(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
            if(Close[0]<Open[0])  OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
          if(Close[0]>Open[0])  OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+------------------------------------------------------------------+
Simon Gniadkowski
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.

Hamidreza Taheri
1942
Hamidreza Taheri  
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 !

I am not a coder please help

Simon Gniadkowski
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.
William Roeder
25838
William Roeder  
  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)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_TICKET))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
           Alert("OrderModify failed: ", GetLastError());
         */
    

  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))
        Alert("OrderClose Failed: ", GetLastError());

romanz
7
romanz  

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?

Please, give me some tips or links where I can find more information about this subject.

Thanks a lot!

Hamidreza Taheri
1942
Hamidreza Taheri  
RaptorUK:
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.

The problem is that i want to make positions accourding to price moving on Current Bar and i do not use any moving average. When i run my Ea on strategy tester it opens order but it will not closes. I do not want to set a certain T.p. . I mean when the reversal signal comes from Close[0]<Open[0] or Close[0]>Open[0], the current order must be closed and new reversal order should be opened and by next bar must closed current opened orders.
Simon Gniadkowski
17411

Do you realise that Close[0] = Bid ? it changes as Bar 0 is forming . . . bar 0 never has a fixed Close price, when it is actually closed it becomes Bar 1 . . .

Perhaps you should post the latest version of your code.