//+------------------------------------------------------------------+ //| OpenTradeAtMarket.mq4 | //| Copyright © 2008 Robert Hill All Rights Reserved.| //| | //| EA to open trades at market when a message box button is clicked | //| Uses hidden StopLoss and TakeProfit by adding EmergencyPips | //| to the values for the brokerto see. | //| There is also the standard trailing stop when TarilingStop has | //| a value greater than 0. | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008 Robert Hill. All Rights Reserved." #property link "" //---- input parameters #include #include #include extern double Lots=0.1; extern double StopLoss = 15; extern double TakeProfit=20; extern int EmergencyPips = 10; extern double TrailingStop=10; extern int MagicNumber = 12345; extern int Slippage = 3; extern string UserComment = ""; string gIntTPpriceName; string gIntSTpriceName; double IntTPprice, IntSTprice; double myPoint; int totalTries = 5; int retryDelay = 1000; int init() { myPoint = SetPoint(); GetGlobalVars(); } int start() { int NumOrders, msg; NumOrders = CalculateCurrentOrders(); //---- if(NumOrders < 1) { msg =MessageBox("TO SELL PRESS YES, TO BUY PRESS NO", "Script",MB_YESNOCANCEL|MB_ICONQUESTION|MB_DEFBUTTON3); if (msg == IDNO) { OpenTrade(OP_BUY); } if (msg == IDYES) { OpenTrade(OP_SELL); } return(0); } //================================================================ HandleOpenPositions(); return(0); } void OpenTrade(int signal) { int ticket, err; double TPprice,STprice; double OrderOP; RefreshRates(); if (signal==OP_BUY) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage * myPoint,0,0,UserComment,MagicNumber,0,Green); if (ticket > 0) { OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); OrderOP = OrderOpenPrice(); if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; IntTPprice = 0; if (TakeProfit > 0) { TPprice=TakeLong(OrderOP, TakeProfit + EmergencyPips); IntTPprice=TakeLong(OrderOP, TakeProfit); } STprice = 0; IntSTprice = 0; if (StopLoss > 0) { STprice=StopLong(OrderOP, StopLoss); STprice = ValidStopLoss(OP_BUY,Bid, STprice); IntSTprice=StopLong(OrderOP, StopLoss + EmergencyPips); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); IntSTprice = NormalizeDouble( IntSTprice, Digits); IntTPprice = NormalizeDouble( IntTPprice, Digits); } ModifyOrder(ticket, OrderOP, STprice, TPprice, LightGreen); SaveIntSTprice (IntSTprice); SaveIntTPprice (IntTPprice); } } } else if (signal==OP_SELL) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage * myPoint,0,0,UserComment,MagicNumber,0,Red); if (ticket > 0) { OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); OrderOP = OrderOpenPrice(); if (StopLoss != 0 || TakeProfit != 0) { TPprice = 0; IntTPprice = 0; if (TakeProfit > 0) { TPprice=TakeShort(OrderOP,TakeProfit + EmergencyPips); IntTPprice=TakeShort(OrderOP, TakeProfit); } STprice = 0; IntSTprice = 0; if (StopLoss > 0) { STprice=StopShort(OrderOP ,StopLoss + EmergencyPips); STprice = ValidStopLoss(OP_SELL,Ask, STprice); IntSTprice=StopShort(OrderOP, StopLoss); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); IntSTprice = NormalizeDouble( IntSTprice, Digits); IntTPprice = NormalizeDouble( IntTPprice, Digits); } ModifyOrder(ticket, OrderOP, STprice, TPprice, LightGreen); SaveIntSTprice (IntSTprice); SaveIntTPprice (IntTPprice); } } } if(ticket<0) { err = GetLastError(); Print("OrderSend failed with error(" + err + ") " + ErrorDescription(err) ); } return; } void HandleOpenPositions() { int cnt, total; bool Done; int ticket; double OrderLTS, OrderOP; double OrderTP, OrderSL; RefreshRates(); total = OrdersTotal(); //=============================== for(cnt=total - 1;cnt >= 0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol()!=Symbol()) continue; if (OrderMagicNumber() != MagicNumber) continue; ticket = OrderTicket(); OrderLTS = OrderLots(); OrderOP = OrderOpenPrice(); OrderTP = OrderTakeProfit(); OrderSL = OrderStopLoss(); if(OrderType()==OP_BUY) { Done = false; Done = CheckTPClose(OP_BUY, ticket, OrderOP, OrderLTS, OrderTP); if (Done == true) return; // Check for internal StopLoss Done = false; Done = CheckSLClose(OP_BUY, ticket, OrderOP, OrderLTS, OrderSL); if (Done == true) return; // If we get to here the trade is still Open HandleTrailingStop(OP_BUY, ticket, OrderOP, OrderSL, OrderTP); } if(OrderType()==OP_SELL) { // Check for TakeProfit Exits Done = false; Done = CheckTPClose(OP_SELL, ticket, OrderOP, OrderLTS, OrderTP); if (Done == true) return; // Check for internal StopLoss Done = false; Done = CheckSLClose(OP_SELL, ticket, OrderOP, OrderLTS, OrderSL); if (Done == true) return; // If we get to here the trade is still Open HandleTrailingStop(OP_SELL, ticket, OrderOP, OrderSL, OrderTP); } } } bool CheckSLClose(int cmd, int ticket, double op, double ol, double sl) { if (cmd == OP_BUY) { if (IntSTprice > 0.1) { if(Bid <= IntSTprice) { CloseOrder(ticket,ol,OP_BUY); return (true); } } return(false); } if (cmd == OP_SELL) { if (IntSTprice > 0.1) { if(Ask >= IntSTprice) { CloseOrder(ticket,ol,OP_SELL); return(true); } } } return(false); } bool CheckTPClose(int cmd, int ticket, double op, double ol, double tp) { if (cmd == OP_BUY) { if(Bid >= IntTPprice) { CloseOrder(ticket,ol,OP_BUY); return(true); } return(false); } if (cmd == OP_SELL) { if(Ask <= IntTPprice) { CloseOrder(ticket,ol,OP_SELL); return(true); } } return(false); } int CloseOrder(int ticket,double numLots,int cmd) { bool exit_loop = false, result; int cnt, err; double myPrice; if (cmd == OP_BUY) myPrice = Bid; if (cmd == OP_SELL) myPrice = Ask; if (Digits > 0) myPrice = NormalizeDouble( myPrice, Digits); // try to close 3 Times cnt = 0; while (!exit_loop) { if (IsTradeAllowed() == true) { result = OrderClose(ticket,numLots,myPrice,Slippage,Violet); err = GetLastError(); } else cnt++; if (result == true) exit_loop = true; err=GetLastError(); switch (err) { case ERR_NO_ERROR: exit_loop = true; break; 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: case ERR_TRADE_TIMEOUT: // for modify this is a retryable error, I hope. cnt++; // a retryable error break; case ERR_PRICE_CHANGED: case ERR_REQUOTE: RefreshRates(); continue; // we can apparently retry immediately according to MT docs. default: // an apparently serious, unretryable error. exit_loop = true; break; } // end switch if (cnt > totalTries) exit_loop = true; if (!exit_loop) { Sleep(retryDelay); RefreshRates(); } } // we have now exited from loop. if ((result == true) || (err == ERR_NO_ERROR)) { // OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); return(true); // SUCCESS! } Print(" Error closing order : (", err , ") " + ErrorDescription(err)); return(false); } //+------------------------------------------------------------------+ //| Delayed_TrailingStop.mq4 | //| Copyright © 2006, Forex-TSD.com | //| Written by MrPip,robydoby314@yahoo.com | //| | //| Waits for price to move the amount of the BeginTrailingStop | //| Moves the stoploss pip for pip after delay. | //+------------------------------------------------------------------+ void HandleTrailingStop(int cmd, int ticket, double oOP, double oSL, double oTP ) { bool Modify; double myStopLoss; double BrokerStopLoss; double StopLossPrice; if (TrailingStop == 0) return; BrokerStopLoss = oSL; if(cmd == OP_BUY) { if(Bid >= oOP + TrailingStop * myPoint) { myStopLoss = Bid - TrailingStop * myPoint; BrokerStopLoss = Bid - (TrailingStop + EmergencyPips) * myPoint; BrokerStopLoss = ValidStopLoss(OP_BUY,Bid, BrokerStopLoss); BrokerStopLoss = NormalizeDouble( BrokerStopLoss, Digits); if (oSL < BrokerStopLoss) { IntSTprice = myStopLoss; SaveIntSTprice (IntSTprice); ModifyOrder(ticket,oOP, BrokerStopLoss, oTP,Green); } } } if (cmd == OP_SELL) { if(Ask <= oOP - TrailingStop * myPoint) { myStopLoss = Ask + TrailingStop * myPoint; BrokerStopLoss = Ask + (TrailingStop + EmergencyPips) * myPoint; BrokerStopLoss = ValidStopLoss(OP_SELL,Ask, BrokerStopLoss); BrokerStopLoss = NormalizeDouble( BrokerStopLoss, Digits); if ((oSL > BrokerStopLoss) || (oSL < 0.1)) { IntSTprice = myStopLoss; SaveIntSTprice (IntSTprice); ModifyOrder(ticket,oOP, BrokerStopLoss, oTP,Red); } } } } int ModifyOrder(int ord_ticket,double op, double oSL, double oTP, color mColor) { int CloseCnt, err; double myStop, myTake; /* if (Digits > 0) { myStop = NormalizeDouble( oSL, Digits); myTake = NormalizeDouble( oTP, Digits); } */ CloseCnt=0; while (CloseCnt < 3) { if (OrderModify(ord_ticket,op,oSL,oTP,0,mColor)) { CloseCnt = 3; } else { err=GetLastError(); Print(CloseCnt," Error modifying order : (", err , ") " + ErrorDescription(err)); if (err>0) CloseCnt++; } } } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } double StopLong(double price,int stop) { if(stop==0) return(0); else return(price-(stop*myPoint)); } double StopShort(double price,int stop) { if(stop==0) return(0); else return(price+(stop*myPoint)); } double TakeLong(double price,int take) { if(take==0) return(0); else return(price+(take*myPoint)); } double TakeShort(double price,int take) { if(take==0) return(0); else return(price-(take*myPoint)); } double ValidStopLoss(int type, double price, double SL) { double minstop; if (SL < 0.1) return(SL); minstop = MarketInfo(Symbol(),MODE_STOPLEVEL); if (Digits == 3 || Digits == 5) minstop = minstop / 10; if (type == OP_BUY) { if((price - SL) < minstop*myPoint) SL = price - minstop*myPoint; } if (type == OP_SELL) { if((SL-price) < minstop*myPoint) SL = price + minstop*myPoint; } return(SL); } int CalculateCurrentOrders() { int buys = 0, sells = 0, num = 0; for(int i=0;i