-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakeven-indicator.mq5
259 lines (230 loc) · 8.94 KB
/
breakeven-indicator.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
#property copyright "yuu"
#property version "1.00"
#property indicator_chart_window
#property indicator_plots 0
input bool ignore_long = false;
input bool ignore_short = false;
input color line_color_buy = clrTeal;
input color line_color_sell = clrPink;
input color line_color_neutral = clrSlateGray;
input ENUM_LINE_STYLE line_style = STYLE_SOLID;
input int line_width = 1;
input color font_color = clrYellow;
input int font_size = 10;
input string font_face = "Courier";
input string object_prefix = "Y_INDICATOR_"; // 他のインジケーター/EAとの混同を防ぐため
// グローバル変数
int positions_long, positions_short, positions_total;
double volume_long, price_long, profit_long, volume_short, price_short, profit_short;
double volume_total, profit_total, price_average, distance_points;
double pip_value = 0;
double pip_size = 0;
double lot_step = 0;
int lot_step_digits = 0;
string object_line, object_label;
void OnInit()
{
object_line = object_prefix + "Line" + Symbol();
object_label = object_prefix + "Label" + Symbol();
EventSetMillisecondTimer(200); // 1秒間に5回
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, object_prefix);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if (iBars(Symbol(), Period()) <= 0) return 0; // まだロードされてない
if (CalculateData()) DrawData();
return rates_total;
}
//+------------------------------------------------------------------+
//| チャートイベントハンドラー |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
if (id == CHARTEVENT_CHART_CHANGE) DrawData();
else if (id == CHARTEVENT_KEYDOWN)
{
// 取引方向
if ((lparam == 'B') && (TerminalInfoInteger(TERMINAL_KEYSTATE_SHIFT) < 0))
{
if (ObjectGetInteger(0, object_line, OBJPROP_TIMEFRAMES) == OBJ_NO_PERIODS) // 非表示
{
ObjectSetInteger(0, object_line, OBJPROP_TIMEFRAMES, OBJ_ALL_PERIODS);
ObjectSetInteger(0, object_label, OBJPROP_TIMEFRAMES, OBJ_ALL_PERIODS);
}
else // 表示
{
ObjectSetInteger(0, object_line, OBJPROP_TIMEFRAMES, OBJ_NO_PERIODS);
ObjectSetInteger(0, object_label, OBJPROP_TIMEFRAMES, OBJ_NO_PERIODS);
}
ChartRedraw();
}
}
}
//+------------------------------------------------------------------+
//| 取引イベントハンドラー |
//+------------------------------------------------------------------+
void OnTrade()
{
if (CalculateData()) DrawData();
ChartRedraw();
}
void OnTimer()
{
/*if (CalculateData())*/ DrawData();
ChartRedraw();
}
bool CalculateData()
{
positions_long = 0;
volume_long = 0;
price_long = 0;
profit_long = 0;
positions_short = 0;
volume_short = 0;
price_short = 0;
profit_short = 0;
positions_total = 0;
volume_total = 0;
profit_total = 0;
price_average = 0;
distance_points = 0;
pip_value = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE);
pip_size = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE);
if ((pip_value == 0) || (pip_size == 0)) return false;
int total = PositionsTotal();
for (int i = 0; i < total; i++)
{
string pos_symbol = PositionGetSymbol(i);
if (pos_symbol != "")
{
if (pos_symbol != Symbol()) continue; // 現在のシンボルの取引のみを考慮
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
if (ignore_long) continue;
positions_long++;
price_long += PositionGetDouble(POSITION_PRICE_OPEN) * PositionGetDouble(POSITION_VOLUME);
volume_long += PositionGetDouble(POSITION_VOLUME);
ulong ticket = PositionGetInteger(POSITION_TICKET);
HistoryDealGetDouble(ticket, DEAL_COMMISSION);
profit_long += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP) + HistoryDealGetDouble(ticket, DEAL_COMMISSION);
}
else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
if (ignore_short) continue;
positions_short++;
price_short += PositionGetDouble(POSITION_PRICE_OPEN) * PositionGetDouble(POSITION_VOLUME);
volume_short += PositionGetDouble(POSITION_VOLUME);
ulong ticket = PositionGetInteger(POSITION_TICKET);
HistoryDealGetDouble(ticket, DEAL_COMMISSION);
profit_short += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP) + HistoryDealGetDouble(ticket, DEAL_COMMISSION);
}
}
else Print("ポジション #", i, " の選択エラー: ", GetLastError());
}
if (price_long > 0)
{
price_long /= volume_long; // 平均購入価格
}
if (price_short > 0)
{
price_short /= volume_short; // 平均売却価格
}
positions_total = positions_long + positions_short;
volume_total = volume_long - volume_short;
profit_total = profit_long + profit_short;
if (positions_total > 0)
{
double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
if (volume_total != 0)
{
distance_points = profit_total / MathAbs(volume_total * pip_value) * pip_size;
if (volume_total > 0)
{
price_average = Bid - distance_points;
}
else // volume_total < 0
{
price_average = Ask + distance_points;
}
}
else // volume_total == 0
{
distance_points = profit_total / pip_value * pip_size;
price_average = (Ask + Bid) / 2 - distance_points;
}
}
return true;
}
void DrawData()
{
// ライン
if (ObjectFind(0, object_line) < 0)
{
ObjectCreate(0, object_line, OBJ_HLINE, 0, 0, price_average);
ObjectSetInteger(0, object_line, OBJPROP_WIDTH, 0, line_width);
ObjectSetInteger(0, object_line, OBJPROP_STYLE, 0, line_style);
ObjectSetInteger(0, object_line, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, object_line, OBJPROP_HIDDEN, true);
}
else
{
ObjectMove(0, object_line, 0, 0, price_average);
}
color line_color = line_color_buy;
if (volume_total < 0) line_color = line_color_sell;
else if (volume_total == 0) line_color = line_color_neutral;
ObjectSetInteger(0, object_line, OBJPROP_COLOR, line_color);
// ラベル
if (ObjectFind(0, object_label) < 0)
{
ObjectCreate(0, object_label, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, object_label, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, object_label, OBJPROP_HIDDEN, true);
ObjectSetInteger(0, object_label, OBJPROP_FONTSIZE, font_size);
ObjectSetString(0, object_label, OBJPROP_FONT, font_face);
ObjectSetInteger(0, object_label, OBJPROP_COLOR, font_color);
// 一度だけ実行する
lot_step = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_STEP);
lot_step_digits = CountDecimalPlaces(lot_step);
}
int x, y;
long real_x;
uint w, h;
string text = IntegerToString((int)MathRound(distance_points / _Point)) + " pips (" + DoubleToString(profit_total, 2) + " " + AccountInfoString(ACCOUNT_CURRENCY) + ") " + DoubleToString(volume_total, lot_step_digits) + " lots, N = " + IntegerToString(positions_total);
ObjectSetString(0, object_label, OBJPROP_TEXT, text);
real_x = ChartGetInteger(0, CHART_WIDTH_IN_PIXELS) - 2;
// yのみに必要、xはチャートの幅から導出
ChartTimePriceToXY(0, 0, iTime(Symbol(), Period(), 0), price_average, x, y);
// フォントとそのサイズに基づくテキストの幅を取得。OSに依存するため負、ptの1/10で設定されるため*10
TextSetFont(font_face, font_size * -10);
TextGetSize(text, w, h);
ObjectSetInteger(0, object_label, OBJPROP_XDISTANCE, real_x - w);
y -= int(h + 1); // ラインの上
ObjectSetInteger(0, object_label, OBJPROP_YDISTANCE, y);
}
int CountDecimalPlaces(double number)
{
// 数値の最大長として100
for (int i = 0; i < 100; i++)
{
double pwr = MathPow(10, i);
if (MathRound(number * pwr) / pwr == number) return(i);
}
return(-1);
}