-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.mq4
233 lines (177 loc) · 13.6 KB
/
template.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
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
#property copyright "Codinal Systems"
#property link "https://codinal-systems.com/"
#property version "1.00"
#property strict
//--------------------------------------------------------------------------------------------//
//口座番号
//記述例1 #define ACCOUNT_NUMBER 123456 //口座番号123456のみ動作
//記述例2 #define ACCOUNT_NUMBER 0 //0の場合は口座縛りなし
#define ACCOUNT_NUMBER 0
//制限期間
//""を忘れずに
//記述例1 #define EXPIRY_DATE "2022.12.31 00:00" //2022/12/31 00:00まで使用可能
//記述例2 #define EXPIRY_DATE "0" //0の場合は制限期間なし
#define EXPIRY_DATE "0"
//--------------------------------------------------------------------------------------------//
input string orderSetting = "---------------注文設定---------------"; //▼注文
input int magicNum = 123; //マジックナンバー
input int slippagePips = 20; //スリッページ
input double lots = 0.01; //Lotサイズ
input double slPips = 10; //SL(Pips)
input double tpPips = 10; //TP(Pips)
input double maxSpreadPips = 10; //許容スプレッド(Pips)
input int maxPosition = 5; //最大ポジション数
input string otherMargin=""; //
input string otherSetting = "---------------その他設定---------------"; //▼その他
input int retry = 5; //注文失敗時のリトライ回数
input int interval = 1000; //インターバル秒数(ms)
//--------------------------------------------------------------------------------------------//
int slippage;
datetime lastTime;
bool authed = false;
// EAの挿入時に行う処理を記述する
int OnInit(){
if (TimeCurrent() > StrToTime(EXPIRY_DATE) && EXPIRY_DATE != "0"){
Alert("It has expired.\n有効期限が切れています。");
return INIT_FAILED;
}
slippage = int(slippagePips * Pips() / Point);
EventSetMillisecondTimer(3000);
return INIT_SUCCEEDED;
}
// EAの削除時に行う処理を記述する
void OnDeinit(const int reason){
}
void OnTick(){
if (!IsTradeAllowed()) return;
// 毎tickごとに処理を行う
if (lastTime == iTime(Symbol(), Period(), 0)) return;
lastTime = iTime(Symbol(), Period(), 0);
// 新しい足形成時に一度だけ処理を行う
}
// 注文処理を記述する
void OrderOpen(){
}
// 決済処理を記述する
void OrderExit(){
}
// 〇秒毎に行う処理を記述する
void OnTimer(){
if(AccountNumber() != ACCOUNT_NUMBER && ACCOUNT_NUMBER != 0 && !authed){
Alert("Auth falied.\n許可されていない口座番号です。");
ExpertRemove();
authed = true;
}
EventSetMillisecondTimer(500);
}
// イベント処理を記述する
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam){
if (id == CHARTEVENT_OBJECT_CLICK){
}
}
// 成行買い注文
int OrderSendBuy(){
if (maxSpreadPips < MarketInfo(Symbol(), MODE_SPREAD) / 10) return -1;
int ticket = -1;
double sl = 0, tp = 0;
if (slPips != 0) sl = Ask - slPips * Pips();
if (tpPips != 0) tp = Ask + tpPips * Pips();
for (int retryCnt = 0; retryCnt < retry; retryCnt++){
ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, slippage, sl, tp, "", magicNum);
if (ticket != -1) {
break;
}else {
Print("buy order send error " + (string)GetLastError());
Sleep(interval);
RefreshRates();
}
}
return ticket;
}
// 成行売り注文
int OrderSendSell(){
if (maxSpreadPips < MarketInfo(Symbol(), MODE_SPREAD) / 10) return -1;
int ticket = 0;
double sl = 0, tp = 0;
if (slPips != 0) sl = Bid + slPips * Pips();
if (tpPips != 0) tp = Bid - tpPips * Pips();
for (int retryCnt = 0; retryCnt < retry; retryCnt++){
ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, slippage, sl, tp, "", magicNum);
if (ticket != -1) {
break;
}else {
Print("sell order send error " + (string)GetLastError());
Sleep(interval);
RefreshRates();
}
}
return ticket;
}
// 合計収支を取得する
double OrdersProfitTotal(int orderType){
double profit = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--){
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol() || OrderMagicNumber() != magicNum || OrderType() != orderType) continue;
profit += (OrderProfit() + OrderSwap() + OrderCommission());
}
return profit;
}
// 保有しているポジション数を返す
int OrdersCount(int orderType){
int cnt = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--){
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol() || OrderMagicNumber() != magicNum || OrderType() != orderType) continue;
cnt++;
}
return cnt;
}
// 全決済
void OrdersCloseAll(int orderType){
for(int i = OrdersTotal() - 1; i >= 0; i--){
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol() || OrderMagicNumber() != magicNum || OrderType() != orderType) continue;
for (int retryCnt = 0; retryCnt < retry; retryCnt++){
bool closed = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0);
if (closed){
break;
}else{
Print("order close error " + (string)GetLastError());
Sleep(interval);
RefreshRates();
}
}
}
}
// 1Pips辺りの価格を返す関数
double Pips(){
int digits = (int)MarketInfo(Symbol(), MODE_DIGITS);
if (StringFind(Symbol(), "XAUUSD", 0) != -1 || StringFind(Symbol(), "GOLD", -1) != -1){
return NormalizeDouble(Point * 10, digits - 1);
}
if(digits == 3 || digits == 5){
return NormalizeDouble(Point * 10, digits - 1);
}
if(digits == 4 || digits == 2){
return Point;
}
return 0;
}
// ボタンを作成
void CreateButtonObject(string objName, int posX, int posY, int sizeX, int sizeY, string text, color fontColor, int fontSize, color bgColor, color borderColor){
ObjectCreate(0, objName, OBJ_BUTTON, 0, 0, 0);
ObjectSetString(0, objName, OBJPROP_TEXT, text);
ObjectSetString (0, objName, OBJPROP_FONT, "Meiryo UI");
ObjectSetInteger(0, objName, OBJPROP_FONTSIZE, fontSize);
ObjectSetInteger(0, objName, OBJPROP_COLOR, fontColor);
ObjectSetInteger(0, objName, OBJPROP_BGCOLOR, bgColor);
ObjectSetInteger(0, objName, OBJPROP_BORDER_COLOR, borderColor);
ObjectSetInteger(0, objName, OBJPROP_XDISTANCE, posX);
ObjectSetInteger(0, objName, OBJPROP_YDISTANCE, posY);
ObjectSetInteger(0, objName, OBJPROP_XSIZE, sizeX);
ObjectSetInteger(0, objName, OBJPROP_YSIZE, sizeY);
ObjectSetInteger(0, objName, OBJPROP_CORNER, CORNER_LEFT_LOWER);
ObjectSetInteger(0, objName, OBJPROP_STATE, false);
ObjectSetInteger(0, objName, OBJPROP_SELECTABLE, true);
}