Forex



Go Back   Forex Trading > Discussion Areas > Metatrader 4
Forex Forum Register More recent Blogs Calendar Advertising Others Help






Register
Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.

From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.

Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
  • Elite Section
    Get access to private discussions, specialized support, indicators and trading systems reported every week.
  • Advanced Elite Section
    For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-17-2006, 07:04 PM
Senior Member
 
Join Date: Mar 2006
Posts: 793
Maji is on a distinguished road
How to close an open order after X hours?

How do I close opened order after "X" hours? I can use expiration=X to close pending orders, but how can I close active positions after X hours that they have been opened. I have multiple simultaneous orders and open positions that open based on stop prices placed at various levels and they are triggered off at various times.

Hope some of the gurus can help me.

Maji
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 03-17-2006, 11:05 PM
Nicholishen's Avatar
Senior Member
 
Join Date: Dec 2005
Posts: 531
Nicholishen is on a distinguished road
Quote:
Originally Posted by Maji
How do I close opened order after "X" hours? I can use expiration=X to close pending orders, but how can I close active positions after X hours that they have been opened. I have multiple simultaneous orders and open positions that open based on stop prices placed at various levels and they are triggered off at various times.

Hope some of the gurus can help me.

Maji
You could insert this function into your code.
PHP Code:
extern int MaxHours=3;
int YOUR_MAGIC_NUMBER_VARIABLE_HERE=298374;

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

void AfterHours(){
   for(
int i=0;i<OrdersTotal();i++){
      if(
OrderSelect(i,SELECT_BY_POS)){
         if(
OrderSymbol()==Symbol() && OrderMagicNumber()== YOUR_MAGIC_NUMBER_VARIABLE_HERE){
            if(
CurTime() - OrderOpenTime() > MaxHours * (60 60) ){
               if(
OrderType()==OP_BUY){
                  
OrderClose(OrderTicket(),OrderLots(),Bid,2,Red);
               }
               if(
OrderType()==OP_SELL){ 
                  
OrderClose(OrderTicket(),OrderLots(),Ask,2,Red);
               }
            }
         }
      }
   }
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
AfterHours();
//----
   
return(0);
  } 
__________________
"Anyone who has never made a mistake has never tried anything new." -Albert Einstein
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 03-18-2006, 02:14 AM
Senior Member
 
Join Date: Mar 2006
Posts: 793
Maji is on a distinguished road
Thank you very much
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 11-03-2008, 08:30 PM
4X1 4X1 is offline
Junior Member
 
Join Date: Nov 2007
Posts: 2
4X1 is on a distinguished road
What to add to this code...?

Hello, everyone!

I have three questions:

1. Can the code posted above be used all by itself as an EA, by simply putting the code into a plain text file and then renaming the text file with the .mq4 extension?

2. If I want to use this code/EA to close trades that have been placed by another different EA, does the Magic Number in this code have to match the Magic Number used in the other EA that placed the trades?

3. If I want this EA to, for example, close all trades after 24 hours after they have been opened, but to ignore Saturday and Sunday, what lines of code would I need to add? In other words, if I set MaxHours to 24 and if a trade was opened on Friday at a certain time, and I want this code/EA to close that trade on Monday at the same certain time, thus ignoring Saturday and Sunday, what lines of code would I need to add to this existing code posted above?

Thank You to anyone who can answer these questions!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 11-06-2008, 12:52 AM
Senior Member
 
Join Date: Oct 2008
Location: Vancouver, BC
Posts: 158
Roger09 is on a distinguished road
Quote:
Originally Posted by 4X1 View Post
Hello, everyone!

I have three questions:

1. Can the code posted above be used all by itself as an EA, by simply putting the code into a plain text file and then renaming the text file with the .mq4 extension?

2. If I want to use this code/EA to close trades that have been placed by another different EA, does the Magic Number in this code have to match the Magic Number used in the other EA that placed the trades?

3. If I want this EA to, for example, close all trades after 24 hours after they have been opened, but to ignore Saturday and Sunday, what lines of code would I need to add? In other words, if I set MaxHours to 24 and if a trade was opened on Friday at a certain time, and I want this code/EA to close that trade on Monday at the same certain time, thus ignoring Saturday and Sunday, what lines of code would I need to add to this existing code posted above?

Thank You to anyone who can answer these questions!
1. Yes
2. You have to set your own MN.
3. It's possible, but why. Usually traders close all orders on Friday. If they use weekly or monthly charts, they don't count hours.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
EA to close all open order of GBPJPY hedge4x Expert Advisors - Metatrader 4 3 12-22-2007 03:34 PM
Open/close trade on same bar's close? WNW Expert Advisors - Metatrader 4 2 03-29-2007 06:37 AM
Open a new order !! ammar Expert Advisors - Metatrader 4 14 09-18-2006 06:34 PM
Close All Order when Profit.... FXfish Metatrader 4 1 08-15-2006 04:43 PM


All times are GMT. The time now is 12:21 PM.



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