-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathIchimoku-Chikou-Cross.mq5
267 lines (232 loc) · 20.4 KB
/
Ichimoku-Chikou-Cross.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//+------------------------------------------------------------------+
//| Ichimoku Chikou Cross |
//| Copyright © 2016, EarnForex.com |
//| https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, EarnForex"
#property link "https://www.earnforex.com/metatrader-expert-advisors/Ichimoku-Chikou-Cross/"
#property version "1.01"
#property description "Trades using Ichimoku Kinko Hyo indicator."
#property description "Implements Chikou/Price cross strategy."
#property description "Chikou crossing price (close) from below is a bullish signal."
#property description "Chikou crossing price (close) from above is a bearish signal."
#property description "No SL/TP. Positions remain open from signal to signal."
#property description "Entry confirmed by current price above/below Kumo, latest Chikou outside Kumo."
#include <Trade/Trade.mqh>
#include <Trade/PositionInfo.mqh>
// Main input parameters
input int Tenkan = 9; // Tenkan line period. The fast "moving average".
input int Kijun = 26; // Kijun line period. The slow "moving average".
input int Senkou = 52; // Senkou period. Used for Kumo (Cloud) spans.
// Money management
input double Lots = 0.1; // Basic lot size.
input bool MM = false; // MM - If true - ATR-based position sizing.
input int ATR_Period = 20;
input double ATR_Multiplier = 1;
input double Risk = 2; // Risk - Risk tolerance in percentage points.
input double FixedBalance = 0; // FixedBalance - If greater than 0, position size calculator will use it instead of actual account balance.
input double MoneyRisk = 0; // MoneyRisk - Risk tolerance in base currency.
input bool UseMoneyInsteadOfPercentage = false;
input bool UseEquityInsteadOfBalance = false;
input int LotDigits = 2; // LotDigits - How many digits after dot supported in lot size. For example, 2 for 0.01, 1 for 0.1, 3 for 0.001, etc.
// Miscellaneous
input string OrderComment = "Ichimoku-Chikou-Cross";
input int Slippage = 100; // Tolerated slippage in brokers' pips.
// Main trading objects
CTrade *Trade;
CPositionInfo PositionInfo;
// Global variables
// Common
ulong LastBars = 0;
bool HaveLongPosition;
bool HaveShortPosition;
double StopLoss; // Not actual stop-loss - just a potential loss of MM estimation.
// Indicator handles
int IchimokuHandle;
int ATRHandle;
// Entry signals
bool ChikouPriceBull = false;
bool ChikouPriceBear = false;
bool KumoBullConfirmation = false;
bool KumoBearConfirmation = false;
bool KumoChikouBullConfirmation = false;
bool KumoChikouBearConfirmation = false;
// Buffers
double SenkouSpanA[];
double SenkouSpanB[];
double ChikouSpan[];
//+------------------------------------------------------------------+
//| Expert Initialization Function |
//+------------------------------------------------------------------+
void OnInit()
{
// Initialize the Trade class object
Trade = new CTrade;
Trade.SetDeviationInPoints(Slippage);
IchimokuHandle = iIchimoku(_Symbol, _Period, Tenkan, Kijun, Senkou);
ATRHandle = iATR(NULL, 0, ATR_Period);
}
//+------------------------------------------------------------------+
//| Expert Deinitialization Function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
delete Trade;
}
//+------------------------------------------------------------------+
//| Expert Every Tick Function |
//+------------------------------------------------------------------+
void OnTick()
{
if ((!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) || (!TerminalInfoInteger(TERMINAL_CONNECTED)) || (SymbolInfoInteger(_Symbol, SYMBOL_TRADE_MODE) != SYMBOL_TRADE_MODE_FULL)) return;
int bars = Bars(_Symbol, _Period);
// Trade only if new bar has arrived
if (LastBars != bars) LastBars = bars;
else return;
MqlRates rates[];
int copied = CopyRates(NULL, 0, 1, Kijun + 2, rates); // Will need pre-last bar and a two bars under latest closed Chikou line.
if (copied <= 0) Print("Error copying price data ", GetLastError());
if (CopyBuffer(IchimokuHandle, 2, 1, Kijun + 1, SenkouSpanA) != Kijun + 1) return;
if (CopyBuffer(IchimokuHandle, 3, 1, Kijun + 1, SenkouSpanB) != Kijun + 1) return;
// Kijun + 1 because we are not interested in latest "unclosed" Chikou bar, only two latest closed bars.
if (CopyBuffer(IchimokuHandle, 4, Kijun + 1, 2, ChikouSpan) != 2) return;
// Getting the potential loss value based on current ATR.
if (MM)
{
double ATR[1];
if (CopyBuffer(ATRHandle, 0, 1, 1, ATR) != 1) return;
StopLoss = ATR[0] * ATR_Multiplier;
}
// Chikou/Price Cross
// Bullish entry condition
if ((ChikouSpan[1] > rates[1].close) && (ChikouSpan[0] <= rates[0].close))
{
ChikouPriceBull = true;
ChikouPriceBear = false;
}
// Bearish entry condition
else if ((ChikouSpan[1] < rates[1].close) && (ChikouSpan[0] >= rates[0].close))
{
ChikouPriceBull = false;
ChikouPriceBear = true;
}
else if (ChikouSpan[1] == rates[1].close) // Voiding entry conditions if cross is ongoing.
{
ChikouPriceBull = false;
ChikouPriceBear = false;
}
// Kumo confirmation. When cross is happening current price (latest close) should be above/below both Senkou Spans, or price should close above/below both Senkou Spans after a cross.
if ((rates[Kijun + 1].close > SenkouSpanA[Kijun]) && (rates[Kijun + 1].close > SenkouSpanB[Kijun])) KumoBullConfirmation = true;
else KumoBullConfirmation = false;
if ((rates[Kijun + 1].close < SenkouSpanA[Kijun]) && (rates[Kijun + 1].close < SenkouSpanB[Kijun])) KumoBearConfirmation = true;
else KumoBearConfirmation = false;
// Kumo/Chikou confirmation. When cross is happening Chikou at its latest close should be above/below both Senkou Spans at that time, or it should close above/below both Senkou Spans after a cross.
if ((ChikouSpan[1] > SenkouSpanA[0]) && (ChikouSpan[1] > SenkouSpanB[0])) KumoChikouBullConfirmation = true;
else KumoChikouBullConfirmation = false;
if ((ChikouSpan[1] < SenkouSpanA[0]) && (ChikouSpan[1] < SenkouSpanB[0])) KumoChikouBearConfirmation = true;
else KumoChikouBearConfirmation = false;
GetPositionStates();
if (ChikouPriceBull)
{
if (HaveShortPosition) ClosePrevious();
if ((KumoBullConfirmation) && (KumoChikouBullConfirmation))
{
ChikouPriceBull = false;
fBuy();
}
}
else if (ChikouPriceBear)
{
if (HaveLongPosition) ClosePrevious();
if ((KumoBearConfirmation) && (KumoChikouBearConfirmation))
{
fSell();
ChikouPriceBear = false;
}
}
}
//+------------------------------------------------------------------+
//| Check what position is currently open |
//+------------------------------------------------------------------+
void GetPositionStates()
{
// Is there a position on this currency pair?
if (PositionInfo.Select(_Symbol))
{
if (PositionInfo.PositionType() == POSITION_TYPE_BUY)
{
HaveLongPosition = true;
HaveShortPosition = false;
}
else if (PositionInfo.PositionType() == POSITION_TYPE_SELL)
{
HaveLongPosition = false;
HaveShortPosition = true;
}
}
else
{
HaveLongPosition = false;
HaveShortPosition = false;
}
}
//+------------------------------------------------------------------+
//| Buy |
//+------------------------------------------------------------------+
void fBuy()
{
double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
Trade.PositionOpen(_Symbol, ORDER_TYPE_BUY, LotsOptimized(), Ask, 0, 0, OrderComment);
}
//+------------------------------------------------------------------+
//| Sell |
//+------------------------------------------------------------------+
void fSell()
{
double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
Trade.PositionOpen(_Symbol, ORDER_TYPE_SELL, LotsOptimized(), Bid, 0, 0, OrderComment);
}
//+------------------------------------------------------------------+
//| Calculate position size depending on money management parameters.|
//+------------------------------------------------------------------+
double LotsOptimized()
{
if (!MM) return (Lots);
double Size, RiskMoney, PositionSize = 0;
// If could not find account currency, probably not connected.
if (AccountInfoString(ACCOUNT_CURRENCY) == "") return(-1);
if (FixedBalance > 0)
{
Size = FixedBalance;
}
else if (UseEquityInsteadOfBalance)
{
Size = AccountInfoDouble(ACCOUNT_EQUITY);
}
else
{
Size = AccountInfoDouble(ACCOUNT_BALANCE);
}
if (!UseMoneyInsteadOfPercentage) RiskMoney = Size * Risk / 100;
else RiskMoney = MoneyRisk;
double UnitCost = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double TickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
if ((StopLoss != 0) && (UnitCost != 0) && (TickSize != 0)) PositionSize = NormalizeDouble(RiskMoney / (StopLoss * UnitCost / TickSize), LotDigits);
if (PositionSize < SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN)) PositionSize = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
else if (PositionSize > SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX)) PositionSize = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
return(PositionSize);
}
//+------------------------------------------------------------------+
//| Close open position |
//+------------------------------------------------------------------+
void ClosePrevious()
{
for (int i = 0; i < 10; i++)
{
Trade.PositionClose(_Symbol, Slippage);
if ((Trade.ResultRetcode() != 10008) && (Trade.ResultRetcode() != 10009) && (Trade.ResultRetcode() != 10010))
Print("Position Close Return Code: ", Trade.ResultRetcodeDescription());
else return;
}
}
//+------------------------------------------------------------------+