-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTradingViewBackTest
44 lines (36 loc) · 2.17 KB
/
TradingViewBackTest
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
//@version=4
strategy("Crypto Monday-Wednesday Strategy", overlay=true)
// User-configurable parameters
entryHour = input(defval=22, title="Entry Hour", minval=0, maxval=23)
entryDay = input(defval=dayofweek.monday, title="Entry Day", options=[dayofweek.monday, dayofweek.tuesday, dayofweek.wednesday, dayofweek.thursday, dayofweek.friday, dayofweek.saturday, dayofweek.sunday])
exitHour = input(defval=1, title="Exit Hour", minval=0, maxval=23)
exitDay = input(defval=dayofweek.wednesday, title="Exit Day", options=[dayofweek.monday, dayofweek.tuesday, dayofweek.wednesday, dayofweek.thursday, dayofweek.friday, dayofweek.saturday, dayofweek.sunday])
positionSizePercent = input(defval=0.80, title="Position Size (%)", minval=0.01, maxval=1.0)
lossPercent = input(defval=0.02, title="Loss Percentage (%)", minval=0.01, maxval=1.0)
// Calculate position size based on initial equity
initial_equity = strategy.equity[0]
positionSize = initial_equity * positionSizePercent / close
// Mutable variables
var tradeActive = false
var stopLossPrice = 0.0
// Entry Condition: User-defined day and hour
longCondition = (hour == entryHour) and (dayofweek == entryDay)
if (longCondition)
tradeActive := true
stopLossPrice := close * (1 - (lossPercent / positionSizePercent))
strategy.entry("Trade Entry", strategy.long, qty=positionSize, stop=stopLossPrice)
// Exit Condition: User-defined day and hour
closeCondition = (hour == exitHour) and (dayofweek == exitDay)
// Check if stop loss was hit
stopLossHitCondition = tradeActive and low <= stopLossPrice
if (stopLossHitCondition)
strategy.close("Trade Entry")
tradeActive := false
// Close on user-defined exit day and hour
if (closeCondition)
strategy.close("Trade Entry")
tradeActive := false
// Plotting
plotshape(series=longCondition, title="Entry Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="Buy")
plotshape(series=closeCondition or stopLossHitCondition, title="Exit Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")
plotshape(series=stopLossHitCondition, title="Stop Loss Hit", location=location.abovebar, color=color.orange, style=shape.labeldown, text="SL")