-
Notifications
You must be signed in to change notification settings - Fork 3
/
BanBands RSI_v1.mq5
232 lines (164 loc) · 6.19 KB
/
BanBands RSI_v1.mq5
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
//+------------------------------------------------------------------+
//| Bands/RSI|
//| kassi antoine |
//| http://www.expertadvisorbook.com |
//+------------------------------------------------------------------+
#property copyright "kassi antoine"
#property link ""
#property description "trading system using Bollinger Bands and RSI"
/*
Creative Commons Attribution-NonCommercial 3.0 Unported
http://creativecommons.org/licenses/by-nc/3.0/
You may use this file in your own personal projects. You
may modify it if necessary. You may even share it, provided
the copyright above is present. No commercial use permitted.
*/
// Trade
#include <MyMql5Book\Trade.mqh>
CTrade Trade;
// Price
#include <MyMql5Book\Price.mqh>
CBars Bar;
// Money management
#include <MyMql5Book\MoneyManagement.mqh>
// Trailing stops
#include <MyMql5Book\TrailingStops.mqh>
CTrailing Trail;
// Timer
#include <MyMql5Book\Timer.mqh>
CTimer Timer;
CNewBar NewBar;
// Indicators
#include <MyMql5Book\Indicators.mqh>
CiBollinger Bands;
CiRSI RSI;
//+------------------------------------------------------------------+
//| Input variables |
//+------------------------------------------------------------------+
input ulong Slippage = 3;
input bool TradeOnNewBar = true;
sinput string MM; // Money Management
input bool UseMoneyManagement = true;
input double RiskPercent = 2;
input double FixedVolume = 0.1;
sinput string SL; // Stop Loss & Take Profit
input int StopLoss = 0;
input int TakeProfit = 0;
sinput string TS; // Trailing Stop
input bool UseTrailingStop = false;
input int TrailingStop = 0;
input int MinimumProfit = 0;
input int Step = 0;
sinput string BE; // Break Even
input bool UseBreakEven = false;
input int BreakEvenProfit = 0;
input int LockProfit = 0;
sinput string BB; // Bollinger Bands
input int BandsPeriod = 20;
input int BandsShift = 0;
input double BandsDeviation = 2;
input ENUM_APPLIED_PRICE BandsPrice = PRICE_CLOSE;
sinput string RS; // RSI
input int RSIPeriod = 8;
input ENUM_APPLIED_PRICE RSIPrice = PRICE_CLOSE;
sinput string TI; // Timer
input bool UseTimer = false;
input int StartHour = 0;
input int StartMinute = 0;
input int EndHour = 0;
input int EndMinute = 0;
input bool UseLocalTime = false;
//+------------------------------------------------------------------+
//| Global variables |
//+------------------------------------------------------------------+
bool glBuyPlaced, glSellPlaced;
enum Signal
{
SIGNAL_BUY,
SIGNAL_SELL,
SIGNAL_NONE,
};
Signal glSignal;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
Bands.Init(_Symbol,_Period,BandsPeriod,BandsShift,BandsDeviation,BandsPrice);
RSI.Init(_Symbol,_Period,RSIPeriod,RSIPrice);
Trade.Deviation(Slippage);
return(0);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check for new bar
bool newBar = true;
int barShift = 0;
if(TradeOnNewBar == true)
{
newBar = NewBar.CheckNewBar(_Symbol,_Period);
barShift = 1;
}
// Timer
bool timerOn = true;
if(UseTimer == true)
{
timerOn = Timer.DailyTimer(StartHour,StartMinute,EndHour,EndMinute,UseLocalTime);
}
// Update prices
Bar.Update(_Symbol,_Period);
// Order placement
if(newBar == true && timerOn == true)
{
// Money management
double tradeSize;
if(UseMoneyManagement == true) tradeSize = MoneyManagement(_Symbol,FixedVolume,RiskPercent,StopLoss);
else tradeSize = VerifyVolume(_Symbol,FixedVolume);
if(Bar.Close(barShift) < Bands.Lower(barShift) && RSI.Main(barShift) < 30) glSignal = SIGNAL_BUY;
else if(Bar.Close(barShift) > Bands.Upper(barShift) && RSI.Main(barShift) > 70) glSignal = SIGNAL_SELL;
// Open buy order
//use the last current and close price with the upper or lower of the bb bands. barShift+1: means the previous bar
if(glSignal == SIGNAL_BUY && Bar.Close(barShift) > Bands.Lower(barShift) && Bar.Close(barShift+1) <= Bands.Lower(barShift+1))
{
glBuyPlaced = Trade.Buy(_Symbol,tradeSize);
if(glBuyPlaced == true)
{
double openPrice = PositionOpenPrice(_Symbol);
double buyStop = BuyStopLoss(_Symbol,StopLoss,openPrice);
if(buyStop > 0) AdjustBelowStopLevel(_Symbol,buyStop);
double buyProfit = BuyTakeProfit(_Symbol,TakeProfit,openPrice);
if(buyProfit > 0) AdjustAboveStopLevel(_Symbol,buyProfit);
if(buyStop > 0 || buyProfit > 0) Trade.ModifyPosition(_Symbol,buyStop,buyProfit);
glSignal = SIGNAL_NONE;
}
}
// Open sell order
if(glSignal == SIGNAL_SELL && Bar.Close(barShift) < Bands.Upper(barShift) && Bar.Close(barShift+1) >= Bands.Upper(barShift+1))
{
glSellPlaced = Trade.Sell(_Symbol,tradeSize);
if(glSellPlaced == true)
{
double openPrice = PositionOpenPrice(_Symbol);
double sellStop = SellStopLoss(_Symbol,StopLoss,openPrice);
if(sellStop > 0) sellStop = AdjustAboveStopLevel(_Symbol,sellStop);
double sellProfit = SellTakeProfit(_Symbol,TakeProfit,openPrice);
if(sellProfit > 0) sellProfit = AdjustBelowStopLevel(_Symbol,sellProfit);
if(sellStop > 0 || sellProfit > 0) Trade.ModifyPosition(_Symbol,sellStop,sellProfit);
glSignal = SIGNAL_NONE;
}
}
} // Order placement end
// Break even
if(UseBreakEven == true && PositionType(_Symbol) != -1)
{
Trail.BreakEven(_Symbol,BreakEvenProfit,LockProfit);
}
// Trailing stop
if(UseTrailingStop == true && PositionType(_Symbol) != -1)
{
Trail.TrailingStop(_Symbol,TrailingStop,MinimumProfit,Step);
}
}