//+------------------------------------------------------------------+ //| cot.mq4 | //| | //| | //+------------------------------------------------------------------+ /* COT index formula: The COT index is stochastic oscillator of Net. Net = Positions long - Positions short. WILLCO index formula: The WILLCO index is stochastic oscillator of %Net. %Net = 100%*(Positions long - Positions short)/OI. The OI is open interest. OI index formula: The OI index is stochastic oscillator of OI. Movement index formula: Movement index = COT(t) - COT(t+n). The n is a rate of change for Movement index. The t is an interval. The COT(t) is current value of COT index. The COT(t+n) is value of COT index n weeks ago. Spread movement index formula: Spread movement index = Movement index - OI Movement index. Movement index = COT(t) - COT(t+n). OI Movement index = OI index(t) - OI index(t+n). The n is a rate of change for Spread movement index. The t is an interval. The COT(t) is current value of COT index. The OI index(t) is current value of OI index. The COT(t+n) is value of COT index n weeks ago. The OI index(t+n) is value of OI index n weeks ago. How to begin to work? The indicator uses historical compressed files type of annual.txt, or annualof.txt for categories "Futures Only", or "Futures-and-Options-Combined". These files have identical names for each year. Therefore, you need to rename these files. For example, you want to use 5 files type of annual.txt. The file annual.txt for current year must be renamed in year.txt The name "year" is the number of current year. For example, renamed files can have such names: 2015.txt, 2014.txt, 2013.txt, 2012.txt, 2011.txt. Then, place these files into the folder Files/MQL4. Now, the indicator is ready for a work. Maximal number of used files type of year.txt is 50 for this version. How to update files code.bin every week? The indicator creates files type of code.bin. The name "code" is market's code. So as to update these files, do next steps: 1. Update the file year.txt for current year. 2. Open the window "Global Variables", choose the tab "Tools", or press the key F3. 3. Change the value of variable "Update files code.bin" from 0 to 1. 4. Close the window "Global Variables". 5. Update the chart, choose the tab "Charts" and press "Refresh". After that, all of files code.bin will be updated. */ #property strict #property indicator_separate_window #property indicator_buffers 6 #property indicator_color1 LimeGreen #property indicator_color2 DodgerBlue #property indicator_color3 OrangeRed #property indicator_color4 DarkGray #property indicator_color5 LightSlateGray #property indicator_color6 LightSlateGray #property indicator_style1 STYLE_SOLID #property indicator_style2 STYLE_SOLID #property indicator_style3 STYLE_SOLID #property indicator_style4 STYLE_SOLID #property indicator_style5 STYLE_DOT #property indicator_style6 STYLE_DOT #property indicator_width1 2 #property indicator_width2 2 #property indicator_width3 2 #property indicator_width4 2 #property indicator_width5 1 #property indicator_width6 1 #property indicator_levelcolor LightSlateGray #property indicator_levelstyle STYLE_DOT //---- indicator parameters input string Code="xxxxxx"; input int period_0=26; // Period of COT index and WILLCO index input int period_1=52; // Period of OI index input int period_2=6; // Rate of change for Movement index and Spread movement index //+------------------------------------------------------------------+ //| Indicators | //+------------------------------------------------------------------+ enum id { COT, // COT index WILLCO, // WILLCO index Spread_move, // Spread movement index Movement, // Movement index Open_interest, // Open interest Net, // Net = Long - Short Long, // Long Short, // Short Pct_net, // % Net = 100% * Net / Open interest Pct_long, // % Long = 100% * Long / Open interest Pct_short, // % Short = 100% * Short / Open interest Change_Long, // Change Long Change_Short // Change Short }; input id Indicator=COT; input bool OI_index=false; // OI index input bool Large=false; // Noncommercial input bool Comm=true; // Commercial input bool Specs=false; // Nonreportable input bool Inverted=false; // Inverted chart input bool Histogram=false; //---- bool work=true; double zi=-1; double l0=100; double l1=0; double Buffer0[],Buffer1[],Buffer2[],Buffer3[],Buffer4[],Buffer5[],E1[],E2[],E3[],az; int OI[],L1[],L2[],L3[],S1[],S2[],S3[],C1[],C2[],C3[],b,p0,p1,p2; datetime DT[]; string c,n,w; char j=2; uint x; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { if(_Periodk; i--) { h=(string)i+".txt"; if(FileIsExist(h)) { files[d]=h; d++; } else if(d>0) break; } //--- if(d==0) { Alert("There are not files year.txt in the folder Files/MQL4!"); GlobalVariableSet(n,0); work=false; return(rates_total); } //--- e=d*432; ArrayResize(y,e,e); //--- column for a reading string column1="\"CFTC Contract Market Code\""; string column2="\"As of Date in Form YYYY-MM-DD\""; string column3="\"Open Interest (All)\""; string column4="\"Noncommercial Positions-Long (All)\""; string column5="\"Noncommercial Positions-Short (All)\""; string column6="\"Commercial Positions-Long (All)\""; string column7="\"Commercial Positions-Short (All)\""; string column8="\"Nonreportable Positions-Long (All)\""; string column9="\"Nonreportable Positions-Short (All)\""; //--- to read files year.txt for(z=0; z20) continue; //--- to search column if(f<=18) { if(text[i]==column1) n1=i; if(text[i]==column2) n2=i; if(text[i]==column3) n3=i; if(text[i]==column4) n4=i; if(text[i]==column5) n5=i; if(text[i]==column6) n6=i; if(text[i]==column7) n7=i; if(text[i]==column8) n8=i; if(text[i]==column9) n9=i; if(f==18) { if(n1==0 || n2==0 || n3==0 || n4==0 || n5==0 || n6==0 || n7==0 || n8==0 || n9==0) { v=" "; if(n1==0) v+="\n"+column1; if(n2==0) v+="\n"+column2; if(n3==0) v+="\n"+column3; if(n4==0) v+="\n"+column4; if(n5==0) v+="\n"+column5; if(n6==0) v+="\n"+column6; if(n7==0) v+="\n"+column7; if(n8==0) v+="\n"+column8; if(n9==0) v+="\n"+column9; Alert("These column are not found in the file "+files[z]+":"+"\n"+v); work=false; break; } } f++; } //--- if(i<5) { k=StringLen(text[i])-2; if(StringFind(text[i],"\"",k)!=-1) r=i-1; } //--- if(i<20) continue; v=StringTrimRight(text[n1+r]); u=StringTrimLeft(v); if(e==0) { if(u!=c) continue; e=1; if(g==0) { h=text[1]; if(r>0) h+=", "+text[2]; } } else if(u!=c) break; //--- v=StringTrimRight(text[n2+r]); // Date in form yyyy-mm-dd u=StringTrimLeft(v); v=StringSubstr(u,0,4)+"."+StringSubstr(u,5,2)+"."+StringSubstr(u,8,2); // Date in form yyyy.mm.dd s=(int)StrToTime(v); if(s>t) { y[g]=s; g++; // Date y[g]=StrToInteger(text[n3+r]); g++; // Open interest y[g]=StrToInteger(text[n4+r]); g++; // Noncommercial positions-long y[g]=StrToInteger(text[n6+r]); g++; // Commercial positions-long y[g]=StrToInteger(text[n8+r]); g++; // Nonreportable positions-long y[g]=StrToInteger(text[n5+r]); g++; // Noncommercial positions-short y[g]=StrToInteger(text[n7+r]); g++; // Commercial positions-short y[g]=StrToInteger(text[n9+r]); g++; // Nonreportable positions-short } else { z=50; break; } } FileClose(handle); if(!work || (s!=0 && e==0)) break; } else { Alert("Error in FileOpen(). Line 447. Error code = ",GetLastError()); if(tx=="updated!") GlobalVariableSet(n,0); work=false; break; } //--- if(s==0) { Alert("Entered code "+c+" is not found in the file "+files[z]+"!"); work=false; break; } //--- if(g==0) { PrintFormat("The updating of the file "+n+" for "+h+" is not required!"); GlobalVariableSet(n,0); break; } //--- limitation of quantity of used files year.txt if(z==50) break; //--- used files year.txt o++; m+=files[z]+", "; if(o>9) { m+="\n"; o=0; } } //--- if(!work || g==0) return(rates_total); //--- to write data to the file code.bin handle=FileOpen(n,FILE_READ|FILE_WRITE|FILE_BIN); if(handle!=INVALID_HANDLE) { FileSeek(handle,0,SEEK_END); i=g-1; //--- do { x=FileWriteInteger(handle,y[i],INT_VALUE); if(x==0) break; i--; } while(i>=0); //--- FileClose(handle); //--- if(x>0) { if(tx=="created!") Alert("The file "+n+" for "+h+" has been "+tx+"\nUsed files: "+m); else PrintFormat("The file "+n+" for "+h+" has been "+tx); } else { Alert("Error in FileWriteInteger(). Line 579. The file "+n+" has been deleted. Error code = ",GetLastError()); GlobalVariableSet(n,0); work=false; FileDelete(n); return(rates_total); } } else { Alert("Error in FileOpen(). Line 571. Error code = ",GetLastError()); GlobalVariableSet(n,0); return(rates_total); } } //--- to read the file code.bin if(q==1 || j!=-1) { j=-1; GlobalVariableSet(n,0); if(FileIsExist(n)) { handle=FileOpen(n,FILE_READ|FILE_BIN); if(handle!=INVALID_HANDLE) { i=0; while(!FileIsEnding(handle)) { S3[i]=FileReadInteger(handle,INT_VALUE); // Nonreportable positions-short S2[i]=FileReadInteger(handle,INT_VALUE); // Commercial positions-short S1[i]=FileReadInteger(handle,INT_VALUE); // Noncommercial positions-short L3[i]=FileReadInteger(handle,INT_VALUE); // Nonreportable positions-long L2[i]=FileReadInteger(handle,INT_VALUE); // Commercial positions-long L1[i]=FileReadInteger(handle,INT_VALUE); // Noncommercial positions-long OI[i]=FileReadInteger(handle,INT_VALUE); // Open interest DT[i]=(datetime)FileReadInteger(handle,INT_VALUE); // Date C3[i]=L3[i]-S3[i]; // Nonreportable positions-net C2[i]=L2[i]-S2[i]; // Commercial positions-net C1[i]=L1[i]-S1[i]; // Noncommercial positions-net E3[i]=100.0*C3[i]/OI[i]; // % of open interest for nonreportable positions-net E2[i]=100.0*C2[i]/OI[i]; // % of open interest for commercial positions-net E1[i]=100.0*C1[i]/OI[i]; // % of open interest for noncommercial positions-net i++; } FileClose(handle); //--- permutation of data in backwards order datetime l; b=i; g=0; i--; do { e=S3[g]; S3[g]=S3[i]; S3[i]=e; e=S2[g]; S2[g]=S2[i]; S2[i]=e; e=S1[g]; S1[g]=S1[i]; S1[i]=e; e=L3[g]; L3[g]=L3[i]; L3[i]=e; e=L2[g]; L2[g]=L2[i]; L2[i]=e; e=L1[g]; L1[g]=L1[i]; L1[i]=e; e=OI[g]; OI[g]=OI[i]; OI[i]=e; l=DT[g]; DT[g]=DT[i]; DT[i]=l; e=C3[g]; C3[g]=C3[i]; C3[i]=e; e=C2[g]; C2[g]=C2[i]; C2[i]=e; e=C1[g]; C1[g]=C1[i]; C1[i]=e; g0=E3[g]; E3[g]=E3[i]; E3[i]=g0; g0=E2[g]; E2[g]=E2[i]; E2[i]=g0; g0=E1[g]; E1[g]=E1[i]; E1[i]=g0; g++; i--; } while(i>g); //--- if(ObjectFind("code")==-1) { ObjectCreate("code",OBJ_LABEL,0,0,0,0,0,0,0); ObjectSet("code",OBJPROP_XDISTANCE,-50); ObjectSet("code",OBJPROP_YDISTANCE,-50); } ObjectSetText("code",c,8,"Arial",CLR_NONE); } else { Alert("Error in FileOpen(). Line 618. Error code = ",GetLastError()); return(rates_total); } IndicatorShortName(w+" History: "+(string)b+" weeks. Last report: "+TimeToStr(DT[0],TIME_DATE)+" "); } else { Alert("There is not the file "+n+" in the folder Files/MQL4! For creating this file,\n"+ "change the value of global variable \""+n+"\" from 0 to 1 and then, refresh a chart"); return(rates_total); } } //--- calculations double g1=0; double g2=0; double g3=0; d=0; o=0; for(i=0; iDT[d+1+f]) break; } d+=f; //--- OI index if((Indicator<2 && OI_index && o==0) || Indicator==2) { e=ArrayMaximum(OI,p1,d); z=ArrayMinimum(OI,p1,d); s=OI[e]-OI[z]; if(s>0) g3=100.0*(OI[d]-OI[z])/s; } else g3=EMPTY_VALUE; //--- COT index if(Indicator!=1 && Indicator<4) { e=ArrayMaximum(C1,p0,d); z=ArrayMinimum(C1,p0,d); s=C1[e]-C1[z]; if(s>0) g0=100.0*(az-zi*(C1[d]-C1[z])/s); e=ArrayMaximum(C2,p0,d); z=ArrayMinimum(C2,p0,d); s=C2[e]-C2[z]; if(s>0) g1=100.0*(az-zi*(C2[d]-C2[z])/s); e=ArrayMaximum(C3,p0,d); z=ArrayMinimum(C3,p0,d); s=C3[e]-C3[z]; if(s>0) g2=100.0*(az-zi*(C3[d]-C3[z])/s); //--- Movement index if(Indicator==2 || Indicator==3) { r=d+p2; e=ArrayMaximum(C1,p0,r); z=ArrayMinimum(C1,p0,r); s=C1[e]-C1[z]; if(s>0) g0=g0-100.0*(az-zi*(C1[r]-C1[z])/s); e=ArrayMaximum(C2,p0,r); z=ArrayMinimum(C2,p0,r); s=C2[e]-C2[z]; if(s>0) g1=g1-100.0*(az-zi*(C2[r]-C2[z])/s); e=ArrayMaximum(C3,p0,r); z=ArrayMinimum(C3,p0,r); s=C3[e]-C3[z]; if(s>0) g2=g2-100.0*(az-zi*(C3[r]-C3[z])/s); //--- OI Movement index if(Indicator==2) { e=ArrayMaximum(OI,p0,r); z=ArrayMinimum(OI,p0,r); s=OI[e]-OI[z]; if(s>0) g3=g3-100.0*(OI[r]-OI[z])/s; if(zi==1) g3=-g3; //--- Spread movement index g0-=g3; g1-=g3; g2-=g3; } } } //--- WILLCO index if(Indicator==1) { e=ArrayMaximum(E1,p0,d); z=ArrayMinimum(E1,p0,d); q=E1[e]-E1[z]; if(q>0) g0=100.0*(az-zi*(E1[d]-E1[z])/q); e=ArrayMaximum(E2,p0,d); z=ArrayMinimum(E2,p0,d); q=E2[e]-E2[z]; if(q>0) g1=100.0*(az-zi*(E2[d]-E2[z])/q); e=ArrayMaximum(E3,p0,d); z=ArrayMinimum(E3,p0,d); q=E3[e]-E3[z]; if(q>0) g2=100.0*(az-zi*(E3[d]-E3[z])/q); } //--- OI if(Indicator==4) g3=-zi*OI[d]; //--- Net if(Indicator==5) { g0=-zi*C1[d]; g1=-zi*C2[d]; g2=-zi*C3[d]; } //--- Long if(Indicator==6) { g0=-zi*L1[d]; g1=-zi*L2[d]; g2=-zi*L3[d]; } //--- Short if(Indicator==7) { g0=-zi*S1[d]; g1=-zi*S2[d]; g2=-zi*S3[d]; } //--- % Net if(Indicator==8) { g0=-zi*E1[d]; g1=-zi*E2[d]; g2=-zi*E3[d]; } //--- % Long if(Indicator==9) { g0=-zi*100.0*L1[d]/OI[d]; g1=-zi*100.0*L2[d]/OI[d]; g2=-zi*100.0*L3[d]/OI[d]; } //--- % Short if(Indicator==10) { g0=-zi*100.0*S1[d]/OI[d]; g1=-zi*100.0*S2[d]/OI[d]; g2=-zi*100.0*S3[d]/OI[d]; } //--- Change Long if(Indicator==11) { g0=-zi*(L1[d]-L1[d+1]); g1=-zi*(L2[d]-L2[d+1]); g2=-zi*(L3[d]-L3[d+1]); } //--- Change Short if(Indicator==12) { g0=-zi*(S1[d]-S1[d+1]); g1=-zi*(S2[d]-S2[d+1]); g2=-zi*(S3[d]-S3[d+1]); } d++; } //--- if(d==0) continue; if(Indicator<4) { if(Large) Buffer0[i]=g0; if(Comm) Buffer1[i]=g1; if(Specs) Buffer2[i]=g2; Buffer4[i]=l0; Buffer5[i]=l1; if(Indicator<2) { if(OI_index) { Buffer3[i]=g3; if(d>b-p1) o=1; } if(d>b-p0) d=-1; } else if(d>b-p0-p2) d=-1; } else { if(Indicator>4) { if(Large) Buffer0[i]=g0; if(Comm) Buffer1[i]=g1; if(Specs) Buffer2[i]=g2; if(Indicator>10) if(d>=b-1) d=-1; } else Buffer3[i]=g3; if(d>=b) d=-1; } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+