• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 12:16am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 12:16am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Everything to Nothing to get Everything? 7 replies

CloseAll Scritp!!! Some modifications needed!!! 0 replies

Need help!! Close order on this bar close 5 replies

Question on closeAll function 5 replies

Seeking Correct Open/Close - It's Close but ... 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: my LockInProfit() and CloseAll() fail to close everything
Cancel

my LockInProfit() and CloseAll() fail to close everything

  • Post #1
  • Quote
  • First Post: Sep 13, 2016 11:00pm Sep 13, 2016 11:00pm
  •  johnnybegood
  • | Joined Sep 2009 | Status: Member | 102 Posts
Inserted Code
void LockInProfit()
{
   double Balance = AccountBalance();
   double Equity = AccountEquity();  
  
   if ((Equity) > (Balance*1.01))
   {          
         CloseAll();                
   }    return;
}
void CloseAll()
{
  // for (int i=OrdersTotal()-1;i>=0; i--)
   for (int i = 0; i < OrdersTotal(); i++)    // FIFO rule
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         RefreshRates();            
         if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,clrNONE))
         Print("Error closing (CloseAll) #",IntegerToString(OrderTicket())," Error code ",GetLastError());
      }  
   }
}


I have multiple opened positions in multiple currencies. When LockInProfit() is triggered, it does not close everything.

Is the problem with LockInProfit() or/and CloseAll()?
I suspect that
1) The functions only close the currency pair that the EA is attached to and not all the other currency pairs.
2) At some point, it stop closing when (Equity) is no longer > (Balance*1.01).
How do I close everything? (All opened positions in multiple currencies )

How to close everything without attaching the EA to multiple charts of different pairs.
How do I close everything (including currencies of different pairs) when attaching the EA to only one chart?
  • Post #2
  • Quote
  • Sep 13, 2016 11:44pm Sep 13, 2016 11:44pm
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,428 Posts
You should use the Ask and Bid prices in the OrderClose() function. And separate the function according to the order type (buy or sell) ... as their closure price isn't the same.

Inserted Code
MarketInfo(_Symbol,MODE_ASK)
MarketInfo(_Symbol,MODE_BID)
MT4/MT5 EAs/Indicators/Alerts coder
 
 
  • Post #3
  • Quote
  • Sep 14, 2016 12:01am Sep 14, 2016 12:01am
  •  johnnybegood
  • | Joined Sep 2009 | Status: Member | 102 Posts
Inserted Code
void CloseAll2()
{
 // int total = OrdersTotal();
 // for(int i=total-1;i>=0;i--)
  for (int i = 0; i < OrdersTotal(); i++)    // FIFO rule
  {
   // OrderSelect(i, SELECT_BY_POS);
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    int type   = OrderType();
    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol_1, MODE_BID), slippage, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(Symbol_2, MODE_ASK), slippage, Red );
                          
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }
    }  
  }
  
  return;
}

I tried something like this before and have the same problem
 
 
  • Post #4
  • Quote
  • Sep 14, 2016 12:13am Sep 14, 2016 12:13am
  •  FerruFx
  • Joined May 2007 | Status: MT4/MT5 EAs/Indicators/Alerts coder | 6,428 Posts
I made a small mistake in my last answer ... Below is the right one:

Inserted Code
MarketInfo(OrderSymbol(),MODE_ASK)
MarketInfo(OrderSymbol(),MODE_BID)
MT4/MT5 EAs/Indicators/Alerts coder
 
 
  • Post #5
  • Quote
  • Sep 14, 2016 5:07am Sep 14, 2016 5:07am
  •  fredix
  • | Joined Apr 2016 | Status: Member | 31 Posts
I suggest you check the number of orders. I have the same function in a script and it never close all orders at once.

Try this in your LockInProfit():

Inserted Code
int c=0;
while(OrdersTotal()>0) && c<20)
{
    CloseAll();
    c++;
}

Variable c makes sure it does not go in an infinite loop.
 
 
  • Post #6
  • Quote
  • Sep 14, 2016 6:45am Sep 14, 2016 6:45am
  •  honestknave
  • Joined Nov 2014 | Status: Member | 1,300 Posts
You are getting orphaned orders because your close loop counts up not down. I know you are trying to comply with FIFO rules, but it will not work the way you have it coded.

Imagine you have 4 orders to close:

First loop (i=0)
At the start of the loop, the positions are:
Position 0 = Ticket 990
Position 1 = Ticket 991
Position 2 = Ticket 992
Position 3 = Ticket 993
Ticket 990 (in Position 0) gets closed

Second loop (i=1)
At the start of the loop, the new positions are:
Position 0 = Ticket 991
Position 1 = Ticket 992
Position 2 = Ticket 993
Ticket 992 (in Position 1) gets closed

Third loop (i=2)
At the start of the loop, the new positions are:
Position 0 = Ticket 991
Position 1 = Ticket 993
There is no Position 2, so the order select fails. The loop moves on without closing anything.

Fourth loop (i=3)
At the start of the loop, the positions are still:
Position 0 = Ticket 991
Position 1 = Ticket 993
There is no Position 3, so the order select fails. The loop moves on without closing anything.

The loop has now come to an end.
But it missed 2 tickets (991 and 993).
The more orders you have to close, the more tickets get missed when you count up not down.

Personally, I would always use OrderClosePrice(). Less logic required and less chance of a coding mistake.

HTH
 
 
  • Post #7
  • Quote
  • Last Post: Sep 15, 2016 2:56am Sep 15, 2016 2:56am
  •  johnnybegood
  • | Joined Sep 2009 | Status: Member | 102 Posts
Quoting fredix
Disliked
I suggest you check the number of orders. I have the same function in a script and it never close all orders at once. Try this in your LockInProfit(): int c=0; while(OrdersTotal()>0) && c<20) { CloseAll(); c++; } Variable c makes sure it does not go in an infinite loop.
Ignored
This works!
TY
 
 
  • Platform Tech
  • /
  • my LockInProfit() and CloseAll() fail to close everything
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023