-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDTS.txt
54 lines (40 loc) · 1.96 KB
/
DTS.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
//@version=5
strategy("Directional Trading Strategy", overlay=true)
// Define session in EST
session = input.session('0800-1600', 'Session')
// Check if current time is within trading hours
withinTradingHours = not na(time(timeframe.period, session))
// Initialize variables
var string direction = na
var float entryPrice = na
rangereq = input.int(17, "rangereq")
BTSExtendedTrading = input.bool(true, 'BTSExtendedTrading')
// Check for consecutive bars
consecutiveUp = close > open and close[1] > open[1] and close[2] > open[2] and close[3] > open[3] and close - close[3] > rangereq
consecutiveDown = close < open and close[1] < open[1] and close[2] < open[2] and close[3] < open[3] and close[3] - close > rangereq
// Set direction based on consecutive bars
if ((consecutiveUp) or (strategy.position_size > 0 and close > entryPrice)) and BTSExtendedTrading
direction := "UP"
if ((consecutiveDown) or (strategy.position_size < 0 and close < entryPrice)) and BTSExtendedTrading
direction := "DOWN"
// Manage trades based on direction
if (direction == "DOWN" and close > open) and withinTradingHours // If direction is DOWN and we encounter an UP bar
entryPrice := close
strategy.entry("Short", strategy.short)
direction := na // Reset direction after entry
if (direction == "UP" and close < open) and withinTradingHours // If direction is UP and we encounter a DOWN bar
entryPrice := close
strategy.entry("Long", strategy.long)
direction := na // Reset direction after entry
if (strategy.position_size < 0) // If in a short position
strategy.close("Short")
if (strategy.position_size > 0) // If in a long position
strategy.close("Long")
// Exite all trades at 16:00 EST
if not withinTradingHours
strategy.close_all()
// Check for profitable trades
if (strategy.position_size < 0 and close > entryPrice) // Short not profitable
direction := na
if (strategy.position_size > 0 and close < entryPrice) // Long not profitable
direction := na