//+------------------------------------------------------------------+ //| Script.mq4 | //| Copyright © 2015, MetaQuotes Software Corp | //| | //+------------------------------------------------------------------+ #property copyright "Developed by uwaisqarni_fx" #property link "http://www.karimun.com" #property strict #property show_inputs #include enum SLMODE{SL_FIRST,SL_LAST,SL_PARTIAL}; enum TPMODE{TP_FIRST,TP_LAST,TP_PARTIAL}; enum BUY{BUY_STOP,BUY_LIMIT}; enum SELL{SELL_STOP,SELL_LIMIT}; extern BUY Buy = BUY_LIMIT; extern double LotsBuy = 0.01; //Lots extern double MultiplierBuy = 1.0; //Multiplier extern SLMODE SLmodeBuy = SL_LAST; //SLmode extern TPMODE TPmodeBuy = TP_PARTIAL; //TPmode extern double StopLossBuy = 0; //StopLoss extern double TakeProfitBuy = 150; //TakeProfit extern int DistanceBuy = 150; //Distance extern int FirstStepBuy = 50; //FirstStep extern int NextStepBuy = 50; //NextStep extern int MaxOrderBuy = 50; //MaxOrder extern string Space = "==============================="; //=============================== extern SELL Sell = SELL_LIMIT; extern double LotsSell = 0.01; //Lots extern double MultiplierSell = 1.0; //Multiplier extern SLMODE SLmodeSell = SL_LAST; //SLmode extern TPMODE TPmodeSell = TP_PARTIAL; //TPmode extern double StopLossSell = 0; //StopLoss extern double TakeProfitSell = 150; //TakeProfit extern int DistanceSell = 150; //Distance extern int FirstStepSell = 50; //FirstStep extern int NextStepSell = 50; //NextStep extern int MaxOrderSell = 50; //MaxOrder int MagicNumber = 160302; int PT,SlipPage; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { PT = 1; SlipPage = 2; AutoDigits(); return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if(IsTrade()) SetTrade(); return(0); } //+------------------------------------------------------------------+ void SetTrade() { int step = 0; if(Buy == BUY_STOP) { double price = NormalizeDouble(AskPrice()+(DistanceBuy*Point),Digits); for(int i = 0; i < MaxOrderBuy; i++) { Order(OP_BUYSTOP,price); if(i == 0 ) step = FirstStepBuy; else step = NextStepBuy; price = NormalizeDouble(price+(step*Point),Digits); } } else if(Buy == BUY_LIMIT) { double price = NormalizeDouble(AskPrice()-(DistanceBuy*Point),Digits); for(int i = 0; i < MaxOrderBuy; i++) { Order(OP_BUYLIMIT,price); if(i == 0 ) step = FirstStepBuy; else step = NextStepBuy; price = NormalizeDouble(price-(step*Point),Digits); } } if(Sell == SELL_STOP) { double price = NormalizeDouble(BidPrice()-(DistanceSell*Point),Digits); for(int i = 0; i < MaxOrderSell; i++) { Order(OP_SELLSTOP,price); if(i == 0 ) step = FirstStepSell; else step = NextStepSell; price = NormalizeDouble(price-(step*Point),Digits); } } else if(Sell == SELL_LIMIT) { double price = NormalizeDouble(BidPrice()+(DistanceSell*Point),Digits); for(int i = 0; i < MaxOrderSell; i++) { Order(OP_SELLLIMIT,price); if(i == 0 ) step = FirstStepSell; else step = NextStepSell; price = NormalizeDouble(price+(step*Point),Digits); } } } int TotalOrder(int orderType = -1) { int order = 0; for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType() == orderType || orderType == -1) order++; } } } return(order); } double SetupLot(int orderType) { double lot = 0, lots = 0, multiplier = 0, minLot = MarketInfo(Symbol(),MODE_MINLOT), maxLot = MarketInfo(Symbol(),MODE_MAXLOT), lotStep = MarketInfo(Symbol(),MODE_LOTSTEP); int lotDigits = 1; if(lotStep == 0.01) lotDigits = 2; if(lotStep == 0.001) lotDigits = 3; if(orderType % 2 == 0) {lots = LotsBuy; multiplier = MultiplierBuy;} else {lots = LotsSell; multiplier = MultiplierSell;} for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType() == orderType) lot = MathMax(lot,OrderLots()); } } } if(lot == 0) lot = lots; else { if(multiplier > 0) lot *= multiplier; else lot = lots; } lot = NormalizeDouble(lot,lotDigits); if(lot < minLot) lot = minLot; if(lot > maxLot) lot = maxLot; return(lot); } double AskPrice(string symbol = "") { if(symbol == "") symbol = Symbol(); return(MarketInfo(symbol,MODE_ASK)); } double BidPrice(string symbol = "") { if(symbol == "") symbol = Symbol(); return(MarketInfo(symbol,MODE_BID)); } int StopLevel(string symbol = "") { if(symbol == "") symbol = Symbol(); return((int)(MarketInfo(symbol,MODE_STOPLEVEL))); } string OrderCmd(int orderType) { string label; switch(orderType) { case 0: label = "Buy"; break; case 1: label = "Sell"; break; case 2: label = "Buy Limit"; break; case 3: label = "Sell Limit"; break; case 4: label = "Buy Stop"; break; case 5: label = "Sell Stop"; break; } return(label); } int Order(int orderType, double price = 0) { int ticket = 0; string comment = WindowExpertName(); double lot = SetupLot(orderType), sl = 0, tp = 0; color ColorArrow = clrNONE; int type = -1; double stopLoss = 0, takeProfit = 0; if(orderType % 2 == 0) {stopLoss = StopLossBuy; takeProfit = TakeProfitBuy;} else {stopLoss = StopLossBuy; takeProfit = TakeProfitBuy;} if(orderType == OP_BUY) price = AskPrice(); if(orderType == OP_SELL) price = BidPrice(); if(orderType % 2 == 0) { type = OP_BUY; ColorArrow = clrBlue; if(stopLoss > 0) sl = NormalizeDouble(price-(stopLoss*Point),Digits); if(takeProfit > 0) tp = NormalizeDouble(price+(takeProfit*Point),Digits); } else { type = OP_SELL; ColorArrow = clrRed; if(stopLoss > 0) sl = NormalizeDouble(price+(stopLoss*Point),Digits); if(takeProfit > 0) tp = NormalizeDouble(price-(takeProfit*Point),Digits); } ResetLastError(); if(AccountFreeMarginCheck(Symbol(),type,lot) <= 0 || GetLastError() == 134) ShowError(StringConcatenate("Order ",OrderCmd(orderType)," ",lot," Lots")); else { ticket = OrderSend(Symbol(),orderType,lot,price,SlipPage,sl,tp,comment,MagicNumber,0,ColorArrow); if(ticket == -1) ShowError("Order " + OrderCmd(orderType)); else { UpdateStopLoss(orderType); UpdateTakeProfit(orderType); } } return(ticket); } void UpdateStopLoss(int orderType) { int slMode = -1; double stopLoss = 0; if(orderType % 2 == 0) {stopLoss = StopLossBuy; slMode = SLmodeBuy;} else {stopLoss = StopLossSell; slMode = SLmodeSell;} if(TotalOrder() > 0 && stopLoss > 0) { bool modif = false; double slBuy = 0, slSell = 0; datetime timeBuy = 0, timeSell = 0; for(int i = OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType() == orderType) { if(OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT) { if(slMode == SL_FIRST) { if(OrderOpenTime() < timeBuy || timeBuy == 0) { timeBuy = OrderOpenTime(); slBuy = NormalizeDouble(OrderOpenPrice()-(stopLoss*Point),Digits); } } else if(slMode == SL_LAST) { if(OrderOpenTime() > timeBuy) { timeBuy = OrderOpenTime(); slBuy = NormalizeDouble(OrderOpenPrice()-(stopLoss*Point),Digits); } } } if(OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT) { if(slMode == SL_FIRST) { if(OrderOpenTime() < timeSell || timeSell == 0) { timeSell = OrderOpenTime(); slSell = NormalizeDouble(OrderOpenPrice()+(stopLoss*Point),Digits); } } else if(slMode == SL_LAST) { if(OrderOpenTime() > timeSell) { timeSell = OrderOpenTime(); slSell = NormalizeDouble(OrderOpenPrice()+(stopLoss*Point),Digits); } } } } } } } for(int i = OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType() == orderType) { if(OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT) { if(slMode == SL_PARTIAL) slBuy = NormalizeDouble(OrderOpenPrice()-(stopLoss*Point),Digits); if(slBuy > 0 && slBuy != NormalizeDouble(OrderStopLoss(),Digits)) { modif = OrderModify(OrderTicket(),OrderOpenPrice(),slBuy,OrderTakeProfit(),0); } } if(OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT) { if(slMode == SL_PARTIAL) slSell = NormalizeDouble(OrderOpenPrice()+(stopLoss*Point),Digits); if(slSell > 0 && (slSell != NormalizeDouble(OrderStopLoss(),Digits) || OrderStopLoss() == 0)) { modif = OrderModify(OrderTicket(),OrderOpenPrice(),slSell,OrderTakeProfit(),0); } } } } } } } } void UpdateTakeProfit(int orderType) { int tpMode = -1; double takeProfit = 0; if(orderType % 2 == 0) {takeProfit = TakeProfitBuy; tpMode = TPmodeBuy;} else {takeProfit = TakeProfitSell; tpMode = TPmodeSell;} if(TotalOrder() > 0 && takeProfit > 0) { bool modif = false; double tpBuy = 0, tpSell = 0; datetime timeBuy = 0, timeSell = 0; for(int i = OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType() == orderType) { if(OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT) { if(tpMode == TP_FIRST) { if(OrderOpenTime() < timeBuy || timeBuy == 0) { timeBuy = OrderOpenTime(); tpBuy = NormalizeDouble(OrderOpenPrice()+(takeProfit*Point),Digits); } } else if(tpMode == TP_LAST) { if(OrderOpenTime() > timeBuy) { timeBuy = OrderOpenTime(); tpBuy = NormalizeDouble(OrderOpenPrice()+(takeProfit*Point),Digits); } } } if(OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT) { if(tpMode == TP_FIRST) { if(OrderOpenTime() < timeSell || timeSell == 0) { timeSell = OrderOpenTime(); tpSell = NormalizeDouble(OrderOpenPrice()-(takeProfit*Point),Digits); } } else if(tpMode == TP_LAST) { if(OrderOpenTime() > timeSell) { timeSell = OrderOpenTime(); tpSell = NormalizeDouble(OrderOpenPrice()-(takeProfit*Point),Digits); } } } } } } } for(int i = OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if(OrderType() == orderType) { if(OrderType() == OP_BUYSTOP || OrderType() == OP_BUYLIMIT) { if(tpMode == TP_PARTIAL) tpBuy = NormalizeDouble(OrderOpenPrice()+(takeProfit*Point),Digits); if(NormalizeDouble(OrderTakeProfit(),Digits) != tpBuy) { modif = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tpBuy,0); } } if(OrderType() == OP_SELLSTOP || OrderType() == OP_SELLLIMIT) { if(tpMode == TP_PARTIAL) tpSell = NormalizeDouble(OrderOpenPrice()-(takeProfit*Point),Digits); if(NormalizeDouble(OrderTakeProfit(),Digits) != tpSell) { modif = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tpSell,0); } } } } } } } } void ShowError(string label) { string Error; int error = GetLastError(); Error = StringConcatenate("Terminal: ",TerminalName(),"\n", "Symbol: ",Symbol(),"\n", label," error ",error,"\n", ErrorDescription(error)); if(error > 2) { if(IsTesting()) Comment(Error); else Alert(Error); } } void AutoDigits() { if (Digits == 3 || Digits == 5) { PT = 100; SlipPage *= 100; StopLossBuy *= 100; TakeProfitBuy *= 100; DistanceBuy *= 100; FirstStepBuy *= 100; NextStepBuy *= 100; StopLossSell *= 100; TakeProfitSell *= 100; DistanceSell *= 100; FirstStepSell *= 100; NextStepSell *= 100; } } bool IsTrade() { bool trade = true; if(!IsTesting()) { if(!IsTradeAllowed()) { Alert("Allow live trading is disable, \nselect Common tab, check Allow live trading"); trade = false; } if(!IsExpertEnabled()) { Alert("Expert Advisor is disable, click AutoTrading button to activate it"); trade = false; } } return(trade); }