Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

SuperTrend MULTI TIME FRAME FOR MT4 for Amibroker (AFL)

Copy & Paste Friendly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#property copyright "Copyright © 2011, Xaphod"
#property link      "http://wwww.xaphod.com"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 DimGray
#property indicator_color2 OrangeRed
#property indicator_color3 MediumSeaGreen
#property indicator_width1 1
#property indicator_width2 2
#property indicator_width3 2
#property indicator_style1 STYLE_DOT
#property indicator_maximum 1
#property indicator_minimum 0
 
 
#define INDICATOR_NAME "xSuperTrend MTF"
#define INDICATOR_VERSION "v1.01, www.xaphod.com"
 
// Indicator parameters
extern string Version_Info=INDICATOR_VERSION;
extern string SuperTrend_Settings="——————————————————————————————";
extern int    SuperTrend_Period    = 10;  // SuperTrend ATR Period
extern double SuperTrend_Multiplier= 1.7; // SuperTrend Multiplier
extern int    SuperTrend_TimeFrame = 0;   // SuperTrend Timeframe
extern bool   SuperTrend_AutoTF    = True;    // Select next higher TF. If TF=M15 then H1 is selected
extern bool   alertsOn             = False;
extern bool   alertsOnCurrentBar   = true;
extern bool   alertsMessage        = true;
extern bool   alertsSound          = false;
extern bool   alertsEmail          = false;
 
 
// Global module varables
double gadUpBuf[];
double gadDnBuf[];
double gadSuperTrend[];
int giTimeFrame;
int giRepaintBars;
 
