- 金币:
-
- 奖励:
-
- 热心:
-
- 注册时间:
- 2001-2-25
|
|
//+------------------------------------------------------------------+
//| SuperTrend.mq4 v1.2 |
//| Copyright ?2012, Jason Robinson (jnrtrading). |
//|
//+------------------------------------------------------------------+
#property copyright "Copyright ?2012, Jason Robinson."
#property link "http://www.spreadtrade2win.com"
#property indicator_chart_window
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_buffers 2
double TrendUp[], TrendDown[];
int changeOfTrend;
extern int Nbr_Periods = 10;
extern double Multiplier = 3.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexBuffer(0, TrendUp);
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
SetIndexLabel(0, "Trend Up");
SetIndexBuffer(1, TrendDown);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
SetIndexLabel(1, "Trend Down");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, i, flag, flagh, trend[5000];
double up[5000], dn[5000], medianPrice, atr;
int counted_bars = IndicatorCounted();
//---- check for possible errors
if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
if(counted_bars > 0) counted_bars--;
limit=Bars-counted_bars;
//Print(limit);
//----
for (i = Bars; i >= 0; i--) {
TrendUp = EMPTY_VALUE;
TrendDown = EMPTY_VALUE;
atr = iATR(NULL, 0, Nbr_Periods, i);
//Print("atr: "+atr);
medianPrice = (High+Low)/2;
//Print("medianPrice: "+medianPrice);
up=medianPrice+(Multiplier*atr);
//Print("up: "+up);
dn=medianPrice-(Multiplier*atr);
//Print("dn: "+dn);
trend=1;
if (Close>up[i+1]) {
trend=1;
if (trend[i+1] == -1) changeOfTrend = 1;
//Print("trend: "+trend);
}
else if (Close<dn[i+1]) {
trend=-1;
if (trend[i+1] == 1) changeOfTrend = 1;
//Print("trend: "+trend);
}
else if (trend[i+1]==1) {
trend=1;
changeOfTrend = 0;
}
else if (trend[i+1]==-1) {
trend=-1;
changeOfTrend = 0;
}
if (trend<0 && trend[i+1]>0) {
flag=1;
//Print("flag: "+flag);
}
else {
flag=0;
//Print("flagh: "+flag);
}
if (trend>0 && trend[i+1]<0) {
flagh=1;
//Print("flagh: "+flagh);
}
else {
flagh=0;
//Print("flagh: "+flagh);
}
if (trend>0 && dn<dn[i+1])
dn=dn[i+1];
if (trend<0 && up>up[i+1])
up=up[i+1];
if (flag==1)
up=medianPrice+(Multiplier*atr);
if (flagh==1)
dn=medianPrice-(Multiplier*atr);
//-- Draw the indicator
if (trend==1) {
TrendUp=dn;
if (changeOfTrend == 1) {
TrendUp[i+1] = TrendDown[i+1];
changeOfTrend = 0;
}
}
else if (trend==-1) {
TrendDown=up;
if (changeOfTrend == 1) {
TrendDown[i+1] = TrendUp[i+1];
changeOfTrend = 0;
}
}
}
WindowRedraw();
//----
return(0);
}
//+------------------------------------------------------------------+ |
-
参与人数 1 | 奖励 +18 |
热心 +9 |
金币 +1 |
时间 |
理由
|
红树林园
| + 18 |
+ 9 |
+ 1 |
2014-7-27 14:09 |
謝謝代碼分享!祝周末愉快! |
查看全部评分
|
|