В качестве эксперимента возникла гипотеза о случайном открытии позиций и оказалось, что в TradingView уже готовая стратегия, которая это делает. Её код выглядит так:
//@version=4
strategy(title="Random Entries Work", shorttitle="REW", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100, currency=currency.USD,commission_type=strategy.commission.percent,commission_value=0)
// === GENERAL INPUTS ===
strategy = input(defval="Long Only",title="Direction",options=["Long Only", "Short Only", "Random"])
enter_frequency = input(defval=10,minval=1,maxval=100,title="Percent Chance to Enter")
exit_frequency = input(defval=3, minval=0,maxval=100,title="Percent Chance to Exit",tooltip="This should be much lower than Percent Chance to Enter. Higher values decrease time in market. Lower values increase time in market.")
start_year = input(defval=2020, title="Start Year")
// === LOGIC ===
r = random(0,100)
enter = enter_frequency > r[0]
exit = exit_frequency > r[0]
direction = random(0,100) >= 50
// === STRATEGY - LONG POSITION EXECUTION ===
enterLong() =>
strategy.opentrades == 0 and enter and (strategy == "Long Only" or (strategy == "Random") and direction) and
time > timestamp(start_year, 01, 01, 01, 01)
exitLong() =>
exit
strategy.entry(id="Long", long=strategy.long, when=enterLong())
strategy.close(id="Long", when=exitLong())
// === STRATEGY - SHORT POSITION EXECUTION ===
enterShort() =>
strategy.opentrades == 0 and enter and (strategy == "Short Only" or (strategy == "Random" and not direction)) and
time > timestamp(start_year, 01, 01, 01, 01)
exitShort() =>
exit
strategy.entry(id="Short", long=strategy.short, when=enterShort())
strategy.close(id="Short", when=exitShort())
(
Читать дальше )