//+------------------------------------------------------------------+ //| ytg_TZ_exp.mq4 | //| Yuriy Tokman (YTG) | //| http://ytg.com.ua/ | //+------------------------------------------------------------------+ #property copyright "Yuriy Tokman (YTG)" #property link "http://ytg.com.ua/" #property description "yuriytokman@gmail.com" #property description " " #property description "http://ytg.com.ua/" #property version "1.00" #property strict extern int SL = 400; // - уровень возможных потерь extern int TP = 1000; // - уровень возможной прибыли extern int lev = 200; // - уровень коридора скользящей средней extern bool TSProfitOnly = true; //- Переключатель зоны, в которой трал начинает свою работу. Если True, то трал начнёт работу только после того, как профит позиции достигнет значения TStop.Buy/Sell+TrailingStep пунктов. Если False, то советник будет просто следить за тем, чтобы стоп позиции относительно текущей цены всегда был не далее, чем TStop.Buy/Sell+TrailingStep пунктов. Другими словами при True советник работает только в профитной зоне позиции, а при False и в отрицательной. extern int TStop_Buy = 400; //- Размер трала в пунктах для покупок. extern int TStop_Sell = 50; //- Размер трала в пунктах для продаж. extern int TrailingStep = 50; //- Шаг трала в пунктах. Необходим для того, чтобы не мучить дилера частыми запросами. int Slippage = 30; int POPYTKY = 50; bool gbDisabled = False; #include //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- double ma_UP = iMA(Symbol(),0,85,0,0,0,0)+lev*Point; double ma_DN = iMA(Symbol(),0,85,0,0,0,0)-lev*Point; double sar = iSAR(Symbol(),0,0.008,0.2,1); SimpleTrailing(Symbol()); if(!ExistPositions(Symbol())) { if(Close[0]>sar && Close[0]>ma_UP) OpenPosition(Symbol(), OP_BUY, 1, Bid-SL*Point, Bid+TP*Point); if(Close[0]0) { PlaySound("ok.wav"); break; } else { err=GetLastError(); if (pa==0 && pb==0) Message("Проверьте в Обзоре рынка наличие символа "+sy); // Вывод сообщения об ошибке Print("Error(",err,") opening position: ",ErrorDescription(err),", try ",it); Print("Ask=",pa," Bid=",pb," sy=",sy," ll=",ll," op=",GetNameOP(op), " pp=",pp," sl=",sl," tp=",tp," mn=",mn); // Блокировка работы советника if (err==2 || err==64 || err==65 || err==133) { gbDisabled=True; break; } // Длительная пауза if (err==4 || err==131 || err==132) { Sleep(1000*300); break; } if (err==128 || err==142 || err==143) { Sleep(1000*66.666); if (ExistPositions(sy, op, mn, ot)) { PlaySound("ok.wav"); break; } } if (err==140 || err==148 || err==4110 || err==4111) break; if (err==141) Sleep(1000*100); if (err==145) Sleep(1000*17); if (err==146) while (IsTradeContextBusy()) Sleep(1000*11); if (err!=135) Sleep(1000*7.7); } } return(ticket); } //---- bool ExistPositions(string sy="", int op=-1, int mn=-1, datetime ot=0) { int i, k=OrdersTotal(); if (sy=="0") sy=Symbol(); for (i=0; i0) Print(m); } //---- string GetNameOP(int op) { switch (op) { case OP_BUY : return("Buy"); case OP_SELL : return("Sell"); case OP_BUYLIMIT : return("Buy Limit"); case OP_SELLLIMIT: return("Sell Limit"); case OP_BUYSTOP : return("Buy Stop"); case OP_SELLSTOP : return("Sell Stop"); default : return("Unknown Operation"); } } //---- void SimpleTrailing(string sy="", int op=-1, int mn=-1) { double po, pp; int i, k=OrdersTotal(); if (sy=="0") sy=Symbol(); for (i=0; iTStop_Buy*po) { if (OrderStopLoss()TStop_Sell*po) { if (OrderStopLoss()>pp+(TStop_Sell+TrailingStep-1)*po || OrderStopLoss()==0) { ModifyOrder(-1, pp+TStop_Sell*po, -1); } } } } } } } } //---- void ModifyOrder(double pp=-1, double sl=0, double tp=0, datetime ex=0) { bool fm; color cl = clrAliceBlue; double op, pa, pb, os, ot; int dg=(int)MarketInfo(OrderSymbol(), MODE_DIGITS), er, it; if (pp<=0) pp=OrderOpenPrice(); if (sl<0 ) sl=OrderStopLoss(); if (tp<0 ) tp=OrderTakeProfit(); pp=NormalizeDouble(pp, dg); sl=NormalizeDouble(sl, dg); tp=NormalizeDouble(tp, dg); op=NormalizeDouble(OrderOpenPrice() , dg); os=NormalizeDouble(OrderStopLoss() , dg); ot=NormalizeDouble(OrderTakeProfit(), dg); if (pp!=op || sl!=os || tp!=ot) { for (it=1; it<=5; it++) { if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) break; while (!IsTradeAllowed()) Sleep(5000); RefreshRates(); fm=OrderModify(OrderTicket(), pp, sl, tp, ex, cl); if (fm) { PlaySound(""); break; } else { er=GetLastError(); pa=MarketInfo(OrderSymbol(), MODE_ASK); pb=MarketInfo(OrderSymbol(), MODE_BID); Print("Error(",er,") modifying order: ",ErrorDescription(er),", try ",it); Print("Ask=",pa," Bid=",pb," sy=",OrderSymbol(), " op="+GetNameOP(OrderType())," pp=",pp," sl=",sl," tp=",tp); Sleep(1000*10); } } } } //----