How to do forward or back testing using PineScript and Tradingview.com


Posted by: Invostock.com
Published on: January 15, 2023
How to do forward or back testing using PineScript and Tradingview.com

Pine Script is a programming language used to create indicators and trading strategies for the trading platform TradingView. It is a proprietary language developed and used by TradingView, and is based on the programming language Pine. Pine Script allows users to create custom indicators and automated trading strategies, which can then be backtested and shared on the TradingView platform. The language is designed to be easy to learn, with a simple syntax and built-in functions for common trading operations.

Here are some basic syntax for Pine Script with examples:

  • Variables: Variables are used to store values. In Pine Script, you can declare a variable using the keyword var followed by the variable name and the value you want to assign to it.
​​​​​​var myVariable = 50
  • Functions: Functions are blocks of code that can be reused. In Pine Script, you can define a function using the keyword function followed by the function name and a set of parentheses.
 function myFunction() {
// code goes here
}
  • Conditional statements: Conditional statements are used to control the flow of the program based on certain conditions. In Pine Script, you can use the if statement to check a condition and execute a block of code if the condition is true.
 if myVariable > 50 {
    // code to execute if myVariable is greater than 50
}
  • Loops: Loops are used to execute a block of code multiple times. In Pine Script, you can use the for loop to iterate over a range of values.
for i = 1 to 10 {
    // code to execute for each value of i
}
 
  • Plotting: To draw things on the chart you can use the plot function. The first argument is the value you want to plot, and the second argument is the color of the line.
plot(myVariable, color=blue)
 

Start building strategy using Ai-Strategy Builder by InvoStock.com.

Credit: Image by Freepik