Как я могу принудительно закрыть все открытые позиции в определенное время каждый день в стратегии Pine Script? - PullRequest
0 голосов
/ 25 апреля 2020

Я хотел бы знать, есть ли способ принудительного выхода из всех открытых позиций в определенное время каждый торговый день по сценарию Pine. Я написал код ниже, но он не закрывает мои открытые сделки, и иногда позиции переносятся на следующий день :

strategy("mystrategy", overlay=true)
length = input(5)
numATRs = input(0.75)

from_day   = input(defval = 1,    title = "From Day",   minval = 1)
from_month = input(defval = 1,    title = "From Month", minval = 1)
from_year  = input(defval = 2020, title = "From Year",  minval = 1970)

to_day     = input(defval = 1,    title = "To Day",     minval = 1)
to_month   = input(defval = 1,    title = "To Month",   minval = 1)
to_year    = input(defval = 2100, title = "To Year",    minval = 1970)

time_cond = (time > timestamp(from_year, from_month, from_day, 00, 00)) and (time < timestamp(to_year, to_month, to_day, 23, 59))
atrs = sma(tr, length)*numATRs
sl_inp = input(0.5, title='Stop Loss %', type=input.float)/100
tp_inp = input(1, title='Take Profit %', type=input.float)/100
stop_level = strategy.position_avg_price * (1 - sl_inp)
take_level = strategy.position_avg_price * (1 + tp_inp)
if (time_cond and not na(close[length])and hour>9 and hour<15)
    strategy.entry("VltClsLE", strategy.long, stop=close+atrs, comment = "long")
    strategy.entry("VltClsSE", strategy.short, stop=close-atrs, comment = "short")
    strategy.exit("XL", from_entry = "VltClsLE", profit = take_level, loss = stop_level,comment="exit long")
    strategy.exit("XS", from_entry = "VltClsSE", profit = take_level, loss = stop_level,comment="exit short")
else
    strategy.cancel("VltClsLE")
    strategy.cancel("VltClsSE")
    strategy.cancel("XL")
    strategy.cancel("XS")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
strategy.close_all(when=(hour==14 and minute==30),comment="force exit")

enter image description here

1 Ответ

0 голосов
/ 26 апреля 2020

пример, если я хочу уйти в воскресенье в 13:00

strategy.close("short", when =  hour == 13 and dayofweek == 1)
...