【问题标题】:No data when back testing pine script code回测 pine 脚本代码时没有数据
【发布时间】:2022-01-04 15:39:47
【问题描述】:

我对使用 pinescript 很陌生。我尝试使用移动平均线编写一个简单的交叉交易策略。我从 youtube 上的教程视频中复制了代码。问题是在视频中使用了 pine 4,而我使用的是最新版本 pine 5,我确定这是问题的原因。

问题是当我将我的策略添加到图表时,它没有显示数据概览和交易列表,这意味着我的脚本不能执行交易。

就像我说的我是 pinescript 的新手,因为这是我创建的第一个策略。任何帮助将不胜感激!

//@version=5
strategy("Bitcoin SMA", overlay = true, initial_capital = 1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, margin_long = 100, margin_short = 100)


start_date = input.int(defval = 1, title = "Start Date")
start_month = input.int(defval = 1, title = "Start Month")
start_year = input.int(defval = 2020, title = "Start Year")

end_date = input.int(defval = 1, title = "End Date")
end_month = input.int(defval = 1, title = "End Month")
end_year = input.int(defval = 2021, title = "End Year")

fast_ma_period = input.int(defval = 5, title = "Fast MA")
slow_ma_period = input.int(defval = 15, title = "Slow MA")


fast_ma = ta.sma(close,fast_ma_period)
slow_ma = ta.sma(close, slow_ma_period)

between_dates = (time >= timestamp(start_year, start_month, start_date, 1, 0) and (time < timestamp(end_year, end_month, end_date, 23, 59)))

plot(fast_ma, color = color.green, linewidth = 1)
plot(slow_ma, color = color.yellow, linewidth = 3)

buy_condition = ta.crossover(fast_ma, slow_ma)
sell_condition = ta.crossunder(fast_ma, slow_ma)


if between_dates
    strategy.entry("BTC enter", strategy.long, 1, when = buy_condition)
    strategy.close("BTC close", when = sell_condition)

【问题讨论】:

    标签: pine-script pinescript-v5


    【解决方案1】:

    你有两个错误。

    1. strategy.entry("BTC enter", strategy.long, 1, when = buy_condition) 表示您想购买 1 BTC。但是你没有足够的资金。您的初始资本设置为 1000 美元。将其增加到 10000 美元左右。

    2. strategy.close() 的第一个参数是您要关闭的交易的 id。在这种情况下,您应该传递"BTC enter",而不是"BTC close"

    这里是完整的代码:

    //@version=5
    strategy("Bitcoin SMA", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, margin_long = 100, margin_short = 100)
    
    
    start_date = input.int(defval = 1, title = "Start Date")
    start_month = input.int(defval = 1, title = "Start Month")
    start_year = input.int(defval = 2020, title = "Start Year")
    
    end_date = input.int(defval = 1, title = "End Date")
    end_month = input.int(defval = 1, title = "End Month")
    end_year = input.int(defval = 2021, title = "End Year")
    
    fast_ma_period = input.int(defval = 5, title = "Fast MA")
    slow_ma_period = input.int(defval = 15, title = "Slow MA")
    
    
    fast_ma = ta.sma(close,fast_ma_period)
    slow_ma = ta.sma(close, slow_ma_period)
    
    between_dates = (time >= timestamp(start_year, start_month, start_date, 1, 0) and (time < timestamp(end_year, end_month, end_date, 23, 59)))
    
    plot(fast_ma, color = color.green, linewidth = 1)
    plot(slow_ma, color = color.yellow, linewidth = 3)
    
    buy_condition = ta.crossover(fast_ma, slow_ma)
    sell_condition = ta.crossunder(fast_ma, slow_ma)
    
    
    if between_dates
        strategy.entry("BTC enter", strategy.long, 1, when = buy_condition)
        strategy.close("BTC enter", when = sell_condition)
    

    【讨论】:

    • 您好vitruvius,非常感谢您的回复!有没有办法交易一美元的股票而不是股票的数量,例如买入/卖出 1000 美元与买入/卖出 1 股?我尝试在 pine 5 文档中找到有关该内容的内容,但没有任何运气。
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 2021-05-27
    • 2021-11-18
    • 1970-01-01
    • 2022-08-12
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多