//+-------------------------------------------------------------------------+ //| IBFX - Quick Close & Reverse.mq4 | //+-------------------------------------------------------------------------+ #property copyright "Copyright © 2008, IBFX.com" #property link "www.ibfx.com" //---- int start() { /*+-------------------------------------------------------------------------+ Because these scripts are meant to execute fast there are no user external inputs. Make sure to modify the settings below, then compile the script before assigning a hot key to it and using it. The magicNumber HAS TO TO BE THE SAME ON ALL SCRIPTS if you change it here make sure to change it on all scripts!!! Do not forget to click on COMPILE once your changes have been made!!! +-------------------------------------------------------------------------+*/ int MagicNumber = 915; double Risk = 1.0; int StopLoss = 0; // Number in Pips ie: 50 for 50 pips. int ProfitTarget = 0; // Number in Pips ie: 50 for 50 pips. int Slippage = 1; bool MiniLots = True; // Does your broker offer mini micro lots such as 0.01 lot? string Commentary = " IBFX - Quick Close & Reverse "; string FontName = "Arial"; int FontSize = 12; //+-------------------------------------------------------------------------+ //| DO NOT MODIFY ANYTHING BELOW THIS LINE!!! | //+-------------------------------------------------------------------------+ //---- A few checks before we get started if( !IsConnected() ) { Alert( Commentary + " - No Connection!!" ); return(0); } double InitPrice = Ask; //+------------------------------------------------------------------+ // CloseAllOrders | //+------------------------------------------------------------------+ int type = 0; int ErrorCode = 0; int result = -1; Comment( "IBFX - QuickCloseReverse | Closing Order , Please wait ..." ); if( CountAll( MagicNumber ) > 0 ) { for(int i = OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); string Symbole = Symbol(); if( MagicNumber == OrderMagicNumber() && OrderSymbol() == Symbole ) { type = OrderType(); result = -1; ErrorCode = 0; //-- Switch switch(type) { case OP_BUY : Wait(); while( result < 0 ) { RefreshRates(); result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage * Point, CLR_NONE ); } break; case OP_SELL : Wait(); while( result < 0 ) { RefreshRates(); result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage * Point, CLR_NONE ); } break; case OP_BUYLIMIT : case OP_BUYSTOP : case OP_SELLLIMIT : case OP_SELLSTOP : Wait(); result = OrderDelete( OrderTicket() ); break; default : break; } // Switch //---- Global Vars bool Done = False; int Ticket = 0; double MaxLots = MarketInfo( Symbole, MODE_MAXLOT ); double Lots = MM( Symbole, Risk, MiniLots ); Comment( "IBFX - QuickCloseReverse | Opening Order , Please wait ..." ); if( type == OP_SELL ) { //---- Let's place the order. while( !Done ) { RefreshRates(); double FillPrice = Ask; double StopPrice = Bid; if( MathAbs( InitPrice - FillPrice ) > Slippage * Point ) { Done = true; } Wait(); RefreshRates(); Ticket = OrderSend( Symbole, OP_BUY, Lots, Ask, Slippage * Point, StopLong( StopPrice, StopLoss ), TakeLong(FillPrice, ProfitTarget), Commentary, MagicNumber, 0, CLR_NONE ); if( Ticket >= 0 ) { Done = true; } else { ErrorCode = GetLastError(); if( ErrorCode == 4109 ) { Alert( Commentary + " - You did not allow live trading!" ); Done = true; } else if( ErrorCode == 134 ) { Alert( Commentary + " - Not enough Money!" ); Done = true; } else if( ErrorCode == 138 || ErrorCode == 136 || ErrorCode == 135 ) { Alert( Commentary + " - Requote/Slippage, run the script again" ); Done = true; } else { Alert( Commentary + " Error: " + ErrorCode ); } } } } else if ( type == OP_BUY ) { //---- Let's place the order. while( !Done ) { RefreshRates(); FillPrice = Bid; StopPrice = Ask; if( MathAbs( InitPrice - FillPrice ) > Slippage * Point ) { Done = true; } Wait(); RefreshRates(); Ticket = OrderSend( Symbole, OP_SELL, Lots, Bid, Slippage * Point, StopShrt( StopPrice, StopLoss ), TakeShrt(FillPrice, ProfitTarget), Commentary, MagicNumber, 0, CLR_NONE ); if( Ticket >= 0 ) { Done = true; } else { ErrorCode = GetLastError(); if( ErrorCode == 4109 ) { Alert( Commentary + " - You did not allow live trading!" ); Done = true; } else if( ErrorCode == 134 ) { Alert( Commentary + " - Not enough Money!" ); Done = true; } else if( ErrorCode == 138 || ErrorCode == 136 || ErrorCode == 135 ) { Alert( Commentary + " - Requote/Slippage, run the script again" ); Done = true; } else { Alert( Commentary + " Error: " + ErrorCode ); } } } } } }// For } Comment(""); //---- return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //+ Wait + //+------------------------------------------------------------------+ void Wait() { while( IsTradeContextBusy() ) { Sleep(50); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ // CountAll | //+------------------------------------------------------------------+ int CountAll( int MagicNumber ) { int Count=0; for(int i = OrdersTotal()-1; i>=0; i--) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); if( OrderMagicNumber() == MagicNumber ) { Count++; } } return(Count); } //+------------------------------------------------------------------+ //+-------------------------------------------------------------------------+ //| Calculate Stop Short | //+-------------------------------------------------------------------------+ double StopLong(double price,int stop) { if(stop==0) { return(0); } else { return(price-(stop*Point)); } } //+-------------------------------------------------------------------------+ //| Calculate Profit Target Long | //+-------------------------------------------------------------------------+ double TakeLong(double price,int take) { if(take==0) { return(0);} else { return(price+(take*Point));} } //+-------------------------------------------------------------------------+ //+-------------------------------------------------------------------------+ //| Calculate Stop Short | //+-------------------------------------------------------------------------+ double StopShrt(double price,int stop) { if(stop==0) { return(0); } else { return(price+(stop*Point)); } } //+-------------------------------------------------------------------------+ //| Calculate Profit Target Long | //+-------------------------------------------------------------------------+ double TakeShrt(double price,int take) { if(take==0) { return(0);} else { return(price+(take*Point));} } //+-------------------------------------------------------------------------+ //+-------------------------------------------------------------------------+ //| Money Managment | //+-------------------------------------------------------------------------+ double MM( string Sym, double Risk, bool BrokerAllowsFractionalLots ) { double MinLots = MarketInfo(Sym,MODE_MINLOT); double MaxLots = MarketInfo(Sym,MODE_MAXLOT); double Leverage = AccountLeverage(); double LotSize = MarketInfo(Sym,MODE_LOTSIZE); double LotStep = MarketInfo(Sym,MODE_LOTSTEP); double FinalAccountBalance = MathMin( AccountBalance(), AccountEquity() ); int NormalizationFactor = 0; double Lots = 0.0; if(LotStep == 0.01) { NormalizationFactor = 2; } if(LotStep == 0.1) { NormalizationFactor = 1; } if( BrokerAllowsFractionalLots == true) { Lots = (FinalAccountBalance*(Risk/100.0))/(LotSize/Leverage); Lots = StrToDouble(DoubleToStr(Lots, NormalizationFactor)); if (Lots < MinLots) { Lots = MinLots; } if (Lots > MaxLots) { Lots = MaxLots; } } else if(BrokerAllowsFractionalLots == false) { Lots = (FinalAccountBalance*(Risk/100.0))/(LotSize/Leverage); Lots = MathRound(Lots); if (Lots < MinLots) { Lots = MinLots; } if (Lots > MaxLots) { Lots = MaxLots; } } return( Lots ); }