-
Notifications
You must be signed in to change notification settings - Fork 2
/
3-bar-stop-loss-indicator.mq4
145 lines (106 loc) · 3.84 KB
/
3-bar-stop-loss-indicator.mq4
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
//+------------------------------------------------------------------+
//| 3BarStopLoss.mq4 |
//| Copyright © 2009, Forex-Tools-Cafe.com |
//| http://www.forex-tools-cafe.com |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property indicator_chart_window
#property indicator_buffers 4
extern bool UseSpreadOffset=true;
extern int ManualOffset=4;
extern int MaxBars=2000;
extern int TrendEMAPeriod=2;
extern int lookback=3;
extern string S1="Line Style: 0=line,1=dash,2=dots";
extern int StopLineStyle=2;
extern int EntryLineStyle=0;
extern int EntryLineWidth=3;
extern color SellColor=Red;
extern color BuyColor=Blue;
double SellStop[];
double BuyEntry[];
double SellEntry[];
double BuyStop[];
int Trend;
int init()
{
// IndicatorBuffers(4);
SetIndexBuffer(0,SellStop );
SetIndexBuffer(1,BuyEntry );
SetIndexBuffer(2,SellEntry);
SetIndexBuffer(3,BuyStop );
SetIndexStyle(0,DRAW_LINE, StopLineStyle, 1 , SellColor );
SetIndexStyle(1,DRAW_LINE, EntryLineStyle, 1 , BuyColor );
SetIndexStyle(2,DRAW_LINE, EntryLineStyle, 1 , SellColor );
SetIndexStyle(3,DRAW_LINE, StopLineStyle, 1 , BuyColor );
return(0);
}
int deinit()
{
return(0);
}
int start()
{
int counted_bars=IndicatorCounted();
int limit = Bars-counted_bars ;
if ( limit > MaxBars ) limit = MaxBars;
SetIndexStyle(0,DRAW_LINE, StopLineStyle, 1 , SellColor );
SetIndexStyle(1,DRAW_LINE, EntryLineStyle, EntryLineWidth , BuyColor );
SetIndexStyle(2,DRAW_LINE, EntryLineStyle, EntryLineWidth , SellColor );
SetIndexStyle(3,DRAW_LINE, StopLineStyle, 1 , BuyColor );
int offset;
if ( UseSpreadOffset ) offset = MarketInfo(Symbol(), MODE_SPREAD );
else
offset = ManualOffset;
for(int i=limit; i>=0; i--)
{
double diff = iMA(Symbol(), 0, TrendEMAPeriod, 0, MODE_SMMA, PRICE_CLOSE, i)
- iMA(Symbol(), 0, TrendEMAPeriod+1, 0, MODE_SMMA, PRICE_CLOSE, i);
// double diff2 = iMA(Symbol(), 0, TrendEMAPeriod, 0, MODE_SMMA, PRICE_CLOSE, i+1)
// - iMA(Symbol(), 0, TrendEMAPeriod+1, 0, MODE_SMMA, PRICE_CLOSE, i+1);
if ( diff >= 0.0 ) // just crossed upward
{
Trend = 1;
}
else if ( diff < 0.0 )
{
Trend = -1;
}
if (false && i==14)
{
Comment("SellStop[i]: " + SellStop[i]
+ "\ndiff: " + diff
// + "\ndiff2: " + diff2
+ "\nBuyEntry[i]: " + BuyEntry[i]
+ "\nSellEntry[i]: " + SellEntry[i]
+ "\nBuyStop[i]: " + BuyStop[i]
);
}
if ( Trend == 1 )
{
int lo = ArrayMinimum(Low,lookback,i);
SellStop[i]=EMPTY_VALUE;
BuyEntry[i]=High[i+1];
SellEntry[i]=EMPTY_VALUE;
double sl = Low[lo]-(offset)*Point;
if ( BuyStop[i+1]!=EMPTY_VALUE && sl < BuyStop[i+1] )
BuyStop[i]= BuyStop[i+1];
else
BuyStop[i]= sl;
}
else if ( Trend == -1 )
{
int hi = ArrayMaximum(High,lookback,i);
double slsell = High[hi] + (offset)*Point;
if ( SellStop[i+1]!=EMPTY_VALUE && slsell > SellStop[i+1] )
SellStop[i]= SellStop[i+1];
else
SellStop[i] = slsell;
BuyEntry[i]=EMPTY_VALUE ;
SellEntry[i]=Low[i+1];
BuyStop[i]=EMPTY_VALUE;
}
}
return(0);
}