//+------------------------------------------------------------------+ //| TesterMM.mq4 | //| IgorM | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "IgorM" #property link "http://www.mql4.com/ru/users/IgorM" extern double Balance = 10000; // начальный баланс extern int slipage = 3; // проскальзование в пп при выставлении ордера extern int pause = 300; // пауза в мс для перерисовки extern int timeshift = 10; // смещение на графике в барах extern int linewith = 5; // длина линии иммитирующей ордер extern color color_buy = Red; // цвет линии иммитирующей ордер BUY extern color color_sell = Blue; // цвет линии иммитирующей ордер SELL extern bool comment = true; // включения комментария с информацией о торговом счете extern bool writelog = true; // включения комментария с информацией о торговом счете extern string FileName ="testermm"; string shiftcomm = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n" ; // смещение комментария string sym,acccurr; datetime tim; int pp,symspread,totbuy,totsell; static double balance,equity,margin,lot,ask,sec,profitbuy,profitsell; static bool sendbuy,sendsell,onoff,on,firstrun=true; string inflabel[8] ={"testmmprice", "testmmtimes", "testmmbalance", "testmmequity", "testmmmargin", "testmmlot", "testmmordbuy", "testmmordsell", "testmmallprofit"}; int init(){ if (Point==0.0001) pp=1; if (Point==0.00001) pp=10; sym=Symbol(); acccurr =" "+AccountCurrency(); lot=MarketInfo(sym,MODE_MINLOT); symspread = MarketInfo(sym,MODE_SPREAD); sendbuy =false; sendsell =false; onoff =true; on=false; sec=0; drawlabel("testmmBUY","BUY", 200,10,24,Red); drawlabel("testmmSELL","SELL", 300,10,24,Blue); drawlabel("testmmCTRL","X",320,40,24,Yellow); drawlabel("testmmLOTUP","LOT+", 400,10,24,Red); drawlabel("testmmLOTDN","LOT-", 500,10,24,Blue); drawlabel("testmmLOTUP10","LOTx+",600,10,24,Red); drawlabel("testmmLOTDN10","LOTx-",700,10,24,Blue); drawlabel("testmmON","TIMEon" ,800,10,24,Yellow); drawline("testmmASK",Close[0],Time[0],Time[0],LightGreen,OBJ_HLINE); drawtime("testmmTIME",Time[0]); if (writelog){ int handle = FileOpen(FileName ,FILE_CSV|FILE_READ|FILE_WRITE); if ( handle ==-1 ) Print("Ошибка номер ",GetLastError()," при создании файла : "+FileName); else { FileSeek(handle, 0, SEEK_END); FileWrite(handle, "TesterMM, запуск "+TimeToStr(TimeLocal(),TIME_DATE|TIME_MINUTES)); FileClose(handle); } } start(); return(0); } int deinit(){ string vName=""; for(int i=ObjectsTotal(); i>=0;i--){ vName = ObjectName(i); if (StringFind(vName,"_buy-") !=-1 || StringFind(vName,"_sell-") !=-1 || StringFind(vName,"testmm") !=-1) ObjectDelete(vName); } Comment(""); return(0); } int start(){ if (firstrun){balance=Balance;equity=balance;margin=0;profitbuy=0;profitsell=0;firstrun=false; if (writelog)log();} while(true){ //------расчет текущего времени и цены if (onoff){ ask=Close[bar0()-barall()]; ObjectSet("testmmASK",OBJPROP_PRICE1,ask); tim=bar0()-barall(); ObjectSet("testmmTIME",OBJPROP_TIME1,Time[tim]); }else { ask=ObjectGet("testmmASK",OBJPROP_PRICE1); tim=iBarShift(sym,0, ObjectGet("testmmTIME",OBJPROP_TIME1)); } //------блок управления интерфейсом и выполнения команд открытия ордера if (control("testmmON")) on=true; if (control("testmmBUY"))sendbuy=true; if (control("testmmSELL"))sendsell=true; if (control("testmmLOTUP")) lot+=MarketInfo(sym,MODE_LOTSTEP); if (control("testmmLOTDN")) lot-=MarketInfo(sym,MODE_LOTSTEP); if (control("testmmLOTUP10"))lot+=MarketInfo(sym,MODE_LOTSTEP)*10; if (control("testmmLOTDN10"))lot-=MarketInfo(sym,MODE_LOTSTEP)*10; if (lot3) { sec=0; totbuy =totalorders("_buy-"); totsell =totalorders("_sell-"); if (totsell==0 && totbuy==0) {equity=balance;} else equity=balance+profitbuy+profitsell+margin; info(); if(comment) infocomment(); } sec+=pause; Sleep(pause); } return(0); } //________________________________________________________________________________________________ bool calcmargin(double ll,int op){ bool result=false; double m=NormalizeDouble(AccountFreeMargin()-AccountFreeMarginCheck(sym,op,ll),2); if(balance-m>0){balance-=m;margin+=m;result=true;} return(result); } //------------------------------------------------------------------------------------------ int totalorders(string name){ int result=0; double prof=0; string vName=""; for(int i=ObjectsTotal(); i>=0;i--){ vName = ObjectName(i); if (StringFind(vName,name) !=-1){ if (!closeorder(vName)){prof+=profitorder(vName);result++;} } } if (name=="_sell-") profitsell =prof; if (name=="_buy-") profitbuy =prof; return(result); } //------------------------------------------------------------------------------------------ double profitorder(string name){ double profit=0; int pos=0; string slot=""; double lot=0; double priceorder=ObjectGet(name,OBJPROP_PRICE1); pos = StringFind(name,"_sell-"); if (pos>0){ slot = StringSubstr(name,0,pos); lot=StrToDouble(slot); profit=NormalizeDouble((priceorder-ask)*MarketInfo(sym,MODE_TICKVALUE)*lot/Point,2); } else{ pos = StringFind(name,"_buy-"); if (pos>0){ slot = StringSubstr(name,0,pos); lot=StrToDouble(slot); profit=NormalizeDouble((ask-priceorder)*MarketInfo(sym,MODE_TICKVALUE)*lot/Point,2); } } return(profit); } //------------------------------------------------------------------------------------------ bool closeorder(string name){ bool result=false; double profit=0; int pos=0; string slot="",type=""; double m=0,lot=0; double priceorder=ObjectGet(name,OBJPROP_PRICE1); if (priceorder!=ObjectGet(name,OBJPROP_PRICE2)){ pos = StringFind(name,"_sell-"); if (pos>0){ slot = StringSubstr(name,0,pos); lot=StrToDouble(slot); type="SELL"; profit=(priceorder-ask)*MarketInfo(sym,MODE_TICKVALUE); m=NormalizeDouble(AccountFreeMargin()-AccountFreeMarginCheck(sym,OP_SELL,lot),2); } else{ pos = StringFind(name,"_buy-"); if (pos>0){ slot = StringSubstr(name,0,pos); lot=StrToDouble(slot); type="BUY";profit=(ask-priceorder)*MarketInfo(sym,MODE_TICKVALUE); m=NormalizeDouble(AccountFreeMargin()-AccountFreeMarginCheck(sym,OP_BUY,lot),2); } } if (ObjectDelete(name)){ WindowRedraw(); Sleep(2000); if (ObjectFind(name)<0){ profit=NormalizeDouble(profit*lot/Point,2); balance+=m+profit;margin=margin-m;result=true; if (writelog) log("Close "+type+" прибыль "+DoubleToStr(profit,2)+acccurr); Alert("Закрыт ордер "+type+" объемом ", slot," лота, прибыль ",profit,acccurr); } } } return(result); } void info(){ string s = StringConcatenate("Цена: ",DoubleToStr(ask,Digits)); drawlabel(inflabel[0],s,10,30,10,LightGray); s = StringConcatenate("Время: ",TimeToStr(Time[tim],TIME_DATE|TIME_MINUTES)); drawlabel(inflabel[1],s,10,45,10,LightGray); s = StringConcatenate("Баланс: ",DoubleToStr(balance,2),acccurr); drawlabel(inflabel[2],s,10,60,10,LightGray); s = StringConcatenate("Средства: ",DoubleToStr(equity,2),acccurr); drawlabel(inflabel[3], s,10,75,10,LightGray); s = StringConcatenate("Залог: ",DoubleToStr(margin,2),acccurr); drawlabel(inflabel[4],s,10,90,10,LightGray); s = StringConcatenate("Обьем орд:",DoubleToStr(lot,2)," лот"); drawlabel(inflabel[5],s,10,105,10,LightGray); s = StringConcatenate("Всего Buy: ",DoubleToStr(totbuy,0)," шт"); drawlabel(inflabel[6],s,10,120,10,LightGray); s = StringConcatenate("Всего Sell: ",DoubleToStr(totsell,0)," шт"); drawlabel(inflabel[7],s,10,135,10,LightGray); s = StringConcatenate("Прибыль : ",DoubleToStr(equity-balance-margin,2),acccurr); drawlabel(inflabel[8],s,10,150,10,LightGray); } //------------------------------------------------------------------------------------------ void sendorder(){ if (control("testmmBUY") || control("testmmSELL")) return; string name=""; if (sendbuy){ if (calcmargin(lot,OP_BUY)){ name=DoubleToStr(lot,2)+"_buy-"+TimeToStr(Time[tim],TIME_DATE|TIME_MINUTES)+"/"+DoubleToStr(TimeLocal(),0); drawline(name,ask+slipage*Point+symspread*Point,Time[tim],Time[tim+linewith],color_buy); if (writelog) log("send BUY --> "); } else Alert("Недостаточно средств для выполнения операции BUY ",lot," лот"); } if (sendsell){ if (calcmargin(lot,OP_SELL)){ name=DoubleToStr(lot,2)+"_sell-"+TimeToStr(Time[tim],TIME_DATE|TIME_MINUTES)+"/"+DoubleToStr(TimeLocal(),0); drawline(name,ask-slipage*Point-symspread*Point,Time[tim],Time[tim+linewith],color_sell); if (writelog) log("send SELL --> "); } else Alert("Недостаточно средств для выполнения операции SELL ",lot," лот"); } sendbuy =false; sendsell =false; return; } //------------------------------------------------------------------------------------------ void stoptime(){ if (control("testmmON")) return; if (on) if (onoff) {onoff=false;ObjectSetText("testmmON","TIMEoff");} else{onoff=true;ObjectSetText("testmmON","TIMEon");} on=false; } //------------------------------------------------------------------------------------------ void drawlabel(string label_name,string text,int x=0,int y=0,int font=14,color col=Red){ if (!ObjectSetText(label_name,text)){if(ObjectCreate(label_name, OBJ_LABEL, 0, 0, 0)){ ObjectSet(label_name,OBJPROP_CORNER, 0);ObjectSet(label_name,OBJPROP_XDISTANCE, x); ObjectSet(label_name,OBJPROP_YDISTANCE, y);ObjectSet(label_name,OBJPROP_FONTSIZE, font); ObjectSet(label_name,OBJPROP_COLOR, col);ObjectSetText(label_name,text); } else Print("Ошибка № ",GetLastError(), " при создании обьекта ", label_name); } } //------------------------------------------------------------------------------------------ void drawline(string name,double pr1,datetime tim1,datetime tim2,color col, int type=OBJ_TREND,int with=2){ if (!ObjectSet(name,OBJPROP_PRICE1, pr1)){if(ObjectCreate(name, type,0, tim1,0, tim2,0)){ ObjectSet(name,OBJPROP_PRICE1, pr1); ObjectSet(name,OBJPROP_COLOR, col); ObjectSet(name,OBJPROP_RAY, 0); if (type==OBJ_TREND)ObjectSet(name,OBJPROP_WIDTH, with); } else Print("Ошибка № ",GetLastError(), " при создании обьекта ", "price"); } ObjectSet(name,OBJPROP_PRICE2, pr1); } //------------------------------------------------------------------------------------------ void drawtime(string name,datetime tim){ if (!ObjectSet(name,OBJPROP_TIME1, tim)){if(ObjectCreate(name, OBJ_VLINE,0, tim,0, 0,0)){ ObjectSet(name,OBJPROP_TIME1, tim); } else Print("Ошибка № ",GetLastError(), " при создании обьекта ", "price"); } } //------------------------------------------------------------------------------------------ bool control(string obj){ int x_label = ObjectGet(obj,OBJPROP_XDISTANCE); int y_label = ObjectGet(obj,OBJPROP_YDISTANCE); int x_control = ObjectGet("testmmCTRL",OBJPROP_XDISTANCE); int y_control = ObjectGet("testmmCTRL",OBJPROP_YDISTANCE); if (x_control>x_label && y_control>y_label && x_control