试图编写一个代码,在下一次交易之前给策略一个缓冲时间,声明问题



在脚本进行下一笔交易之前,我正试图找到一个缺口。我正在使用的脚本的前一个版本在特定的测试用例方面存在一些问题。这一次,tris实现了相同的功能,但我在变量声明方面遇到了一些问题

//@version=4
strategy("RSI Strategy", overlay=true)
//timeframe
FromMonth = input(defval = 9, title = "From Month", minval = 1)
FromDay   = input(defval = 9, title = "From Day", minval = 1)
FromYear  = input(defval = 2019, title = "From Year", minval = 2000)
ToMonth   = input(defval = 11, title = "To Month", minval = 1)
ToDay     = input(defval = 23, title = "To Day", minval = 1)
ToYear    = input(defval = 2020, title = "To Year", minval = 2014) 
testPeriod() =>
(time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
//Indicator
length      = input( 14 )
overSold    = input( 30 )
overBought  = input( 70 )
price       = close
vrsi        = rsi(price, length)
co          = crossover(vrsi, overSold)
cu          = crossunder(vrsi, overBought)
//For co
gap = 4
Arr_l = co ? 1 : 0
Mod_gap_l = sum(Arr_l)
buy_w_gap = Arr_l and (Mod_gap_l%gap == 0)? 1 : 0
if (buy_w_gap)
strategy.entry("RsiLE", strategy.long, comment="RsiLE")
//for cu
Arr_s = co ? 1 : 0
Mod_gap_s = sum(Arr_s)
sell_w_gap =  Arr_s and (Mod_gap_s%gap == 0)? 1 : 0
if (sell_w_gap)
strategy.entry("RsiSE", strategy.short, comment="RsiSE")

不确定你想用它做什么,但我假设是这样的。

//@version=4
strategy("RSI Strategy", overlay=true)
//timeframe
FromMonth = input(defval = 9, title = "From Month", minval = 1)
FromDay   = input(defval = 9, title = "From Day", minval = 1)
FromYear  = input(defval = 2019, title = "From Year", minval = 2000)
ToMonth   = input(defval = 11, title = "To Month", minval = 1)
ToDay     = input(defval = 23, title = "To Day", minval = 1)
ToYear    = input(defval = 2020, title = "To Year", minval = 2014) 
testPeriod() =>
(time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
//Indicator
length      = input( 14 )
overSold    = input( 30 )
overBought  = input( 70 )
price       = close
var int     gap         = 4
var float   vrsi        = na
var bool    co          = na
var bool    cu          = na
var int     Mod_gap_l   = na
var int     Mod_gap_s   = na
var bool    buy_w_gap   = na
var bool    sell_w_gap  = na
vrsi        := rsi(price, length)
co          := crossover(vrsi, overSold)
cu          := crossunder(vrsi, overBought)
//For co
// Arr_l = co ? 1 : 0
// Mod_gap_l = sum(Arr_l)
Mod_gap_l := barssince(co)
buy_w_gap := (Mod_gap_l > 0) and (Mod_gap_l%gap == 0)
if buy_w_gap
strategy.entry("RsiLE", strategy.long, comment="RsiLE")
//for cu
// Arr_s = co ? 1 : 0
// Mod_gap_s = sum(Arr_s)
Mod_gap_s   := barssince(cu) // I assume you want to use 'cu' here instead of 'co'
sell_w_gap  := (Mod_gap_s > 0) and (Mod_gap_s%gap == 0)? 1 : 0
if sell_w_gap
strategy.entry("RsiSE", strategy.short, comment="RsiSE")

我同意您提供的代码并不代表您试图实现的目标,而且问题本身也没有以明确的方式提出。。。但我确实理解你想要实现的目标。

您可以使用ta.barssince((函数来计算自条件为true以来的条形数。

例如(PineScript V5(:

//Input for defining how many bars long the cooldown period is.
CoolDownPeriod = input.int(defval=5, minval=1, maxval=100, title="Cool Down 
Period")
//Example of Buy and Sell conditions.
BuyCond  = ema1 > ema2 and ema1[1] < ema2[1]
SellCond = ema1 < ema2 and ema1[1] > ema2[1]
//If the number of bars since the sell condition is greater than the cool down 
//period then we can plot and enter a new buy trade.
if ta.barssince(SellCond) > CoolDownPeriod
plotshape(BuyCond, style=, location=, color=, text=, textcolor=, size=)
strategy.entry("entry", strategy.long, comment="")
if SellCond
plotshape(SellCond, style=, location=, color=, text=, textcolor=, size=)
strategy.close("entry", comment="")

相关内容

  • 没有找到相关文章

最新更新