Open_Interest_IndicatorXR_online_beta_V3
Download scriptThis script is a specialized trading tool designed to provide visual data regarding "Open Interest" (the total number of outstanding contracts in a market) for specific financial instruments. It functions by merging real-time web data with historical analysis to create an informative overlay on your trading chart.
Data Acquisition and Updates
The tool is designed to be autonomous in how it gathers information. It performs the following actions:
- Remote Fetching: It communicates with an external internet server to download the latest market data in a CSV format.
- Local Storage: To ensure efficiency, the script saves this information into a local file on your computer. This allows the tool to reference history without constantly putting a strain on your internet connection.
- Smart Refreshing: The script includes logic to check if it needs to update; it won't fetch data every second but will refresh periodically to ensure you are looking at current market conditions.
Visual Logic and Chart Overlay
Instead of just providing a list of numbers, the tool translates complex data into visual shapes and colors on your chart:
- Dynamic Shapes: It draws rectangles on the chart to represent specific data points. These shapes help highlight different zones of activity or volume.
- Interactive Coloring: The color of these shapes is not static. The script calculates the intensity of certain market values and translates them into a dynamic color gradient. This allows you to see "heat" or intensity levels at a glance—the brighter or different the color, the more significant the data point.
- Textual Labels: Depending on your settings, the script can automatically place text labels next to these shapes, showing specific values (such as price levels or volume counts) directly on the chart.
User Experience Features
The script is built with several quality-of-life features for the trader:
- Customizable Visibility: You can toggle various elements on and off, such as historical data, text labels, or background highlights, allowing you to customize your workspace.
- Adaptive Environment Detection: The tool can detect the color of your trading platform's background (dark vs. light mode) to automatically adjust its text colors, ensuring that information remains readable regardless of your theme.
- Automated Cleanup: When you remove the indicator or close the chart, the script is programmed to "clean up" after itself by deleting the shapes and labels it created, keeping your chart tidy.
Summary for the User
In short, this tool acts as a bridge between an external data source and your visual chart. It goes out to the internet, grabs volume and interest data, stores it locally, and then draws a color-coded map of that information onto your screen so you can see market depth and activity patterns visually rather than just looking at raw numbers.
//+------------------------------------------------------------------+
//| +OPTIONS_VOLUME.mq4 |
//| Copyright © 2009, Xrust Solution. |
//| http://www.xrust.uco.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Xrust Solution."
#property link "http://www.xrust.uco.net"
#import "user32.dll"
int GetWindowDC(int dc);
int ReleaseDC(int h, int dc);
bool GetWindowRect(int h, int& pos[4]);
#import
#import "gdi32.dll"
int GetPixel(int dc, int x, int y);
#import
#import "wininet.dll"
int InternetAttemptConnect (int x);
int InternetOpenA(string sAgent, int lAccessType, string sProxyName = "", string sProxyBypass = "", int lFlags = 0);
int InternetOpenUrlA(int hInternetSession, string sUrl, string sHeaders = "", int lHeadersLength = 0,int lFlags = 0, int lContext = 0);
int InternetReadFile(int hFile, int& sBuffer[], int lNumBytesToRead,int& lNumberOfBytesRead[]);
int InternetCloseHandle(int hInet);
#import
extern string ïðåôèêñ = "";
extern string ñóôôèêñ = "";
extern bool áëèçæàéøèå = true;
extern bool Ïîêàçûâàòü_òåêñò = true;
extern bool Ïîêàçûâàòü_ôîíîì = true;
extern bool Ïîêàçûâàòü_èñòîðèþ = true;
extern int Ãëóáèíà_èñòîðèè = 10;
extern int ÿðêîñòü = 0;
extern int ðàçìåð_øðèôòà = 9;
extern string èìÿ_øðèôòà = "Arial";
color Öâ_ñìåùåíèå ;
double in[15][3];
double hist[1000][61];
double prom[61];
double mno=1;
int Win_color;
int prewtime;
string inetpacth="http://xrust.land.ru//";
string name="";
#property indicator_chart_window
//+------------------------------------------------------------------+
void init(){
int len=0,strlen=0;
if(ïðåôèêñ!=""){len=StringLen(ïðåôèêñ)-1;name=StringSubstr(Symbol(),len,0);}
if(ñóôôèêñ!=""){len=StringLen(ñóôôèêñ);strlen=StringLen(Symbol());name=StringSubstr(Symbol(),0,(strlen-len+1));}
prewtime=0;
if(!ReadFile()){ReadFile();}
IndicatorShortName("Open Interest indicatorXR © ");
return;}
//+------------------------------------------------------------------+
void deinit(){
if(UninitializeReason()==REASON_REMOVE||
UninitializeReason()==REASON_CHARTCLOSE||
UninitializeReason()==REASON_PARAMETERS){delgr();}
return;}
//+------------------------------------------------------------------+
void start(){static int prewmno,bpch;
int indco=IndicatorCounted();
if(indco-Bars<-1){return;}
//----
if(prewtime>TimeCurrent()){
prewtime=TimeCurrent()+3600;
if(!IsVisualMode()||!IsTesting()||!IsOptimization()){
readinet(name);
ReadFile();
}
}
if(GetWndColor(Symbol())>0){color txcol=Black;}else{txcol=White;}
int x=ArrayRange(hist,0);
for(int a=0;a<x-1;a++){int t;
if(a==x-2){
t=Time[0]+(Period()*360);
}else{
t=hist[a+1][0];
}
int summa=0,summb=0;
for(int b=1;b<43;b=b+3){
if(summa<hist[a][b]){summa=hist[a][b];}
if(summb<hist[a][b+2]){summb=hist[a][b+2];}
}
for(b=1;b<43;b=b+3){
if(Ïîêàçûâàòü_èñòîðèþ){
if(hist[a][0]>iTime(Symbol(),1440,Ãëóáèíà_èñòîðèè)){
if(áëèçæàéøèå){
if(hist[a][b+1]<iHigh(Symbol(),1440,iBarShift(Symbol(),1440,t))+(hist[a][b+4]-hist[a][b+1])&&
hist[a][b+4]> iLow(Symbol(),1440,iBarShift(Symbol(),1440,t))-(hist[a][b+4]-hist[a][b+1])){
SetRestangle(öâåò(hist[a][b],hist[a][b+2],summa,summb),b+"/"+a,t,hist[a][b+1],hist[a][0],hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0));
if(Ïîêàçûâàòü_òåêñò)SetText("",txcol,t-(t-hist[a][0])/2,hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0),ðàçìåð_øðèôòà);
}
if(a==x-2){
SetRestangle(öâåò(hist[a][b],hist[a][b+2],summa,summb),b+"/"+a,t,hist[a][b+1],hist[a][0],hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0));
if(Ïîêàçûâàòü_òåêñò)SetText("",txcol,t-(t-hist[a][0])/2,hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0),ðàçìåð_øðèôòà);
}
}else{
SetRestangle(öâåò(hist[a][b],hist[a][b+2],summa,summb),b+"/"+a,t,hist[a][b+1],hist[a][0],hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0));
if(Ïîêàçûâàòü_òåêñò)SetText("",txcol,t-(t-hist[a][0])/2,hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0),ðàçìåð_øðèôòà);
}
}
}else{
if(a==x-2){
SetRestangle(öâåò(hist[a][b],hist[a][b+2],summa,summb),b+"/"+a,t,hist[a][b+1],hist[a][0],hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0));
if(Ïîêàçûâàòü_òåêñò)SetText("",txcol,t-(t-hist[a][0])/2,hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0),ðàçìåð_øðèôòà);
}
}
}
}
string copyrite="Open Interest indicatorXR © ";
SetLabel("Times new roman","cp",copyrite,txcol,1,1,1,9);
//----
return;}
color öâåò(double c,double p,double suma,double sumb)
{ int BB=0,RR=0,GG=0;double x,y;
c=c+1;p=p+1;suma=suma+1;sumb=sumb+1;
if(suma>5000){suma=5000;}
if(sumb>5000){sumb=5000;}
if(c>5000){c=5000;}
if(p>5000){p=5000;}
if(ÿðêîñòü>255){ÿðêîñòü=255;}
if(ÿðêîñòü<0){ÿðêîñòü=0;}
if(c>p){
if(GetWndColor(Symbol())<=0){
x=(MathSqrt(c/suma))*255;
BB=ÿðêîñòü;
GG=ÿðêîñòü;
RR=x;
}else{
x=(MathSqrt(c/suma))*255-ÿðêîñòü;
if(x>255){x=255;}
BB=255-x;
GG=255-x;
RR=255;
}
return((BB<<16)^(GG<<8)^(RR));
}
if(p>c){
if(GetWndColor(Symbol())<=0){
y=(MathSqrt(p/sumb))*255;
BB=y;
GG=ÿðêîñòü;
RR=ÿðêîñòü;
}else{
y=(MathSqrt(p/sumb))*255-ÿðêîñòü;
BB=255;
GG=255-y;
RR=255-y;
}
return((BB<<16)^(GG<<8)^(RR));
}
return((BB<<16)^(GG<<8)^(RR));
}
//+------------------------------------------------------------------+
void readinet(string nm="")
{if(nm==""){nm=Symbol()+"_hist_OI.csv";}else{nm=nm+"_OI.csv";}
if(!IsDllsAllowed()){
Alert("Íåîáõîäèìî â íàñòðîéêàõ ðàçðåøèòü èñïîëüçîâàíèå DLL");
return(0);
}
int rv = InternetAttemptConnect(0);
if(rv != 0){
Print("Îøèáêà ïðè âûçîâå InternetAttemptConnect()");
return(0);
}
int hInternetSession = InternetOpenA("Microsoft Internet Explorer",0, "", "", 0);
if(hInternetSession <= 0){
Print("Îøèáêà ïðè âûçîâå InternetOpenA()");
return(0);
}
int hURL = InternetOpenUrlA(hInternetSession,inetpacth+nm, "", 0, 0, 0);
if(hURL <= 0){
Print("Îøèáêà ïðè âûçîâå InternetOpenUrlA()");
InternetCloseHandle(hInternetSession);
return;
}
int cBuffer[256];
ArrayInitialize(cBuffer,0);
int dwBytesRead[1];
ArrayInitialize(dwBytesRead,0);
string TXT = "";
while(!IsStopped()){
bool bResult = InternetReadFile(hURL, cBuffer, 1024, dwBytesRead);
if(dwBytesRead[0] == 0)break;
string text = "";
for(int i = 0; i < 256; i++){
text = text + CharToStr(cBuffer[i] & 0x000000FF);
if(StringLen(text) == dwBytesRead[0])break;
text = text + CharToStr(cBuffer[i] >> 8 & 0x000000FF);
if(StringLen(text) == dwBytesRead[0])break;
text = text + CharToStr(cBuffer[i] >> 16 & 0x000000FF);
if(StringLen(text) == dwBytesRead[0])break;
text = text + CharToStr(cBuffer[i] >> 24 & 0x000000FF);
if(StringLen(text) == dwBytesRead[0])break;
}
TXT = TXT + text;
Sleep(1);
}
if(TXT != ""){
int h = FileOpen(nm, FILE_CSV|FILE_WRITE);
if(h>0){
FileWrite(h,TXT);
FileClose(h);
}else{
int err=GetLastError();
Print("Îøèáêà ïðè âûçîâå FileOpen() ¹ ",err);
}
}else{Print("Íåò ñ÷èòàííûõ äàííûõ");}
InternetCloseHandle(hInternetSession);
return;}
//+------------------------------------------------------------------+
int GetWndColor(string sy)
{
int hWnd = WindowHandle(sy, Period());
int hDC = GetWindowDC(hWnd);
int rect[4];
GetWindowRect(hWnd, rect);
int wW = rect[2] - rect[0]; // øèðèíà îêíà
int wH = rect[3] - rect[1]; // âûñîòà îêíà
int col = GetPixel(hDC, 2, 2);
if(col==-1) // ëåâûé âåðõíèé óãîë íå âèäåí
{
col = GetPixel(hDC, wW-3, wH-3);
if(col==-1) // ïðàâûé íèæíèé óãîë íå âèäåí
col = GetPixel(hDC, 2, wH-3);
if(col==-1) // ëåâûé íèæíèé óãîë íå âèäåí
col = GetPixel(hDC, wW-3, 2);
if(col==-1) // ïðàâûé âåðõíèé óãîë íå âèäåí
{
ReleaseDC(hWnd, hDC);
return(Win_color);
}
}
ReleaseDC(hWnd, hDC);
return(col);
}
//+------------------------------------------------------------------+
void SetLabel(string fn,string nm, string tx, color cl, int xd, int yd, int cr=0, int fs=9) {
if (ObjectFind(nm)<0) ObjectCreate(nm, OBJ_LABEL, 0, 0,0);
ObjectSetText(nm, tx,fs,fn, cl);
ObjectSet(nm, OBJPROP_COLOR , cl);
ObjectSet(nm, OBJPROP_XDISTANCE, xd);
ObjectSet(nm, OBJPROP_YDISTANCE, yd);
ObjectSet(nm, OBJPROP_CORNER , cr);
ObjectSet(nm, OBJPROP_FONTSIZE , fs);
ObjectSet(nm, OBJPROP_BACK , false);
}
//+------------------------------------------------------------------+
void delgr(){
for(int i=ObjectsTotal();i>=0;i--){
if("sumoi"==StringSubstr(ObjectName(i),0,5)){ObjectDelete(ObjectName(i));}
if("opint"==StringSubstr(ObjectName(i),0,5)){ObjectDelete(ObjectName(i));}
}
ObjectDelete("oi");
ObjectDelete("cp");
return;
}
//+------------------------------------------------------------------+
void SetText(string op,color cl,int t1,double p1,string tx="",int razm=8){
string nm="sumoi"+op+DoubleToStr(p1,Digits)+tx;
if(ObjectFind(nm)<0)ObjectCreate(nm, OBJ_TEXT, 0, 0,0, 0,0);
ObjectSet(nm, OBJPROP_TIME1 , t1);
ObjectSet(nm, OBJPROP_PRICE1, p1);
ObjectSetText(nm,tx,razm,èìÿ_øðèôòà,cl);
ObjectSet(nm, OBJPROP_BACK , Ïîêàçûâàòü_ôîíîì);
return;
}
//+------------------------------------------------------------------+
void SetRestangle(color cl, string nm="",datetime t1=0, double p1=0,
datetime t2=0, double p2=0,string vol=""){
nm="opint "+nm;
if(ObjectFind(nm)<0)ObjectCreate(nm, OBJ_RECTANGLE, 0, 0,0, 0,0);
ObjectSet(nm, OBJPROP_TIME1 , t2);
ObjectSet(nm, OBJPROP_PRICE1, p1);
ObjectSet(nm, OBJPROP_TIME2 , t1);
ObjectSet(nm, OBJPROP_PRICE2, p2);
ObjectSet(nm, OBJPROP_COLOR , cl);
ObjectSet(nm, OBJPROP_RAY , false);
ObjectSet(nm, OBJPROP_STYLE , 0);
ObjectSet(nm, OBJPROP_WIDTH , 1);
ObjectSetText(nm,vol,3,"Arial",White);
ObjectSet(nm, OBJPROP_BACK , Ïîêàçûâàòü_ôîíîì);
}
//+------------------------------------------------------------------+
bool ReadFile(){int i,x,y;int hd;
string fn=Symbol()+"_hist_OI.csv";
double spr=MarketInfo(Symbol(),MODE_SPREAD)*Point;
hd=FileOpen(fn,FILE_CSV|FILE_READ);
if(hd<0){
Print(GetLastError()," ",fn," File not found",hd);
readinet();
return(false);
}else{//x=0;y=0;
for(i=0;i<50;i++){
string out=FileReadString(hd);
for(x=0;x<61;x++){
int pos=StringFind(out,",",0);
hist[i][x]=StrToDouble(StringSubstr(out,0,pos+1));
out=StringSubstr(out,pos+1,0);
}
if(FileIsEnding(hd)){break;}
}
ArrayResize(hist,i);
FileClose(hd);
return(true);
}
return(true);
}
Comments