stochastic + alma
//------------------------------------------------------------------
#property copyright "外汇EA之家,免费下载EA、指标"
#property link "http://www.eazhijia.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1clrDodgerBlue
#property indicator_color2clrSandyBrown
#property indicator_color3clrSandyBrown
#property indicator_color4clrRed
#property indicator_width13
#property indicator_width23
#property indicator_width33
#property indicator_style4STYLE_DOT
#property strict
//
//
//
//
//
enum enPrices
{
pr_close, // Close
pr_open, // Open
pr_high, // High
pr_low, // Low
pr_median, // Median
pr_typical, // Typical
pr_weighted, // Weighted
pr_average, // Average (high+low+open+close)/4
pr_medianb, // Average median body (open+close)/2
pr_tbiased, // Trend biased price
pr_haclose, // Heiken ashi close
pr_haopen , // Heiken ashi open
pr_hahigh, // Heiken ashi high
pr_halow, // Heiken ashi low
pr_hamedian, // Heiken ashi median
pr_hatypical,// Heiken ashi typical
pr_haweighted, // Heiken ashi weighted
pr_haaverage,// Heiken ashi average
pr_hamedianb,// Heiken ashi median body
pr_hatbiased // Heiken ashi trend biased price
};
enum enDoWhat
{
do_sto,// Make stochastic using alma as price filter
do_alm,// Make alma smoothed stochastic
do_sta // Make stochastic with alma signal line
};
enum enColorOn
{
clr_onSlope, // Change color on slope change
clr_onSignal // Change color on signal cross (only if alma signal line chosen)
};
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Time frame to use
extern int pperiod = 14; // Stochastic calculating period
extern int speriod = 14; // Stochastic slowing period
extern enPrices ppriceh = pr_high; // Price to use for high
extern enPrices ppricel = pr_low; // Price to use for low
extern enPrices ppricec = pr_close; // Price to use for close
extern int AlmaPeriod= 7; // Alma calculation length
extern double AlmaSigma = 8.0; // Alma sigma
extern double AlmaSample= 0.85; // Alma sample
extern enDoWhat doWhat = do_sto; // Make what?
extern enColorOn colorOn = clr_onSlope; // When should the color change?
extern int linesWidth=3; // Lines width
extern bool Interpolate = true; // Interpolate in multi time frame mode?
double buffer[];
double bufferda[];
double bufferdb[];
double signal[];
double trend[];
string indicatorFileName;
bool returnBars;
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
int init()
{
IndicatorBuffers(5);
SetIndexBuffer(0,buffer,INDICATOR_DATA); SetIndexStyle(0,EMPTY,EMPTY,linesWidth);
SetIndexBuffer(1,bufferda,INDICATOR_DATA); SetIndexStyle(1,EMPTY,EMPTY,linesWidth);
SetIndexBuffer(2,bufferdb,INDICATOR_DATA); SetIndexStyle(2,EMPTY,EMPTY,linesWidth);
SetIndexBuffer(3,signal,INDICATOR_DATA);
SetIndexBuffer(4,trend ,INDICATOR_CALCULATIONS);
indicatorFileName = WindowExpertName();
returnBars = TimeFrame==-99;
TimeFrame = MathMax(TimeFrame,_Period);
IndicatorShortName(timeFrameToString(TimeFrame)+" stochastic alma ("+(string)pperiod+","+(string)speriod+","+(string)AlmaPeriod+")");
return(0);
}
//
//
//
//
//
int start()
{
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
int limit=MathMin(Bars-counted_bars,Bars-2);
if (returnBars) { buffer = MathMin(limit+1,Bars-1); return(0); }
if (TimeFrame != Period())
{
if (trend==-1) CleanPoint(limit,bufferda,bufferdb);
for(int i=limit; i>=0; i--)
{
int y = iBarShift(NULL,TimeFrame,Time);
buffer = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,pperiod,speriod,ppriceh,ppricel,ppricec,AlmaPeriod,AlmaSigma,AlmaSample,doWhat,0,y);
signal = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,pperiod,speriod,ppriceh,ppricel,ppricec,AlmaPeriod,AlmaSigma,AlmaSample,doWhat,3,y);
trend = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,pperiod,speriod,ppriceh,ppricel,ppricec,AlmaPeriod,AlmaSigma,AlmaSample,doWhat,4,y);
bufferda = EMPTY_VALUE;
bufferdb = EMPTY_VALUE;
if (!Interpolate || (i>0 && y==iBarShift(NULL,TimeFrame,Time))) continue;
//
//
//
//
//
int n,k; datetime time = iTime(NULL,TimeFrame,y);
for(n = 1; (i+n)<Bars && Time >= time; n++) continue;
for(k = 1; k<n && (i+n)<Bars && (i+k)<Bars; k++)
{
buffer = buffer + (buffer - buffer) * k/n;
signal = signal + (signal - signal) * k/n;
}
}
for(int i=limit; i>=0; i--) if (trend == -1) PlotPoint(i,bufferda,bufferdb,buffer);
return(0);
}
//
//
//
//
//
if (trend==-1) CleanPoint(limit,bufferda,bufferdb);
for(int i=limit; i>=0; i--)
{
double priceh = getPrice(ppriceh,Open,Close,High,Low,i,0);
double pricel = getPrice(ppricel,Open,Close,High,Low,i,1);
double pricec = getPrice(ppricec,Open,Close,High,Low,i,2);
//
//
//
//
//
signal = EMPTY_VALUE;
bufferda = EMPTY_VALUE;
bufferdb = EMPTY_VALUE;
if (doWhat==do_sto)
{
priceh = iAlma(priceh,AlmaPeriod,AlmaSigma,AlmaSample,i,0);
pricel = iAlma(pricel,AlmaPeriod,AlmaSigma,AlmaSample,i,1);
pricec = iAlma(pricec,AlmaPeriod,AlmaSigma,AlmaSample,i,2);
buffer = NormalizeDouble(iStoch(pricec,priceh,pricel,pperiod,speriod,i),_Digits);
}
if (doWhat==do_alm)
buffer = iAlma(iStoch(pricec,priceh,pricel,pperiod,speriod,i),AlmaPeriod,AlmaSigma,AlmaSample,i,3);
if (doWhat==do_sta)
{
buffer = iStoch(pricec,priceh,pricel,pperiod,speriod,i);
signal = iAlma(buffer,AlmaPeriod,AlmaSigma,AlmaSample,i,3);
}
if (i<Bars-1)
{
trend = trend;
if (doWhat==do_sta && clr_onSignal)
{
if (buffer>signal) trend =1;
if (buffer<signal) trend = -1;
}
else
{
if (buffer>buffer) trend =1;
if (buffer<buffer) trend = -1;
}
if (trend == -1) PlotPoint(i,bufferda,bufferdb,buffer);
}
}
return(0);
}
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
double workSto[];
#define _hi 0
#define _lo 1
#define _re 2
#define _ma 3
#define _mi 4
double iStoch(double priceR, double priceH, double priceL, int period, int slowing, int i, int instanceNo=0)
{
if (ArrayRange(workSto,0)!=Bars) ArrayResize(workSto,Bars); i = Bars-i-1; instanceNo *= 5;
//
//
//
//
//
workSto = priceH;
workSto = priceL;
workSto = priceR;
workSto = priceH;
workSto = priceL;
for (int k=1; k<period && (i-k)>=0; k++)
{
workSto = MathMin(workSto,workSto);
workSto = MathMax(workSto,workSto);
}
double sumlow= 0.0;
double sumhigh = 0.0;
for(int k=0; k<slowing && (i-k)>=0; k++)
{
sumlow+= workSto-workSto;
sumhigh += workSto-workSto;
}
//
//
//
//
//
if(sumhigh!=0.0)
return(100.0*sumlow/sumhigh);
elsereturn(0);
}
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
#define almaInstances 4
doublealmaWork[];
doublealmaCoeffs[];
double iAlma(double price, int period, double sigma, double sample, int r, int instanceNo=0)
{
if (period<=1) return(price);
if (ArrayRange(almaWork,0)!=Bars) ArrayResize(almaWork,Bars); r = Bars-r-1; almaWork = price;
//
//
//
//
//
if (ArraySize(almaCoeffs)!=period) ArrayResize(almaCoeffs,period);
double m = MathFloor(sample * (period - 1.0));
double s = period/sigma;
for (int i=0; i<period; i++)
almaCoeffs = MathExp(-((i-m)*(i-m))/(2.0*s*s));
double sum=0;
double div=0;
for (int k=0; k<period && (r-k)>=0; k++)
{
sum += almaCoeffs*almaWork;
div += almaCoeffs;
}
double alma = price; if (div!=0) alma = sum/div;
return(alma);
}
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
void CleanPoint(int i,double& first[],double& second[])
{
if ((second!= EMPTY_VALUE) && (second != EMPTY_VALUE))
second = EMPTY_VALUE;
else
if ((first != EMPTY_VALUE) && (first != EMPTY_VALUE) && (first == EMPTY_VALUE))
first = EMPTY_VALUE;
}
//
//
//
//
//
void PlotPoint(int i,double& first[],double& second[],double& from[])
{
if (i>=Bars-2) return;
if (first == EMPTY_VALUE)
{
if (first == EMPTY_VALUE)
{ first= from; first= from; second = EMPTY_VALUE; }
else{ second = from; second = from; first= EMPTY_VALUE; }
}
else{ first = from; second = EMPTY_VALUE; }
}
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//
double workHa[];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
if (tprice>=pr_haclose)
{
if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars); instanceNo*=4;
int r = Bars-i-1;
//
//
//
//
//
double haOpen;
if (r>0)
haOpen= (workHa + workHa)/2.0;
else haOpen= (open+close)/2;
double haClose = (open + high + low + close) / 4.0;
double haHigh= MathMax(high, MathMax(haOpen,haClose));
double haLow = MathMin(low , MathMin(haOpen,haClose));
if(haOpen<haClose) { workHa = haLow;workHa = haHigh; }
else { workHa = haHigh; workHa = haLow;}
workHa = haOpen;
workHa = haClose;
//
//
//
//
//
switch (tprice)
{
case pr_haclose: return(haClose);
case pr_haopen: return(haOpen);
case pr_hahigh: return(haHigh);
case pr_halow: return(haLow);
case pr_hamedian: return((haHigh+haLow)/2.0);
case pr_hamedianb: return((haOpen+haClose)/2.0);
case pr_hatypical: return((haHigh+haLow+haClose)/3.0);
case pr_haweighted:return((haHigh+haLow+haClose+haClose)/4.0);
case pr_haaverage: return((haHigh+haLow+haClose+haOpen)/4.0);
case pr_hatbiased:
if (haClose>haOpen)
return((haHigh+haClose)/2.0);
elsereturn((haLow+haClose)/2.0);
}
}
//
//
//
//
//
switch (tprice)
{
case pr_close: return(close);
case pr_open: return(open);
case pr_high: return(high);
case pr_low: return(low);
case pr_median: return((high+low)/2.0);
case pr_medianb: return((open+close)/2.0);
case pr_typical: return((high+low+close)/3.0);
case pr_weighted:return((high+low+close+close)/4.0);
case pr_average: return((high+low+close+open)/4.0);
case pr_tbiased:
if (close>open)
return((high+close)/2.0);
elsereturn((low+close)/2.0);
}
return(0);
}
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
string sTfTable[] = {"M1","M5","M10","M15","M30","H1","H4","D1","W1","MN"};
int iTfTable[] = {1,5,10,15,30,60,240,1440,10080,43200};
string timeFrameToString(int tf)
{
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tf==iTfTable) return(sTfTable);
return("");
}
楼主辛苦了 LZ真是人才 过来看看的 路过,学习下 路过,支持一下啦 帮你顶下哈!! 谢谢楼主分享 帮你顶下哈!! 谢谢楼主分享
页:
[1]
2