-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTRIVIX Strat.pine
165 lines (119 loc) · 8.84 KB
/
TRIVIX Strat.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
//@version=4
// Copyright (c) 2020-present, @Mojomajor
// This script may be freely distributed if you wanna die slowly and painfully. You'be been warned!
strategy("TRIVIX Strategy",calc_on_every_tick=true, default_qty_type=strategy.percent_of_equity, pyramiding=0, default_qty_value=50, initial_capital=5000, currency='EUR', slippage=1, commission_type=strategy.commission.percent, commission_value=0.05)
//strategy.risk.max_drawdown(10, strategy.percent_of_equity)
// Risk Matrix
// https://docs.google.com/spreadsheets/d/1lEvz_bjApUSMYTpBuGRAaDRMh4LaANw_hp_dVCTqdkI/edit#gid=0
// VIX history
// https://de.wikipedia.org/wiki/CBOE_Volatility_Index
// HOW TO TRADE IT?
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// - set candles to 2h on VIX(use CBOE! not CFDs on VIX!)
// - go long with Turbo-Certificate. (or rather long running options if you can)
// - Leverage: 2x or less, since it's not necessary
// - Knockout FAR below support lines. (e.g. below 8.35 as of 2019 and 2020)
// - set Stopp Loss above Knockout
// - use endless Turbos (no due date)
// - OR: Faktor Zertifikat: 1:1 daily https://www.zertifikate.commerzbank.de/product-search/faktor-zertifikate/all-assettypes/vix%20cboe%20volatility%20index%20future%20feb%202020
// - use 50% of your equity each trade (which will be higher after each success trade)
// - see https://kodify.net/tradingview/strategy-settings/order-size-equity-percentage/
// https://www.zertifikate.commerzbank.de/product-search/unlimited%20turbo-optionsscheine/all-assettypes/vix%20cboe%20volatility%20index%20future%20feb%202020?CallPutRadio=Call&Barrier1Range-Max=10.65&StrikeRange-Max=10.15
// DRAWBACKS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// - you can never be sure VIX does not drop below 9.. so beware and use stopp loss!
// - massive breakouts won't trigger any other signal than sell.
// - therefore you will miss out on trading financial panics, chaos, mayhem and isms.
// USER INPUTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// - If you check 'Strategy: actively short' you'll make a lot more money (5x), but have a way bigger drawdown (4x)
// - and you will have a super high risk of beeing stopped out..the sky is the limit in these days
// - If you set the 'Strategy SMA:' to 180, there seems to a few percent more return
length = input(title="Length", type=input.integer, defval=14)
mult = input(title="TRIX Value Multiplier", type=input.integer, defval=1)
signalType = input(title="Signal Smoothing Type", defval="SMA", options=["EMA", "SMA"])
signalLength = input(title="Signal Smoothing Length", type=input.integer, defval=9)
src = input(title="Source", type=input.source, defval=close)
showSignal = input(title="Show Signal Line ?", type=input.bool, defval=true)
showHistogram = input(title="Show Histogram ?", type=input.bool, defval=false)
highlightCrossovers = input(title="Highlight TRIX/Signal Crossovers ?", type=input.bool, defval=true)
highlightZeroCrossovers = input(title="Highlight Zero Line Crossovers ?", type=input.bool, defval=true)
applyFilling = input(title="Apply Ribbon Filling ?", type=input.bool, defval=false)
drawEquity = input(title="Draw Equity?", type=input.bool, defval=false)
// strategy stuff
baselineSMAlength = input(title="Strategy: SMA", type=input.integer, defval=180) // 180 seems good in H2
stopEnabled = input(title="Strategy: Stopp Loss?", type=input.bool, defval=false)
stopLossPercentage = input(title="Strategy: SL % drawback", type=input.integer, defval=80) / 100
activelyShort = input(title="Strategy: actively Short?", type=input.bool, defval=false)
stopEnabledShort = input(title="Strategy: Stopp Loss Shorts?", type=input.bool, defval=false)
stopLossPercentageShort = input(title="Strategy: SL short % rise", type=input.integer, defval=120) / 100
takeProfitStopEnabledShort = input(title="Strategy: Take Profit Shorts?", type=input.bool, defval=false)
takeProfitStopLossPercentageShort = input(title="Strategy: TP short %", type=input.integer, defval=60) / 100
// BACKTESTING
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
enableBacktest = input(title="Enable Backtest?", type=input.bool, defval=false)
testStartYear = input(defval=2018, title="Backtest Start Year",type=input.integer)
testStartMonth = input(defval=1, title="Backtest Start Month",type=input.integer)
testStartDay = input(defval=1, title="Backtest Start Day",type=input.integer)
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,23,0)
testStopYear = input(defval=2019, title="Backtest Stop Year",type=input.integer)
testStopMonth = input(defval=1, title="Backtest Stop Month",type=input.integer)
testStopDay = input(defval=1, title="Backtest Stop Day",type=input.integer)
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
// A switch to control background coloring of the test period
testPeriodBackground = input(title="Color Background?", type=input.bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? #00FF00 : na
bgcolor(testPeriodBackgroundColor, transp=97)
canTrade() =>
doBackTest = iff(time >= testPeriodStart and time <= testPeriodStop , true, false)
enableBacktest ? doBackTest : true
// CALCULATION
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
triple = ema(ema(ema(src, length), length), length)
trix = mult * roc(triple, 1)
sma_1 = sma(trix, signalLength)
ema_1 = ema(trix, signalLength)
signal = signalType == "SMA" ? sma_1 : ema_1
hist = trix - signal
histColor = hist >= 0 ? hist[1] < hist ? #26A69A : #B2DFDB :
hist[1] < hist ? #FFCDD2 : #EF5350
plot(showHistogram ? hist : na, title="Histogram", style=plot.style_columns, color=histColor, transp=0)
hline(0, title="Zero Level", linestyle=hline.style_dotted)
trendColor = trix > signal ? #0ebb23 : color.red
trixColor = applyFilling ? trendColor : trix > 0 ? #0ebb23 : color.red
signalColor = applyFilling ? trendColor : #ff9800
trixPlot = plot(trix, title="TRIX", linewidth=2, color=trixColor, transp=0)
signalPlot = plot(showSignal ? signal : na, title="Signal", color=signalColor, transp=0)
transparent = color.new(color.white, 100)
fillColor = applyFilling ? trendColor : transparent
fill(trixPlot, signalPlot, color=fillColor, transp=70)
zeroCrossBgColor = highlightZeroCrossovers ? trix > 0 ? color.green : color.red : transparent
bgcolor(zeroCrossBgColor, transp=90)
sma200 = sma(src, baselineSMAlength)
// SIGNALS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
longCondition = highlightCrossovers and crossover(trix, signal) and src < sma200 and strategy.position_size <= 0 and canTrade()
drawbackStop = stopEnabled ? float(src * stopLossPercentage) : na
buyPrice = stopEnabled ? src : na
shortCondition = highlightCrossovers and crossunder(trix, signal) and src > sma200 and strategy.position_size >= 1 and canTrade()
shortStop = stopEnabledShort ? float(src * stopLossPercentageShort) : na
// signal plots
plotshape(longCondition ? trix : na, title="Crossover", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(shortCondition ? trix : na, title="Crossunder", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plot(drawEquity ? strategy.equity : na, title="Equity", style=plot.style_columns, color=color.purple, transp=0)
// stops (better paint them into the chart..)
// plot(drawbackStop)
// plot(buyPrice)
// ORDERS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
if longCondition
strategy.entry("long", strategy.long)
if longCondition and stopEnabled
strategy.exit("Stop Loss","long", stop=drawbackStop)
// https://backtest-rookies.com/2018/04/13/tradingview-stop-losses/
if (shortCondition and not activelyShort)
strategy.close("long", true)
if (shortCondition and activelyShort)
strategy.entry("short", strategy.short)
if (shortCondition and activelyShort and stopEnabledShort)
strategy.exit("Cover Short","short", stop=shortStop)