forked from algocode2022/PineScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridSpotTradingSystem
447 lines (391 loc) · 15.9 KB
/
GridSpotTradingSystem
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// @erolmutlu Grid Spot
//@version=5
strategy(title = 'Grid Spot Sistemi',
overlay = true,
calc_on_every_tick = true,
initial_capital = 10000,
commission_type = strategy.commission.percent,
commission_value = 0.03,
pyramiding = 100,
default_qty_type = strategy.percent_of_equity,
process_orders_on_close = true,
close_entries_rule = 'ANY')
//---------------------------------------------------------------
startDate = input.int(title="START DAY DATE",
defval=1, minval=1, maxval=31,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the day of start backtesting.")
startMonth = input.int(title="START MONTH",
defval=1, minval=1, maxval=12,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the month of start backtesting.")
startYear = input.int(title="START YEAR",
defval=2018, minval=1800, maxval=2100,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the year of start backtesting.")
endDate = input.int(title="END DAY DATE",
defval=30, minval=1, maxval=31,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the day of end backtesting.")
endMonth = input.int(title="END MONTH",
defval=12, minval=1, maxval=12,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the month of end backtesting.")
endYear = input.int(title="END YEAR",
defval=2022, minval=1800, maxval=2100,
group = '1. DATE RANGE: Select the date range for backtesting',
tooltip = "Enter the year of end backtesting.")
inDateRange = (time >= timestamp(syminfo.timezone, startYear,
startMonth, startDate, 0, 0)) and
(time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
//---------------------------------------------------------------
PercentProfit = input.float(defval = 0.50,
title = "GRID PERCENT VALUE",
group = "2. GRID CUSTOM SETTING",
tooltip = "This feature refers to the percentage value of each grid. The value you enter here will allow you to create your custom grid.",
confirm = true)
UpperLimit = input.price(defval = 0.00,
title = 'TOP GRID POSITION',
group = "2. GRID CUSTOM SETTING",
confirm = true,
tooltip = "This feature allows you to set the initial price level of your grid. Click on a price level to start.")
feePerTrade = input.float(defval = 0.03, minval = 0.00, maxval = 100,
title = "FEE PER TRADE",
group = "2. GRID CUSTOM SETTING",
tooltip = "Set the commission cost your broker charges you for each trade.",
confirm = true)
//---------------------------------------------------------------
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
LowerLimit = UpperLimit * x10
//---------------------------------------------------------------
Linea_A = float(UpperLimit * x1)
Linea_B = float(UpperLimit * x2)
Linea_C = float(UpperLimit * x3)
Linea_D = float(UpperLimit * x4)
Linea_50Percent = float(UpperLimit * x5)
Linea_E = float(UpperLimit * x6)
Linea_F = float(UpperLimit * x7)
Linea_G = float(UpperLimit * x8)
Linea_H = float(UpperLimit * x9)
//---------------------------------------------------------------
Long1 = ta.crossunder(close, Linea_A) and inDateRange
Long2 = ta.crossunder(close, Linea_B) and inDateRange
Long3 = ta.crossunder(close, Linea_C) and inDateRange
Long4 = ta.crossunder(close, Linea_D) and inDateRange
Long5 = ta.crossunder(close, Linea_50Percent) and inDateRange
Long6 = ta.crossunder(close, Linea_E) and inDateRange
Long7 = ta.crossunder(close, Linea_F) and inDateRange
Long8 = ta.crossunder(close, Linea_G) and inDateRange
Long9 = ta.crossunder(close, Linea_H) and inDateRange
Long10 = ta.crossunder(close, LowerLimit) and inDateRange
//---------------------------------------------------------------
isExit1 = ta.crossover(close, UpperLimit)
isExit2 = ta.crossover(close, Linea_A)
isExit3 = ta.crossover(close, Linea_B)
isExit4 = ta.crossover(close, Linea_C)
isExit5 = ta.crossover(close, Linea_D)
isExit6 = ta.crossover(close, Linea_50Percent)
isExit7 = ta.crossover(close, Linea_E)
isExit8 = ta.crossover(close, Linea_F)
isExit9 = ta.crossover(close, Linea_G)
isExit10 = ta.crossover(close, Linea_H)
//---------------------------------------------------------------
plot(LowerLimit, color = color.purple, linewidth = 1)
plot(UpperLimit, color = color.purple, linewidth = 1)
plot(Linea_A, color = color.blue, linewidth = 1)
plot(Linea_B, color = color.blue, linewidth = 1)
plot(Linea_C, color = color.blue, linewidth = 1)
plot(Linea_D, color = color.blue, linewidth = 1)
plot(Linea_50Percent, color = color.blue, linewidth = 1)
plot(Linea_E, color = color.blue, linewidth = 1)
plot(Linea_F, color = color.blue, linewidth = 1)
plot(Linea_G, color = color.blue, linewidth = 1)
plot(Linea_H, color = color.blue, linewidth = 1)
//---------------------------------------------------------------
fill(plot(UpperLimit),
plot(Linea_A),
color = color.new(color.purple, 90))
fill(plot(Linea_A),
plot(Linea_B),
color = color.new(color.purple, 85))
fill(plot(Linea_B),
plot(Linea_C),
color = color.new(color.purple, 80))
fill(plot(Linea_C),
plot(Linea_D),
color = color.new(color.purple, 70))
fill(plot(Linea_D),
plot(Linea_50Percent),
color = color.new(color.purple, 60))
fill(plot(Linea_50Percent),
plot(Linea_E),
color = color.new(color.purple, 60))
fill(plot(Linea_E),
plot(Linea_F),
color = color.new(color.purple, 70))
fill(plot(Linea_F),
plot(Linea_G),
color = color.new(color.purple, 80))
fill(plot(Linea_G),
plot(Linea_H),
color = color.new(color.purple, 85))
fill(plot(Linea_H),
plot(LowerLimit),
color = color.new(color.purple, 90))
//---------------------------------------------------------------
if Long1
strategy.entry(id = "LONG1",
direction = strategy.long,
qty = 1)
if isExit1
strategy.exit(id = "EXIT1",
from_entry = "LONG1",
qty = 100,
stop = UpperLimit,
profit = UpperLimit)
if Long2
strategy.entry(id = "LONG2",
direction = strategy.long,
qty = 1)
if isExit2
strategy.exit(id = "EXIT2",
from_entry = "LONG2",
qty = 100,
stop = Linea_A,
limit = Linea_A)
if Long3
strategy.entry(id = "LONG3",
direction = strategy.long,
qty = 1)
if isExit3
strategy.exit(id = "EXIT3",
from_entry = "LONG3",
qty = 100,
stop = Linea_B,
limit = Linea_B)
if Long4
strategy.entry(id = "LONG4",
direction = strategy.long,
qty = 1)
if isExit4
strategy.exit(id = "EXIT4",
from_entry = "LONG4",
qty = 100,
stop = Linea_C,
limit = Linea_C)
if Long5
strategy.entry(id = "LONG5",
direction = strategy.long,
qty = 1)
if isExit5
strategy.exit(id = "EXIT5",
from_entry = "LONG5",
qty = 100,
stop = Linea_D,
limit = Linea_D)
if Long6
strategy.entry(id = "LONG6",
direction = strategy.long,
qty = 1)
if isExit6
strategy.exit(id = "EXIT6",
from_entry = "LONG6",
qty = 100,
stop = Linea_50Percent,
limit = Linea_50Percent)
if Long7
strategy.entry(id = "LONG7",
direction = strategy.long,
qty = 1)
if isExit7
strategy.exit(id = "EXIT7",
from_entry = "LONG7",
qty = 100,
stop = Linea_E,
limit = Linea_E)
if Long8
strategy.entry(id = "LONG8",
direction = strategy.long,
qty = 1)
if isExit8
strategy.exit(id = "EXIT8",
from_entry = "LONG8",
qty = 100,
stop = Linea_F,
limit = Linea_F)
if Long9
strategy.entry(id = "LONG9",
direction = strategy.long,
qty = 1)
if isExit9
strategy.exit(id = "EXIT9",
from_entry = "LONG9",
qty = 100,
stop = Linea_G,
limit = Linea_G)
if Long10
strategy.entry(id = "LONG10",
direction = strategy.long,
qty = 1)
if isExit10
strategy.exit(id = "EXIT10",
from_entry = "LONG10",
qty = 100,
stop = Linea_H,
limit = Linea_H)
//---------------------------------------------------------------
trades = int(strategy.closedtrades)
initialCapital = float(strategy.initial_capital)
grossReturn = trades * PercentProfit
gainCapital = (initialCapital * grossReturn) / 100
totalCapital = initialCapital + gainCapital
feeCost = (totalCapital * feePerTrade) / 100
totalFeeCost = feeCost * trades
netCapital = totalCapital - totalFeeCost - initialCapital
//---------------------------------------------------------------
i_position = input.string(defval = "Bottom Right",
title = "Table Placement",
options = ["Top Right", "Middle Right", "Bottom Right"],
group = "3. Table Characteristics")
position = i_position == "Top Right" ? position.top_right : i_position == "Middle Right" ? position.middle_right : position.bottom_right
i_w = input.int(title = "Width",
defval = 6,
group = "3. Table Characteristics")
i_h = input.int(title = "Height",
defval = 2,
group = "3. Table Characteristics")
i_text_size = input(title = "Text Size",
defval = "Small")
text_size = i_text_size == "Normal" ? size.normal : i_text_size == "Auto" ? size.auto : i_text_size == "Auto" ? size.auto : i_text_size == "Tiny" ? size.tiny
: i_text_size == "Small" ? size.small : i_text_size == "Large" ? size.large : size.huge
i = input.bool(title = "1. Capital",
defval = true,
group = "4. Enable Rows",
tooltip = "Initial capital of the strategy.")
i_1 = input.bool(title = "2. Gross Returns",
defval = true,
group = "4. Enable Rows",
tooltip = "Total strategy return. Contains the initial capital plus the sum of the gains.")
i_2 = input.bool(title = "3. Gain Percent",
defval = true,
group = "4. Enable Rows",
tooltip = "Percentage return of strategy gains.")
i_3 = input.bool(title = "4. Commission Cost",
defval = true,
group = "4. Enable Rows",
tooltip = "Percentage return of strategy gains.")
i_4 = input.bool(title = "5. Net Returns",
defval = true,
group = "4. Enable Rows",
tooltip = "Total commission cost.")
i_5 = input.bool(title = "6. Closed Trades",
defval = true,
group = "4. Enable Rows",
tooltip = "Total trades.")
var table perfTable = table.new(position, 2, 11, frame_color = color.purple, frame_width = 1, border_width = 1)
//---------------------------------------------------------------
if i
table.cell(perfTable, 0, 0, "1. GROSS RETURN",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i
table.cell(perfTable, 1, 0,
text = str.tostring(totalCapital) + " $",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_1
table.cell(perfTable, 0, 1, "2. INITIAL CAPITAL",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_1
table.cell(perfTable, 1, 1, text = str.tostring(initialCapital) + " $",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_2
table.cell(perfTable, 0, 2, "3. GAIN",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_2
table.cell(perfTable, 1, 2,
text = str.tostring(grossReturn) + " %",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_3
table.cell(perfTable, 0, 3, "4. FEE COST",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_3
table.cell(perfTable, 1, 3,
text = str.tostring(totalFeeCost) + " $",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_4
table.cell(perfTable, 0, 4, "5. NET RETURN",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_4
table.cell(perfTable, 1, 4,
text = str.tostring(netCapital) + " $",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size)
if i_5
table.cell(perfTable, 0, 6, "6. CLOSED TRADES",
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size,
text_halign = text.align_left)
if i_5
table.cell(perfTable, 1, 6,
text = str.tostring(trades),
bgcolor = color.new(color.purple, 90),
text_color = color.new(color.white, 0),
width = i_w,
height = i_h,
text_size = text_size)
//---------------------------------------------------------------