//-----------------------------------------------------------------------------
// function: init()
// Description: Custom indicator initialization function.
//-----------------------------------------------------------------------------
int init() {
  // Set buffers
  SetIndexStyle(0, DRAW_LINE);
  SetIndexBuffer(0, gadSuperTrend);
  SetIndexLabel(0, "SuperTrend");
  SetIndexStyle(1, DRAW_LINE);
  SetIndexBuffer(1, gadDnBuf);
  SetIndexLabel(1, "SuperTrend Down");
  SetIndexStyle(2, DRAW_LINE);
  SetIndexBuffer(2, gadUpBuf);
  SetIndexLabel(2, "SuperTrend Up");
   
  // Set Timeframe
  if (SuperTrend_TimeFrame==0)
    SuperTrend_TimeFrame=Period();
  if (SuperTrend_AutoTF==True)
    SuperTrend_TimeFrame=NextHigherTF(Period());
   
  // Calculation call via iCustom
  if (SuperTrend_AutoTF==False && SuperTrend_Settings=="") {
    giRepaintBars=0;
  }
  // Higher Time-frame
  else if (SuperTrend_TimeFrame!=Period()) {
    IndicatorShortName(INDICATOR_NAME+" "+TF2Str(SuperTrend_TimeFrame) +"["+SuperTrend_Period+";"+DoubleToStr(SuperTrend_Multiplier,1)+"]");
    giRepaintBars=SuperTrend_TimeFrame/Period()*2+1;
  }
  // Current Time-frame
  else {
    IndicatorShortName(INDICATOR_NAME+" "+TF2Str(SuperTrend_TimeFrame) +"["+SuperTrend_Period+";"+DoubleToStr(SuperTrend_Multiplier,1)+"]");
    giRepaintBars=0;
  }  
 
  return(0);
}
 
 
//-----------------------------------------------------------------------------
// function: deinit()
// Description: Custom indicator deinitialization function.
//-----------------------------------------------------------------------------
int deinit() {
   return (0);
}
 
 
///-----------------------------------------------------------------------------
// function: start()
// Description: Custom indicator iteration function.
//-----------------------------------------------------------------------------
int start() {
  int iNewBars, iCountedBars, i;
  double dAtr,dUpperLevel, dLowerLevel; 
   
  // Get unprocessed ticks
  iCountedBars=IndicatorCounted();
  if(iCountedBars < 0) return (-1);
  if(iCountedBars>0) iCountedBars--;
  iNewBars=MathMax(giRepaintBars,Bars-iCountedBars);
   
  for(i=iNewBars; i>=0; i--) {
    // Calc SuperTrend Locally
    if (SuperTrend_TimeFrame==Period()) {
      dAtr = iATR(NULL, 0, SuperTrend_Period, i);
      dUpperLevel=(High[i]+Low[i])/2+SuperTrend_Multiplier*dAtr;
      dLowerLevel=(High[i]+Low[i])/2-SuperTrend_Multiplier*dAtr;
     
      // Set supertrend levels
      if (Close[i]>gadSuperTrend[i+1] && Close[i+1]<=gadSuperTrend[i+1]) {
        gadSuperTrend[i]=dLowerLevel;
      }
      else if (Close[i]<gadSuperTrend[i+1] && Close[i+1]>=gadSuperTrend[i+1]) {
        gadSuperTrend[i]=dUpperLevel;
      }
      else if (gadSuperTrend[i+1]<dLowerLevel)
         gadSuperTrend[i]=dLowerLevel;
      else if (gadSuperTrend[i+1]>dUpperLevel)
        gadSuperTrend[i]=dUpperLevel;
      else
        gadSuperTrend[i]=gadSuperTrend[i+1];
       
      // Draw Histo
      gadUpBuf[i]=EMPTY_VALUE;
      gadDnBuf[i]=EMPTY_VALUE;
      if (Close[i]>gadSuperTrend[i] || (Close[i]==gadSuperTrend[i] && Close[i+1]>gadSuperTrend[i+1]))
        gadUpBuf[i]=gadSuperTrend[i];
      else if (Close[i]<gadSuperTrend[i] || (Close[i]==gadSuperTrend[i] && Close[i+1]<gadSuperTrend[i+1]))
        gadDnBuf[i]=gadSuperTrend[i];
         
    }
    // Calc higher TF SuperTrend via iCustom
    else {
      gadUpBuf[i]=EMPTY_VALUE;
      gadDnBuf[i]=EMPTY_VALUE;
      gadSuperTrend[i]=iCustom(Symbol(),SuperTrend_TimeFrame,WindowExpertName(),"","",SuperTrend_Period,
                        SuperTrend_Multiplier,SuperTrend_TimeFrame,False,alertsOn,alertsOnCurrentBar,alertsMessage,alertsSound,alertsEmail,0,iBarShift(Symbol(), SuperTrend_TimeFrame, Time[i]));
      gadDnBuf[i]=iCustom(Symbol(),SuperTrend_TimeFrame,WindowExpertName(),"","",SuperTrend_Period,
                        SuperTrend_Multiplier,SuperTrend_TimeFrame,False,alertsOn,alertsOnCurrentBar,alertsMessage,alertsSound,alertsEmail,1,iBarShift(Symbol(), SuperTrend_TimeFrame, Time[i]));
      gadUpBuf[i]=iCustom(Symbol(),SuperTrend_TimeFrame,WindowExpertName(),"","",SuperTrend_Period,
                        SuperTrend_Multiplier,SuperTrend_TimeFrame,False,alertsOn,alertsOnCurrentBar,alertsMessage,alertsSound,alertsEmail,2,iBarShift(Symbol(), SuperTrend_TimeFrame, Time[i]));                                               
       
    
  }
  if (SuperTrend_TimeFrame==Period()) manageAlerts();
   
  return(0);
}
//+------------------------------------------------------------------+
 
 
//-----------------------------------------------------------------------------
// function: TF2Str()
// Description: Convert time-frame to a string
//-----------------------------------------------------------------------------
string TF2Str(int iPeriod) {
  switch(iPeriod) {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN1");
    default: return("M"+iPeriod);
  }
}
 
 
//-----------------------------------------------------------------------------
// function: NextHigherTF()
// Description: Select the next higher time-frame.
//              Note: M15 and M30 both select H1 as next higher TF.
//-----------------------------------------------------------------------------
int NextHigherTF(int iPeriod) {
  if (iPeriod==0) iPeriod=Period();
  switch(iPeriod) {
    case PERIOD_M1: return(PERIOD_M5);
    case PERIOD_M5: return(PERIOD_M15);
    case PERIOD_M15: return(PERIOD_H1);
    case PERIOD_M30: return(PERIOD_H1);
    case PERIOD_H1: return(PERIOD_H4);
    case PERIOD_H4: return(PERIOD_D1);
    case PERIOD_D1: return(PERIOD_W1);
    case PERIOD_W1: return(PERIOD_MN1);
    case PERIOD_MN1: return(PERIOD_MN1);
    default: return(Period());
  }
}
 
//-------------------------------------------------------------------
//                                                                 
//-------------------------------------------------------------------
//
//
//
//
//
 
void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrentBar)
           int whichBar = 0;
      else     whichBar = 1;
      if ((gadUpBuf[whichBar] != EMPTY_VALUE && gadUpBuf[whichBar+1] == EMPTY_VALUE) || (gadDnBuf[whichBar]!=EMPTY_VALUE && gadDnBuf[whichBar+1]==EMPTY_VALUE))
      {
         if (gadUpBuf[whichBar] != EMPTY_VALUE) doAlert(whichBar,"up");
         if (gadDnBuf[whichBar] != EMPTY_VALUE) doAlert(whichBar,"down");
      }        
   }
}
 
//
//
//
//
//
 
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 =  TF2Str(Period())+" "+Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" super trend signal changed to "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"super trend "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}
Back