//+------------------------------------------------------------------+ //| buy_sell_4040.mq4 | //| Copyright 2019, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2019, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict extern double Lots = 0.1; extern int Magic = 10121; // по 4х знаку extern double TakeProfit = 40; extern double StopLoss = 40;//+ шаг для открытия позиций extern int Slippage = 0; double price_buystop,price_sellstop,price_buy,price_sell; double SL,TP; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { if(Digits == 3 || Digits == 5) { TakeProfit*=10; StopLoss*=10; Slippage *= 10; } return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { price_buystop = NormalizeDouble(Ask+StopLoss*Point,Digits); price_sellstop = NormalizeDouble(price_buystop-StopLoss*2*Point,Digits);// по цене селлстоп открывам байлимит price_buy = PriceOpenLastPosBuy(); price_sell = PriceOpenLastPosSell(); //для байстоп if (BuyStopCount()==0)//если нет байстопа { TP= NormalizeDouble(price_buystop + TakeProfit*Point,Digits); SL= NormalizeDouble(price_buystop - StopLoss*Point,Digits); if(CountBuy()==0)// и нет открытых бай { int ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,price_buystop,Slippage,SL,TP,"buystop",Magic,0,Blue); if (ticket <0) Print ("Не удалось открыть buystop "); } if (CountBuy()>=1) // если нет байстоп но есть открытая { price_buystop = NormalizeDouble(price_buy + StopLoss*Point,Digits); int ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,price_buystop,Slippage,SL,TP,"buystop",Magic,0,Blue); if (ticket <0) Print ("Не удалось открыть buystop2 "); } } else { //если есть байстоп модифицируем if (CountBuy()>=1)//и есть открытые бай { price_buystop = NormalizeDouble(price_buy+StopLoss*Point,Digits); TP= NormalizeDouble(price_buystop + TakeProfit*Point,Digits); SL= NormalizeDouble(price_buystop - StopLoss*Point,Digits); bool res = OrderModify(OrderTicket(),price_buystop,SL,TP,0,Blue); if(!res) Print("Ошибка модификации ордера buystop1. Код ошибки=",GetLastError()); else Print("ордер buystop1 успешно модифицирован."); } if (CountSell()>=1)//и есть открытые и селл { price_buystop = NormalizeDouble(price_sell+StopLoss*Point,Digits); TP= NormalizeDouble(price_buystop + TakeProfit*Point,Digits); SL= NormalizeDouble(price_buystop - StopLoss*Point,Digits); bool res = OrderModify(OrderTicket(),price_buystop,SL,TP,0,Blue); if(!res) Print("Ошибка модификации ордера buystop2. Код ошибки=",GetLastError()); else Print("ордер buystop2 успешно модифицирован."); } } //для селлстоп if (SellStopCount()==0) { if(CountSell()==0)// и нет открытых селл { TP= NormalizeDouble(price_sellstop - TakeProfit*Point,Digits); SL= NormalizeDouble(price_sellstop + StopLoss*Point,Digits); int ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,price_sellstop,Slippage,SL,TP,"sellstop",Magic,0,Red); if (ticket <0) Print ("Не удалось открыть sellstop "); } if (CountSell()>=1) { price_sellstop = NormalizeDouble(price_sell - StopLoss*Point,Digits); TP= NormalizeDouble(price_sellstop - TakeProfit*Point,Digits); SL= NormalizeDouble(price_sellstop + StopLoss*Point,Digits); Comment(DoubleToStr(price_sell,5)+ ""+ DoubleToStr(price_sellstop,5)); int ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,price_sellstop,Slippage,SL,TP,"sellstop",Magic,0,Red); if (ticket <0) Print ("Не удалось открыть sellstop2 "); } } else { //если есть селлстоп модифицируем if (CountBuy()>=1)//и есть открытые и бай { price_sellstop = NormalizeDouble(price_buy-StopLoss*Point,Digits); TP= NormalizeDouble(price_sellstop - TakeProfit*Point,Digits); SL= NormalizeDouble(price_sellstop + StopLoss*Point,Digits); bool res = OrderModify(OrderTicket(),price_sellstop,SL,TP,0,Red); if(!res) Print("Ошибка модификации ордера sellstop1. Код ошибки=",GetLastError()); else Print("ордер sellstop1 успешно модифицирован."); } if (CountSell()>=1)//и есть открытые и селл { price_sellstop = NormalizeDouble(price_sell-StopLoss*Point,Digits); TP= NormalizeDouble(price_sell - TakeProfit*Point,Digits); SL= NormalizeDouble(price_sell + StopLoss*Point,Digits); bool res = OrderModify(OrderTicket(),price_sellstop,SL,TP,0,Red); if(!res) Print("Ошибка модификации ордера sellstop2. Код ошибки=",GetLastError()); else Print("ордер sellstop2 успешно модифицирован."); } } } //+------------------------------------------------------------------+ // проверяем есть ли байстоп ордера с конца int BuyStopCount() { int count = 0; for (int i=OrdersTotal()-1;i>=0;i--) { if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES) == true && OrderMagicNumber()== Magic && OrderType() == OP_BUYSTOP ) { count ++; } } return(count); } // проверяем есть ли селлстоп ордера с конца int SellStopCount() { int count = 0; for (int i=OrdersTotal()-1;i>=0;i--) { if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES) == true && // если ордер успешно выбан OrderMagicNumber()== Magic && // номер одреда наш OrderType() == OP_SELLSTOP ) // { count ++; } } return(count); } //проверяем открытые ордера покупку int CountBuy() { int count = 0; for (int trade = OrdersTotal()-1; trade>=0; trade --) { if (OrderSelect (trade, SELECT_BY_POS, MODE_TRADES) ==true) { if (OrderSymbol()==Symbol() && OrderMagicNumber()== Magic && OrderType()== OP_BUY) count++; } }return(count); } //проверяем открытые ордера продажу int CountSell() { int count = 0; for (int trade = OrdersTotal()-1; trade>=0; trade --) { if (OrderSelect (trade, SELECT_BY_POS, MODE_TRADES) ==true) { if (OrderSymbol()==Symbol() && OrderMagicNumber()== Magic && OrderType()== OP_SELL) count++; } }return(count); } // возвращает цену открытия последней открытой позиции double PriceOpenLastPosBuy() { int k=OrdersTotal(); double r =0; int i; for (i=0; i