-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChandelierStops.txt
90 lines (58 loc) · 3.67 KB
/
ChandelierStops.txt
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
input shortMAlength = 10;
input longMAlength = 50;
## START STUDY
## Chandelier_Stops
## linus, 2014-07-18, v0.2
#hint: thinkScript adaptation of Chandelier stops.
#hint n: lookback length for highest highs, lowest lows.
input n = 15;
#hint atrLength: Period length of avg. true range.
input atrLength = 5;
#hint atrMult: Multiplier of avg. true range.
input atrMult = 3.0;
#hint atrType: Moving average type of ATR.
input atrType = AverageType.SIMPLE;
#hint shift: Offsets the data this many bars.
input shift = 1;
#hint hideOppositeStop: Set to No to see the inactive stop.
input hideOppositeStop = No;
#hint label: Toggles P/L label.
input label = No;
#hint bubbles: Toggles P/L bubbles.
input bubbles = No;
#hint Stop Loss: Loss protection
input StopLoss = 5;
#hint tradeNum: Number of futures contracts
input tradeNum = 5;
def atr = MovingAverage(atrType, TrueRange(high, close, low)) * atrMult;
def smax = Lowest(low, n)[shift] + atr[shift];
def smin = Highest(high, n)[shift] - atr[shift];
def dir = compoundValue(1, if close > smax[1] then 1 else if close < smin[1] then -1 else dir[1], 0);
def rUB = compoundValue(1, if dir > 0 then if smax > rUB[1] then smax else rUB[1] else if dir < 0 then if smax < rUB[1] then smax else rUB[1] else rUB[1], high);
def rLB = compoundValue(1, if dir < 0 then if smin < rLB[1] then smin else rLB[1] else if dir > 0 then if smin > rLB[1] then smin else rLB[1] else rLB[1], low);
plot UB = if !hideOppositeStop or dir < 0 then rUB else Double.NaN;
plot LB = if !hideOppositeStop or dir > 0 then rLB else Double.NaN;
UB.SetDefaultColor(Color.MAGENTA);
LB.SetDefaultColor(Color.CYAN);
UB.SetLineWeight(1);
LB.SetLineWeight(1);
def orderDir = dir;
def entryPrice = EntryPrice();
def priceChangeSinceEntry = close - entryPrice;
def isOrder = orderDir crosses 0;
def orderCount = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);
def noBar = isNaN(open[-1]);
def orderPrice = if isOrder then if noBar then close else open[-1] else orderPrice[1];
def profitLoss = if !isOrder or orderCount == 1 then 0 else if orderDir > 0 then orderPrice[1] - orderPrice else if orderDir < 0 then orderPrice - orderPrice[1] else 0;
def profitLossSum = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);
AddLabel(label, orderCount + " orders | P/L " + AsDollars((profitLossSum / tickSize()) * tickValue()), if profitLossSum > 0 then Color.GREEN else if profitLossSum < 0 then Color.RED else Color.GRAY);
AddChartBubble(bubbles and isOrder and orderDir > 0, low, profitLoss, if noBar then Color.LIGHT_GRAY else Color.GREEN, 0);
AddChartBubble(bubbles and isOrder and orderDir < 0, high, profitLoss, if noBar then Color.GRAY else Color.RED, 1);
def buy = isOrder and orderDir > 0;
def sell = isOrder and orderDir < 0;
addOrder(OrderType.SELL_TO_OPEN, sell,tradeSize = tradeNum,tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "SHORT");
addOrder(OrderType.BUY_TO_CLOSE, orderDir crosses 0 and priceChangeSinceEntry <> 0,tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "CLOSE_SHORT");
addOrder(OrderType.BUY_TO_CLOSE, priceChangeSinceEntry > (StopLoss * -1),tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "CLOSE_SHORT_STOP");
addOrder(OrderType.BUY_TO_OPEN, buy,tradeSize = tradeNum,tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "LONG");
addOrder(OrderType.BUY_TO_CLOSE, orderDir crosses 0 and priceChangeSinceEntry <> 0 ,tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "CLOSE_LONG");
addOrder(OrderType.BUY_TO_CLOSE, priceChangeSinceEntry < StopLoss,tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "CLOSE_LONG_STOP");