-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paththinkscript_template
193 lines (160 loc) · 5.41 KB
/
thinkscript_template
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# thinkscript strategy template
##############################################################
####################### Strategy Setup #####################
##############################################################
# Common settings for studies
input length = 14;
input over_sold = 20;
input over_bought = 80;
input look_back = 3;
input fastLength = 20;
input slowLength = 60;
input trendLength = 200;
input averageType = AverageType.Exponential;
def FastMA = MovingAverage(averageType, close, fastLength);
def SlowMA = MovingAverage(averageType, close, slowLength);
# Trading Hours
input marketOpenBuffer = 15;
input marketCloseBuffer = 30;
def min_until_close =
(RegularTradingEnd(GetYYYYMMDD()) - GetTime()) / AggregationPeriod.MIN;
AddLabel(yes, "Minutes until close (mins): " + min_until_close);
def min_from_open =
(GetTime() - RegularTradingStart(GetYYYYMMDD())) / AggregationPeriod.MIN;
AddLabel(yes, "Mintues since the open: " + min_from_open);
# Position sizing
input margin = 5000;
input account_balance = 10000;
input position_size = .01; # Risk 1% per trade
input risk_reward_ratio = 2;
def orderSize =
if (GetSymbol() == "/ES") then 1
else RoundDown(margin / (close / 2), 0);
# Targets
def entryPrice = EntryPrice();
input ES_Prof_Tar = 20;
input ES_Loss_Lim = 8;
input offsetType = {percent, value, default tick};
def mult;
switch (offsetType) {
case percent:
mult = entryPrice / 100;
case value:
mult = 1;
case tick:
mult = TickSize();
}
def tickLossLim =
if (GetSymbol() == "/ES") then ES_Loss_Lim
else ((account_balance * position_size) / orderSize) * 100;
def tickProfTarget =
if (GetSymbol() == "/ES") then ES_Prof_Tar
else ((account_balance * position_size) / orderSize) * 100 * risk_reward_ratio;
def longStopPrice = entryPrice - tickLossLim * mult;
def shortStopPrice = entryPrice + tickLossLim * mult;
def longLimitPrice = entryPrice + tickLossLim * mult * risk_reward_ratio;
def shortLimitPrice = entryPrice - tickLossLim * mult * risk_reward_ratio;
# Trailing stops
def price = if IsNaN(entryPrice[1]) then entryPrice else if !IsNaN(entryPrice) then Max(high, price[1]) else entryPrice;
def trailStopPrice = price - tickLossLim * mult;
addOrder(OrderType.SELL_TO_CLOSE,
low <= trailStopPrice,
name = "Trailing Stop");
addOrder(OrderType.BUY_TO_CLOSE,
high >= trailStopPrice,
name = "Trailing Stop");
# Are profit targets enabled
input enable_profit_target = {default Y, N};
def p_target;
switch (enable_profit_target) {
case Y:
p_target = 1;
case N:
p_target = 0;
}
# Are stop losses enabled
input enable_loss_limit = {default Y, N};
def l_target;
switch (enable_profit_target) {
case Y:
l_target = 1;
case N:
l_target = 0;
}
# Trade direction
input trade_direction = {default Both, Long, Short};
def open_trade;
switch (trade_direction) {
case Both:
open_trade = 2;
case Long:
open_trade = 1;
case Short:
open_trade = 0;
}
def open_longs = if open_trade == 2 or
open_trade == 1 then 1
else 0;
def open_shorts = if open_trade == 2 or
open_trade == 0 then 1
else 0;
##############################################################
####################### Strategy Logic #####################
##############################################################
##############################################################
###################### Enter Position ######################
##############################################################
AddOrder(OrderType.BUY_TO_OPEN,
open_longs == 1 and
min_until_close > marketCloseBuffer and
min_from_open > marketOpenBuffer,
price = open[-1],
tradeSize = orderSize,
name = "Long Open");
AddOrder(OrderType.SELL_TO_OPEN,
open_shorts == 1 and
min_until_close > marketCloseBuffer and
min_from_open > marketOpenBuffer,
price = open[-1],
tradeSize = orderSize,
name = "Short Open");
##############################################################
################# Exit Profit Target #######################
##############################################################
AddOrder(OrderType.SELL_TO_CLOSE,
p_target == 1 and
high >= longLimitPrice,
price = longLimitPrice,
name = "Profit Target");
AddOrder(OrderType.BUY_TO_CLOSE,
p_target == 1 and
low <= shortLimitPrice,
price = shortLimitPrice,
name = "Profit Target");
##############################################################
################# Exit Loss Limit ##########################
##############################################################
AddOrder(OrderType.SELL_TO_CLOSE,
l_target == 1 and
low <= longStopPrice,
price = longStopPrice,
name = "Stop Loss");
AddOrder(OrderType.BUY_TO_CLOSE,
l_target == 1 and
high >= shortStopPrice,
price = shortStopPrice,
name = "Loss Limit");
##############################################################
################# Exit Market Closing ######################
##############################################################
AddOrder(OrderType.SELL_TO_CLOSE,
min_until_close <= 0,
price = close,
name = "Flat at Market Close");
AddOrder(OrderType.BUY_TO_CLOSE,
min_until_close <= 0,
price = close,
name = "Flat at Market Close");
##############################################################
################# End Script ###############################
##############################################################