0

我对使用 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)
4

1 回答 1

2

你有两个错误。

  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)

在此处输入图像描述

于 2022-01-04T21:02:09.780 回答