//@version=5 strategy("Fibonacci Strategy - Tesla", overlay=true) // Calculate Fibonacci…
- User
//@version=5 strategy("Fibonacci Strategy - Tesla", overlay=true)
// Calculate Fibonacci retracement levels highPrice = ta.highest(high, 50) lowPrice = ta.lowest(low, 50) priceRange = highPrice - lowPrice
fibonacciLevels = [0.0, 0.236, 0.382, 0.5, 0.618, 0.786]
fibonacciRetracements = [] for i = 0 to 5 level = highPrice - priceRange * fibonacciLevels[i] fibonacciRetracements := array.concat(fibonacciRetracements, [level])
// Plot Fibonacci retracement levels for i = 0 to 5 line.new(x1=bar_index[1], y1=fibonacciRetracements[i], x2=bar_index, y2=fibonacciRetracements[i], color=color.gray, width=1, extend=extend.right)
// Define buy and sell conditions buyCondition = ta.crossover(close, fibonacciRetracements[2]) sellCondition = ta.crossunder(close, fibonacciRetracements[4])
// Execute trades strategy.entry("Buy", strategy.long, when=buyCondition) strategy.close("Buy", when=sellCondition)
// Plot buy and sell signals plotshape(buyCondition, title="Buy Signal", color=color.green, style=shape.labelup, location=location.belowbar) plotshape(sellCondition, title="Sell Signal", color=color.red, style=shape.labeldown, location=location.abovebar)
above is the pine script for tesla stock. Can you please fix the error below:
4:48:19 PM Error at 12:19 Syntax error at input '['
- Assistant
It looks like the issue stems from using the square brackets
[ ]to define your lists. For Pine Script version 5, you should utilize the arrays module when creating a list. Here's a modified script with the required changes://@version=5 strategy("Fibonacci Strategy - Tesla", overlay=true) // Calculate Fibonacci retracement levels highPrice = ta.highest(high, 50) lowPrice = ta.lowest(low, 50) priceRange = highPrice - lowPrice fibonacciLevels = array.new_float(6) array.set(fibonacciLevels, 0, 0.0) array.set(fibonacciLevels, 1, 0.236) array.set(fibonacciLevels, 2, 0.382) array.set(fibonacciLevels, 3, 0.5) array.set(fibonacciLevels, 4, 0.618) array.set(fibonacciLevels, 5, 0.786) fibonacciRetracements = array.new_float(6) for i = 0 to 5 level = highPrice - priceRange * array.get(fibonacciLevels, i) array.set(fibonacciRetracements, i, level) // Plot Fibonacci retracement levels for i = 0 to 5 line.new(x1=bar_index[1], y1=array.get(fibonacciRetracements, i), x2=bar_index, y2=array.get(fibonacciRetracements, i), color=color.gray, width=1, extend=extend.right) // Define buy and sell conditions buyCondition = ta.crossover(close, array.get(fibonacciRetracements, 2)) sellCondition = ta.crossunder(close, array.get(fibonacciRetracements, 4)) // Execute trades strategy.entry("Buy", strategy.long, when=buyCondition) strategy.close("Buy", when=sellCondition) // Plot buy and sell signals plotshape(buyCondition, title="Buy Signal", color=color.green, style=shape.labelup, location=location.belowbar) plotshape(sellCondition, title="Sell Signal", color=color.red, style=shape.labeldown, location=location.abovebar)With this updated version, you should not receive any syntax errors.
- User
now its giving me this error:
Error at 5:10 no viable alternative at character '“'
- Assistant
Apologies for the confusion. It appears the quotation marks are not standard ASCII double quotes. Please replace the fancy quotes with standard double quotes like so:
//@version=5 strategy("Fibonacci Strategy - Tesla", overlay=true) // Calculate Fibonacci retracement levels highPrice = ta.highest(high, 50) lowPrice = ta.lowest(low, 50) priceRange = highPrice - lowPrice fibonacciLevels = array.new_float(6) array.set(fibonacciLevels, 0, 0.0) array.set(fibonacciLevels, 1, 0.236) array.set(fibonacciLevels, 2, 0.382) array.set(fibonacciLevels, 3, 0.5) array.set(fibonacciLevels, 4, 0.618) array.set(fibonacciLevels, 5, 0.786) fibonacciRetracements = array.new_float(6) for i = 0 to 5 level = highPrice - priceRange * array.get(fibonacciLevels, i) array.set(fibonacciRetracements, i, level) // Plot Fibonacci retracement levels for i = 0 to 5 line.new(x1=bar_index[1], y1=array.get(fibonacciRetracements, i), x2=bar_index, y2=array.get(fibonacciRetracements, i), color=color.gray, width=1, extend=extend.right) // Define buy and sell conditions buyCondition = ta.crossover(close, array.get(fibonacciRetracements, 2)) sellCondition = ta.crossunder(close, array.get(fibonacciRetracements, 4)) // Execute trades strategy.entry("Buy", strategy.long, when=buyCondition) strategy.close("Buy", when=sellCondition) // Plot buy and sell signals plotshape(buyCondition, title="Buy Signal", color=color.green, style=shape.labelup, location=location.belowbar) plotshape(sellCondition, title="Sell Signal", color=color.red, style=shape.labeldown, location=location.abovebar)This version should not generate any syntax errors.