//+------------------------------------------------------------------+ //| IBFX - Quick Scripts.mq4 | //| Copyright © 2010, InterbankFX LLC | //| http://www.ibfx.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, Interbank FX LLC" #property link "http://www.ibfx.com" #include #define OP_ALL 1024 //+------------------------------------------------------------------+ //| Global Variables / Includes | //+------------------------------------------------------------------+ string Sym = ""; int SymDigits = 5; double SymPoints = 0.0001; //+------------------------------------------------------------------+ //+-------------------------------------------------------------------------+ //| Money Managment | //+-------------------------------------------------------------------------+ double MM( string Sym, double Risk ) { 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; } Lots = (FinalAccountBalance*(Risk/100.0))/(LotSize/Leverage); Lots = StrToDouble(DoubleToStr(Lots, NormalizationFactor)); if (Lots < MinLots) { Lots = MinLots; } if (Lots > MaxLots) { Lots = MaxLots; } //---- return( Lots ); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Stop Long | //+------------------------------------------------------------------+ double StopLong(double price,double stop,double point,double SymDgts ) { if(stop==0) { return(0); } else { return(NormalizeDouble( price-(MathAbs(stop)*point),SymDgts)); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Stop Short | //+------------------------------------------------------------------+ double StopShrt(double price,double stop,double point,double SymDgts ) { if(stop==0) { return(0); } else { return(NormalizeDouble( price+(MathAbs(stop)*point),SymDgts)); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Profit Target Long | //+------------------------------------------------------------------+ double TakeLong(double price,double take,double point,double SymDgts ) { if(take==0) { return(0);} else { return(NormalizeDouble( price+(MathAbs(take)*point),SymDgts));} } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Calculate Profit Target Long | //+------------------------------------------------------------------+ double TakeShrt(double price,double take,double point,double SymDgts ) { if(take==0) { return(0);} else { return(NormalizeDouble( price-(MathAbs(take)*point),SymDgts));} } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| CloseAll | //+------------------------------------------------------------------+ void CloseAll( string Sym, int MagicNumber, int Type, int MaxTry, int Slippage, string Commentary ) { bool NullSym = False; if( Sym == "None" ) { NullSym = True; } bool NullMag = False; if( MagicNumber == 0 ) { NullMag = True; } for( int i = OrdersTotal()-1; i >= 0; i-- ) { if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false ) { continue; } if( NullMag ) { MagicNumber = OrderMagicNumber(); } if( MagicNumber != OrderMagicNumber() ) { continue; } if( NullSym ) { Sym = OrderSymbol(); } if( Sym != OrderSymbol() ) { continue; } SymPoints = MarketInfo( Sym, MODE_POINT ); SymDigits = MarketInfo( Sym, MODE_DIGITS ); if( SymPoints == 0.001 ) { SymPoints = 0.01; SymDigits = 3; } else if( SymPoints == 0.00001 ) { SymPoints = 0.0001; SymDigits = 5; } int OrdType = OrderType(); if( OrdType != Type && Type != OP_ALL ) { continue; } int err = 0; bool OrderLoop = False; int TryCount = MaxTry; while( !OrderLoop ) { while( IsTradeContextBusy() ) { Sleep( 10 ); } RefreshRates(); double SymAsk = NormalizeDouble( MarketInfo( Sym, MODE_ASK ), SymDigits ); double SymBid = NormalizeDouble( MarketInfo( Sym, MODE_BID ), SymDigits ); if( OrdType == OP_BUY ) { OrderClose( OrderTicket(), OrderLots(), SymBid, Slippage*SymPoints, CLR_NONE ); } else if( OrdType == OP_SELL ) { OrderClose( OrderTicket(), OrderLots(), SymAsk, Slippage*SymPoints, CLR_NONE ); } int Err=GetLastError(); switch (Err) { //---- Success case ERR_NO_ERROR: OrderLoop = true; break; //---- Retry Error case ERR_INVALID_STOPS: case ERR_SERVER_BUSY: case ERR_NO_CONNECTION: case ERR_INVALID_PRICE: case ERR_OFF_QUOTES: case ERR_BROKER_BUSY: case ERR_TRADE_CONTEXT_BUSY: TryCount++; break; case ERR_PRICE_CHANGED: case ERR_REQUOTE: continue; //---- Fatal known Error case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Alert( Commentary + " Invalid Lots" ); break; case ERR_MARKET_CLOSED: OrderLoop = true; Alert( Commentary + " Market Close" ); break; case ERR_TRADE_DISABLED: OrderLoop = true; Alert( Commentary + " Trades Disabled" ); break; case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Alert( Commentary + " Not Enough Money" ); break; case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Alert( Commentary + " Too Many Orders" ); break; case 149: OrderLoop = true; Alert( Commentary + " Hedge is prohibited" ); break; //---- Fatal Unknown Error case ERR_NO_RESULT: default: OrderLoop = true; Print( "Unknown Error - " + Err ); break; //---- } // end switch if( TryCount > 10) { OrderLoop = true; } } } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Place Long Order | //+------------------------------------------------------------------+ int EnterLong( string FinalSymbol, double FinalLots, string Commentary, int StopLoss, int ProfitTarget, int MagicNumber, int MaxTry, int Slippage ) { SymPoints = MarketInfo( FinalSymbol, MODE_POINT ); SymDigits = MarketInfo( FinalSymbol, MODE_DIGITS ); if( SymPoints == 0.001 ) { SymPoints = 0.01; SymDigits = 3; } else if( SymPoints == 0.00001 ) { SymPoints = 0.0001; SymDigits = 5; } double MaximumLots = MarketInfo(FinalSymbol, MODE_MAXLOT ); double RemainingLots = FinalLots; int OrdersToPlace = 1; if( FinalLots > MaximumLots ) { OrdersToPlace = MathFloor( FinalLots / MaximumLots )+1; } for( int i = 0; i < OrdersToPlace; i++ ) { int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0; double thisLot = NormalizeDouble( FinalLots / MathMax( OrdersToPlace, 1 ), 2 ); if( RemainingLots - MaximumLots <= 0 ) { thisLot = RemainingLots; } while( !OrderLoop ) { while( IsTradeContextBusy() ) { Sleep( 10 ); } RefreshRates(); double SymAsk = NormalizeDouble( MarketInfo( FinalSymbol, MODE_ASK ), SymDigits ); double SymBid = NormalizeDouble( MarketInfo( FinalSymbol, MODE_BID ), SymDigits ); Ticket = OrderSend( FinalSymbol, OP_BUY, thisLot, SymAsk, 0, 0.0, 0.0 , Commentary, MagicNumber, Slippage * SymPoints, CLR_NONE ); int Err=GetLastError(); switch (Err) { //---- Success case ERR_NO_ERROR: OrderLoop = true; if( OrderSelect( Ticket, SELECT_BY_TICKET ) ) { OrderModify( Ticket, OrderOpenPrice(), StopLong(SymBid,StopLoss, SymPoints,SymDigits), TakeLong(SymAsk,ProfitTarget,SymPoints,SymDigits), 0, CLR_NONE ); } break; //---- Retry Error case ERR_INVALID_STOPS: case ERR_SERVER_BUSY: case ERR_NO_CONNECTION: case ERR_INVALID_PRICE: case ERR_OFF_QUOTES: case ERR_BROKER_BUSY: case ERR_TRADE_CONTEXT_BUSY: TryCount++; break; case ERR_PRICE_CHANGED: case ERR_REQUOTE: continue; //---- Fatal known Error // case ERR_INVALID_STOPS: OrderLoop = true; Alert( Commentary + " Invalid Stops" ); break; case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Alert( Commentary + " Invalid Lots" ); break; case ERR_MARKET_CLOSED: OrderLoop = true; Alert( Commentary + " Market Close" ); break; case ERR_TRADE_DISABLED: OrderLoop = true; Alert( Commentary + " Trades Disabled" ); break; case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Alert( Commentary + " Not Enough Money" ); break; case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Alert( Commentary + " Too Many Orders" ); break; case 149: OrderLoop = true; Alert( Commentary + " Hedge is prohibited" ); break; //---- Fatal Unknown Error case ERR_NO_RESULT: default: OrderLoop = true; Print( "Unknown Error - " + Err ); break; //---- } // end switch if( TryCount > MaxTry ) { OrderLoop = true; } } RemainingLots -= thisLot; } //---- return(Ticket); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Place Shrt Order | //+------------------------------------------------------------------+ int EnterShrt( string FinalSymbol, double FinalLots, string Commentary, int StopLoss, int ProfitTarget, int MagicNumber, int MaxTry, int Slippage ) { SymPoints = MarketInfo( FinalSymbol, MODE_POINT ); SymDigits = MarketInfo( FinalSymbol, MODE_DIGITS ); if( SymPoints == 0.001 ) { SymPoints = 0.01; SymDigits = 3; } else if( SymPoints == 0.00001 ) { SymPoints = 0.0001; SymDigits = 5; } double MaximumLots = MarketInfo(FinalSymbol, MODE_MAXLOT ); double RemainingLots = FinalLots; int OrdersToPlace = 1; if( FinalLots > MaximumLots ) { OrdersToPlace = MathFloor( FinalLots / MaximumLots )+1; } for( int i = 0; i < OrdersToPlace; i++ ) { int Ticket = -1; int err = 0; bool OrderLoop = False; int TryCount = 0; double thisLot = NormalizeDouble( FinalLots / MathMax( OrdersToPlace, 1 ), 2 ); if( RemainingLots - MaximumLots <= 0 ) { thisLot = RemainingLots; } while( !OrderLoop ) { while( IsTradeContextBusy() ) { Sleep( 10 ); } RefreshRates(); double SymAsk = NormalizeDouble( MarketInfo( FinalSymbol, MODE_ASK ), SymDigits ); double SymBid = NormalizeDouble( MarketInfo( FinalSymbol, MODE_BID ), SymDigits ); Ticket = OrderSend( FinalSymbol, OP_SELL, thisLot, SymBid, 0, 0.0, 0.0, Commentary, MagicNumber, Slippage * SymPoints, CLR_NONE ); int Err=GetLastError(); switch (Err) { //---- Success case ERR_NO_ERROR: OrderLoop = true; if( OrderSelect( Ticket, SELECT_BY_TICKET ) ) { OrderModify( Ticket, OrderOpenPrice(), StopShrt(SymAsk,StopLoss, SymPoints,SymDigits), TakeShrt(SymBid,ProfitTarget, SymPoints,SymDigits), 0, CLR_NONE ); } break; //---- Retry Error case ERR_INVALID_STOPS: case ERR_SERVER_BUSY: case ERR_NO_CONNECTION: case ERR_INVALID_PRICE: case ERR_OFF_QUOTES: case ERR_BROKER_BUSY: case ERR_TRADE_CONTEXT_BUSY: TryCount++; break; case ERR_PRICE_CHANGED: case ERR_REQUOTE: continue; //---- Fatal known Error //case ERR_INVALID_STOPS: OrderLoop = true; Alert( Commentary + " Invalid Stops " + GetLastError() ); break; case ERR_INVALID_TRADE_VOLUME: OrderLoop = true; Alert( Commentary + " Invalid Lots" ); break; case ERR_MARKET_CLOSED: OrderLoop = true; Alert( Commentary + " Market Close" ); break; case ERR_TRADE_DISABLED: OrderLoop = true; Alert( Commentary + " Trades Disabled" ); break; case ERR_NOT_ENOUGH_MONEY: OrderLoop = true; Alert( Commentary + " Not Enough Money" ); break; case ERR_TRADE_TOO_MANY_ORDERS: OrderLoop = true; Alert( Commentary + " Too Many Orders" ); break; case 149: OrderLoop = true; Alert( Commentary + " Hedge is prohibited" ); break; //---- Fatal Unknown Error case ERR_NO_RESULT: default: OrderLoop = true; Print( "Unknown Error - " + Err ); break; //---- } // end switch if( TryCount > MaxTry ) { OrderLoop = true; } } RemainingLots -= thisLot; } //---- return(Ticket); } //+------------------------------------------------------------------+