Options_levels_and_VolunsXR_online_beta_V2

Author: Copyright � 2009, Xrust Solution.
0 Views
0 Downloads
0 Favorites
Options_levels_and_VolunsXR_online_beta_V2
//+------------------------------------------------------------------+
//|                                              +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"
#property indicator_chart_window
extern  string      ïðåôèêñ       = "";
extern  string      ñóôôèêñ       = "";
extern  int      Ôèëüòð_ïî_îáüåìó = 0;
extern  bool    Ïîêàçûâàòüèñòîðèþ = true;
extern  bool   Ïîêàçûâàòüèñòîáüåì = true;
extern  int   ÃëóáèíàÈñòîðèèÁàðîâ = 300;
extern  int         ðàçìåð_øðèôòà = 15;
extern  int         Òîëùèíà_ëèíèé = 3;
extern  int         ðàçìåð_øð_èñò = 10;
extern  int     Òîëùèíà_èñò_ëèíèé = 2;
extern  color    Öâåò_Îïöèîíà_PUT = Blue;
extern  color   Öâåò_Îïöèîíà_CALL = Red;
extern  string      èìÿ_øðèôòà    = "Times New Roman";
#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
double in[61][4];
double hist[1000][61];
double prom[61];
int Win_color;int bar_count;
string inetpacth="http://xrust.land.ru//";
string name="";
string fn,histfn;
//+------------------------------------------------------------------+
void   init(){int len=0,strlen=0;name=Symbol();
if(ïðåôèêñ!=""){len=StringLen(ïðåôèêñ)-1;name=StringSubstr(Symbol(),len,0);}
if(ñóôôèêñ!=""){len=StringLen(ñóôôèêñ);strlen=StringLen(Symbol());name=StringSubstr(Symbol(),0,(strlen-len+1));}
fn=name+".csv";
histfn=name+"_opt_hist.csv";
return;}
//+------------------------------------------------------------------+
void deinit(){
  if(UninitializeReason()==REASON_REMOVE||UninitializeReason()==REASON_CHARTCLOSE){
    FileDelete(fn);
    int h = FileOpen(histfn, FILE_CSV|FILE_WRITE);
    FileClose(h);
    delgr();
  } 
return;}
//+------------------------------------------------------------------+
int start()
  {static int prewtime,prewbar,maxbar;
  int indco=IndicatorCounted();
  if(indco-Bars<-1){delgr();return;}
//----
   if(GetWndColor(Symbol())>0){color txcol=Black;}else{txcol=White;}
   if(prewtime<TimeCurrent()){
     prewtime=TimeCurrent()+300;
     readinet(fn,fn);
     ReadFile();
     WriteHistory();
     delgr();
   }
   WindowRedraw();
   if(Ïîêàçûâàòüèñòîðèþ){
   if(ÃëóáèíàÈñòîðèèÁàðîâ==0){ÃëóáèíàÈñòîðèèÁàðîâ=WindowFirstVisibleBar();}
   int res=ArrayRange(hist,0);
   for(int x=0;x<res;x++){
     int tm0=hist[x][0];
     int tm1=hist[x+1][0];
     if(tm0>iTime(Symbol(),0,ÃëóáèíàÈñòîðèèÁàðîâ)){
     double z=0;
     double lp1,lp2;
     for(int y=1;y<59;y=y+2){z++;
       if(MathMod(z,2)>0){
         if(hist[x][y+1]>Ôèëüòð_ïî_îáüåìó){
         SetTLine(false,Öâåò_Îïöèîíà_CALL,"Call", tm0,hist[x][y],tm1,hist[x][y],Òîëùèíà_èñò_ëèíèé,hist[x][y+1]);
         if(lp1!=hist[x][y]){
           if(Ïîêàçûâàòüèñòîáüåì){SetText(false,Öâåò_Îïöèîíà_CALL,tm1,hist[x][y],DoubleToStr(hist[x][y+1],0),ðàçìåð_øð_èñò);}
           lp1=hist[x][y];
         }
         }
       }else{
         if(hist[x][y+1]>Ôèëüòð_ïî_îáüåìó){
         SetTLine(false,Öâåò_Îïöèîíà_PUT,"Put", tm0,hist[x][y],tm1,hist[x][y],Òîëùèíà_èñò_ëèíèé,hist[x][y+1]);
         if(lp1!=hist[x][y]){
           if(Ïîêàçûâàòüèñòîáüåì){SetText(false,Öâåò_Îïöèîíà_PUT,tm1,hist[x][y],"              "+DoubleToStr(hist[x][y+1],0),ðàçìåð_øð_èñò);}
           lp1=hist[x][y];
         }         
         }
       }
     } 
     }
   }
   }
   int a=ArrayRange(in,0);
   for(int i=0;i<15;i++){
     if(in[i][1]>Ôèëüòð_ïî_îáüåìó){
       SetText(true,Öâåò_Îïöèîíà_CALL,Time[0],in[i][0],DoubleToStr(in[i][1],0),ðàçìåð_øðèôòà);
       SetTLine(true,Öâåò_Îïöèîíà_CALL,"Call", Time[1],in[i][0],Time[0]+Period()*300,in[i][0],Òîëùèíà_ëèíèé,in[i][1]);
     }
     if(in[i][3]>Ôèëüòð_ïî_îáüåìó){
       SetText(true,Öâåò_Îïöèîíà_PUT,Time[0],in[i][2],"          "+DoubleToStr(in[i][3],0),ðàçìåð_øðèôòà);
       SetTLine(true,Öâåò_Îïöèîíà_PUT,"Put", Time[1],in[i][2],Time[0]+Period()*300,in[i][2],Òîëùèíà_ëèíèé,in[i][3]);    
     }
   }
   string copyrite="OptionsLevels&VolumesXR ©  ";
   SetLabel("cpo",copyrite,txcol,1,1,3,9);
   return(0);
  }
