Forex Forums | ForexLasers.com


Go Back   Forex Lasers Forum > METATRADER FORUM > Expert Advisors / Robots


Help with small EA that closes all open orders.

Expert Advisors / Robots


Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old 24-03-2009, 02:53
Level 1 Lasers Member
 
Join Date: Mar 2009
Posts: 2
Default Help with small EA that closes all open orders.

Hello, can anyone help me finish this EA. I know I'm doing something wrong or a piece of code could be missing. The EA should close all open orders once the Account Profit reaches x amount. It should be placed in all open charts simultaneously that have the same account so that all open orders close once the account's profit is reached even though some charts may have losses.

Thanks.

Manny

#property copyright ""
#property link ""

#include <stdlib.mqh>

#define MAGICMA 0

string SYMBOL = " ";

//Expert Settings

extern double ACCOUNTPROFITDOLLARS = 80.00;

//Globals
datetime LastBarTraded = 0;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
void start()
{
CheckForAccountProfitDollars();
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
void ClosePositions(int OrderTickets2Close[])
{
int i;
bool is_good;

for(i = 0; i < ArraySize(OrderTickets2Close); i++)
{
OrderSelect(OrderTickets2Close[i], SELECT_BY_TICKET);
if(OrderType() == OP_SELL)
{
while (true)
{
is_good=OrderClose(OrderTicket(), OrderLots(), Ask, 3, Orange);

}
}

if(OrderType() == OP_BUY)
{
while (true)
{
is_good=OrderClose(OrderTicket(), OrderLots(), Bid, 3, Orange);

}
}
}
return;
}

//+------------------------------------------------------------------------------------+
//| CHECK FOR ACCOUNT PROFIT DOLLARS |
//+------------------------------------------------------------------------------------+

void CheckForAccountProfitDollars()
{
int i, j;
int OrderTickets2Close[];
ArrayResize(OrderTickets2Close, 0);

for(i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS);
if( (OrderType() == OP_BUY) &&
(AccountProfit >= (ACCOUNTPROFITDOLLARS))
{
ArrayResize(OrderTickets2Close, j + 1);
OrderTickets2Close[i] = OrderTicket();
j++;
}
if( (OrderType() == OP_SELL) &&
(AccountProfit >= (ACCOUNTPROFITDOLLARS))
{
ArrayResize(OrderTickets2Close, j + 1);
OrderTickets2Close[i] = OrderTicket();
j++;
}
}

ClosePositions(OrderTickets2Close);

return;
}
Reply With Quote
  #2  
Old 30-03-2009, 08:02
Level II Lasers Member
 
Join Date: Feb 2009
Posts: 27
Default

You've got quite a few problems with that code.

Rather than explain what's going wrong, I'm pretty sure there's a purpose built EA out there already which will save time.

I'll see if I can find it and paste a link.

If I can't find it I'll try to explain what you're doing wrong.
Reply With Quote
  #3  
Old 30-03-2009, 08:13
Level II Lasers Member
 
Join Date: Feb 2009
Posts: 27
Default

I can't immediately see the EA that I've been looking for, but you basically just need two functions in your EA.

int start()

{
if(AccountEquity()-AccountBalance()>=ACCOUNTPROFITDOLLARS)
CloseAll();

else return(0);
}

void CloseAll()

{
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage, Color);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage, Color);
}
}

Last edited by jezzer1961; 30-03-2009 at 12:42.
Reply With Quote
  #4  
Old 03-04-2009, 15:23
Level 1 Lasers Member
 
Join Date: Mar 2009
Posts: 2
Default Thanks

Hey thanks jezzer1961.
I'll try it.
M
Reply With Quote
  #5  
Old 27-04-2009, 01:20
Level 1 Lasers Member
 
Join Date: Apr 2009
Posts: 6
Default

codebase.mql4.com/page13 may provide an EA that does what you want.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The difference between Stop Loss and Limit orders! Johny Forex Discussion 2 06-06-2014 02:09


All times are GMT. The time now is 14:22.


Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2019, vBulletin Solutions, Inc.