Download MetaTrader 5

Managing two trader

To add comments, please log in or register
Marek
9
Marek  

Hi,

i am trying to write a simple EA, which will close two specified trades when net profit from them both reach specified value. For example i bought EURCAD and sold USDCAD and i want EA to close them both when overall profit would reach 10 pips. I have written that code below but there is a problem. That EA close only one trade which was taken from the chart on which EA was activated. I will appriopriate any clues. (I'm really noob, this is my first EA ever)

 

 

//+------------------------------------------------------------------+
//|                                                   zamykanie2.mq4 |
//|                                                                s |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "s"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern int position1;
extern int position2;
extern int overallprofit;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int profit1,profit2,rezultat;
   double pricee;
   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderTicket()==position1)
            profit1=(OrderProfit() - OrderCommission()) / OrderLots() / MarketInfo( OrderSymbol(), MODE_TICKVALUE);
         if(OrderTicket()==position2)
            profit2=(OrderProfit() - OrderCommission()) / OrderLots() / MarketInfo( OrderSymbol(), MODE_TICKVALUE);
         }
      }
   rezultat=profit1+profit2;
   Alert(profit1+","+profit2);    
   if(rezultat>0&&rezultat>overallprofit){
      close(position1);
      close(position2);
    }
    
   if(rezultat<0&&rezultat<overallprofit){   
      close(position1);
      close(position2);
      
    }
  }
//+------------------------------------------------------------------+
void close(int ticket){
double pricee;
if(OrderSelect(ticket,SELECT_BY_TICKET, MODE_TRADES)){
   if(OrderType()==OP_BUY)
    pricee=Bid;
   if(OrderType()==OP_SELL)
    pricee=Ask;
   OrderClose(ticket,OrderLots(),pricee,20);
    }
    }
whroeder1
16496
whroeder1  
dzony: overall profit would reach 10 pips... that EA close only one trade which was taken from the chart on which EA was activated
  1. Are you converting overallprofit from pips to ticks (profit1/2.)?
  2. Your close() uses predefined variables They are only for the current pair.
       if(OrderType()==OP_BUY)
        pricee=Bid;
       if(OrderType()==OP_SELL)
        pricee=Ask;
    
  3. MODE_TRADES is ignored when selecting by ticket.
    void close(int ticket){
       if(OrderSelect(ticket,SELECT_BY_TICKET)){
          OrderClose(ticket,OrderLots(),OrderClosePrice(),20);
       }
    }
  4. If you restart the terminal (power failure, etc) you've lost the values of position1/2. Do you recover?
  5. What are Function return values ? How do I use them ? - MQL4 forum
qjol
3245
qjol  
    pricee=MarketInfo("YourSymbol", MODE_BID);
   if(OrderType()==OP_SELL)
    pricee=MarketInfo("YourSymbol", MODE_ASK);


or even better

OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 20);
whroeder1
16496
whroeder1  
qjol:
 pricee=MarketInfo("YourSymbol", MODE_BID);
if(OrderType()==OP_SELL)
 pricee=MarketInfo("YourSymbol", MODE_ASK);

or even better ...

Problem is it's not "YourSymbol" but multiple symbols from multiple orders.
 pricee=MarketInfo(OrderSymbol(), MODE_BID);
if(OrderType()==OP_SELL)
 pricee=MarketInfo(OrderSymbol(), MODE_ASK);
Unnecessary with OrderClosePrice()
To add comments, please log in or register