//+------------------------------------------------------------------+
//| 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 (lot<MarketInfo(sym,MODE_MINLOT)) lot=MarketInfo(sym,MODE_MINLOT);
if (sendbuy || sendsell) sendorder();
if (on) stoptime();
//-----ñ÷åò÷èêè è ïåðåðèñîâêà
WindowRedraw();
if (sec>3) {
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<x_label+50 && y_control<y_label+30 ) return(true);
else return(false);
}
//------------------------------------------------------------------------------------------
datetime bar0(){return(WindowFirstVisibleBar());}
//------------------------------------------------------------------------------------------
int barall(){return(WindowBarsPerChart()-timeshift);}
//------------------------------------------------------------------------------------------
void infocomment(){
string s=StringConcatenate(shiftcomm,
"Èíôîðìàöèÿ îá àêòèâíîì òîðãîâîì ñ÷åòå:","\n",
"Áðîêåð: ", AccountCompany(),"\n",
"Èìÿ ñåðâåðà: ",AccountServer(),"\n",
"Êðåäèòíîå ïëå÷î: 1:",AccountLeverage(),"\n",
"Êðåäèò äëÿ ñ÷åòà: ",AccountCredit(),"\n",
"Âàëþòà ñ÷åòà: ",AccountCurrency(),"\n",
"Èìÿ ñ÷åòà: ",AccountName(),"\n",
"Íîìåð ñ÷åòà: ",AccountNumber(),"\n",
"Êðèòè÷åñêèé óðîâåíü ïîòåðü - stop-out: ", AccountStopoutLevel()," %");
Comment(s);
}
//------------------------------------------------------------------------------------------
void log(string ss=""){
int handle = FileOpen(FileName ,FILE_CSV|FILE_READ|FILE_WRITE);
if ( handle ==-1 ) Print("Îøèáêà íîìåð ",GetLastError()," ïðè ñîçäàíèè ôàéëà : "+FileName);
else {
string s = StringConcatenate(ss,
DoubleToStr(lot,2)," ëîò",
", öåíà=",DoubleToStr(ask,Digits),
", âðåìÿ = ",TimeToStr(Time[tim],TIME_DATE|TIME_MINUTES),
", áàëàíñ = ",DoubleToStr(balance,2),acccurr,
", ñðåäñòâà = ",DoubleToStr(equity,2),acccurr,
", çàëîã = ",DoubleToStr(margin,2),acccurr,
", âñåãî Buy = ",DoubleToStr(totbuy,0)," øò",
", âñåãî Sell = ",DoubleToStr(totsell,0)," øò",
", ïðèáûëü = ",DoubleToStr(equity-balance-margin,2),acccurr);
FileSeek(handle, 0, SEEK_END);
FileWrite(handle, s);
FileFlush(handle);
FileClose(handle);
}
}
Comments