-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathback_testing_tradingview_script
68 lines (37 loc) · 1.87 KB
/
back_testing_tradingview_script
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
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © awakae
//@version=4
strategy(title="EMA & SMA & RSI", shorttitle="EMA & SMA & RSI", overlay=true, initial_capital=50000, currency="EUR", default_qty_value=250000, pyramiding=2)
// === INPUT BACKTEST RANGE ===
FromMonth = input(defval=8, title="From Month", minval=1, maxval=12)
FromDay = input(defval=22, title="From Day", minval=1, maxval=31)
FromYear = input(defval=2020, title="From Year", minval=2015)
ToMonth = input(defval=10, title="To Month", minval=1, maxval=12)
ToDay = input(defval=26, title="To Day", minval=1, maxval=31)
ToYear = input(defval=2020, title="To Year", minval=2010)
// === Back testing ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() =>
time >= start and time <= finish ? true : false
//SMA
BBbasis = sma(close, 20)
//EMA
fastEMA = ema(close, 5)
slowEMA = ema(close, 75)
//RSI
RSI = rsi(close, 21)
// Chart
plot(BBbasis, title="SMA", color=color.green)
plot(fastEMA, title="FastEMA", color=color.white)
plot(slowEMA, title="SlowEMA", color=color.yellow)
//Strategy
buy = BBbasis<close and slowEMA<close and fastEMA>slowEMA
short = BBbasis>close and slowEMA>close and fastEMA<slowEMA
if(window())
strategy.entry(id="long", long=true, when=buy)
strategy.entry(id="short", long=false, when=short)
if (strategy.position_size > 0 and window())
strategy.exit(id="tsl long", from_entry="long", profit=200, trail_points= close * 0.02 / syminfo.mintick, trail_offset=close * 0.02 / syminfo.mintick)
if (strategy.position_size < 0 and window())
strategy.exit(id="tsl short", from_entry="short", profit=200, trail_points= close * 0.02 / syminfo.mintick, trail_offset=close * 0.02 / syminfo.mintick)