Hi,
I am new to MetaTrader and was wondering if it's easy to create a script to close all open positions at a certain time - and if possible to activate this as some form as script when I am away from my PC. Using MetaTrader with FXCM if this helps.
Any help greatly appreciated.
Many thanks,
Nick
For a lot of us it is easy to create such a script
for help start writing and learning and ask if you have tried and gets trouble you don't know how to solve
For a lot of us it is easy to create such a script
for help start writing and learning and ask if you have tried and gets trouble you don't know how to solve
Are there any scripts available that do this already? I have no experience
Are there any scripts available that do this already? I have no experience
Do you get any experiece creating your own programs by getting available scripts that do this already ??
I don't think so.... But yes there are Google can help
OK I have found a script that does what I want - how do I schedule this to run at say 9pm every day? Without me manually running the script? Thanks
for help start writing and learning and ask if you have tried and gets trouble you don't know how to solve
It sounds as if a expert advisor would suit your purposes better, it runs constantly and you can check if it 9pm. If it is then close orders:
int start(){ static datetime currDay; if(Hour() == 21 && currDay != iTime(Symbol(),PERIOD_D1,0)){ Insert your script here to close orders }else return; }
This hasn't been tested and works off the server time so if your server time is different to your local time just change the 21. You could just open a chart and leave this running in the background.
Hope this helps,
heelflip43
If you have a script, that is a file saved in the scripts folder, you can simply make it an EA by copying it to the experts folder.
the functions TimeCurrent() returns your brokers server time in seconds from a starting date of 00:00 january 1970.
Time[0] is the time and date of the current bar and other Time and Date functions to achieve your goal.
so all you need do is add an if statement to check if it is time to close all the trades
and if you search for remove EA from chart should be able to find to stop the EA from running.
*edit posted too slow!
It sounds as if a expert advisor would suit your purposes better, it runs constantly and you can check if it 9pm. If it is then close orders:
This hasn't been tested and works off the server time so if your server time is different to your local time just change the 21. You could just open a chart and leave this running in the background.
Hope this helps,
heelflip43
This is VERY useful thank you. The script I am working with is below. There is an entry in this code called StartCloseTime. Does this mean when the script will be executed? Or will I have to embed it within the code you suggest above? If I have to combine it with the script you mention, please can you kindly advise where I need to put this in the text below? As you can tell, I am a complete novice!
Thank you!:)
//+------------------------------------------------------------------+ //| CloseOrders.mq4 | //| | //| | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //|History: //|Ver 0.01 2009.5.26 //|Ver 0.02 2011.11.06 //| 1. Added time control //| 2. changed some names for convenient use //+------------------------------------------------------------------+ #property copyright "Robbie Ruan Ver 0.02 2011.11.06" #property link "robbie.ruan@gmail.com" extern bool CloseAllSymbols = true; // if CloseAllSymbols = false, just close the symbol you are applying your EA; if CloseAllSymbols = true, it will close all symbol satisfying the upper condition. extern bool CloseOpenLongOrders = true; // if close open positions extern bool CloseOpenShortOrders = true; // if close open positions extern bool ClosePendingLongOrders = true; extern bool ClosePendingShortOrders = true; extern string Note1 = "If JustCloseSpecificMagicNumber is 0, close all MagicNumber orders;if JustCloseSpecificMagicNumber is not 0, just close the specific MagicNumber orders"; extern int JustCloseSpecificMagicNumber = 0; extern string Note2 = "Just close orders whose open prices are with this range"; extern bool JustCloseOrdersWithinTheRange = false; extern double CloseRangeHigh = 0; // close orders within the specified region, both should be above 0 if JustCloseOrdersWithinTheRange = false extern double CloseRangeLow = 0; extern bool EnableCloseTimeControl = false; extern double StartCloseTime = 2.00; extern double StopCloseTime = 2.30; extern string Note3 = "Time set 0.00~23.59"; string GridName = "Grid"; // identifies the grid. allows for several co-existing grids //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- #property show_inputs // shows the parameters //---- // GridName = StringConcatenate( "Grid", Symbol() ); return(0); } //+------------------------------------------------------------------------+ //| closes orders | //+------------------------------------------------------------------------+ void CloseOrders() { int total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; if ( ( OrderSymbol()==Symbol() || CloseAllSymbols == true ) && ( OrderMagicNumber() == JustCloseSpecificMagicNumber || JustCloseSpecificMagicNumber == 0 ) ) // only look if mygrid and symbol... { // close orders within the specified region. if ( (!JustCloseOrdersWithinTheRange && (CloseRangeLow==0) && (CloseRangeHigh ==0)) || ( JustCloseOrdersWithinTheRange && (CloseRangeLow >0) && (CloseRangeHigh >0) && (OrderOpenPrice()<CloseRangeHigh) && (OrderOpenPrice()>CloseRangeLow) ) ) { // close pending Long orders if ( ClosePendingLongOrders == true ) { switch(type) { //Close pending orders case OP_BUYLIMIT : result = OrderDelete( OrderTicket() ); break; case OP_BUYSTOP : result = OrderDelete( OrderTicket() ); break; } } // close pending Short orders if ( ClosePendingShortOrders == true ) { switch(type) { //Close pending orders case OP_SELLLIMIT : result = OrderDelete( OrderTicket() ); break; case OP_SELLSTOP : result = OrderDelete( OrderTicket() ); break; } } // close open Long orders if ( CloseOpenLongOrders == true ) { switch(type) { //Close opened long positions case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); break; } } // close open Short orders if (CloseOpenShortOrders == true ) { switch(type) { //Close opened short positions case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); break; } } } // if region control end here } // if symgol and magicnumber control end here if(result == false) { // Alert("Order ", OrderTicket(), " failed to close. Error:", GetLastError() ); // Sleep(3000); } } //for end here return; } //+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ int start() { //---- int i; int CurrentHour = Hour(); double CurrentMinute = Minute(); double CurrentTime = CurrentHour + CurrentMinute/100; //Print("CurrentTime:",CurrentTime); if ( EnableCloseTimeControl == true ) { if ( StartCloseTime < StopCloseTime ) { //example, execute close operation in 19->20->21->22 if (CurrentTime < StartCloseTime || CurrentTime >= StopCloseTime ) { return(0); } } else if ( StartCloseTime > StopCloseTime ) { //example, execute close operation in 22->23->0->1 if ( CurrentTime < StartCloseTime && CurrentTime >= StopCloseTime ) { return(0); } } else if ( StartCloseTime == StopCloseTime ) { return(0); } } CloseOrders(); return(0); } //+------------------------------------------------------------------+
This is VERY useful thank you. The script I am working with is below. There is an entry in this code called StartCloseTime. Does this mean when the script will be executed? Or will I have to embed it within the code you suggest above? If I have to combine it with the script you mention, please can you kindly advise where I need to put this in the text below? As you can tell, I am a complete novice!
Thank you!:)
//+------------------------------------------------------------------+
//| CloseOrders.mq4 |
//| |
//| |
.................................................
Use SRC button to post your code
Please re-edit yout comment there.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I am new to MetaTrader and was wondering if it's easy to create a script to close all open positions at a certain time - and if possible to activate this as some form as script when I am away from my PC. Using MetaTrader with FXCM if this helps.
Any help greatly appreciated.
Many thanks,
Nick