| New signals service! | |
|
|||||||
| Register in Forex TSD! | |
|
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time). Click here to register and get more information |
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
The following code closes all the opened long and short positions.
Code:
int Slippage = 3;
void CloseOrders() {
int cnt = OrdersTotal();
for (int i=cnt-1; i>=0; i--) {
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
//if (OrderSymbol() != Symbol()) continue;
//if (OrderMagicNumber() != Magic) continue;
if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, Slippage);
if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage);
}
}
__________________
MQL4: idea * experience + creative solution |
|
|||
|
This is the code that the EA GENERATOR created for closing open positions(down below)
The problem is that because of whatever reason..both positions are not getting closed all the time when the conditions are right. maybe it's timing with broker, I don't know. SO, if this code is Correct...instead of doing it this way... can it be written LIKE- WHILE OrdersTotal() > 0 CLose all BUYS or CLOSE all SELLS It would SEEM that this would keep looping and trying to close all orders until there were none left before proceeding.. Is this Doable? Present close code below "bool IsTradeopen = False; Total = OrdersTotal(); for (int i = 0; i < Total; i ++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) { IsTradeopen = True; if(OrderType() == OP_BUY) { if (CloseBuy1_1 <= CloseBuy1_2 && CloseBuy2_1 < CloseBuy2_2 && CloseBuy3_1 < CloseBuy3_2) Order = SIGNAL_CLOSEBUY; if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen); if (!EachTickMode) BarCount = Bars; IsTradeopen = False; Sleep(150); } else { if (CloseSell1_1 >= CloseSell1_2 && CloseSell2_1 > CloseSell2_2 && CloseSell3_1 > CloseSell3_2) Order = SIGNAL_CLOSESELL; if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) { OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange); if (!EachTickMode) BarCount = Bars; IsTradeopen = False; Sleep(150); } } } } " |
|
|||
|
That's interesting..i kinda thought it was a Timing issue with the broker but i only put a 150 MILLISECOND sleep time between orders. It SEEMS like I'm not having problems placing 2 ORDERS immediatley with NO delay between sends.
But I AM having problems getting both orders CLOSED on the close of the same bar (like they are suppose to) A Question i had is when you CLOSE 2 orders... WHen you do orderstotal() and you do For( int I... and then CLOSE 1 open position, does that affect the "OrderSelect(i, " meaning you HAD 2 orders, after one got closed your 'i' gets incremented to '1' but there's 1 order left does THAT remaining order turn into Order '0'? does This QUestion makes sense? and the EA "UniversalMACrossEA" lets you choose how many Retries for placing an order which tells me if there is an Error it will keep trying right? And I'll be only using ONE EA on ONE chart..so I won't get into that other problem Thanks COWBOY Quote:
|
|
||||
|
Quote:
This is the the type of code checks I refered to above. To answer your present question, you do not have to count down with OrderTotal for OrderSelect to work correctly. edit[ see attached code blocks] The CockeyedCowboy Code:
//«« LABEL 500 »» POSITION ASSESSMENT: »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
//«« <<<< Segment 520 >>>> »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
// Code to close open market orders and repeal pending orders at closing time.
if( ! OrdersTotal() == Empty && CurrentGMTime > TradeQuitingTime ) {
for( Counter = Zero; Counter <= OrdersTotal(); Counter = Counter + One ) {
if( OrderSelect( Counter, SELECT_BY_POS, MODE_TRADES ) == False ) continue;
for( Record = Zero; Record <= Four; Record = Record + One ) {
if( OrderMagicNumber() == (( SystemTag * Ten ) + Record )) {
switch( OrderType() ) {
case BuyLong:
if( ExitAllTrades ) {
TerminatePoistion( OrderTicket(), Bid );
} else if( ExitLossingTrades ) {
if( OrderOpenPrice() > Bid ) {
TerminatePoistion( OrderTicket(), Bid );
} // End If, Bid:
} else { // RD!{add ErrorHandler}:
} // End If, ExitAllTrades:
break;
case SellShort:
if( ExitAllTrades ) {
TerminatePoistion( OrderTicket(), Ask );
} else if( ExitLossingTrades ) {
if( OrderOpenPrice() < Ask ) {
TerminatePoistion( OrderTicket(), Ask );
} // End If, Ask:
} else { // RD!{add ErrorHandler}:
} // End If, ExitAllTrades:
break;
default: // RD!{Pending orders expire, add ErrorHandler}:
DeletePosition( OrderTicket() );
} // End Switch Case, OrderType:
} // End If, OrderMagicNumber:
} // End For Loop, Record:
} // End For Loop, Counter:
} // End If, OrdersTotal:
Code:
//«« <<<< Terminate Position, Sub Routine >>>> »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
void
TerminatePoistion ( int Position, double ClosePrice ) {
int Order = Failed;
int AttemptsTried = Empty;
bool PositionSet = False;
bool AccountAvailable = False;
color Arrow = DefaultColor;
AccountAvailable = CheckWithOrderDesk();
while ( AccountAvailable && ! PositionSet ) {
AttemptsTried = AttemptsTried + One;
Order = OrderClose( Position, OrderLots(), ClosePrice, Slippage, Arrow );
if( ! IsTesting() ) Sleep( PauseInterval );
if( Order == Filled ) { // Position Closed Successful.
PositionSet = True;
Print( " Order " + Position + " Successfully Closed." );
} else if( Order == UnFilled && AttemptsTried >= MaximumAttempts ) {
// RD!{add ErrorHandler}:
AccountAvailable = False;
Print( AttemptsTried + " attempts to close Market order " +
Position + " failed. Error [ " + GetLastError() + " ]" );
} // End If, Order:
} // End While Loop, AccountAvailable:
GetOrderComformation( Position, Order );
return;
} // End Procedure, TerminatePoistion:
Last edited by cockeyedcowboy; 08-13-2006 at 07:04 AM. Reason: add code blocks |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Open/close trade on same bar's close? | WNW | Expert Advisors - Metatrader 4 | 2 | 03-29-2007 05:37 AM |
| Markets Open And Close | Kurka Fund | Expert Advisors - Metatrader 4 | 4 | 11-18-2006 12:26 PM |
| close all open trades ea | G-Riper | General Discussion | 2 | 08-02-2006 09:41 PM |
| Close all open positions | jonjonau | Expert Advisors - Metatrader 4 | 6 | 07-12-2006 05:01 AM |