Results 1 to 6 of 6
 0 Attachment(s)    

Thread: Close Specific Trade Positions

  1. #1
    Junior Member
    Join Date
    Jan 2009
    Posts
    6

    Default Close Specific Trade Positions

    Hi,
    How would you close all the open positions in a specific pair when the sum profit had reached a certain level?

    Without closing all other open positions

    Example

    Profit Target = 50

    Sum all EURUSD positions
    If sum of EURUSD positions >= Profit Target
    Close all EURUSD positions

    Thanks in Advance

  2. #2
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    Quote Originally Posted by jimhook_uk1 View Post
    Hi,
    How would you close all the open positions in a specific pair when the sum profit had reached a certain level?

    Without closing all other open positions

    Example

    Profit Target = 50

    Sum all EURUSD positions
    If sum of EURUSD positions >= Profit Target
    Close all EURUSD positions

    Thanks in Advance
    Hi jimhook_uk,

    Parameters

    Code:
    extern double ProfitTarget=50;
    extern string SpecificPairs="EURUSD";
    Start

    Code:
    if(AccountEquity()>=(AccountBalance()+ProfitTarget))
    {
        CloseBuyOrders(Magic);
        CloseSellOrders(Magic);
        return(0);
    }

    Functions


    Code:
    //|---------close buy orders
    
    int CloseBuyOrders(int Magic)
    {
      int result,total=OrdersTotal();
    
      for (int cnt=total-1;cnt>=0;cnt--)
      {
        OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
        if(OrderMagicNumber()==Magic&&OrderSymbol()==SpecificPairs)
        {
          if(OrderType()==OP_BUY)
          {
            OrderClose(OrderTicket(),OrderLots(),Bid,3);
            switch(OrderType())
            {
              case OP_BUYLIMIT:
              case OP_BUYSTOP:
              result=OrderDelete(OrderTicket());
            }
          }
        }
      }
      return(0);
    }
    
    //|---------close sell orders
    
    int CloseSellOrders(int Magic)
    {
      int result,total=OrdersTotal();
    
      for(int cnt=total-1;cnt>=0;cnt--)
      {
        OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
        if(OrderMagicNumber()==Magic&&OrderSymbol()==SpecificPairs)
        {
          if(OrderType()==OP_SELL)
          {
            OrderClose(OrderTicket(),OrderLots(),Ask,3);
            switch(OrderType())
            {
              case OP_SELLLIMIT:
              case OP_SELLSTOP:
              result=OrderDelete(OrderTicket());
            }
          }
        }
      }
      return(0);
    }

  3. #3

    Default

    Surely that will close the open fiber positions when the overall account profit hits 50. As I had read the question, jimhook wanted to close the fiber positions when the open profit from just the fiber positions had hit 50 regardless of any other profit or loss on the account.

    Or have I misunderstood?

    If I've not misunderstood, you'd need to add the profits together as you're counting the open orders.

  4. #4
    Junior Member
    Join Date
    Jan 2009
    Posts
    6

    Default

    Hi jezzer,
    You are correct; I want this code to only close positions in a specific pair if the total profit of that pair hits the target. I want all other open positions to remain open until they are closed by some other process.

  5. #5

    Default

    Jim,

    I thought this was what you wanted. The logic is quite simple.

    Firstly you need to define 2 doubles. One double we'll call TotalOpenProfit. It will sit before the OrderSelect() function and will = 0 so that it will reset as the code returns.

    The second double will be called OpenProfit and will sit within the OrderSelect() function before any conditons.

    OpenProfit = OrderProfit()+OrderSwap()+OrderCommission();

    Then, when the OrderSelect() function starts counting the open trades, TotalOpenProfit will equal TotalOpenProfit+OpenProfit and will either increase or decrease with each order in the pool that the OrderSelect() function counts.

    Once the TotalOpenProfit>=50 then you can start closing open positions and deleting pending orders.

    I hope that helps. I'd rather post the idea behind the logic in the hope that you understand, than a load of code which is probably meaningless to many.
    Last edited by jezzer1961; 02-17-2009 at 20:39.

  6. #6
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default

    Quote Originally Posted by jimhook_uk1 View Post
    Hi jezzer,
    You are correct; I want this code to only close positions in a specific pair if the total profit of that pair hits the target. I want all other open positions to remain open until they are closed by some other process.
    Ok good question, you should create first a loop, something like this :

    Code:
    double TotalProfits=0;
    
    int i;
    int Limit=500;
    for(i=0;i<Limit;i++)
    {
       OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
       if(OrderSymbol()==SpecificPairs&&OrderMagicNumber()==Magic)
       {
          TotalProfits+=(OrderProfit());
    
       }
    }
    And then to replace this :

    Code:
    if(AccountEquity()>=(AccountBalance()+ProfitTarget))
    by :

    Code:
    if(TotalProfits>=ProfitTarget)

Similar Threads

  1. Close open trade in second.
    By ecity999 in forum Ideas for expert advisors
    Replies: 0
    Last Post: 06-01-2013, 14:31
  2. Specific times to trade my ea
    By forexbunny in forum MQL programming
    Replies: 1
    Last Post: 03-21-2013, 18:59
  3. Auto trade entries at specific time
    By andrewte in forum Ideas for expert advisors
    Replies: 1
    Last Post: 10-25-2009, 19:12
  4. One action(Open or Close a trade) per bar
    By snkpool in forum MQL programming
    Replies: 0
    Last Post: 08-25-2009, 07:25
  5. Close Trade
    By abroco in forum Ideas for expert advisors
    Replies: 4
    Last Post: 06-30-2009, 14:39

Tags for this Thread

closing, eurusd, level, magic, profit, sells, simple

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •