michell1551
michell1551

Reputation: 11

No data when back testing pine script code

I am very new to using pinescript. I tried just writing a simple crossover trading strategy using moving averages. I copied the code from a tutorial video on youtube. The issue is that in the video pine 4 was used, and I am using the newest version, pine 5, which I'm sure is the cause of the issue.

The issue is that when I add my strategy to the chart, it shows no data for overview and list of trades, meaning my script must not be executing the trades.

Like I said I am new to pinescript, as this is my first strategy I created. Any help would be greatly appreciated!

//@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)

Upvotes: 0

Views: 266

Answers (1)

vitruvius
vitruvius

Reputation: 21294

You have two mistakes.

  1. With strategy.entry("BTC enter", strategy.long, 1, when = buy_condition), you are telling that you want to buy 1 BTC. But you don't have enough capital. Your initial capital is set to $1000. Increase it to something like $10000.

  2. First argument of strategy.close() is the id of the trade you want to close. In this case you should pass "BTC enter", instead of "BTC close".

Here is the full code for you:

//@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)

enter image description here

Upvotes: 2

Related Questions