想适应这种交易视图策略,但是又遇到了问题

时间:2019-09-21 07:19:05

标签: algorithmic-trading indicator pine-script

我找到了一个我认为自己理解的简单策略,并试图通过在其中添加一个简单的“ if”逻辑来适应它,然后我打破了它。

//@version=4
strategy("Kozlod - RSI Strategy - 1 minute", overlay=true, 
 default_qty_type=strategy.percent_of_equity, commission_value=0.0025, default_qty_value=100)

// 
// author: Kozlod
// date: 2019-05-12
// https://www.tradingview.com/u/Kozlod/
// https://t.me/quantnomad
//

// Inputs
length = input(65)
overSold = input(40)
overBought = input(60)
price = input(close)

// RSI
vrsi = rsi(price, length)

boughtp = if crossover(vrsi, overSold)
    strategy.entry("RsiLE", strategy.long, comment="RsiLE")
    close

if crossunder(vrsi, overBought) and close >= boughtp*0.0025 +boughtp
    strategy.entry("RsiSE", strategy.short, comment="RsiSE")

原始代码是

vrsi = rsi(price, length)

if crossover(vrsi, overSold)
    strategy.entry("RsiLE", strategy.long, comment="RsiLE")

if crossunder(vrsi, overBought)
    strategy.entry("RsiSE", strategy.short, comment="RsiSE")

我试图做的是诱使算法仅在最初购买后的价格大于某个百分比+购买价格的情况下才出售。

这样一来,在确定某项策略是否使我感到不适时,我可以将交易费用考虑在内。

如果这不值得,那就没有必要进行买卖。它应该等到价格达到买入和卖出产生的利润。

任何想法都值得赞赏。

1 个答案:

答案 0 :(得分:0)

您之前的代码并没有保存条形图上的买入价,而是始终使用=运算符对其进行了初始化。此代码可以正确保存它并绘制测试值,以便您可以看到它。我在头寸大小上添加了一个条件,以便在执行多头输入时仅保存新的买入值,否则每次发生交叉时,都会为您的boughtp变量分配一个新值,但这并不总是转换为新订单。

var boughtp = 0.0
if crossover(vrsi, overSold) and strategy.position_size <= 0
    strategy.entry("RsiLE", strategy.long, comment="RsiLE")
    boughtp := close

plot(boughtp * 1.0025, style = plot.style_circles)