//+------------------------------------------------------------------+ //| Experts_Two_Absorption.mq4 | //| Copyright 2016, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict input int Magic = 1111; input int MaxRisk = 5; // The maximum risk, percentage input double lots = 0.01; // Fixed lot, 0-off input int TP = 120; // Take profit, points, 0-off input int SL = 40; // Stop loss, points, 0-off input int slippage = 3; // Slippage input int total = 100; // The number of candles to find pogloshenie double Lots; bool result; int ticket; datetime time; double old_point_b; double old_point_s; string symbol="0";//Здесь пишем один инструмент для торговли если нужно торговать только по одному или 0 торгует на всех напр "EURUSD" обязательно в кавычках //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //и после смены компелируем //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { if(Symbol()!=symbol && symbol!="0") return; int IBARS=Bars; if(IBARSClose[2] && Open[2]Close[i+1] && Open[i+1]point2) { gg=1; } break; } } if(gg==1) { double pips=(point1-point2)/(point22-point11); double pr_tr; for(int i=point11+2;i=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL) { ClosePosBySelect(); } } Lots=Lots(); result=CheckVolumeValue(Lots); if(result==false) return; double free_margin=AccountFreeMarginCheck(Symbol(),OP_BUY,Lots); if(free_margin<0) { Print("Not enough Deposit to Buy ",Lots," ",Symbol()," Error code=",GetLastError()); return; } if((TP0) || (SL0)) { Print("Take profit or stop loss is less than acceptable!"); return; } double Tp=0; double Sl=0; if(TP>0) Tp=NormalizeDouble(Bid+TP*Point,Digits); if(SL>0) Sl=NormalizeDouble(Bid-SL*Point,Digits); ticket=OpenPosition(NULL,OP_BUY,Lots,Sl,Tp,"",Magic); } } //+------------------------------------------------------------------+sell if(Open[1]>Close[1] && Open[2]Close[1]) { int gg=0; double point1=0; double point2=0; int point11=0; int point22=0; point1=Open[1]; point11=1; for(int i=3;i<=total;i++) { if(Open[i]>Close[i] && Open[i+1]Close[i]) { point2=Open[i]; point22=i; if(point1=pr_tr) { gg=0; break; } } } if(gg==1 && old_point_b!=point2) { old_point_b=point1; for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY) { ClosePosBySelect(); } } Lots=Lots(); result=CheckVolumeValue(Lots); if(result==false) return; double free_margin=AccountFreeMarginCheck(Symbol(),OP_SELL,Lots); if(free_margin<0) { Print("Not enough Deposit to Sell ",Lots," ",Symbol()," Error code=",GetLastError()); return; } if((TP0) || (SL0)) { Print("Take profit or stop loss is less than acceptable!"); return; } double Tp=0; double Sl=0; if(TP>0) Tp=NormalizeDouble(Ask-TP*Point,Digits); if(SL>0) Sl=NormalizeDouble(Ask+SL*Point,Digits); ticket=OpenPosition(NULL,OP_SELL,Lots,Sl,Tp,"",Magic); } } } } //+------------------------------------------------------------------+ int OpenPosition(string sy,int op,double ll,double sl,double tp,string komeent="",int mn=0) { datetime ot; double pp,pa,pb; int dg,it,Ticket=0; if(sy=="" || sy=="0") sy=Symbol(); for(it=1; it<=10; it++) { if(!IsTesting() && (!IsExpertEnabled() || IsStopped())) { Print("Stop function!"); break; } while(!IsTradeAllowed()) Sleep(5000); RefreshRates(); dg=(int)MarketInfo(sy, MODE_DIGITS); pa=MarketInfo(sy, MODE_ASK); pb=MarketInfo(sy, MODE_BID); if(op==OP_BUY) pp=pa; else pp=pb; pp=NormalizeDouble(pp, dg); ot=TimeCurrent(); Ticket=OrderSend(sy,op,ll,pp,slippage,sl,tp,komeent,mn,0,CLR_NONE); if(Ticket>0) { break; } else { if(!Error(GetLastError(),it,"",5000)) break; } } return(Ticket); } //+------------------------------------------------------------------+ bool Error(int err,int j,string informer="",int pause=5000) { bool is_open=true; switch(err) { case ERR_NO_ERROR: case ERR_NO_RESULT: case ERR_COMMON_ERROR:{is_open=false; break;} case ERR_BROKER_BUSY: {Print("BROKER_BUSY, try №"+DoubleToStr(j,0)); Sleep(pause);} break; case ERR_SERVER_BUSY: {Print("SERVER_BUSY, try №"+DoubleToStr(j,0)); Sleep(pause);} break; case ERR_NO_CONNECTION: {Print("NO_CONNECTION, try №"+DoubleToStr(j,0)); Sleep(pause);} break; case ERR_TRADE_TIMEOUT: {Print("TRADE_TIMEOUT, try №"+DoubleToStr(j,0)); Sleep(pause);} break; case ERR_PRICE_CHANGED: {Print("PRICE_CHANGED, try №"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break; case ERR_REQUOTE: {Print("REQUOTE, try №"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break; case ERR_TOO_FREQUENT_REQUESTS: {Print("TOO_FREQUENT_REQUESTS, try №"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break; case ERR_ORDER_LOCKED: {is_open=false; Print("ORDER_LOCKED");} break; case ERR_INVALID_STOPS: {Print("INVALID_STOPS, try №"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break; case ERR_INVALID_PRICE: {Print("INVALID_PRICE, try №"+DoubleToStr(j,0)+" "+informer);} break; case ERR_OFF_QUOTES: {Print("OFF_QUOTES, try №"+DoubleToStr(j,0)); Sleep(pause);} break; case ERR_INVALID_TRADE_VOLUME: {is_open=false; Print("INVALID_TRADE_VOLUME, "+informer);} break; case ERR_MARKET_CLOSED: {is_open=false; Print("MARKET_CLOSED");} break; case ERR_TRADE_DISABLED: {is_open=false; Print("TRADE_DISABLED");} break; case ERR_NOT_ENOUGH_MONEY: {is_open=false; Print("NOT_ENOUGH_MONEY, "+informer);} break; case ERR_LONG_POSITIONS_ONLY_ALLOWED: {is_open=false; Print("LONG_POSITIONS_ONLY_ALLOWED");} break; case ERR_TRADE_MODIFY_DENIED: {Print("TRADE_MODIFY_DENIED, try №"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break; case ERR_TRADE_EXPIRATION_DENIED: {is_open=false; Print("TRADE_EXPIRATION_DENIED");} break; case ERR_TRADE_TOO_MANY_ORDERS: {is_open=false; Print("TRADE_TOO_MANY_ORDERS.");} break; case ERR_TRADE_HEDGE_PROHIBITED: {is_open=false; Print("TRADE_HEDGE_PROHIBITED.");} break; case ERR_TRADE_PROHIBITED_BY_FIFO: {is_open=false; Print("TRADE_PROHIBITED_BY_FIFO.");} break; case ERR_ARRAY_INDEX_OUT_OF_RANGE: {is_open=false; break;} case ERR_LONGS_NOT_ALLOWED: {is_open=false; break;} case ERR_SHORTS_NOT_ALLOWED: {is_open=false; break;} case ERR_INVALID_TICKET: {is_open=false; break; } default: {is_open=false; Print("Unknown error: "+(string)err+" "+informer);} break; } return (is_open); } //+------------------------------------------------------------------+ double Lots() { if(lots>0) return(lots); double Free=AccountFreeMargin(); double LotVal=MarketInfo(Symbol(),MODE_TICKVALUE); double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT); double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT); double Step=MarketInfo(Symbol(),MODE_LOTSTEP); double Lot=0; if(SL>0) Lot =MathFloor((AccountFreeMargin()* MaxRisk * 0.01)/(SL*LotVal)/Step)*Step; if(LotMax_Lot) Lot=Max_Lot; return(Lot); } //+------------------------------------------------------------------+ bool CheckVolumeValue(double volume) { double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN); if(volumemax_volume) { Print("The volume is greater than the maximum :",max_volume); return(false); } double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP); int ratio=(int)MathRound(volume/volume_step); if(MathAbs(ratio*volume_step-volume)>0.0000001) { Print("The volume is not a multiple of the minimum gradation closest to the correct amount :",volume_step," ",ratio*volume_step); return(false); } return(true); } //+------------------------------------------------------------------+ bool ClosePosBySelect() { bool fc=false; double ll,pa,pb,pp; int it; if(OrderType()==OP_BUY || OrderType()==OP_SELL) { for(it=1; it<=10; it++) { if(!IsTesting() && (!IsExpertEnabled() || IsStopped())) break; while(!IsTradeAllowed()) Sleep(5000); RefreshRates(); pa=MarketInfo(OrderSymbol(), MODE_ASK); pb=MarketInfo(OrderSymbol(), MODE_BID); if(OrderType()==OP_BUY) { pp=pb; } else { pp=pa; } ll=OrderLots(); fc=OrderClose(OrderTicket(), ll, pp, slippage, CLR_NONE); if(fc) { break; } else { if(!Error(GetLastError(),it,"",5000)) break; } } return(fc); } else {Print("Incorrect trading. Close");return(false);} } //+------------------------------------------------------------------+