简体   繁体   中英

Pine script gives Compilation error. Line 19: Syntax error at input 'end of line without line continuation'

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.

  1. You must have a strategy() call.
  2. This is v4, so you should be using syminfo.tickerid in your security() call.
  3. A local block must be indented by a tab or four spaces.
  4. If you want to wrap your statement to the next line, then the continuation of the statement must begin with one or several (different from multiple of 4) spaces.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM