-
Notifications
You must be signed in to change notification settings - Fork 5
/
TurtleTrendFollowStrategy.pinescript
68 lines (51 loc) · 2.67 KB
/
TurtleTrendFollowStrategy.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
63
64
65
66
67
//@version=5
//@Author: @GeorgeFreelanceDeveloper, @LucyFreelanceDeveloper
strategy("Turtle Trend Follow Strategy", initial_capital=1000000, overlay=true, pyramiding = 0)
// ********************************
// User defined inputs
// ********************************
var G_BASIC_SETTINGS = "Basic settings"
var G_FILTER_SETTINGS = "Filter settings"
var G_TIME = "Time settings"
entryLength = input.int(defval = 20, title = "Entry length", group = G_BASIC_SETTINGS)
exitLength = input.int(defval = 10, title = "Exit length", group = G_BASIC_SETTINGS)
riskPercentage = input.float(defval = 1, title = "Risk per trade", group = G_BASIC_SETTINGS, tooltip = "Values from 0.00 to 100")
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
// **********************************
// Basic
upperChannel = ta.highest(high[1], entryLength)
lowerChannel = ta.lowest(low[1], entryLength)
exitLong = ta.lowest(low[1], exitLength)
// Trade amount
qty = ((riskPercentage/100)*strategy.equity) / (atrMultiplier * ta.atr(atrLength))
// Filter
benchmarkSymbolClose = request.security(benchmarkSymbol,"1D", close)
filter = enableFilter ? benchmarkSymbolClose[1] >= ta.sma(source = benchmarkSymbolClose, length = 200) : true
buyCondition = ta.crossover(close, upperChannel) and filter and tradeDateIsAllowed()
sellCondition = ta.crossunder(close, exitLong) and tradeDateIsAllowed()
// ********************************
// Draw outputs
// ********************************
plot(upperChannel, color=color.green, title="Upper Channel")
plot(lowerChannel, color=color.red, title="Lower Channel")
plot(exitLong, color=color.gray, title="Exit Long", style=plot.style_cross)
// ********************************
// Manage trade
// ********************************
// Entry
if (buyCondition)
strategy.entry("Long", strategy.long, qty = qty)
// Exit
if (sellCondition)
strategy.close("Long")