Thread: MQL4 Guide
View Single Post
  #5 (permalink)  
Old 05-27-2007, 06:20 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
Quote:
Originally Posted by trader101
This is a very informative thread..Please do not stop..Continue teaching us who are new to this programming...

How do i code this procedure???
1. I want to open 3 trades in 3 different chart only after checking that there is not trade open at the moment then ...
2. I want to check the PL and if it is greater then 0, it will close all open and pending orders.
3. Then I want to open the same 3 trades in the opposite directions.
Thanks
ok.

1. I want to open 3 trades in 3 different chart only after checking that there is not trade open at the moment then ...

3. Then I want to open the same 3 trades in the opposite directions.

Code:
  int Magic = ...

  int BuyCnt = 0;
  int SellCnt = 0;
  
  int cnt = OrdersTotal();
  for (int i=0; i < cnt; i++) 
{
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
  }
  
  if (BuyCnt > 0 || SellCnt > 0) return;

  //OrderSend(OP_BUY, ...
  //OrderSend(OP_SELL, ...
Run this code on 3 different charts you need.


2. I want to check the PL and if it is greater then 0, it will close all open and pending orders.

Code:
  if (AccountProfit() > 0) 
  {
    DeleteOrders();
    CloseOrders();
  }

void CloseOrders() 
{  
  int cnt = OrdersTotal();
  for (int i=cnt-1; i >= 0; i--) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    
    int type = OrderType();
    
    if (type == OP_BUY) 
    {
      RefreshRates();
      OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3);
    }
          
    if (type == OP_SELL) 
    {
      RefreshRates();
      OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3);
    }
  }
  
}

void DeleteOrders() 
{
  int cnt = OrdersTotal();
  for (int i=cnt-1; i >= 0; i--) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
        
    int type = OrderType();
    
    if (type == OP_BUYSTOP || type == OP_SELLSTOP || type == OP_BUYLIMIT || type == OP_SELLLIMIT) 
    {
      OrderDelete(OrderTicket());
    }
  }

}
Reply With Quote