This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NDAQ_Scalper_CCI_strategy.pine
63 lines (51 loc) · 3.3 KB
/
NDAQ_Scalper_CCI_strategy.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
//@version=3
strategy(title="CCI Scalper Strategy", shorttitle="CCI Scalper Strategy", overlay=false, default_qty_value=1, backtest_fill_limits_assumption=1, calc_on_every_tick=false)
FastMA = input(10, minval=1)
SlowMA = input(20, minval=1)
upperBoundLine = input(137, "CCI Upper line", type=integer)
lowerBoundLine = input(-142, "CCI Lower line", type=integer)
useSMA = input(false, "Use SMA with CCI", type=bool)
trailPoints = input(140, "Trail Points (in ticks)", type=integer)
trailOffset = input(100, "Trail Offset (in ticks)", type=integer)
testType = input('Trail Points', "Test Points or Price", options=['Trail Points', 'Trail Price'])
cciPeriod = input(20, "CCI Period (number of bars)", type=integer)
// === INPUT BACKTEST RANGE ===
FromDay = input(defval = 14, title = "From Day", minval = 1, maxval = 31)
FromMonth = input(defval = 11, title = "From Month", minval = 1, maxval = 12)
FromYear = input(defval = 2018, title = "From Year", minval = 2017)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)
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 // create function "within window of time"
hline(upperBoundLine, title = "Overbought upper line", color=orange, linestyle=dashed)
hline(lowerBoundLine, title = "Oversold lower line", color=orange, linestyle=dashed)
xCCI = cci(close, cciPeriod)
xSMA = sma(xCCI, SlowMA)
xFMA = sma(xCCI, FastMA)
pos = 0
pos := iff(xSMA < xFMA , 1, iff(xSMA > xFMA, -1, nz(pos[1], 0)))
inOpenPosition = strategy.position_size != 0
inPreviousBuy = strategy.position_size > 0
inPreviousSell = strategy.position_size < 0
isSellSignal = useSMA ? crossunder(xSMA, xFMA) : crossunder(xCCI, upperBoundLine)
isBuySignal = useSMA ? crossover(xSMA, xFMA) : crossover(xCCI, lowerBoundLine)
// Visualise the Opening positions
plotshape((isSellSignal and inPreviousSell) ? 1 : na, style=shape.arrowdown, location=location.absolute, color=green, size=size.large)
plotshape((isBuySignal and inPreviousBuy) ? 1 : na, style=shape.arrowup, location=location.absolute, color=red, size=size.large)
plotshape((isSellSignal and not inPreviousSell) ? 1 : na, style=shape.arrowdown, location=location.absolute, color=green, size=size.large, text="Sell")
plotshape((isBuySignal and not inPreviousBuy) ? 1 : na, style=shape.arrowup, location=location.absolute, color=red, size=size.large, text="Buy")
// barcolor(pos == -1 ? red: pos == 1 ? green : blue)
plot(useSMA ? xSMA : na, color=red, title="CCI MA Slow")
plot(useSMA ? xFMA : na, color=blue, title="CCI MA FAST")
plot(not useSMA ? xCCI : na, color=(isSellSignal or isBuySignal) ? red : green, title="CCI Standard")
if (isBuySignal and not inPreviousBuy)
strategy.close("sell")
strategy.order("buy", strategy.long, 1, comment="buy", when=window())
if (isSellSignal and not inPreviousSell)
strategy.close("buy")
strategy.order("sell", strategy.short, 1, comment="sell", when=window())
// Visualise price change for open positions
bgcolor(inPreviousBuy ? green : na, transp=97)
bgcolor(inPreviousSell ? red : na, transp=97)