//+------------------------------------------------------------------+
//| GUI.mq4 |
//| WaihuiTuan |
//| https://www.waihuituan.com |
//+------------------------------------------------------------------+
#property copyright "https://www.WaihuiTuan.com"
#property link "https://www.waihuituan.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#include <WindowLib.mqh>
extern color titlebar_bg=clrCornflowerBlue;
extern color panel_bg=clrHoneydew;
extern color good_spread_clr = clrGreen;
extern color bad_spread_clr = clrRed;
extern color tips_clr=clrCadetBlue;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
string winname = "tSpread";
const string prefix="spd_";
const string fullwindowname = prefix+winname;
int OnInit()
{
int pwidth = 200;
int pheight= 150;
int curspread_font_size = 40;
IndicatorSetString(INDICATOR_SHORTNAME,winname);
if (false == createWindow("Spread Panel",fullwindowname,0,0,0,pwidth,pheight,1,panel_bg,titlebar_bg)) return (INIT_FAILED);
double initspread = (double)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)/10;
string curspread = DoubleToString(initspread,1);
labelCreate(prefix+"curspread"+hidemark(),curspread,(pwidth-20-(curspread_font_size-10)*(StringLen(curspread)-1))/2,(pheight-curspread_font_size)/2,curspread_font_size,CornflowerBlue);
labelCreate(prefix+"spreadtip"+hidemark(),getSpreadTips(),10,pheight-20,10,tips_clr);
movewindow(prefix,getTitlebarName(fullwindowname),loadPosX(),loadPosY(),ALL);
return(INIT_SUCCEEDED);
}
bool minimize = false;
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
if (id==CHARTEVENT_OBJECT_CLICK){
if (sparam == getClosebtnName(fullwindowname)){
//关闭窗口
ChartIndicatorDelete(ChartID(),0,winname);
}else if (sparam == getMinbtnName(fullwindowname)){
if (minimize == false){
//最小化窗口
minwindow(fullwindowname);
minimize = true;
}else{
//最大化窗口
maxwindow(fullwindowname);
minimize = false;
}
}
}else if (id == CHARTEVENT_OBJECT_DRAG){
string titlebarname = getTitlebarName(fullwindowname);
long newx = getObjX(titlebarname);
long newy = getObjY(titlebarname);
if (sparam == titlebarname && (newx != titlebar_x || newy != titlebar_y)){
movewindow(prefix,titlebarname,newx-titlebar_x,newy-titlebar_y,AUTO);
savePos(newx,newy);
}
}
}
void savePos(const long x,const long y)
{
GlobalVariableSet(getPosName()+"_x",(double)x);
GlobalVariableSet(getPosName()+"_y",(double)y);
}
long loadPosX()
{
return (long)GlobalVariableGet(getPosName()+"_x");
}
long loadPosY()
{
return (long)GlobalVariableGet(getPosName()+"_y");
}
string getPosName()
{
return "SpreadPanel_"+TerminalName()+"_"+Symbol();
}
void OnDeinit(const int reason){
ObjectsDeleteAll(ChartID(),prefix,EMPTY,EMPTY);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
double maxspd = 0 ;
double minspd = 0 ;
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
double initspread = (double)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)/10;
double avgspread = Spread(initspread);
//点差最大/最小值初始化
if (maxspd == 0 && minspd == 0){
maxspd = initspread;
minspd = initspread;
}
maxspd = (initspread > maxspd) ? initspread : maxspd;
minspd = (initspread < minspd) ? initspread : minspd;
string showavgspread = DoubleToString(avgspread,1);
string curspread = DoubleToString(initspread,1);
//Print("Cur:"+curspread+"Avg:"+showavgspread+"Count:"+IntegerToString(ArraySize(Spread)));
//设置点差数值和颜色
ObjectSetString(ChartID(),prefix+"curspread"+hidemark(),OBJPROP_TEXT,curspread);
ObjectSetInteger(ChartID(),prefix+"curspread"+hidemark(),OBJPROP_COLOR,initspread > avgspread ? bad_spread_clr : good_spread_clr);
//设置点差提示文字
ObjectSetString(ChartID(),prefix+"spreadtip"+hidemark(),OBJPROP_TEXT,getSpreadTips());
//--- return value of prev_calculated for next call
return(rates_total);
}
string getSpreadTips()
{
return "Avg:"+DoubleToStr(Spread(),1)+" Max: "+DoubleToStr(maxspd,1)+" Min:"+DoubleToStr(minspd,1);
}
extern int SpreadSampleSize = 10000; // number of ticks to average spread over set to 0 to disable
double Spread[]; // array for spread MA
//+-----------------------------------------------------+
//| Spread function - used to calculate SMA of Spread |
//+-----------------------------------------------------+
double Spread(double AddValue=0)
{
double LastValue;
static double ArrayTotal=0;
if (AddValue == 0 && SpreadSampleSize <= 0) return(Ask-Bid);
if (AddValue == 0 && ArrayTotal == 0) return(Ask-Bid);
if (AddValue == 0 ) return(ArrayTotal/ArraySize(Spread));
ArrayTotal = ArrayTotal + AddValue;
ArraySetAsSeries(Spread, true);
if (ArraySize(Spread) == SpreadSampleSize)
{
LastValue = Spread[0];
ArrayTotal = ArrayTotal - LastValue;
ArraySetAsSeries(Spread, false);
ArrayResize(Spread, ArraySize(Spread)-1 );
ArraySetAsSeries(Spread, true);
ArrayResize(Spread, ArraySize(Spread)+1 );
}
else ArrayResize(Spread, ArraySize(Spread)+1 );
// Print("ArraySize = ",ArraySize(lSpread)," AddedNo. = ",AddValue);
ArraySetAsSeries(Spread, false);
Spread[0] = AddValue;
return(NormalizeDouble(ArrayTotal/ArraySize(Spread), Digits));
} // end of Spread
//+--------------------------------------------------------+