2
votes

I'm trying to convert a TradingView strategy into a study with alerts. I'm relatively new to this idea. I've created plots so that the long and short entries show up on the chart, and then I created alerts that corresponded to those entries. But my issue comes up here, because I can't find out where to create the corresponding exit alerts.

//@version=3
study("Generalized SSL Backtest w/ TSSL", shorttitle="Judgement", overlay=true)

lb = input(10, title="Lb", minval=1)
maType = input(type=string, defval="SMA", title="MA Type", options=["SMA","EMA","HMA","McG","WMA","Tenkan"])

fixedSL = input(title="SL Activation", defval=300)
trailSL = input(title="SL Trigger", defval=1)
fixedTP = input(title="TP Activation", defval=150)
trailTP = input(title="TP Trigger", defval=1)

ma(t,sig,len) =>
    sss=na
    if t =="SMA"
        sss := sma(sig,len)
    if t == "EMA"
        sss := ema(sig,len)
    if t == "HMA"
        sss := hma(sig,len)
    if t == "McG" // Mc Ginley
        sss := mcg(sig,len)
    if t == "Tenkan"
        sss := tenkan(sig,len)
    if t == "WMA"
        sss := wma(sig,len)
    sss

base(mah, mal) =>
    bbb = na
    inChannel = close<mah and close>mal
    belowChannel = close<mah and close<mal
    bbb := inChannel? bbb[1]: belowChannel? -1: 1
    uuu = bbb==1? mal: mah
    ddd = bbb==1? mah: mal
    [uuu, ddd]

maH = ma(maType, high, lb)
maL = ma(maType, low, lb)

[up, dn] = base(maH,maL)

plot(up, title="High MA", color=lime, linewidth=3)
plot(dn, title="Low MA", color=orange, linewidth=3)

long = crossover(dn,up)
short = crossover(up,dn)

// === STRATEGY - LONG POSITION EXECUTION ===
//strategy.entry("Long", strategy.long, when= long and startTimeOk())
//strategy.exit("Exit", qty_percent = 100, loss=fixedSL, trail_offset=trailTP, trail_points=fixedTP) 
//strategy.exit("Exit", when= short)
// === STRATEGY - SHORT POSITION EXECUTION ===
//strategy.entry("Short", strategy.short, when= short and startTimeOk())
//strategy.exit("Exit", qty_percent = 100, loss=fixedSL, trail_offset=trailTP, trail_points=fixedTP)
//strategy.exit("Exit", when= long) 


plotchar(long, char = "Long", color = green)
plotchar(short, char = "Short", color = red)

alertcondition(long, "ENTRY LONG", "ENTRY LONG")
alertcondition(short, "ENTRY SHORT", "ENTRY SHORT")

Any help would be appreciated.

1

1 Answers

1
votes

Your problematic stems from the fact that there is no way in Pine to automatically transfer into a study the strategy's broker emulator logic that kicks in when you use strategy.*() parameters such as loss=, trail_offset=, etc.

You have two choices:

  1. In cases where your ultimate goal is to generate alerts to feed orders to a third-party order execution app, and if your strategy happens to use fixed trade management parameters (as seems to be your case) then you may be able to get away with transferring those parameters into the third-party's order syntax through the alert messages your script generates, and thus delegate that part of trade management to the third-party app.

  2. Code all your trade management manually in Pine so that it works both in study and strategy modes and only relies on strategy.*() calls to have your strategy execute simple, generic market orders using none of the trade management strategy.*() parameters you are currently using. Once you've achieved that, converting between study and strategy mode is a simple matter of commenting/uncommenting a few lines of code which are incompatible between study and strategy script modes.

    A good example of this is the PineCoders Backtesting & Trading Engine here: https://www.tradingview.com/script/dYqL95JB-Backtesting-Trading-Engine-PineCoders/

    A simpler example of hybrid code that easily converts between strategy and study mode, but contains only elementary trade management logic is this: https://www.tradingview.com/script/pjZKWnHl-Volatility-System-by-Wilder-LucF/

Note: A soon to be released dynamic message feature for alerts will allow you to generate alerts with series values (variable information). While this will still not allow automatic conversion of broker emulator logic into studies, it should make the conversion easier.