-
Notifications
You must be signed in to change notification settings - Fork 3
/
[TVExtBot]그리드현물매매전략.pine
318 lines (235 loc) · 16.4 KB
/
[TVExtBot]그리드현물매매전략.pine
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TVExtBot
//@version=5
strategy(title='그리드현물매매전략', overlay=true,
initial_capital=1000, commission_type = strategy.commission.percent, commission_value = 0.06, pyramiding = 10,
default_qty_value=10, default_qty_type=strategy.percent_of_equity,
process_orders_on_close=true, close_entries_rule='ANY')
//---------------------------------------백테스트기간 설정--------------------------------------{
// Make input options that configure backtest date range
startYear = input.int(title="시작 년",
defval=2020, minval=1800, maxval=2100, inline="1", group = '백테스트기간')
startMonth = input.int(title="월",
defval=1, minval=1, maxval=12, inline="1", group = '백테스트기간')
startDate = input.int(title="일",
defval=1, minval=1, maxval=31, inline="1", group = '백테스트기간')
endYear = input.int(title="종료 년",
defval=2050, minval=1800, maxval=2100, inline="2", group = '백테스트기간')
endMonth = input.int(title="월",
defval=12, minval=1, maxval=12, inline="2", group = '백테스트기간')
endDate = input.int(title="일",
defval=31, minval=1, maxval=31, inline="2", group = '백테스트기간')
// Look if the close time of the current bar
// Falls inside the date range
inDateRange = (time >= timestamp(syminfo.timezone, startYear,
startMonth, startDate, 0, 0)) and
(time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
//--------------------------------------------------------------------------------}
//---------------------------------------주문정보설정--------------------------------------{
token = input.string(title = "인증키", defval = "", inline="3", group = "주문정보", tooltip = "TVExtBot 인증키")
buyOrderid = input.string(title = "매수주문ID", defval = "", group = "주문정보", inline = "4")
buyMemo = input.string(title = " 메모", defval = "", group = "주문정보", inline = "4")
sellOrderid = input.string(title = "매도주문ID", defval = "", group = "주문정보", inline = "5")
sellMemo = input.string(title = " 메모", defval = "", group = "주문정보", inline = "5")
closeOrderid = input.string(title = "손절주문ID", defval = "", group = "주문정보", inline = "6")
closeMemo = input.string(title = " 메모", defval = "", group = "주문정보", inline = "6")
//---------------------------------------그리드설정--------------------------------------{
PercentProfit = input(0.50, "간격(%)", group = "그리드 간격 설정")
x1 = (100 - (PercentProfit * 1)) / 100
x2 = (100 - (PercentProfit * 2)) / 100
x3 = (100 - (PercentProfit * 3)) / 100
x4 = (100 - (PercentProfit * 4)) / 100
x5 = (100 - (PercentProfit * 5)) / 100
x6 = (100 - (PercentProfit * 6)) / 100
x7 = (100 - (PercentProfit * 7)) / 100
x8 = (100 - (PercentProfit * 8)) / 100
x9 = (100 - (PercentProfit * 9)) / 100
x10 = (100 - (PercentProfit * 10)) / 100
UpperLimit = input.price(0.00, '상단 그리드 가격', group = "", confirm=true)
plot(UpperLimit, color = color.purple)
LowerLimit = UpperLimit * x10
plot(LowerLimit, color = color.purple)
//-----------------------------------------------------------------------------------------}
//---------------------------------------그리드 라인 설정--------------------------------------{
Linea_A = UpperLimit * x1
plot(Linea_A, color = color.blue)
Linea_B = UpperLimit * x2
plot(Linea_B, color = color.blue)
Linea_C = UpperLimit * x3
plot(Linea_C, color = color.blue)
Linea_D = UpperLimit * x4
plot(Linea_D, color = color.blue)
Linea_50Percent = UpperLimit * x5
plot(Linea_50Percent, color = color.purple)
Linea_E = UpperLimit * x6
plot(Linea_E, color = color.blue)
Linea_F = UpperLimit * x7
plot(Linea_F, color = color.blue)
Linea_G = UpperLimit * x8
plot(Linea_G, color = color.blue)
Linea_H = UpperLimit * x9
plot(Linea_H, color = color.blue)
//----------------------------------------------------------------------------------------}
//---------------------------------------매수 주문 설정--------------------------------------{
FloorLimit = close > LowerLimit
Cond1 = close < Linea_A
Cd1 = FloorLimit and Cond1
LongA = Cd1 and inDateRange
isLong1 = if (LongA and barstate.isconfirmed)
1
else
0
FloorLimit2 = close > LowerLimit
Cond2 = close < Linea_B
Cd2 = FloorLimit2 and Cond2
LongB = Cd2 and inDateRange
isLong2 = if (LongB and barstate.isconfirmed)
1
else
0
FloorLimit3 = close > LowerLimit
Cond3 = close < Linea_C
Cd3 = FloorLimit and Cond3
LongC = Cd3 and inDateRange
isLong3 = if (LongC and barstate.isconfirmed)
1
else
0
FloorLimit4 = close > LowerLimit
Cond4 = close < Linea_D
Cd4 = FloorLimit and Cond4
LongD = Cd4 and inDateRange
isLong4 = if (LongD and barstate.isconfirmed)
1
else
0
FloorLimit5 = close > LowerLimit
Cond5 = close < Linea_50Percent
Cd5 = FloorLimit and Cond5
Long_E = Cd5 and inDateRange
isLong5 = if (Long_E and barstate.isconfirmed)
1
else
0
FloorLimit6 = close > LowerLimit
Cond6 = close < Linea_E
Cd6 = FloorLimit and Cond6
Long_F = Cd6 and inDateRange
isLong6 = if (Long_F and barstate.isconfirmed)
1
else
0
FloorLimit7 = close > LowerLimit
Cond7 = close < Linea_F
Cd7 = FloorLimit and Cond7
Long_G = Cd7 and inDateRange
isLong7 = if (Long_G and barstate.isconfirmed)
1
else
0
FloorLimit8 = close > LowerLimit
Cond8 = close < Linea_G
Cd8 = FloorLimit and Cond8
Long_H = Cd8 and inDateRange
isLong8 = if (Long_H and barstate.isconfirmed)
1
else
0
FloorLimit9 = close > LowerLimit
Cond9 = close < Linea_H
Cd9 = FloorLimit and Cond9
LongH = Cd9 and inDateRange
isLong9 = if (Long_H and barstate.isconfirmed)
1
else
0
Cond10 = close < LowerLimit
Long_I = Cond10 and inDateRange
isLong10 = if (Long_I and barstate.isconfirmed)
1
else
0
//----------------------------------------------------------------------------------------}
//---------------------------------------매도 주문 설정--------------------------------------{
isExit1 = if (close > UpperLimit and barstate.isconfirmed)
close
isExit2 = if (close > Linea_A and barstate.isconfirmed)
close
isExit3 = if (close > Linea_B and barstate.isconfirmed)
close
isExit4 = if (close > Linea_C and barstate.isconfirmed)
close
isExit5 = if (close > Linea_D and barstate.isconfirmed)
close
isExit6 = if (close > Linea_50Percent and barstate.isconfirmed)
close
isExit7 = if (close > Linea_E and barstate.isconfirmed)
close
isExit8 = if (close > Linea_F and barstate.isconfirmed)
close
isExit9 = if (close > Linea_G and barstate.isconfirmed)
close
isExit10 = if (close > Linea_H and barstate.isconfirmed)
close
//----------------------------------------------------------------------------------------}
//---------------------------------------매수 매도 주문--------------------------------------{
strategy.entry(id = "BUY1", direction = strategy.long, qty = 10, when = (isLong1 and strategy.position_size == 0), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY2", direction = strategy.long, qty = 10, when = (isLong2 and strategy.position_size == 10), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY3", direction = strategy.long, qty = 10, when = (isLong3 and strategy.position_size == 20), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY4", direction = strategy.long, qty = 10, when = (isLong4 and strategy.position_size == 30), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY5", direction = strategy.long, qty = 10, when = (isLong5 and strategy.position_size == 40), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY6", direction = strategy.long, qty = 10, when = (isLong6 and strategy.position_size == 50), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY7", direction = strategy.long, qty = 10, when = (isLong7 and strategy.position_size == 60), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY8", direction = strategy.long, qty = 10, when = (isLong8 and strategy.position_size == 70), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY9", direction = strategy.long, qty = 10, when = (isLong9 and strategy.position_size == 80), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.entry(id = "BUY10", direction = strategy.long, qty = 10, when = (isLong10 and strategy.position_size == 90), alert_message='TVM:{"orderid":"' + buyOrderid +'","memo":"' + buyMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT1", from_entry = "BUY1", qty = 100, stop = isExit1, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT2", from_entry = "BUY2", qty = 100, stop = isExit2, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT3", from_entry = "BUY3", qty = 100, stop = isExit3, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT4", from_entry = "BUY4", qty = 100, stop = isExit4, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT5", from_entry = "BUY5", qty = 100, stop = isExit5, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT6", from_entry = "BUY6", qty = 100, stop = isExit6, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT7", from_entry = "BUY7", qty = 100, stop = isExit7, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT8", from_entry = "BUY8", qty = 100, stop = isExit8, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT9", from_entry = "BUY9", qty = 100, stop = isExit9, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
strategy.exit(id = "EXIT10", from_entry = "BUY10", qty = 100, stop = isExit10, alert_message='TVM:{"orderid":"' + sellOrderid +'","memo":"' + sellMemo + '","token":"' + token + '"}:MVT')
//--------------------------------------------------------------------------------------}
//---------------------------------------스탑로스 설정--------------------------------------{
ExitConditionOn = input.string("NO", title="사용여부", options=["NO", "YES"], group = "스탑로스 설정") == "YES"
StopLoss = input(0.00, "손절비율(%)", group = "스탑로스 설정")
x = (100 - StopLoss) / 100
StopLossLevel = LowerLimit * x
GridDestroyed = close < StopLossLevel and barstate.isconfirmed
if ExitConditionOn
strategy.close_all(when = GridDestroyed, comment = "손절", alert_message='TVM:{"orderid":"' + closeOrderid +'","memo":"' + closeMemo + '","token":"' + token + '"}:MVT')
//--------------------------------------------------------------------------------------------}
//---------------------------------------그리드가격 테이블설정--------------------------------------{
i_position = input.string(defval = "Bottom Right", title = "테이블위치", options = ["Top Right", "Middle Right", "Bottom Right"], group = "테이블설정")
position = i_position == "Top Right" ? position.top_right : i_position == "Middle Right" ? position.middle_right : position.bottom_right
i_w = 12
i_h = 2
text_size = size.normal
var table perfTable = table.new(position, 2, 11, frame_color = color.blue, frame_width = 2, border_width = 1)
table.cell(perfTable, 0, 0, "상단 그리드 가격", bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 0, text = str.tostring(UpperLimit), bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 1, "그리드가격 1", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 2, "그리드가격 2", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 3, "그리드가격 3", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 4, "그리드가격 4", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 1, text = str.tostring(Linea_A), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 2, text = str.tostring(Linea_B), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 3, text = str.tostring(Linea_C), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 4, text = str.tostring(Linea_D), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 5, "중간 그리드 가격", bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 5, text = str.tostring(Linea_50Percent), bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 6, "그리드가격 5", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 7, "그리드가격 6", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 8, "그리드가격 7", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 9, "그리드가격 8", bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 6, text = str.tostring(Linea_E), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 7, text = str.tostring(Linea_F), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 8, text = str.tostring(Linea_G), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 9, text = str.tostring(Linea_H), bgcolor = color.new(color.blue, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 0, 10, "하단 그리드 가격(손절)", bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
table.cell(perfTable, 1, 10, text = str.tostring(StopLossLevel), bgcolor = color.new(color.purple, 74), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size)
//--------------------------------------------------------------------------------}