josh
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: December 23rd, 2022

Error that I can't figure out.

Hi guys, I keep getting this compilation error and can not for the life of me figure out the fix.
Compilation error. Line 23: mismatched character 'i' expecting '='

Line 23 is
if (close > MA_200 and close < MA_10 and !is_long and !is_short)

from the // Check if long buy condition is met block.

Any help in understanding what I'm getting wrong would be awesome. It's fun learning pine but it has got strange errors.


Code: Select all

// @version=5
// Define strategy input parameters
input MA_length_10 = 10
input MA_length_200 = 200
input stop_loss_percent = 10

// Define strategy variables
var float balance = 100000
var float fee_rate = 0.0026 // Kraken fee rate
var float stop_loss_price
var bool is_long = false
var bool is_short = false

// Define strategy plots
plot long_plot
plot short_plot

// Calculate moving averages
var float MA_10 = sma(close, MA_length_10)
var float MA_200 = sma(close, MA_length_200)

// Check if long buy condition is met
if (close > MA_200 and close < MA_10 and !is_long and !is_short)
{
    // Set long stop loss price
    stop_loss_price = (1 - stop_loss_percent/100) * close

    // Open long position
    is_long = true
    balance -= balance * fee_rate
    long_plot = 1
}

// Check if long sell condition is met
if ((close > MA_10 or close < stop_loss_price) and is_long)
{
    // Close long position
    is_long = false
    balance += balance * fee_rate
    long_plot = 0
}

// Check if long position should be held
if (is_long and close < MA_200 and close > MA_10)
{
    // Check for red candle that closes below preceding green candle
    if (close < open and close[1] > open[1])
    {
        // Close long position
        is_long = false
        balance += balance * fee_rate
        long_plot = 0
    }
}

// Check if short sell condition is met
if (close < MA_200 and close > MA_10 and !is_long and !is_short)
{
    // Set short stop loss price
    stop_loss_price = (1 + stop_loss_percent/100) * close

    // Open short position
    is_short = true
    balance -= balance * fee_rate
    short_plot = 1
}

// Check if short buy condition is met
if ((close < MA_10 or close > stop_loss_price) and is_short)
{
    // Close short position
    is_short = false
    balance += balance * fee_rate
    short_plot = 0
}

// Check if short position should be held
if (is_short and close > MA_200 and close < MA_10)
{
    // Check for green candle that closes above preceding red candle
    if (close > open and close[1] < open[1])
    {
        // Close short position
        is_short = false
        balance += balance * fee_rate
        short_plot = 0
    }
}

// Calculate final balance
strategy.entry("Profit").profit = balance - 100000


purplemint22
Pine Script Rookie
Pine Script Rookie
Posts: 17
Joined: October 14th, 2022

Re: Error that I can't figure out.

I went through and changed some things so it would compile. I added comments to the script and highlight them with *** so you will be able to see what I changed. This way you can you go through it in a pinescript editor.

Code: Select all

// @version=5
// Define strategy input parameters

//*************I added a strategy call to script but I'm assuming you have one on your script side
strategy("added to script")

//*********I changed these input declarations
MA_length_10 = input.int(10)
MA_length_200 = input.int(200)
stop_loss_percent = input.int(10)

// Define strategy variables
var float balance = 100000
var float fee_rate = 0.0026 // Kraken fee rate
var float stop_loss_price = 0
var bool is_long = false
var bool is_short = false

// Define strategy plots
//************I changed these declarations and took out plot because it is not a call 
//and it doesn't look like they are variables that are storing a plotted value
//they look like they are used to store an integer value so I assigned them 
//zero to start with
long_plot = 0
short_plot = 0

//I added the ta library reference to calculate an sma
// Calculate moving averages
var float MA_10 = ta.sma(close, MA_length_10)
var float MA_200 = ta.sma(close, MA_length_200)

//********I added not as the conditional operator instead of using the exclamation point
//I also took out the curly braces, because they are not needed in pine script.  
//pinescript uses indendation to format if statements.  
// Check if long buy condition is met
if (close > MA_200 and close < MA_10 and not is_long and not is_short)

    // Set long stop loss price
    stop_loss_price = (1 - stop_loss_percent/100) * close

    // Open long position
    is_long = true
    balance -= balance * fee_rate
    long_plot = 1


// Check if long sell condition is met
if ((close > MA_10 or close < stop_loss_price) and is_long)
    // Close long position
    is_long = false
    balance += balance * fee_rate
    long_plot = 0


// Check if long position should be held
if (is_long and close < MA_200 and close > MA_10)

    // Check for red candle that closes below preceding green candle
    if (close < open and close[1] > open[1])
    
        // Close long position
        is_long = false
        balance += balance * fee_rate
        long_plot = 0
    


// Check if short sell condition is met
if (close < MA_200 and close > MA_10 and not is_long and not is_short)

    // Set short stop loss price
    stop_loss_price = (1 + stop_loss_percent/100) * close

    // Open short position
    is_short = true
    balance -= balance * fee_rate
    short_plot = 1


// Check if short buy condition is met
if ((close < MA_10 or close > stop_loss_price) and is_short)

    // Close short position
    is_short = false
    balance += balance * fee_rate
    short_plot = 0


// Check if short position should be held
if (is_short and close > MA_200 and close < MA_10)

    // Check for green candle that closes above preceding red candle
    if (close > open and close[1] < open[1])
    
        // Close short position
        is_short = false
        balance += balance * fee_rate
        short_plot = 0
    

// Calculate final balance

//******I took out the profit = balance - 10000 because you can't set the take profit in the entry. 
//You have to use strategy.exit to set a take profit.  
//This also doesn't show a result in the strategy window because there is not condition that is specified
//for entering a position yet.  
strategy.entry("Profit", strategy.long)

Return to “Pine Script Q&A”