Forex
Google
New signals service!

Go Back   Forex Trading > Downloads > Expert Advisors - Metatrader 4


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

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-10-2006, 03:17 PM
Member
 
Join Date: May 2006
Posts: 30
skorcht is an unknown quantity at this point
any EA's that OPEN/CLOSE more than 1position?

I'm just looking for an example of an EA that opens and closes more than 1 position at a time..

THANKS
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-10-2006, 08:48 PM
RickD's Avatar
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
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);
  }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-11-2006, 03:21 PM
Member
 
Join Date: May 2006
Posts: 30
skorcht is an unknown quantity at this point
sweet..
that Helps!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-11-2006, 05:50 PM
Member
 
Join Date: May 2006
Posts: 30
skorcht is an unknown quantity at this point
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);
}

}
}
}
"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-12-2006, 05:30 AM
cockeyedcowboy's Avatar
Senior Member
 
Join Date: Nov 2005
Posts: 267
cockeyedcowboy is on a distinguished road
The reason that both positions are not getting filled is you need to program a delay between sending orders ( 10 seconds ) or your secondary orders will just time out. the loop you refer too can be program it into a your order routine, but if the order is not getting filled because of an error your EA will get locked into that loop not a good programing answer to the problem. Instead use the sleep() function to put a delay in your order execution routine. Also remember that if your running two EAs at the same time weather there are the same EA on different currencies or different EAs on the same currency or any combinations of EAs and currencies on the same account. You can still have this time out problem if any two EAs are trying to place orders at the same time on the same account. To get around this you should code your order entry routines to check if your account is being used first, if not then the routine should lock the account for its use only preventing other EAs from forceing orders between yours. After placeing an order you should program an 10 second delay. once all your orders have been placed you should verify that in fact they were executed before releaseing the account for other EAs to use.


Quote:
Originally Posted by skorcht
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);
}

}
}
}
"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-12-2006, 02:33 PM
Member
 
Join Date: May 2006
Posts: 30
skorcht is an unknown quantity at this point
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:
Originally Posted by cockeyedcowboy
The reason that both positions are not getting filled is you need to program a delay between sending orders ( 10 seconds ) or your secondary orders will just time out. the loop you refer too can be program it into a your order routine, but if the order is not getting filled because of an error your EA will get locked into that loop not a good programing answer to the problem. Instead use the sleep() function to put a delay in your order execution routine. Also remember that if your running two EAs at the same time weather there are the same EA on different currencies or different EAs on the same currency or any combinations of EAs and currencies on the same account. You can still have this time out problem if any two EAs are trying to place orders at the same time on the same account. To get around this you should code your order entry routines to check if your account is being used first, if not then the routine should lock the account for its use only preventing other EAs from forceing orders between yours. After placeing an order you should program an 10 second delay. once all your orders have been placed you should verify that in fact they were executed before releaseing the account for other EAs to use.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-13-2006, 07:03 AM
cockeyedcowboy's Avatar
Senior Member
 
Join Date: Nov 2005
Posts: 267
cockeyedcowboy is on a distinguished road
Quote:
Originally Posted by skorcht
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

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT. The time now is 04:27 PM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.