//---------------------------------------------------------------------------------------- //| cubic spline.mq4 //| mladen //---------------------------------------------------------------------------------------- #property copyright "mladen" #property link "www.forex-tsd.com" #property indicator_chart_window #property indicator_buffers 2 #property indicator_color1 PaleVioletRed #property indicator_color2 PaleVioletRed #property indicator_width1 2 #property indicator_style2 STYLE_DOT // // // // // extern int PointsNumber = 5; extern int PointsIntialBarsSpacing = 10; extern string PointsPrefix = "CubicSpline1"; extern color PointsColor = PaleVioletRed; extern bool SaveAndRestorePoints = true; extern int ExtrapolateBars = 50; // // // // // double spline[]; double splinex[]; string fileName; //---------------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------------- // // // // // int init() { SetIndexBuffer(0,spline); SetIndexBuffer(1,splinex); PointsNumber = MathMax(PointsNumber,2); fileName = Symbol()+PointsPrefix+".csv"; if (SaveAndRestorePoints) restorePoints(); return(0); } // // // // // int deinit() { if (SaveAndRestorePoints) savePoints(); deletePoints(0); return(0); } //---------------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------------- // // // // // double x[]; double y[]; // // // // // int start() { int i,k,limit=0,end=Bars; // // // // // if (ArrayRange(x,0)!= PointsNumber) { ArrayResize(x,PointsNumber); ArrayResize(y,PointsNumber); } deletePoints(PointsNumber); // // // // // datetime lastTime; double price; for (int p = 0; p=end; i--) spline[i] = spline(PointsNumber,x,y,i); for(; i>=0; i--) spline[i] = EMPTY_VALUE; for(i=0; i=0; i--) { string name = ObjectName(i); if (StringSubstr(name,0,searchLength)==searchFor) if (StrToInteger(StringSubstr(name,searchLength)) >= startFromPoint) ObjectDelete(name); } } } //---------------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------------- // // // // // double iBarNo(datetime time) { if (time<=Time[0]) return(iBarShift(NULL,0,time)); else return((Time[0]-time)/(Period()*60)); } // // // // // double iBarPreviousTime(double x[], int p) { if (p==0) return(Time[0]); // // // // // int previousBar = x[p-1]+PointsIntialBarsSpacing; if (previousBar < 0) return(Time[0]-previousBar*Period()*60); return(Time[previousBar]); } //---------------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------------- // // // // // double b[]; double c[]; double d[]; // // // // // void calcCubicSplineCoeffs(int n, double x[], double y[]) { if (n<2) return; if (ArraySize(b)!= n) { ArrayResize(b,n); ArrayInitialize(b,0); ArrayResize(c,n); ArrayInitialize(c,0); ArrayResize(d,n); ArrayInitialize(d,0); } // // // // // if (n==2) { b[0] = (y[1]-y[0])/(x[1]-x[0]); b[1] = b[0]; c[0] = 0.0; c[1] = 0.0; d[0] = 0.0; d[1] = 0.0; return; } // // // // // d[0] = x[1]-x[0]; c[1] = sDiv(y[1]-y[0],d[0]); for (int i = 1; i<(n-1); i++) { d[i] = x[i+1]-x[i]; b[i] = 2.0*(d[i-1]+d[i]); c[i+1] = sDiv(y[i+1]-y[i],d[i]); c[i] = c[i+1]-c[i]; } b[0] = -d[0]; b[n-1] = -d[n-2]; c[0] = 0.0; c[n-1] = 0.0; // // // // // if (n > 3) { c[0] = sDiv(c[2],x[3]-x[1])-sDiv(c[1],x[2]-x[0]); c[n-1] = sDiv(c[n-2],x[n-1]-x[n-3])-sDiv(c[n-3],x[n-2]-x[n-4]); c[0] = sDiv(c[0]*d[0]*d[0],x[3]-x[0]); c[n-1] = sDiv(-c[n-1]*d[n-2]*d[n-2],x[n-1]-x[n-4]); } for (i = 1; i=0; i--) { dx=barNo-x[i]; if (dx>=0) break; } return (y[i]+dx*(b[i]+dx*(c[i]+dx*d[i]))); } double splinex(double& y[], double barNo) { return (y[0]+barNo*(b[0]+barNo*(c[0]+barNo*d[0]))); } //---------------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------------- // // // // // // void restorePoints() { int handle = FileOpen(fileName, FILE_CSV|FILE_READ,';'); if (handle < 1)return; // // // // // int current = 0; FileSeek(handle,0,SEEK_SET); while (!FileIsEnding(handle)) { double time = StrToDouble(FileReadString(handle)); double price = StrToDouble(FileReadString(handle)); string name = FileReadString(handle); FileReadString(handle); if (price!=0) { ObjectCreate(name,OBJ_ARROW,0,time,price); current++; } } // // // // // FileClose(handle); } // // // // // void savePoints() { string searchFor = PointsPrefix+":"; int searchLength = StringLen(searchFor); // // // // // double times[]; double prices[]; string names[]; for (int i = ObjectsTotal(); i>=0; i--) { string name = ObjectName(i); if (StringSubstr(name,0,searchLength)==searchFor) { int size = ArraySize(times); ArrayResize(times ,size+1); times[size] = ObjectGet(name,OBJPROP_TIME1); ArrayResize(prices,size+1); prices[size] = ObjectGet(name,OBJPROP_PRICE1); ArrayResize(names ,size+1); names[size] = name; } } // // // // // if (ArraySize(times)>0) { int handle = FileOpen(fileName,FILE_CSV|FILE_WRITE); if (handle < 1) return; for (i=0; i