pacificbay / sar

Lucid SAR - a parabolic SAR indicator, written in Pine Script, based on section II of J. Welles Wilder, Jr.'s book “New Concepts in Technical Trading Systems” (1978)
MIT License
21 stars 9 forks source link

Cannot add sawcruhteez-strategy.pine to tradingview #5

Open pv0192 opened 5 years ago

pv0192 commented 5 years ago

1) cargo build 2) cargo run 3) Add generated sawcruhteez-strategy.pine to scripts, add to chart leads to the following error

Processing script...
Add to Chart operation failed, reason: line 462: syntax error at input 'strategy.position_size'
line 462: syntax error at input 'strategy.position_size'

My version of sawcruhteez-strategy.pine

//@version=4
strategy("Sawcruhteez SAR v1.2.5 - Strategy v1.2.6", shorttitle="Sawcruhteez SAR Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=10000)

// SAR #1
// Lucid Sar
// Branded under the name "Lucid SAR"
// as agreed to with Lucid Investment Strategies LLC on July 9, 2019
// https://lucidinvestmentstrategies.com/

// SAR #2
// Peterbolic Sar
// Using the name "Peterbolic SAR"
// as agreed to by Peter Brandt on October 2, 2019
// - https://twitter.com/PeterLBrandt/status/1179365590668075008
// in response to request from Sawcruhteez
// - https://twitter.com/Sawcruhteez/status/1179213105705836544
// Sawcruhteez gives credit to @CrazyGabey for coming up with the name
// - https://twitter.com/Sawcruhteez/status/1179213196583940097

// SAR #3
// Sawcruhteez Sar
// Branded under the name "Sawcruhteez SAR"
// as agreed to Sawcruhteez on September 11, 2019

// Open Source on github
// https://github.com/casey-bowman/sar

// Created by Casey Bowman on July 4, 2019

// MIT License

// Copyright (c) 2019 Casey Bowman

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// The following global variable provide event signalling, after a fashion,
// from the results of the LUCID_SAR and TSR functions to the SAWCRUHTEEZ indicator function
TSR_reversal_triggered = false
TSR_setup_towards_uptrend = false
TSR_setup_towards_downtrend = false
TSR_reversal_to_uptrend = false
TSR_reversal_to_downtrend = false
TSR_in_up_trend = false

SAR_reversal_triggered = false
SAR_reversal_to_uptrend = false
SAR_reversal_to_downtrend = false
SAR_in_uptrend = false
SAR_in_downtrend = false

AF_initial = input(0.02)
AF_increment = input(0.02)
AF_maximum = input(0.2)

LUCID_SAR(initial, increment, maximum) =>

    // start with uptrend
    uptrend = true
    new_trend = false
    EP = high
    SAR = low
    AF = AF_initial

    // before a reversal, the reversal_state is 0;
    // after a reversal, within the same candle as the reversal,
    // the reversal_state can be 1 (uptrend) or 2 (downtrend)
    reversal_state = 0

    if not na(uptrend[1]) and not na(new_trend[1])
        if reversal_state == 0
            if uptrend[1]
                EP := max(high, EP[1])
            else
                EP := min(low, EP[1])
            if new_trend[1]
                AF := AF_initial
            else
                if EP != EP[1]
                    AF := min(AF_maximum, AF[1] + AF_increment)
                else
                    AF := AF[1]
            SAR := SAR[1] + AF * (EP - SAR[1])
            if uptrend[1]
                SAR := min(SAR, low[1])
                if not na(low[2])
                    SAR := min(SAR, low[2])
                if SAR > low
                    uptrend := false
                    new_trend := true
                    SAR := max(high, EP[1])
                    EP := min(low, low[1])
                    reversal_state := 2
                else
                    uptrend := true
                    new_trend := false
            else
                SAR := max(SAR, high[1])
                if not na(high[2])
                    SAR := max(SAR, high[2])
                if SAR < high
                    uptrend := true
                    new_trend := true
                    SAR := min(low, EP[1])
                    EP := max(high, high[1])
                    reversal_state := 1
                else
                    uptrend := false
                    new_trend := false
        else
            if reversal_state == 1
                EP := high
                if low < SAR
                    SAR := EP
                    EP := low
                    reversal_state == 2
                    uptrend := false
            else
                EP := low
                if high > SAR
                    SAR := EP
                    EP := high
                    reversal_state == 1
                    uptrend := true
    [SAR, reversal_state, uptrend]

[L_SAR, reversal, uptrend] =  LUCID_SAR(AF_initial, AF_increment, AF_maximum)

SAR_reversal_triggered := (reversal != 0)
SAR_reversal_to_uptrend := (reversal == 1)
SAR_reversal_to_downtrend := (reversal == 2)
SAR_in_uptrend := uptrend
SAR_in_downtrend := not uptrend

TSR() =>

    // start with uptrend
    var uptrend = true
    var EP = high       // extreme price - high or low depending on trend
    var SP = low        // setup price
    var TP = float(na)  // trigger price

    var setup   = low
    var trigger = float(na)

    if barstate.isnew
        setup := low
        trigger = float(na)

    extreme_candle = false
    first_extreme_candle = false
    setup_candle = false
    trigger_candle = false

    waiting_for_setup = false
    waiting_for_trigger = false

    var since_extreme = 0
    var since_setup = 0

    waiting_for_setup   := not extreme_candle and not na(SP)
    waiting_for_trigger := not na(TP)

    if not barstate.isfirst
        if barstate.isnew and extreme_candle[1]
            trigger := float(na)
        if barstate.isnew and setup_candle[1]
            setup := float(na)
        if barstate.isnew and waiting_for_trigger
            since_setup := since_setup + 1
            trigger := TP
        if barstate.isnew and waiting_for_setup
            since_extreme := since_extreme + 1
            setup := SP
        if uptrend

            if extreme_candle
                EP := high
                SP := low
            else
                if high > EP
                    extreme_candle := true
                    EP := high
                    SP := low
                    since_extreme := 0
                    since_setup   := 0
                else
                    if waiting_for_setup
                        if barstate.isconfirmed
                            if close < SP
                                setup_candle := true
                                SP := float(na)
                                TP := low
            if waiting_for_trigger
                if low < TP
                    trigger_candle := true
                    extreme_candle := true
                    EP := low
                    SP := high
                    TP := float(na)
                    uptrend := false
                    since_extreme := 0
                    since_setup := 0
                else
                    if barstate.isconfirmed and extreme_candle
                        TP := float(na)
                        trigger := float(na)

        else
            if extreme_candle
                EP := low
                SP := high
            else
                if low <  EP
                    extreme_candle := true
                    EP := low
                    SP := high
                    since_extreme := 0
                    since_setup   := 0
                else
                    if waiting_for_setup
                        if barstate.isconfirmed
                            if close > SP
                                setup_candle := true
                                SP := float(na)
                                TP := high
            if waiting_for_trigger
                if high > TP
                    trigger_candle := true
                    extreme_candle := true
                    EP := high
                    SP := low
                    TP := float(na)
                    uptrend := true
                    since_extreme := 0
                    since_setup := 0
                else
                    if barstate.isconfirmed and extreme_candle
                        TP := float(na)
                        trigger := float(na)

    [trigger_candle, trigger, since_setup, setup_candle, setup, since_extreme, extreme_candle, uptrend]

// [trigger_candle, trigger, since_setup, setup_candle, setup, since_extreme, extreme_candle, uptrend]

[TC, T, SS, SC, S, SE, EC, up] = TSR()

TSR_reversal_triggered := TC
TSR_setup_towards_uptrend := SC and (not up)
TSR_setup_towards_downtrend := SC and up
TSR_reversal_to_uptrend := TC and up
TSR_reversal_to_downtrend := TC and (not up)
TSR_in_up_trend := up

SAWCRUHTEEZ(SAR_reverse, SAR_reverse_up, SAR_reverse_down, SAR_up, SAR_down,
             TSR_reverse, TSR_setup_up, TSR_setup_down, TSR_reverse_up, TSR_reverse_down, TSR_up) =>

    var trend_has_had_tsr_trigger = false
    var recent_tsr_trigger_pre_trend = false

    profit_take_buy = false
    profit_take_sell = false
    stop_and_reverse_buy = false
    stop_and_reverse_sell = false
    stop_buy = false
    stop_sell = false
    long = false
    short = false
    buy_signal = false
    sell_signal = false

    signal = string(na)

    RSI = rsi(close, 14)

    if (SAR_reverse_up and TSR_reverse_up)
        signal := "SAR"

        trend_has_had_tsr_trigger := true
        stop_and_reverse_buy := true
    else
        if (SAR_reverse_down and TSR_reverse_down)
            signal := "SAR"

            trend_has_had_tsr_trigger := true
            stop_and_reverse_sell := true
        else
            if (SAR_reverse_up and not TSR_reverse_up)
                if (recent_tsr_trigger_pre_trend)
                    signal := "SAR"

                    trend_has_had_tsr_trigger := true
                    stop_and_reverse_buy := true
                else
                    if TSR_up
                        signal := "Buy"

                        buy_signal := true
                    else
                        signal := "Stop"

                        trend_has_had_tsr_trigger := false
                        stop_buy := true
            else
                if (SAR_reverse_down and not TSR_reverse_down)
                    if (recent_tsr_trigger_pre_trend)
                        signal := "SAR"

                        trend_has_had_tsr_trigger := true
                        stop_and_reverse_sell := true
                    else
                        if (not TSR_up)
                            signal := "Sell"

                            sell_signal := true
                        else
                            signal := "Stop"

                            trend_has_had_tsr_trigger := false
                            stop_sell := true

                else

                    if (not trend_has_had_tsr_trigger)
                        if (TSR_reverse_up and SAR_up)
                            signal := "Long"

                            long := true
                            trend_has_had_tsr_trigger := true
                        else
                            if (TSR_reverse_down and SAR_down)
                                signal := "Short"

                                short := true
                                trend_has_had_tsr_trigger := true

                    if (not long and not short)
                        if (TSR_setup_down and SAR_up)
//                         and (RSI <= 75))
                            signal := "Buy"

                            buy_signal := true
                        else
                            if (TSR_reverse_down and SAR_up)
//                             and (RSI <= 75))
                                signal := "Buy"

                                buy_signal := true
                            else
                                if (TSR_reverse_down and SAR_up)
//                                 and (RSI > 75))
                                    signal := "Sell-FPT"

                                    profit_take_sell := true

                                else
                                    if (TSR_setup_up and SAR_down)
//                                     and (RSI >= 25))
                                        signal := "Sell"

                                        sell_signal := true
                                    else
                                        if (TSR_reverse_up and SAR_down)
//                                         and (RSI >= 25))
                                            signal := "Sell"

                                            sell_signal := true
                                        else
                                            if (TSR_reverse_up and SAR_down)
//                                             and (RSI < 25))
                                                signal := "Buy-FPT"

                                                profit_take_buy := true
    if (SAR_reverse_up or SAR_reverse_down)
        recent_tsr_trigger_pre_trend := false

    if (TSR_reverse_up and SAR_down)
        recent_tsr_trigger_pre_trend := true
    if (TSR_reverse_down and SAR_down)
        recent_tsr_trigger_pre_trend := false
    if (TSR_reverse_down and SAR_up)
        recent_tsr_trigger_pre_trend := true
    if (TSR_reverse_up and SAR_up)
        recent_tsr_trigger_pre_trend := false

    [signal, profit_take_buy, profit_take_sell, stop_and_reverse_buy, stop_and_reverse_sell, stop_buy, stop_sell, long, short, buy_signal, sell_signal]

[signall, profit_take_buy_, profit_take_sell_, stop_and_reverse_buy_, stop_and_reverse_sell_, stop_buy_, stop_sell_, long_, short_, buy_signal_, sell_signal_]= SAWCRUHTEEZ(SAR_reversal_triggered, SAR_reversal_to_uptrend, SAR_reversal_to_downtrend, SAR_in_uptrend, SAR_in_downtrend,
                     TSR_reversal_triggered, TSR_setup_towards_uptrend, TSR_setup_towards_downtrend, TSR_reversal_to_uptrend, TSR_reversal_to_downtrend, TSR_in_up_trend)

plotshape(SC and not up, color = color.yellow, style = shape.triangleup, location = location.belowbar, size = size.auto, transp = 0, title = "Setup to Buy")

plotshape(TC and up, color = color.green, style = shape.triangleup, location = location.belowbar, size = size.auto, title = "Trigger to Buy")

plotshape(SC and up, color = color.yellow, style = shape.triangledown, location = location.abovebar, size = size.auto, transp = 0, title = "Setup to Sell")

plotshape(TC and not up, color = color.red, style = shape.triangledown, location = location.abovebar, size = size.auto, title = "Trigger to Sell")

plot(L_SAR, color = color.blue, style = plot.style_cross, linewidth = 2)

plotshape(profit_take_buy_, color = color.green, style = shape.labelup, location=location.belowbar, size=size.auto, text = "Buy (FPT)", textcolor= color.black)

plotshape(profit_take_sell_, color = color.red, style = shape.labeldown, location= location.abovebar, size=size.auto, text = "Sell (FPT)", textcolor = color.black)

plotshape(stop_and_reverse_buy_, color = color.purple, style = shape.labeldown, location=location.abovebar, size = size.auto, text = "SAR", textcolor = color.black)

plotshape(stop_and_reverse_sell_, color = color.purple, style = shape.labelup, location= location.belowbar, size=size.auto, text = "SAR", textcolor = color.black)

plotshape(stop_buy_, color = color.blue, style = shape.labeldown, location=location.abovebar, size = size.auto, text = "Stop", textcolor = color.black)

plotshape(stop_sell_, color = color.blue, style = shape.labelup, location= location.belowbar, size=size.auto, text = "Stop", textcolor = color.black)

plotshape(long_, color = color.green, style = shape.labeldown, location= location.abovebar, size=size.auto, text = "Buy (long)", textcolor = color.black)

plotshape(short_, color = color.red, style = shape.labelup, location= location.belowbar, size=size.auto, text = "Sell (short)", textcolor = color.black)

plotshape(buy_signal_, color = color.green, style = shape.labelup, location= location.belowbar, size=size.auto, text = "Buy", textcolor = color.black)

plotshape(sell_signal_, color = color.red, style = shape.labeldown, location= location.abovebar, size=size.auto, text = "Sell", textcolor = color.black)

strategy.entry("long", strategy.long, 1, when = (((profit_take_buy_ or long_ or buy_signal_) and strategy.position_size = 0) or (stop_and_reverse_buy_ and strategy.position_size = -1))
strategy.entry("short", strategy.short, 1, when = (((profit_take_sell_ or sell_ or sell_signal_) and strategy.position_size = 0) or (stop_and_reverse_sell_ and strategy.position_size = 1))
strategy.exit("stop_long", "long", when = (stop_and_reverse_sell_ or stop_sell_))
strategy.exit("stop_short", "short", when = (stop_and_reverse_buy_ or stop_buy_))
thorsteneb commented 4 years ago

PR for fix is up. Very simply replace the last four lines with:

strategy.entry("long", strategy.long, 1, when = (((profit_take_buy_ or long_ or buy_signal_) and strategy.position_size == 0) or (stop_and_reverse_buy_ and strategy.position_size < 0)))
strategy.entry("short", strategy.short, 1, when = (((profit_take_sell_ or short_ or sell_signal_) and strategy.position_size == 0) or (stop_and_reverse_sell_ and strategy.position_size > 0)))
strategy.exit("stop_long", "long", when = (stop_and_reverse_sell_ or stop_sell_))
strategy.exit("stop_short", "short", when = (stop_and_reverse_buy_ or stop_buy_))
thorsteneb commented 4 years ago

Note that doesn't fix the warnings on strategy.exit (no stops etc) or the fact that this buys/sells 1 BTC, independent of starting capital.

To fix those, try strategy.close instead of strategy.exit, and replace the quantity "1" in the entry with "na", which uses "however much cash you have / market price". Top of the strategy shows you the assumed starting capital.

If the PR for the syntax issues gets accepted, I may do another one for those two changes.