-
Notifications
You must be signed in to change notification settings - Fork 6
/
[STRAT]AwesomeADX.pine
230 lines (178 loc) · 7.37 KB
/
[STRAT]AwesomeADX.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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kaushal
// Strategy By TradePro
// https://www.youtube.com/watch?v=cKuTSFwUpPE&ab_channel=TradePro
//@version=4
SystemName = "AwesomeADX"
is_overlay = true
InitCapital = 1000
InitPosition = 100
InitCommission = 0
CalcOnorderFills = false
strategy(title=SystemName, shorttitle=SystemName,
initial_capital=InitCapital, default_qty_type=strategy.percent_of_equity,
default_qty_value=InitPosition,commission_type=strategy.commission.percent, commission_value=InitCommission, calc_on_order_fills=CalcOnorderFills,
precision=6, max_lines_count=500, max_labels_count=500, overlay=is_overlay, max_bars_back=20)
DateFilter = input(false, "═════════════ Backtest Date Range Filtering", group="Date")
i_startTime = input(defval = timestamp("01 Jun 2021 00:00 +0000"), title = "Start Time", type = input.time, group="Date")
i_endTime = input(defval = timestamp("30 Dec 2021 23:30 +0000"), title = "End Time", type = input.time, group="Date")
TradeDateIsAllowed() => DateFilter ? time >= i_startTime and time <= i_endTime : true
//Filter Parameter
section1 = input(false, "═════════ Filter Parameter ════════")
use_mafilt = input(true, title="Use MA Filter")
mafilt_type = input("EMA", title="MA Filter Type", options = ["EMA","SMA","WMA","RMA","VWMA"])
mafilt_len_1 = input(5, title="EMA 01")
mafilt_len_2 = input(21, title="EMA 02")
mafilt_len_3 = input(50, title="EMA 03")
mafilt_len_4 = input(100, title="EMA 04")
//Indicator Parameter
section2 = input(false, "═════════ Signal Parameter ════════")
src = input(close, title="Source", type=input.source)
//************************************************//
// Main Indicator Function
//************************************************//
//Filter Indicator Calculation --> MA
get_MA(typ, len) =>
float maVal = na
if (typ == "EMA")
maVal := ema(src, len)
if (typ == "SMA")
maVal := sma(src, len)
if (typ == "WMA")
maVal := wma(src, len)
if (typ == "RMA")
maVal := rma(src, len)
if (typ == "VWMA")
maVal := vwma(src, len)
maVal
MAfilt_01 = get_MA(mafilt_type, mafilt_len_1)
MAfilt_02 = get_MA(mafilt_type, mafilt_len_2)
MAfilt_03 = get_MA(mafilt_type, mafilt_len_3)
MAfilt_04 = get_MA(mafilt_type, mafilt_len_4)
// ADX
adxlen = input(14, title="ADX Smoothing", group="ADX")
dilen = input(14, title="DI Length", group="ADX")
dirmov(len) =>
up = change(high)
down = -change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = rma(tr, len)
plus = fixnan(100 * rma(plusDM, len) / truerange)
minus = fixnan(100 * rma(minusDM, len) / truerange)
[plus, minus]
adx(dilen, adxlen) =>
[plus, minus] = dirmov(dilen)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
// BB %
length = input(20, minval=1, group="BB%")
mult = input(2.0, minval=0.001, maxval=50, title="StdDev", group="BB%")
basis = sma(src, length)
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev
bbr = (src - lower)/(upper - lower)
// Awesome oscillator
ao = sma(hl2,5) - sma(hl2,34)
Uptrend = false
Dntrend = false
if(((use_mafilt==true and MAfilt_01 > MAfilt_02 and MAfilt_03 > MAfilt_04) or use_mafilt==false))
Uptrend := true
if(((use_mafilt==true and MAfilt_01 < MAfilt_02 and MAfilt_03 < MAfilt_04) or use_mafilt==false))
Dntrend := true
BuySignal = (sig > 15 and bbr > 0.75 and ao > 2)
SellSignal = (sig > 15 and bbr < 0.25 and ao < -2)
//****************************************************************************************************************************************************************************************//
// Backtest Section
//****************************************************************************************************************************************************************************************//
BTsection = input(false, "═════════ Backtest Parameter ════════")
SL_Fixed_Perc = input(2, title="Stop Loss Fixed Percentage", minval=0.1, step=0.1)
reward_ratio = input(0.5, title="TP Reward to Risk Ratio", minval=0.1, step=0.1)
//************************************************//
int open_pos = 0
int long_pos = 0
int short_pos = 0
float _EntryPrice = na
float _StopLoss = na
float _TakeProfit = na
float _EP = na
float _SL = na
float _TP = na
open_pos := (open_pos [1] == 1) ? 1 : 0
long_pos := (long_pos [1] == 1) ? 1 : 0
short_pos := (short_pos[1] == 1) ? 1 : 0
_EP := (open_pos[1] == 1) ? _EP[1] : na
_SL := (open_pos[1] == 1) ? _SL[1] : na
_TP := (open_pos[1] == 1) ? _TP[1] : na
//************************************************//
// Stoploss Function
//************************************************//
get_SL(_entry,_signal)=>
ret_sl = 0.0
if(_signal=="Buy")
ret_sl := _entry - (_entry * (SL_Fixed_Perc/100))
if(_signal=="Sell")
ret_sl := _entry + (_entry * (SL_Fixed_Perc/100))
ret_sl
//************************************************//
// Generate Entry/Exit Level
//************************************************//
//Buy
if(Uptrend and BuySignal)
_EntryPrice := close
_StopLoss := get_SL(_EntryPrice,"Buy")
_TakeProfit := (_EntryPrice + ((_EntryPrice - _StopLoss)*reward_ratio))
//Sell
if(Dntrend and SellSignal)
_EntryPrice := close
_StopLoss := get_SL(_EntryPrice,"Sell")
_TakeProfit := (_EntryPrice - ((_StopLoss - _EntryPrice)*reward_ratio))
//************************************************//
// Trade Entry Function
//************************************************//
//Buy
if(open_pos == 0 and Uptrend and BuySignal and TradeDateIsAllowed())
long_pos := 1
open_pos := 1
_EP := _EntryPrice
_SL := _StopLoss
_TP := _TakeProfit
strategy.entry("Long", strategy.long)
//Sell
if(open_pos == 0 and Dntrend and SellSignal and TradeDateIsAllowed())
short_pos := 1
open_pos := 1
_EP := _EntryPrice
_SL := _StopLoss
_TP := _TakeProfit
strategy.entry("Short", strategy.short)
//************************************************//
// Exit Function
//************************************************//
bool long_Profit_Label = false
bool short_Profit_Label = false
bool long_Loss_Label = false
bool short_Loss_Label = false
//Exit Long Condition
if (long_pos[0] == 1 and low <= _SL)
long_pos := 0
open_pos := 0
long_Loss_Label := true
strategy.close(id="Long", comment="sl", when=_SL)
if (long_pos[0] == 1 and high >= _TP)
long_pos := 0
open_pos := 0
long_Profit_Label := true
strategy.close(id="Long", comment="tp", when=_TP)
if (short_pos[0] == 1 and high >= _SL)
short_pos := 0
open_pos := 0
short_Loss_Label := true
strategy.close(id="Short", comment="sl", when=_SL)
if (short_pos[0] == 1 and low <= _TP)
short_pos := 0
open_pos := 0
short_Profit_Label := true
strategy.close(id="Short", comment="tp", when=_TP)