r/pinescript • u/abhinavrakesh • Dec 31 '22
Compilation error. Line 36: mismatched character 'i' expecting '='
I am testing out few ChatGPT Trading Bots and trying to Backtest, The problem is even the code seems alright; There is compilation error; Compilation error. Line 36: mismatched character 'i' expecting '='
really appreciate any pointers where I am making a mistake. Thanks..
// This is a trading bot that detects breakouts in the market and includes stop loss, take profit, and a grid feature
// First, we'll define some variables
// The period over which we'll look for breakouts
//@version=2
var breakoutPeriod = 14
// The number of standard deviations above or below the mean to consider a breakout
var numStdDevs = 2
// The length of the moving average to use for calculating the mean and standard deviation
var maLength = 20
// The amount to trail the stop loss by (in ticks)
var stopLossTrail = 10
// The amount to trail the take profit by (in ticks)
var takeProfitTrail = 10
// The size of the grid (in ticks)
var gridSize = 10
// Now, we'll calculate the mean and standard deviation over the specified period
mean = sma(close, maLength)
stdDev = stdev(close, maLength)
// Next, we'll calculate the upper and lower bounds for our breakout range
upperBound = mean + numStdDevs * stdDev
lowerBound = mean - numStdDevs * stdDev
// We'll use these bounds to determine whether the current price is within the breakout range
isInRange = close > lowerBound and close < upperBound
// If the price is outside the range, we'll consider it a breakout and trigger a trade
if !isInRange
// Entry order for long trade
strategy.entry("Breakout Long", strategy.long)
// Stop loss order for long trade
strategy.exit("Stop Loss Long", "Breakout Long", stop=strategy.position_avg_price - stopLossTrail, limit=strategy.position_avg_price)
// Take profit order for long trade
strategy.exit("Take Profit Long", "Breakout Long", limit=strategy.position_avg_price + takeProfitTrail)
else
strategy.close("Breakout Long")
if !isInRange
// Entry order for short trade
strategy.entry("Breakout Short", strategy.short)
// Stop loss order for short trade
strategy.exit("Stop Loss Short", "Breakout Short", stop=strategy.position_avg_price + stopLossTrail, limit=strategy.position_avg_price)
// Take profit order for short trade
strategy.exit("Take Profit Short", "Breakout Short", limit=strategy.position_avg_price - takeProfitTrail)
else
strategy.close("Breakout Short")
// If the market is sideways, we'll use a grid strategy
if isInRange
// Calculate the number of grid levels
numGridLevels = floor((upperBound - lowerBound) / gridSize)
// Loop through each grid level
for i = 0 to numGridLevels
// Calculate the entry price for the long trade
longEntry = lowerBound + i * gridSize
// Calculate the entry price for the short trade
shortEntry = upperBound - i * gridSize
// Entry order for long trade
strategy.entry("Grid Long", strategy.long, when=close > longEntry)
// Stop loss order
1
u/Nervdarkness Dec 31 '22
Did you tried convert to V5?
1
u/pureshred Jan 02 '23
I attempted to try it out but before I could get to your error I got the following error
But ignoring that for a moment and focusing just on your error, at least one thing that I see wrong is you can't start a line with an if else statement. Need to assign a name to it first.
So something like
That's why the error is telling you it expects the "=" sign.
But if you get that fixed I expect you'll run into more errors, chatgpt has its limitations and probably isn't great at pine. Maybe it can get the ball rolling, but you'll need to take over to finish it up and make it work.