MQL4 Example Script : Close Order


Close All Trades

//+------------------------------------------------------------------+
//| |
//| Copyright © 2005, Renato |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, rodrigokaus"
#property link "http://www.forex-tsd.com/expert-advisors-metatrader-4/703-100-pips.html"

extern color clCloseBuy = Yellow;
extern color clCloseSell = Yellow;
extern string Name_Expert = "CloseAllTrades";

void deinit() {
Comment("");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start(){

CloseAllTrades();
return (0);
}

void CloseAllTrades() {
for (int i=0; i if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,GetSlippage(),clCloseBuy);
if (OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,GetSlippage(),clCloseSell);
if (OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT ||
OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
OrderDelete(OrderTicket());
}
}
}
}

double GetSlippage() { return((Ask-Bid)/Point); }




Close All Open And Pending Orders

//+------------------------------------------------------------------+
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"

int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;

//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
break;

//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT :
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
}

if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}

return(0);
}







Close On Specific Time

//+------------------------------------------------------------------+
//| CloseOnTime.mq4 |
//| http://www.kimiv.ru |
//+------------------------------------------------------------------+
#property copyright ""
#property link "http://www.kimiv.ru"

//---- input parameters
extern int CloseHour=23;
extern int CloseMinute=55;

int start() {
int cnt, ticket, total;
if (Hour() == CloseHour && Minute() >= CloseMinute) {
total=OrdersTotal();
for (cnt=0; cnt < total ;cnt++) {
if (OrderType() == OP_BUY) {
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);
return(0); // exit
}
if (OrderType() == OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);
return(0); // exit
}
}
}
return(0);
}





Close On Chart

//+------------------------------------------------------------------+
//| CloseOnChart.mq4 |
//| Copyright © 2006, Igor Morozov |
//+------------------------------------------------------------------+
// This script can be used to quickly close a trade by dragging
// and dropping over it.
#property copyright "Igor Morozov"
//----
#include
//+------------------------------------------------------------------+
//| script "close position where script was dropped" |
//+------------------------------------------------------------------+
int start()
{
int ret = 0;
double target = PriceOnDropped();
double within = 2.0;
int OpenOrdersBuffer[1];
ArrayResize(OpenOrdersBuffer, OrdersTotal());
ArrayInitialize(OpenOrdersBuffer, 0);
int i, j = 0;
for(i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderSymbol() != Symbol())
continue;
if((OrderOpenPrice() >= target - within*Point) &&
(OrderOpenPrice() <= target + within*Point))
{
OpenOrdersBuffer[j] = OrderTicket();
j++;
}
}
}
if(j > 1)
{
for(i = 0; i < j; i++)
{
if(MessageBox("Multiple Tickets Found\nClose Ticket " +
DoubleToStr(OpenOrdersBuffer[i],0), "Close Order",
MB_YESNO|MB_ICONQUESTION) == IDYES)
{
if(OrderSelect(OpenOrdersBuffer[i], SELECT_BY_TICKET))
if(OrderClose(OrderTicket(), OrderLots(), GetPrice(OrderType()), 2))
PlaySound("ok.wav");
}
}
}
else
if(j > 0)
{
if(MessageBox("Will Close Ticket " + DoubleToStr(OpenOrdersBuffer[0], 0),
"Close Order", MB_OKCANCEL|MB_ICONQUESTION) == IDOK)
{
if(OrderSelect(OpenOrdersBuffer[0], SELECT_BY_TICKET))
if(OrderClose(OrderTicket(), OrderLots(), GetPrice(OrderType()), 2))
PlaySound("ok.wav");
}
}
return(ret);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetPrice(int ordertype)
{
switch(ordertype)
{
case OP_BUY: return(Bid);
case OP_SELL: return(Ask);
}
}



Close All Open Positions

//+------------------------------------------------------------------+
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"

int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;

//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

}

if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}

return(0);
}


Close All Profitable Positions Loss

//+------------------------------------------------------------------+
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"

extern int ProfitTarget = 1; // Profit target in dollars

int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY : if ( OrderProfit() <= ProfitTarget) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;

//Close opened short positions
case OP_SELL : if ( OrderProfit() <= ProfitTarget) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

}

if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}

return(0);
}


Close All Pending Orders

//+------------------------------------------------------------------+
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"

int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT :
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
}

if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}

return(0);
}




Close All Profitable Positions Profit

//+------------------------------------------------------------------+
//| close-all-orders.mq4 |
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"

extern int ProfitTarget = 10; // Profit target in dollars

int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY : if ( OrderProfit() >= ProfitTarget) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;

//Close opened short positions
case OP_SELL : if ( OrderProfit() >= ProfitTarget) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

}

if(result == false)
{
//Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(300);
}
}

return(0);
}





FXcast - Your Prime Online Forex Broker

Learning Forex (STEP BY STEP)

Steps to learn forex :

Forex Book For Advance     more

Forex Wave Theory,A Technical Analysis for Spot and Futures Curency Traders
by: James L. Bickford
publisher: McGraw-Hill Companies
date: 2007-05-30
Financial options,from theory to practice
by: Stephen Figlewski,William L. Silber,Marti G. Subrahmanyam,Salomon Brothers Center for the Study of Financial Institutions
publisher: Richard d Irwin
date: 1990
Applying Elliott Wave theory profitably
by: Steven W. Poser
publisher: John Wiley & Sons Inc
date: 2003-07-16
Chaos theory in the financial markets,applying fractals, fuzzy logic, genetic algorithms, swarm simulation &, the Monte Carlo method to manage market chaos &, volatility
by: Dimitris N. Chorafas
publisher: McGraw-Hill
date: 1994
Discover How You Can Make Money in the Currency Markets Even If You Know Nothing about Forex - And Much More - 101 World Class Expert Facts, Hints, Ti
by: Sam Robinson
publisher:
date: 2009-04
powered by :

Free Forex eBooks

If you are looking to download the best Forex eBooks, then look no further. We provide a range of Foreign Currency Trading eBook from reading the basic chart to trading profitably in the Forex market.

You are please to download forex ebooks below for your learning materials. Its may needs Adobe Acrobat Reader and some files may be in the form of a separate and zip format.



Reverse Divergences And Momentum  (810 kb) by Martin J. Pring.
Strategy 10  (305 kb) by W. R. Booker & Co.
The Original Turtle Trading Rules (255 kb) by OrignalTurtles.org
Study Book for Successful Foreign Exchange Dealing (1040 kb) by Royal Forex.
Introduction to Forex (840 kb)
Online Trading Courses (135 kb) by Jake Bernstein.


more free forex eBooks

Use this search below to find forex ebooks/PDF from internet(WWW)
Forex eBooks