This is the code it gives this error for the if function in line 19
// @version=4
// This is a trading bot that opens long positions if the price goes over the last day's maximum price,
// and opens short positions if the price goes below the last day's minimum price.
// The number of pips to use for the stop loss.
var stopLoss = 20
// The number of pips to use for the take profit.
var takeProfit = 40
// Get the last day's high and low prices.
var lastDayHigh = security(tickerid, 'D', high, lookahead=barmerge.lookahead_on)
var lastDayLow = security(tickerid, 'D', low, lookahead=barmerge.lookahead_on)
// Check if the current price is above the last day's high.
if close[0]>lastDayHigh[1]
// Open a long position with a stop loss and take profit.
strategy.entry("Long", strategy.long, stop=strategy.position_avg_price * (1 + stopLoss / 100),
limit=strategy.position_avg_price * (1 + takeProfit / 100))
// Check if the current price is below the last day's low.
else if close[0] < lastDayLow[1]
// Open a short position with a stop loss and take profit.
strategy.entry("Short", strategy.short, stop=strategy.position_avg_price * (1 - stopLoss / 100),
limit=strategy.position_avg_price * (1 - takeProfit / 100))
Tried all solutions given by stackOverFlow and the PineScript guides, but it still gives me the error, any help is appreciated.
There are four issues with your code.
strategy()
call.syminfo.tickerid
in your security()
call.Here is the fix:
// @version=4
// This is a trading bot that opens long positions if the price goes over the last day's maximum price,
// and opens short positions if the price goes below the last day's minimum price.
strategy("Test")
// The number of pips to use for the stop loss.
var stopLoss = 20
// The number of pips to use for the take profit.
var takeProfit = 40
// Get the last day's high and low prices.
var lastDayHigh = security(syminfo.tickerid, 'D', high, lookahead=barmerge.lookahead_on)
var lastDayLow = security(syminfo.tickerid, 'D', low, lookahead=barmerge.lookahead_on)
// Check if the current price is above the last day's high.
if (close[0]>lastDayHigh[1])
// Open a long position with a stop loss and take profit.
strategy.entry("Long", strategy.long, stop=strategy.position_avg_price * (1 + stopLoss / 100),
limit=strategy.position_avg_price * (1 + takeProfit / 100))
// Check if the current price is below the last day's low.
else if (close[0] < lastDayLow[1])
// Open a short position with a stop loss and take profit.
strategy.entry("Short", strategy.short, stop=strategy.position_avg_price * (1 - stopLoss / 100),
limit=strategy.position_avg_price * (1 - takeProfit / 100))