I am having an issue with pinescript. Never used it before and havent coded in years so im very rusty. I would love an opinion of why I am getting the error on line 21, which is the only 'else if' line in the block.
//@version=4
tradeDirection = input(direction.up, "Trade Direction") // direction of the breakout (up or down)
breakoutPeriod = input(14, "Breakout Period") // number of bars to look back for the breakout
stopLoss = input(0.002, "Stop Loss") // stop loss in percentage of the trade value
takeProfit = input(0.004, "Take Profit") // take profit in percentage of the trade value
maxTrades = input(2, "Maximum Number of Trades") // maximum number of trades to have open at the same time
maxRisk = input(0.01, "Maximum Risk per Trade") // maximum risk per trade in percentage of the account value
// Next, we create a variable to track the highest or lowest price in the breakout period
breakoutPrice = tradeDirection == direction.up ? highest(high, breakoutPeriod) : lowest(low, breakoutPeriod)
// Then, we check if the current price has broken out of the breakout period and if we have not reached the maximum number of open trades
if (tradeDirection == direction.up and close > breakoutPrice and strategy.opentrades < maxTrades)
// If it has, we calculate the trade size based on the maximum risk per trade
tradeSize = maxRisk / (stopLoss * close)
// And enter a long position with the calculated trade size
strategy.entry("Long", strategy.long, tradeSize)
else if (tradeDirection == direction.down and close < breakoutPrice and strategy.opentrades < maxTrades)
// If it has, we calculate the trade size based on the maximum risk per trade
tradeSize = maxRisk / (stopLoss * close)
// And enter a short position with the calculated trade size
strategy.entry("Short", strategy.short, tradeSize)
// Finally, we set our stop loss and take profit levels
strategy.exit("Stop Loss", "Long", stopLossType.percent, stopLoss)
strategy.exit("Take Profit", "Long", profitType.percent, takeProfit)
// We repeat the same process for the short position
strategy.exit("Stop Loss", "Short", stopLossType.percent, stopLoss)
strategy.exit("Take Profit", "Short", profitType.percent, takeProfit)
I've looked around at other versions to see if its just a version issue but nothing that gives me a reaosn why its erroring out. I get the feeling its something before or within the else if statement.
CodePudding user response:
I retyped your script so it would compile and have no warnings or errors. One problem was the indentation was missing on the if
and else
. Another problem was the direction.up
variable was missing. It is not allowed to have a .
in a variable name. Another problem was that strategy("breakout strat")
was missing, so I added it. The logic in the code I typed is probably wrong, but there are no errors.
//@version=4
strategy("breakout strat")
tradeDirection = input(true, "Trade Direction") // direction of the breakout (up=true or down=false)
breakoutPeriod = input(14, "Breakout Period") // number of bars to look back for the breakout
stopLoss = input(0.002, "Stop Loss") // stop loss in percentage of the trade value
takeProfit = input(0.004, "Take Profit") // take profit in percentage of the trade value
maxTrades = input(2, "Maximum Number of Trades") // maximum number of trades to have open at the same time
maxRisk = input(0.01, "Maximum Risk per Trade") // maximum risk per trade in percentage of the account value
// Next, we create a variable to track the highest or lowest price in the breakout period
breakoutPrice = tradeDirection == true ? highest(high, breakoutPeriod) : lowest(low, breakoutPeriod)
// Then, we check if the current price has broken out of the breakout period and if we have not reached the maximum number of open trades
if (tradeDirection == true and close > breakoutPrice and strategy.opentrades < maxTrades)
// If it has, we calculate the trade size based on the maximum risk per trade
tradeSize = maxRisk / (stopLoss * close)
// And enter a long position with the calculated trade size
strategy.entry("Long", strategy.long, tradeSize)
else if (tradeDirection == false and close < breakoutPrice and strategy.opentrades < maxTrades)
// If it has, we calculate the trade size based on the maximum risk per trade
tradeSize = maxRisk / (stopLoss * close)
// And enter a short position with the calculated trade size
strategy.entry("Short", strategy.short, tradeSize)
// Finally, we set our stop loss and take profit levels
strategy.exit("Stop Loss", "Long", loss=stopLoss)
strategy.exit("Take Profit", "Long", loss=takeProfit)
// We repeat the same process for the short position
strategy.exit("Stop Loss", "Short", loss=stopLoss)
strategy.exit("Take Profit", "Short", loss=takeProfit)
CodePudding user response:
Put an indentation on line 21 to get rid of this error :
if .....
----> First block
else if
----> Other block
In your code other indentations are missing, and other error are present (direction.up doesn't exist for example... but you should ask another question for another error)