-
Notifications
You must be signed in to change notification settings - Fork 14
/
STRG-KijunArrow.pine
96 lines (88 loc) · 4.98 KB
/
STRG-KijunArrow.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
//@version=5
//Buy and sell depending on K arrow, no pyramiding, keep it simple.
_maxPyramiding = 0
strategy('STRG-Kijun', overlay=true, initial_capital = 100000, pyramiding=_maxPyramiding, default_qty_value = 0)
_year = input.int(2018, minval=1, title='Look back since(yr)')
chikou_span_filter = input.bool(true, "Chikou span filter")
kumo_filter = input.bool(false, "Kumo filter")
predefinedRR = input.bool(false, 'Use Predefined R/R')
_atrNumStop = input.int(3, 'Stop by No. ATR')
_atrNumProfit = input.int(6, 'Profit by No. ATR')
_atr = ta.atr(14)
contract = input.int(0, minval=0, title='Contract size')
//BEGINING OF COPY KIJUN ARROW
conversionPeriods = input.int(9, minval=1, title='Conversion Line Periods')
basePeriodsK = input.int(26, minval=1, title='Base Line Periods')
getMidPoint(_len, __offset) =>
MidPointOffset = math.avg(ta.lowest(_len)[__offset], ta.highest(_len)[__offset])
MidPointOffset
Kijun = getMidPoint(basePeriodsK, 0)
//MODIFIED FROM OFFICIAL ICHIMOKU PINE SCRIPT
laggingSpan2Periods = input.int(52, minval=1, title='Lagging Span 2 Periods')
displacement = input.int(26, minval=1, title='Displacement')
donchian(len) =>
math.avg(ta.lowest(len), ta.highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriodsK)
leadLine1 = math.avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)
plot(conversionLine, color=color.new(color.fuchsia, 0), linewidth = 2, title="Conversion Line", display = display.none)
plot(baseLine, color=color.new(color.blue, transp=0), linewidth=4, title='Base Line', display = display.all)
plot(close, offset=-displacement, color=color.new(color.black, transp=0), title='Lagging Span', style=plot.style_circles, linewidth=3, display = display.all)
p1 = plot(leadLine1, offset=displacement, color=color.new(color.green, 0), title='Lead 1', display = display.all)
p2 = plot(leadLine2, offset=displacement, color=color.new(color.red, 0), title='Lead 2', display = display.all)
fill(p1, p2, color=leadLine1 > leadLine2 ? color.new(color.green, 90) : color.new(color.red, 90), display = display.all)
//END OF OFFICIAL ICHIMOKU PINE SCRIPT
_curArrowDirection = if Kijun[0] - Kijun[1] > 0
1
else if Kijun[0] - Kijun[1] < 0
-1
var _existingArrowDirection = 0
_direction = 0
if _curArrowDirection != _existingArrowDirection
_direction := _curArrowDirection
_existingArrowDirection := _curArrowDirection
_existingArrowDirection
plotarrow(_direction, colorup=color.new(color.black, 0), colordown=color.new(color.black, 0), minheight=15, maxheight=15)
//END OF COPY KIJUN ARROW
KijunOffset = getMidPoint(basePeriodsK, 1)
chikou_span_long = close[0] > close[displacement]
chikou_span_short = close[0] < close[displacement]
kumo_top = math.max(leadLine1[displacement],leadLine2[displacement])
kumo_bottom = math.min(leadLine1[displacement],leadLine2[displacement])
kumo_long = close[0] > kumo_top
kumo_short = close[0] < kumo_bottom
// plotshape(leadLine2[displacement], 'KUMO')
longConditionK = _direction == 1 and year >= _year
shortConditionK = _direction == -1 and year >= _year
longCondition = longConditionK
shortCondition = shortConditionK
if chikou_span_filter and kumo_filter == false
longCondition := _direction == 1 and year >= _year and chikou_span_long
shortCondition := _direction == -1 and year >= _year and chikou_span_short
if kumo_filter and chikou_span_filter == false
longCondition := _direction == 1 and year >= _year and kumo_long
shortCondition := _direction == -1 and year >= _year and kumo_short
if chikou_span_filter and kumo_filter
longCondition := _direction == 1 and year >= _year and chikou_span_long and kumo_long
shortCondition := _direction == -1 and year >= _year and chikou_span_short and kumo_short
exitCondition = _existingArrowDirection == 1 and year >= _year
__stopLossDis = _atr * _atrNumStop
__takeProfitDis = _atr * _atrNumProfit
_contract = contract == 0 ? strategy.initial_capital / close: contract //get contract no. based on initial capital if left to 0
if longCondition
strategy.close('S', comment = 'Close S')
strategy.entry('L', strategy.long, _contract)
alert("Go long at " + str.tostring(close, "#.00)"))
if(predefinedRR)
strategy.exit("Exit L", "L", stop = close - __stopLossDis, limit = close + __takeProfitDis, comment_loss = 'StopLoss', comment_profit = 'Profit')
if shortCondition
strategy.close('L', comment = 'Close L')
strategy.entry('S', strategy.short, _contract)
alert("Go short at " + str.tostring(close, "#.00)"))
if(predefinedRR)
strategy.exit("Exit S", "S", stop = close + __stopLossDis, limit = close - __takeProfitDis, comment_loss = 'StopLoss', comment_profit = 'Profit')
if(predefinedRR == false and strategy.position_size>0 and shortConditionK) //exit long position once K turns south
strategy.close('L', comment = 'Stoploss L')
if(predefinedRR == false and strategy.position_size<0 and longConditionK) //exit short position once K turns north
strategy.close('S', comment = 'Stoploss S')