void delgr(){
   for(int i=ObjectsTotal();i>=0;i--){
     if("txt"==StringSubstr(ObjectName(i),0,3)){ObjectDelete(ObjectName(i));}
     if("option"==StringSubstr(ObjectName(i),0,6)){ObjectDelete(ObjectName(i));}
     ObjectDelete("cpo");
   }
return;
}
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void readinet(string nm,string inetnm){
   //if(nm==""){nm=Symbol()+".csv";}else{nm=nm+".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+inetnm+"?z="+TimeCurrent(), "", 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(nm,"  Îøèáêà ïðè âûçîâå 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 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,èìÿ_øðèôòà, 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 SetText(bool eraze,color cl,int t1,double p1,string tx="",int razm=8){
  string nm="txt"+tx+DoubleToStr(p1,Digits);
  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 , false);
return;
}   
//+------------------------------------------------------------------+
void SetTLine(bool eraze,color cl, string nm="",
              datetime t1=0, double p1=0, datetime t2=0, double p2=0,int wd=1,int vol=0) {
  nm="option "+nm+" Strike="+DoubleToStr(p1,4)+" Volume="+vol+" Time"+TimeToStr(t1,TIME_DATE|TIME_MINUTES);
  if (t1<=0) t1=Time[10];
  if (p1<=0) p1=Low[10];
  if (t2<=0) t2=Time[0];
  if (p2<=0) p2=Bid;
  if(ObjectFind(nm)<0)ObjectCreate(nm, OBJ_TREND, 0, 0,0, 0,0);
  ObjectSet(nm, OBJPROP_TIME1 , t1);
  ObjectSet(nm, OBJPROP_PRICE1, p1);
  ObjectSet(nm, OBJPROP_TIME2 , t2);
  ObjectSet(nm, OBJPROP_PRICE2, p2);
  ObjectSet(nm, OBJPROP_COLOR , cl);
  ObjectSet(nm, OBJPROP_RAY   , false);
  ObjectSet(nm, OBJPROP_STYLE , 0);
  ObjectSet(nm, OBJPROP_WIDTH , wd);
  ObjectSetText(nm,DoubleToStr(vol,0),3,"Arial",White);
  ObjectSet(nm, OBJPROP_BACK , false);
}   
//+------------------------------------------------------------------+
bool WriteHistory(){bool write=false;int i,a=0;int hd;
  string last=TimeCurrent()+",";
  ArrayInitialize(hist,0);
  ArrayInitialize(prom,0);
   hd=FileOpen(histfn,FILE_CSV|FILE_READ); 
   if(hd<0){
     readinet(histfn,histfn);
   }else{ 
     if(FileSize(hd)<500){readinet(histfn,histfn);} 
     hd=FileOpen(histfn,FILE_CSV|FILE_READ|FILE_WRITE,",");
     int razm=FileSize(hd);
     int pos;
     int x=0,y=0;
     for(i=0;i<razm;i++){
       hist[x][y]=NormalizeDouble(StrToDouble(FileReadString(hd)),4);
       if(FileIsLineEnding(hd)){x++;y=0;}else{y++;}
       if(FileIsEnding(hd)){break;}    
     }
     
     ArrayResize(hist,x);
     if(hist[x][0]==TimeCurrent()){
       FileClose(hd);
       return(false);
     }
     for(i=0;i<15;i++){for(y=0;y<4;y++){a++;prom[a]=in[i][y];}}
     prom[0]=TimeCurrent();
     for(i=1;i<62;i++){
       if(NormalizeDouble(prom[i],4)!=NormalizeDouble(hist[x-2][i],4)){write=true;break;}
     }
     if(write){
       FileSeek(hd,0,SEEK_END);
       FileWrite(hd,prom[0],prom[1],prom[2],prom[3],prom[4],prom[5],prom[6],prom[7],prom[8],prom[9]
                   ,prom[10],prom[11],prom[12],prom[13],prom[14],prom[15],prom[16],prom[17],prom[18],prom[19]
                   ,prom[20],prom[21],prom[22],prom[23],prom[24],prom[25],prom[26],prom[27],prom[28],prom[29]
                   ,prom[30],prom[31],prom[32],prom[33],prom[34],prom[35],prom[36],prom[37],prom[38],prom[39]
                   ,prom[40],prom[41],prom[42],prom[43],prom[44],prom[45],prom[46],prom[47],prom[48],prom[49]
                   ,prom[50],prom[51],prom[52],prom[53],prom[54],prom[55],prom[56],prom[57],prom[58],prom[59]
                   ,prom[60]);
       FileClose(hd);
       return(true);                     
     }
     FileClose(hd);
     return(false);     
   }
  return(false); 
}
//+------------------------------------------------------------------+  
bool ReadFile(){int i,x,y;int hd;
   string fn=Symbol()+".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);   
     return(false);
   }else{
   for(i=0;i<50;i++){
     string out=FileReadString(hd);
     for(x=0;x<4;x++){
     int pos=StringFind(out,",",0);
     in[i][x]=StrToDouble(StringSubstr(out,0,pos+1));
     out=StringSubstr(out,pos+1,0);
     }
     if(FileIsEnding(hd)){break;}
   }  
   ArrayResize(in,i);
   FileClose(hd);
   return(true);
   }
   return(true);
}   
//+------------------------------------------------------------------+  

Comments