per fare un esempio
codice del DSS
//+------------------------------------------------------------------+
//| DSS Bressert.mq4 |
//| mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link "
[email protected]"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 DimGray
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_minimum 0
#property indicator_maximum 100
//
//
//
//
//
extern string TimeFrame = "current time frame";
extern int StochasticLength = 32;
extern int SmoothEMA = 9;
extern int SignalEMA = 5;
extern bool Interpolate = true;
extern bool ShowHigherTimeFrame = false;
extern bool ChangeOnDirectionChange = true;
//
//
//
//
//
extern bool alertsOn = false;
extern bool alertsOnCurrent = false;
extern bool alertsMessage = false;
extern bool alertsSound = false;
extern bool alertsEmail = false;
//
//
//
//
//
double dssBuffer[];
double ddaBuffer[];
double ddbBuffer[];
double sigBuffer[];
double st1Buffer[];
double ss1Buffer[];
double trend[];
//
//
//
//
//
string indicatorFileName;
bool returningBars = false;
bool calculatingDss = false;
int timeFrame = 0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
IndicatorBuffers(7);
SetIndexBuffer(0,dssBuffer);
SetIndexBuffer(1,ddaBuffer);
SetIndexBuffer(2,ddbBuffer);
SetIndexBuffer(3,sigBuffer);
SetIndexBuffer(4,st1Buffer);
SetIndexBuffer(5,ss1Buffer);
SetIndexBuffer(6,trend);
StochasticLength = MathMax(1,StochasticLength);
SignalEMA = MathMax(0,SignalEMA);
SmoothEMA = MathMax(0,SmoothEMA);
//
//
//
//
//
if (TimeFrame=="calculateDss")
{
calculatingDss = true;
return(0);
}
if (TimeFrame=="returnBars")
{
returningBars = true;
return(0);
}
if (SignalEMA<1) ChangeOnDirectionChange=true;
//
//
//
//
//
if (ShowHigherTimeFrame)
timeFrame = nextTimeFrame(Period());
else timeFrame = stringToTimeFrame(TimeFrame);
indicatorFileName = WindowExpertName();
IndicatorShortName("DSS Bressert "+timeFrameToString(timeFrame)+" ("+StochasticLength+","+SmoothEMA+","+SignalEMA+")");
return(0);
}
int deinit(){ return(0); }
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
int counted_bars = IndicatorCounted();
int limit,i;
if(counted_bars < 0) return(-1);
if(counted_bars > 0) counted_bars--;
limit = MathMin(Bars-counted_bars,Bars-1);
if (returningBars) { dssBuffer[0] = limit; return(0); }
if (calculatingDss) { calculateDss(limit); return(0); }
//
//
//
//
//
if (timeFrame > Period()) limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
//
//
//
//
//
if (trend[limit]==-1) CleanPoint(limit,ddaBuffer,ddbBuffer);
for(i = limit; i >= 0; i--)
{
int shift = iBarShift(NULL,timeFrame,Time
);
datetime time = iTime (NULL,timeFrame,shift);
ddaBuffer = EMPTY_VALUE;
ddbBuffer = EMPTY_VALUE;
dssBuffer = iCustom(NULL,timeFrame,indicatorFileName,"calculateDss",StochasticLength,SmoothEMA,SignalEMA,0,shift);
sigBuffer = iCustom(NULL,timeFrame,indicatorFileName,"calculateDss",StochasticLength,SmoothEMA,SignalEMA,3,shift);
trend = trend[i+1];
if (ChangeOnDirectionChange)
{
if (dssBuffer>dssBuffer[i+1]) trend = 1;
if (dssBuffer<dssBuffer[i+1]) trend = -1;
}
else
{
if (dssBuffer>sigBuffer[i+1]) trend = 1;
if (dssBuffer<sigBuffer[i+1]) trend = -1;
}
if(timeFrame <= Period() || shift==iBarShift(NULL,timeFrame,Time[i-1])) continue;
if(!Interpolate) continue;
//
//
//
//
//
for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
double factor = 1.0 / n;
for(int k = 1; k < n; k++)
{
dssBuffer[i+k] = k*factor*dssBuffer[i+n] + (1.0-k*factor)*dssBuffer;
sigBuffer[i+k] = k*factor*sigBuffer[i+n] + (1.0-k*factor)*sigBuffer;
}
}
for(i=limit; i>=0; i--) if (trend==-1) PlotPoint(i,ddaBuffer,ddbBuffer,dssBuffer);
//
//
//
//
//
if (alertsOn)
{
if (alertsOnCurrent)
int whichBar = 0;
else whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
if (trend[whichBar] != trend[whichBar+1])
if (trend[whichBar] == 1)
doAlert(whichBar,"up");
else doAlert(whichBar,"down");
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
void calculateDss(int limit)
{
double alpha1 = 2.0 / (1.0+SmoothEMA);
double alpha2 = 2.0 / (1.0+SignalEMA);
for(int i = limit; i>=0; i--)
{
double close = iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i);
//
//
//
//
//
double min = iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW ,i);
double max = iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i);
for (int k=1; k<StochasticLength; k++)
{
min = MathMin(min,iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW ,i+k));
max = MathMax(max,iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i+k));
}
st1Buffer = 0; if (min!=max) st1Buffer = 100*(close-min)/(max-min);
if (i==(Bars-1))
{
ss1Buffer = st1Buffer;
dssBuffer = st1Buffer;
sigBuffer = st1Buffer;
continue;
}
ss1Buffer = ss1Buffer[i+1]+alpha1*(st1Buffer-ss1Buffer[i+1]);
//
//
//
//
//
min = ss1Buffer;
max = ss1Buffer;
for (k=1; k<StochasticLength; k++)
{
min = MathMin(min,ss1Buffer[i+k]);
max = MathMax(max,ss1Buffer[i+k]);
}
double stoch = 0; if (min!=max) stoch = 100*(ss1Buffer-min)/(max-min);
//
//
//
//
//
dssBuffer = dssBuffer[i+1]+alpha1*(stoch -dssBuffer[i+1]);
if (SignalEMA>1) sigBuffer = sigBuffer[i+1]+alpha2*(dssBuffer-sigBuffer[i+1]);
}
}
//+-------------------------------------------------------------------
//|
//+-------------------------------------------------------------------
//
//
//
//
//
void CleanPoint(int i,double& first[],double& second[])
{
if ((second != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
second[i+1] = EMPTY_VALUE;
else
if ((first != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
first[i+1] = EMPTY_VALUE;
}
//
//
//
//
//
void PlotPoint(int i,double& first[],double& second[],double& from[])
{
if (first[i+1] == EMPTY_VALUE)
{
if (first[i+2] == EMPTY_VALUE) {
first = from;
first[i+1] = from[i+1];
second = EMPTY_VALUE;
}
else {
second = from;
second[i+1] = from[i+1];
first = EMPTY_VALUE;
}
}
else
{
first = from;
second = EMPTY_VALUE;
}
}
//+-------------------------------------------------------------------
//|
//+-------------------------------------------------------------------
//
//
//
//
//
void doAlert(int forBar, string doWhat)
{
static string previousAlert="nothing";
static datetime previousTime;
string message;
if (previousAlert != doWhat || previousTime != Time[forBar]) {
previousAlert = doWhat;
previousTime = Time[forBar];
//
//
//
//
//
message = StringConcatenate(Symbol(),timeFrameToString(timeFrame)+" at ",TimeToStr(TimeLocal(),TIME_SECONDS)," DSS trend changed to ",doWhat);
if (alertsMessage) Alert(message);
if (alertsEmail) SendMail(StringConcatenate(Symbol(),"DSS trend "),message);
if (alertsSound) PlaySound("alert2.wav");
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};
//
//
//
//
//
int stringToTimeFrame(string tfs)
{
tfs = StringUpperCase(tfs);
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tfs==sTfTable || tfs==""+iTfTable) return(MathMax(iTfTable,Period()));
return(Period());
}
string timeFrameToString(int tf)
{
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tf==iTfTable) return(sTfTable);
return("");
}
int nextTimeFrame(int tf)
{
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tf==iTfTable)
if ((i+1)>=ArraySize(iTfTable))
return(iTfTable);
else return(iTfTable[i+1]);
return(0);
}
//
//
//
//
//
string StringUpperCase(string str)
{
string s = str;
for (int length=StringLen(str)-1; length>=0; length--)
{
int _char = StringGetChar(s, length);
if((_char > 96 && _char < 123) || (_char > 223 && _char < 256))
s = StringSetChar(s, length, _char - 32);
else if(_char > -33 && _char < 0)
s = StringSetChar(s, length, _char + 224);
}
return(s);
}
e viene così
Vedi l'allegato 713790