-
Notifications
You must be signed in to change notification settings - Fork 1
/
SuperTrendFollowStrategy.pinescript
62 lines (44 loc) · 2.64 KB
/
SuperTrendFollowStrategy.pinescript
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
//@version=5
//@Author: @GeorgeFreelanceDeveloper, @LucyFreelanceDeveloper
strategy("Supertrend Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=15)
// ********************************
// User defined inputs
// ********************************
var G_BASIC_SETTINGS = "Basic settings"
var G_FILTER_SETTINGS = "Filter settings"
var G_TIME = "Time settings"
atrPeriod = input.int(defval = 10, title = "ATR Length", group = G_BASIC_SETTINGS)
factor = input.float(defval = 3.0, title = "Factor", step = 0.01, group = G_BASIC_SETTINGS)
riskPercentage = input.float(defval = 1.00, title = "Risk per trade", step = 0.01, group = G_BASIC_SETTINGS)
atrMultiplier = input.int(defval = 2, title = "ATR multiplier", group = G_BASIC_SETTINGS)
atrLength = input.int(defval = 20, title = "ATR length", group = G_BASIC_SETTINGS)
enableFilter = input.bool(defval = false, title = "Enable filter", tooltip = "Filter is close price is above 200 day simple moving average of benchmark", group = G_FILTER_SETTINGS)
benchmarkSymbol = input.symbol(defval = "SPX", title = "Benchmark", group = G_FILTER_SETTINGS)
fromDateTime = input.time(defval = timestamp("2012-01-01T01:01+0000"), title = "From Date and time", group = G_TIME)
toDateTime = input.time(defval = timestamp("9999-01-01T01:01+0000"), title = "To Date and time", group = G_TIME)
// ********************************
// Functions
// ********************************
tradeDateIsAllowed() => time >= fromDateTime and time <= toDateTime
// **********************************
// Perform calculations and analysis
// **********************************
// Filter
benchmarkSymbolClose = request.security(benchmarkSymbol,"1D", close)
filter = enableFilter ? benchmarkSymbolClose[1] >= ta.sma(source = benchmarkSymbolClose, length = 200) : true
// Trade amount
qty = ((riskPercentage/100)*strategy.equity) / (atrMultiplier * ta.atr(atrLength))
[superTrendValue, direction] = ta.supertrend(factor, atrPeriod)
buyCondition = ta.change(direction) < 0 and filter and tradeDateIsAllowed() // ta.change(direction) < 0 if ta.change(direction) return -1 then we go long
sellCondition = ta.change(direction) > 0 and tradeDateIsAllowed() // ta.change(direction) > 0 if ta.change(direction) return 1 then we go short
// ********************************
// Draw outputs
// ********************************
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
// ********************************
// Manage trade
// ********************************
if (buyCondition)
strategy.entry("My Long Entry Id", strategy.long,qty = qty)
if (sellCondition)
strategy.close("My Long Entry Id")