Open_Interest_IndicatorXR_online_beta_V3_eng
Download scriptFunctional Overview
This script is designed to provide a visual representation of "Open Interest" data directly on your trading charts. Open Interest typically refers to the total number of outstanding derivative contracts (like futures) that have not been settled, and this tool helps traders visualize those levels to identify market sentiment or liquidity zones.
Core Logic and Workflow
1. Data Acquisition The script is designed to be "self-healing" regarding its data source. It follows a specific hierarchy to get the information it needs:
- Local Files: First, it attempts to read historical data from a CSV file stored on your computer.
- Web Retrieval: If that local file is missing, the script automatically connects to a specific web server to download the latest data. It then saves this data locally for future use.
- Security Requirement: Because it interacts with external files and the internet, it requires the user to enable "DLL Imports" in the platform settings.
2. Data Processing & Filtering Once the data is loaded into the system, the script performs several background calculations:
- Symbol Identification: It identifies the specific currency or asset you are trading (e.g., EURUSD) and prepares the data specifically for that pair.
- Historical Depth: The tool looks back through a set amount of historical data (defined by the user) to populate the chart with past information.
- Position Mapping: It analyzes specific price points and timeframes to determine where historical "Open Interest" levels occurred.
3. Visual Rendering The final step is the visual delivery of this information on your screen:
- Dynamic Shapes: The script draws rectangles on the chart to represent specific data zones. These are not just static lines; they are colored based on the strength or type of the data being displayed.
- Smart Coloring: The script features an "adaptive" color logic. It can detect your chart's background color and automatically adjust the text and shapes (switching between light and dark modes) to ensure they remain visible to you.
- Labels and Text: Depending on your settings, the tool can overlay text directly onto these zones, showing specific values or identifiers for each data point.
- History Management: It manages the visibility of past data, allowing you to choose whether to see a long history of markers or just the most recent information.
User Experience Features
- Customizable Aesthetics: Users can adjust font sizes, font names, and "brightness" levels to match their personal trading environment.
- Clean Up: The script includes a cleanup routine that ensures when you remove the tool from your chart, all the rectangles and text labels it created are deleted, leaving your workspace clean.
- Background Mode: There is an option to show the data "as background," which allows the information to stay visible without cluttering the foreground of your price action.
//+------------------------------------------------------------------+
//| +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 prefix = "";
extern string suffix = "";
extern bool closest = true;
extern bool show_text = true;
extern bool show_as_background = true;
extern bool show_history = true;
extern int history_depth = 10;
extern int brightness = 0;
extern int font_size = 9;
extern string font_name = "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(prefix!=""){len=StringLen(prefix)-1;name=StringSubstr(Symbol(),len,0);}
if(suffix!=""){len=StringLen(suffix);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(show_history){
if(hist[a][0]>iTime(Symbol(),1440,history_depth)){
if(closest){
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(show_text)SetText("",txcol,t-(t-hist[a][0])/2,hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0),font_size);
}
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(show_text)SetText("",txcol,t-(t-hist[a][0])/2,hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0),font_size);
}
}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(show_text)SetText("",txcol,t-(t-hist[a][0])/2,hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0),font_size);
}
}
}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(show_text)SetText("",txcol,t-(t-hist[a][0])/2,hist[a][b+4],DoubleToStr(hist[a][b+0],0)+"/"+DoubleToStr(hist[a][b+2],0),font_size);
}
}
}
}
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(brightness>255){brightness=255;}
if(brightness<0){brightness=0;}
if(c>p){
if(GetWndColor(Symbol())<=0){
x=(MathSqrt(c/suma))*255;
BB=brightness;
GG=brightness;
RR=x;
}else{
x=(MathSqrt(c/suma))*255-brightness;
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=brightness;
RR=brightness;
}else{
y=(MathSqrt(p/sumb))*255-brightness;
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("Allow DLL use in settings");
return(0);
}
int rv = InternetAttemptConnect(0);
if(rv != 0){
Print("Error in call of InternetAttemptConnect()");
return(0);
}
int hInternetSession = InternetOpenA("Microsoft Internet Explorer",0, "", "", 0);
if(hInternetSession <= 0){
Print("Error in call InternetOpenA()");
return(0);
}
int hURL = InternetOpenUrlA(hInternetSession,inetpacth+nm, "", 0, 0, 0);
if(hURL <= 0){
Print("Error in call 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("Error in call FileOpen() ¹ ",err);
}
}else{Print("No data");}
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]; // Window width
int wH = rect[3] - rect[1]; // Window height
int col = GetPixel(hDC, 2, 2);
if(col==-1) // Left upper corner is invisible
{
col = GetPixel(hDC, wW-3, wH-3);
if(col==-1) // Right lower corner is invisible
col = GetPixel(hDC, 2, wH-3);
if(col==-1) // Left lower corner is invisible
col = GetPixel(hDC, wW-3, 2);
if(col==-1) // Right upper corner is invisible
{
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,font_name,cl);
ObjectSet(nm, OBJPROP_BACK , show_as_background);
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 , show_as_background);
}
//+------------------------------------------------------------------+
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