Pinescript Template_Testing
MAKE SURE TO GENERATE CODE WITH THESE ATTRIBUTES:
- Write all code below suitable for Pine Script version 5
- It must be suitable for TradingView Pine Editor
- Never use the word "crossover" or "crossunder" and replace with "ta.crossover" and "ta.crossunder" respectively
// This code is meant to allow backtesting of your strategy over defined time periods
// @version=5
strategy("insert_strategy_name_here", overlay=true)
// BACKTESTING RANGE
// Start of back testing (Change defval numbers for day, month and year. This is when back testing starts)
fromDay = input.int(defval=1, title="From Day", minval=1, maxval=31)
fromMonth = input.int(defval = 12, title = "From Month", minval = 1, maxval = 12)
fromYear = input.int(defval = 2023, title = "From Year", minval = 1970)
// End of back testing (Change defval numbers for day, month and year. This is when back testing ends)
toDay = input.int(defval = 7, title = "To Day", minval = 1, maxval = 31)
toMonth = input.int(defval = 12, title = "To Month", minval = 1, maxval = 12)
toYear = input.int(defval = 2024, title = "To Year", minval = 1970)
// Calculate start/end date and time condition (DO NOT TOUCH)
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = time >= startDate and time <= finishDate
// STRATEGIES
// Input Strategy Variables
// Buy condition:
buy_signal = (insert_buy_signal_here)
// Sell condition:
sell_signal = (insert_sell_signal_here)
// Change this value to determine the $ amount to trade with
qty_value = 1000
// DO NOT TOUCH
price = close
qty = qty_value / price
// Plot buy and sell signals on the chart (DO NOT TOUCH)
plotshape(series=buy_signal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(series=sell_signal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
// Check if there are any open trades (DO NOT TOUCH)
open_trades = strategy.opentrades
// Order Execution (DO NOT TOUCH)
if (buy_signal and open_trades == 0 and time_cond)
strategy.entry("Buy", strategy.long, qty=qty)
if (sell_signal)
strategy.close("Buy")
// (DO NOT TOUCH)
if (not time_cond)
strategy.close_all()
strategy.cancel_all()
Last updated