//+------------------------------------------------------------------+ //| DealsLent.mq5 | //| Copyright 2016, prostotrader | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, prostotrader" #property link "https://www.mql5.com" #property version "1.00" #define DEBUG #define on_call -111 input int ActSize=0; //Размер актуальной истории в буферах индикатора //--- #property indicator_separate_window #property indicator_buffers 4 #property indicator_plots 2 //--- plot Label1 #property indicator_label1 "Sell" #property indicator_type1 DRAW_LINE #property indicator_color1 clrLightPink #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot Label1 #property indicator_label2 "Buy" #property indicator_type2 DRAW_LINE #property indicator_color2 clrLightSkyBlue #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- Levels #property indicator_level1 0 #property indicator_level2 0 #property indicator_levelwidth 1 #property indicator_levelstyle STYLE_DOT //--- indicator buffers double SellBuffer[]; double BuyBuffer[]; double SellVol[]; double BuyVol[]; long sell_deals; long buy_deals; ulong sell_vol; ulong buy_vol; ulong start_time; bool new_bar; int cur_bars; int event_cnt; MqlTick ticks[]; ulong mem_time; long mem_deals; long last_deals; #ifdef DEBUG int file_handle; #endif // //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { mem_time=0; new_bar=false; start_time=0; event_cnt =0; if(!MarketBookAdd(Symbol())) { Print(__FUNCTION__,": Стакан символа "+Symbol()+" не добавден!"); return( INIT_FAILED ); } //--- Bars cur_bars=Bars(Symbol(),PERIOD_CURRENT); if(cur_bars<(ActSize+1)) { Print(__FUNCTION__,": Не достаточно баров на текущем таймфрейме! должно быть не менее ",ActSize+1); return( INIT_FAILED ); } if(cur_bars<2) { Print(__FUNCTION__,": Не достаточно баров на текущем таймфрейме! Должно быть не менее 2."); return( INIT_FAILED ); } //--- IndicatorSetInteger(INDICATOR_DIGITS,0); IndicatorSetString(INDICATOR_SHORTNAME,"DealsLent"); //--- SetIndexBuffer(0,SellBuffer,INDICATOR_DATA); PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE); ArraySetAsSeries(SellBuffer,true); //--- SetIndexBuffer(1,BuyBuffer,INDICATOR_DATA); PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE); ArraySetAsSeries(BuyBuffer,true); //--- SetIndexBuffer(2,SellVol,INDICATOR_CALCULATIONS); ArraySetAsSeries(SellVol,true); //--- SetIndexBuffer(3,BuyVol,INDICATOR_CALCULATIONS); ArraySetAsSeries(BuyVol,true); //--- int window=ChartWindowFind(ChartID(),"DealsLent"); ObjectCreate(ChartID(),"Label_1",OBJ_LABEL,window,0,0); ObjectCreate(ChartID(),"Label_2",OBJ_LABEL,window,0,0); ObjectCreate(ChartID(),"Label_3",OBJ_LABEL,window,0,0); ObjectCreate(ChartID(),"Label_4",OBJ_LABEL,window,0,0); ObjectSetInteger(ChartID(),"Label_1",OBJPROP_YDISTANCE,30); ObjectSetInteger(ChartID(),"Label_1",OBJPROP_XDISTANCE,0); ObjectSetInteger(ChartID(),"Label_2",OBJPROP_YDISTANCE,60); ObjectSetInteger(ChartID(),"Label_2",OBJPROP_XDISTANCE,0); ObjectSetInteger(ChartID(),"Label_3",OBJPROP_YDISTANCE,15); ObjectSetInteger(ChartID(),"Label_3",OBJPROP_XDISTANCE,0); ObjectSetInteger(ChartID(),"Label_4",OBJPROP_YDISTANCE,45); ObjectSetInteger(ChartID(),"Label_4",OBJPROP_XDISTANCE,0); ObjectSetInteger(ChartID(),"Label_1",OBJPROP_COLOR,clrLightPink); ObjectSetInteger(ChartID(),"Label_2",OBJPROP_COLOR,clrLightSkyBlue); ObjectSetInteger(ChartID(),"Label_3",OBJPROP_COLOR,clrLightPink); ObjectSetInteger(ChartID(),"Label_4",OBJPROP_COLOR,clrLightSkyBlue); ObjectSetString(ChartID(),"Label_1",OBJPROP_TEXT,"Сум. объём Sell: 0"); ObjectSetString(ChartID(),"Label_2",OBJPROP_TEXT,"Сум. объём Buy: 0"); ObjectSetString(ChartID(),"Label_3",OBJPROP_TEXT,"Сум. кол-во Sell: 0"); ObjectSetString(ChartID(),"Label_4",OBJPROP_TEXT,"Сум. кол-во Buy: 0"); IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrLightPink); IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrLightSkyBlue); PlotIndexSetInteger(0,PLOT_SHOW_DATA,false); PlotIndexSetString(0,PLOT_LABEL," "); PlotIndexSetInteger(1,PLOT_SHOW_DATA,false); PlotIndexSetString(1,PLOT_LABEL," "); ChartRedraw(ChartID()); #ifdef DEBUG file_handle=FileOpen(Symbol()+"_lent.csv",FILE_WRITE|FILE_CSV); if(file_handle==INVALID_HANDLE) { return(INIT_FAILED); } FileWrite(file_handle,"Time,Bid,Ask,Last,Volume,Type"); #endif return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { MarketBookRelease(Symbol()); ObjectDelete(ChartID(),"Label_1"); ObjectDelete(ChartID(),"Label_2"); ObjectDelete(ChartID(),"Label_3"); ObjectDelete(ChartID(),"Label_4"); if(reason==REASON_INITFAILED) { int window=ChartWindowFind(ChartID(),"DealsLent"); ChartIndicatorDelete(ChartID(),window,"DealsLent"); } #ifdef DEBUG if(file_handle!=INVALID_HANDLE) FileClose(file_handle); #endif } //+------------------------------------------------------------------+ //| Custom indicator Convrt ulong to time msc | //+------------------------------------------------------------------+ string ConvTimeMscToStr(const ulong msc_time) { string result=""; double new_time= double(msc_time); double pr_time = new_time/1000; datetime a_time= datetime(ulong(pr_time)); MathMod(new_time,1000); result=TimeToString(a_time,TIME_DATE|TIME_SECONDS); pr_time= MathMod(new_time,1000); result+= "."+string(ulong(pr_time)); return(result); } //+------------------------------------------------------------------+ //| Custom indicator On book event function | //+------------------------------------------------------------------+ void OnBookEvent(const string &symbol) { if(symbol==Symbol()) { if(start_time==0) { if(CopyTicks(Symbol(),ticks,COPY_TICKS_TRADE,0,1)==1) { start_time=ulong(ticks[0].time_msc); } } else { sell_deals= 0; buy_deals = 0; sell_vol= 0; buy_vol = 0; last_deals=0; int result=CopyTicks(Symbol(),ticks,COPY_TICKS_TRADE,start_time,0); if(result>0) { for(int i=0; i0) { for(int i=0; imem_deals) { long res_deals=new_deals-mem_deals; result=CopyTicks(Symbol(),t_ticks,COPY_TICKS_TRADE,mem_time,uint(new_deals)); if(result>0) { for(int i=0; i0) { res_deals--; add_buy++; add_b_vol+=t_ticks[i].volume; } } else if((t_ticks[i].flags &TICK_FLAG_SELL)==TICK_FLAG_SELL) { if(res_deals>0) { res_deals--; add_sell++; add_s_vol+=t_ticks[i].volume; } } } } sell_deals+=add_sell; sell_vol+=add_s_vol; buy_deals+=add_buy; buy_vol+=add_b_vol; #ifdef DEBUG FileWrite(file_handle," Add deals = "+string(new_deals-mem_deals)+" Time dials = "+ConvTimeMscToStr(mem_time)); Print("Add ticks done"); #endif } } } } mem_deals=last_deals; mem_time=ticks[a_size-1].time_msc; switch(int(sell_vol)) { case 0: case 1: case 2: PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1); break; case 3: case 4: case 5: PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2); break; case 6: case 7: case 8: PlotIndexSetInteger(0,PLOT_LINE_WIDTH,3); break; default: PlotIndexSetInteger(0,PLOT_LINE_WIDTH,4); break; } switch(int(buy_vol)) { case 0: case 1: case 2: PlotIndexSetInteger(1,PLOT_LINE_WIDTH,1); break; case 3: case 4: case 5: PlotIndexSetInteger(1,PLOT_LINE_WIDTH,2); break; case 6: case 7: case 8: PlotIndexSetInteger(1,PLOT_LINE_WIDTH,3); break; default: PlotIndexSetInteger(1,PLOT_LINE_WIDTH,4); break; } ObjectSetString(ChartID(),"Label_1",OBJPROP_TEXT,"Сум. объём Sell: "+string(sell_vol)); ObjectSetString(ChartID(),"Label_2",OBJPROP_TEXT,"Сум. объём Buy: "+string(buy_vol)); ObjectSetString(ChartID(),"Label_3",OBJPROP_TEXT,"Сум. кол-во Sell: "+string(sell_deals)); ObjectSetString(ChartID(),"Label_4",OBJPROP_TEXT,"Сум. кол-во Buy: "+string(buy_deals)); ChartRedraw(ChartID()); } if(rates_total==prev_calculated) { if(begin==on_call) { if(new_bar) { new_bar=false; SellBuffer[0]= double(sell_deals); BuyBuffer[0] = double(buy_deals); SellVol[0]= double(sell_vol); BuyVol[0] = double(buy_vol); } else { if(ActSize==0) { for(int i=rates_total-1; i>0; i--) { SellBuffer[i]= SellBuffer[i-1]; BuyBuffer[i] = BuyBuffer[i-1]; SellVol[i]= SellVol[i-1]; BuyVol[i] = BuyVol[i-1]; } } else { for(int i=ActSize-1; i>0; i--) { SellBuffer[i]= SellBuffer[i-1]; BuyBuffer[i] = BuyBuffer[i-1]; SellVol[i]= SellVol[i-1]; BuyVol[i] = BuyVol[i-1]; } } SellBuffer[0]= double(sell_deals); BuyBuffer[0] = double(buy_deals); SellVol[0]= double(sell_vol); BuyVol[0] = double(buy_vol); } } } else { new_bar=true; if(begin==on_call) { new_bar=false; SellBuffer[0]= double(sell_deals); BuyBuffer[0] = double(buy_deals); SellVol[0]= double(sell_vol); BuyVol[0] = double(buy_vol); } if(ActSize>0) { SellBuffer[ActSize]= EMPTY_VALUE; BuyBuffer[ActSize] = EMPTY_VALUE; SellVol[ActSize]= EMPTY_VALUE; BuyVol[ActSize] = EMPTY_VALUE; } } } event_cnt=rates_total; //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+