Forex Forums | ForexLasers.com |
|
|
LinkBack | Thread Tools | Search this Thread |
#1
|
|||
|
|||
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; } |
#2
|
|||
|
|||
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. |
#3
|
|||
|
|||
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. |
#4
|
|||
|
|||
Thanks
Hey thanks jezzer1961.
I'll try it. M |
#5
|
|||
|
|||
codebase.mql4.com/page13 may provide an EA that does what you want.
|
Thread Tools | Search this Thread |
|
|
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 |