尝试编写代码,使该策略在进行下一笔交易之前有一个缓冲时间 [英] Trying to write a code which gives the strategy a buffer time before it takes the next trades

查看:68
本文介绍了尝试编写代码,使该策略在进行下一笔交易之前有一个缓冲时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要达到的目标是差距".几支蜡烛,在这种情况下说4支,然后再进行下一笔交易nexT是柱数,在此之前不应进行下一笔交易但是每次代码运行时,nextT的值始终会重置为0.可能会对nexT的值不会自动重置的代码进行修改.

 //@ version = 4策略("RSI策略",overlay = true)//大体时间//timframeFromMonth =输入(默认值= 9,标题="From Month",minval = 1)FromDay =输入(默认值= 9,标题="From Day",minval = 1)FromYear =输入(默认值= 2019,标题="From Year",minval = 2000)ToMonth =输入(默认值= 11,标题="To Month",minval = 1)ToDay =输入(默认值= 23,标题=今天",minval = 1)ToYear =输入(默认值= 2020,标题="To Year",minval = 2014)nexT =输入(defval = 0)差距=输入(4,defval = 4)testPeriod()=>(time&time; timestamp(FromYear,FromMonth,FromDay,00,00))和(time< timestamp(ToYear,ToMonth,ToDay,23,59))//指标长度=输入(14)超卖=输入(30)超买=输入(70)价格=收盘价vrsi = rsi(价格,长度)co =交叉(vrsi,overSold)cu = crossunder(vrsi,超买)//longonly//strategy.risk.allow_entry_in(strategy.direction.long)trade = co或cuif(bar_index> = nexT)如果(交易)如果(合作)strategy.entry("RsiLE",strategy.long,comment ="RsiLE")如果(cu)strategy.entry("RsiSE",strategy.short,comment ="RsiSE")nextT = bar_index +差距 

解决方案

您的 nexT 变量是输入变量,因此是不可变的.
这意味着您无法通过代码更改其值.

解决方案是创建一个新变量 new_nexT ,该变量使用关键字 var 定义,并使用您的 nexT 输入变量进行初始化./p>

当定义不带 var 关键字的变量时,它会在每个小节上重新初始化,因此其值是易变的.
使用 var 关键字定义变量时,除非在变量上更改它的值,否则它仅在第一个小节上初始化一次,并在随后的小节上保留其值.
要更改 var 变量的值,必须使用:= 而不是 = .

使用下面的示例代码,将 new_nexT 初始化一次(因为它是 var ),并在初始化期间为其分配 nexT 输入变量.
代码中更远的地方:= 用于为其分配一个新值.

 //@ version = 4策略("RSI策略",overlay = true)//大体时间FromMonth =输入(默认值= 9,标题="From Month",minval = 1)FromDay =输入(默认值= 9,标题="From Day",minval = 1)FromYear =输入(默认值= 2019,标题="From Year",minval = 2000)ToMonth =输入(默认值= 11,标题="To Month",minval = 1)ToDay =输入(默认值= 23,标题=今天",minval = 1)ToYear =输入(默认值= 2020,标题="To Year",minval = 2014)nexT =输入(默认值= 0)差距=输入(默认值= 4)var int new_nexT = nexTtestPeriod()=>(time&time; timestamp(FromYear,FromMonth,FromDay,00,00))和(time< timestamp(ToYear,ToMonth,ToDay,23,59))//指标长度=输入(14)超卖=输入(30)超买=输入(70)价格=收盘价vrsi = rsi(价格,长度)co =交叉(vrsi,overSold)cu = crossunder(vrsi,超买)//longonly//strategy.risk.allow_entry_in(strategy.direction.long)交易= co或cuif(bar_index> = new_nexT和交易)如果(合作)strategy.entry("RsiLE",strategy.long,comment ="RsiLE")如果(cu)strategy.entry("RsiSE",strategy.short,comment ="RsiSE")new_nexT:= bar_index +差距 

What I'm trying to achieve is a "gap" of a few candles, say 4 in this case before the next trade happens Here nexT is the number of bars, before which the next trade should not take place But the value of nextT always resets to 0, everytime the code runs. What can be a possible modification to the code that the value of nexT does not reset automatically.

//@version=4
strategy("RSI Strategy", overlay=true)
//timeframe
//timframe
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) 
nexT      = input(defval=0)
gap       = input(4,defval=4)
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)
//longonly
//strategy.risk.allow_entry_in(strategy.direction.long)
trade= co or cu
if(bar_index>=nexT)
    if(trade)
        if (co)
            strategy.entry("RsiLE", strategy.long, comment="RsiLE")
        if (cu)
            strategy.entry("RsiSE", strategy.short, comment="RsiSE")
        nextT= bar_index+gap

解决方案

Your nexT variable is an input variable, so it is immutable.
That means that you cannot change it's value through code.

The solution is to create a new variable new_nexT which is defined with the keyword var and is initialized with your nexT input variable.

When you define a variable without the var keyword, it is re-initialized on every bar, so it's value is volatile.
When you define a variable with the var keyword, it's only initialized once, on the first bar, and keeps it's value on the subsequent bars, unless you change it's value.
To change a value for a var variable, you must use := instead of =.

Using my edited code example hereunder, new_nexT is initialized once (because it's a var) and assigned the nexT input variable during that initialization.
A bit further in the code, := is used to assign a new value to it.

//@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) 
nexT      = input(defval = 0)
gap       = input(defval = 4)

var int new_nexT = nexT

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)

//longonly
//strategy.risk.allow_entry_in(strategy.direction.long)

trade = co or cu

if(bar_index >= new_nexT and trade)
    if (co)
        strategy.entry("RsiLE", strategy.long, comment="RsiLE")
    if (cu)
        strategy.entry("RsiSE", strategy.short, comment="RsiSE")
    new_nexT := bar_index + gap

这篇关于尝试编写代码,使该策略在进行下一笔交易之前有一个缓冲时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