Sage Investment Club

This is the supplementary blog article describing how to manipulate the alert words at various scenario or conditions for TradingView indicators and strategies. At my product of “TradingView alerts to MT4/MT5 trader copier”, there are some alert keywords set as default at copier EA variables: Keywords for LONG trade: buy,long,up,bullish,bull Keywords for SHORT trade: sell,short,down,bearish,bear Keywords to close LONG trade: closebuy,closelong,close buy,buy close,close long,long close,exit buy,buy exit,exit long,long exit Keywords to close SHORT trade: closesell,closeshort,close sell,sell close,close short,short close,exit sell,sell exit,exit short,short exit If you want to close all trades, set keywords list to ‘Keywords to close both LONG & SHORT trades’. You could use any of above existing keywords at your TradingView indicator/strategy alert messages . Or you could add your preferred keywords by adding comma plus your new keyword to above list. Below suggestions will first go through TradingView indicator alerts and then strategy’s. Indicator alerts There are two methods to write pinescript alert codes: ‘alert’  and  ‘alertcondition’  if you’re using ‘alert’ function, i share one sample code snippet as below.  It will simply raise an BUY alert if a bullish candle bar gets closed or SELL if bearish.  You could put this sample alert indicator on an one minute chart for fast trigger.

indicator(title = “Indicator alert test”, overlay = true)

sigBuy = close >= open
sigSell = close < open

if sigBuy
    alert(“Bullish candle, BUY”, alert.freq_once_per_bar_close)
if sigSell
    alert(“Bearish candle, SELL”, alert.freq_once_per_bar_close)

plot(close, color = na) This example will trigger an alert after a bar close.  If you want that alert to be raised at realtime mode, just simply write alert(“Bullish candle, BUY”)  Pinescript ‘alert’ default frequency is to trigger once a bar. If you prefer using ‘alertcondition’, it will look like as below codes – and you will be able to choose what conditions to be alerted at setting:

indicator(title = “Indicator alertcondition test”, overlay = true)

conBuy = close >= open
conSell = close < open

alertcondition(conBuy, title = “Buy Signal”, message = “Bullish candle, BUY”)
alertcondition(conSell, title = “Sell Signal”, message = “Bearish candle, SELL”)

plot(close, color = na) But please be aware the default alert frequency of ‘alertcondition’ is only raised ONCE.  It’s essential to change this setting to ‘Once Per Bar’ or ‘Once Per Bar Close’ if you want continuous alerts. Strategy alerts Below are my pinescript code snippet showing how to add trade entry/exit actions by using order ‘comment’.
strategy(title = “Strategy Alert Test – Market Order”, overlay = true)

lot = input.int(title = “Trade lot”, defval = 100, minval = 1)
startDate = input.time(title = “Start date”, defval = timestamp(‘2022-01-01’))

bool inDateRange = time >= startDate

rrr = 2.0
atrMulti = 3.0
atrVal = ta.atr(50)

maShort = ta.sma(close, 50)
maLong = ta.sma(close, 100)
sigBuy = ta.crossover(maShort, maLong)
sigSell = ta.crossunder(maShort, maLong)
plot(maShort, color = color.blue)
plot(maLong, color = color.red)

if sigBuy and inDateRange and strategy.opentrades.size(strategy.opentrades – 1) <= 0
        strategy.entry(id = “buy”, direction = strategy.long, qty = lot, comment = “buy”)
        slPrice = math.round_to_mintick(close + syminfo.mintick * 1 – atrVal * atrMulti)
        tpPrice = math.round_to_mintick(close + syminfo.mintick * 1 + atrVal * atrMulti * rrr)
        strategy.exit(id = “buy exit”, from_entry = “buy”, stop = slPrice, limit = tpPrice, comment = “close buy”)
if sigSell and inDateRange and strategy.opentrades.size(strategy.opentrades – 1) >= 0
        strategy.entry(id = “sell”, direction = strategy.short, qty = lot, comment = “sell”)
        slPrice = math.round_to_mintick(close + atrVal * atrMulti)
        tpPrice = math.round_to_mintick(close – atrVal * atrMulti * rrr)
        strategy.exit(id = “sell exit”, from_entry = “sell”, stop = slPrice, limit = tpPrice, comment = “close sell”) The words of order comments are in fact the exact texts shown on TradingView chart for each trade action.  Or you could use ‘alert_message’ feature at strategy.entry or strategy.exit as well. This sample strategy takes the up or down cross of two simple moving average lines as buy and sell signals.  Stoploss price is the 3 times of atr values (atr period = 50).  Take profit price is based on the ‘risk reward ratio’ = 2.0. Then you will need to input {{strategy.order.comment}}  at below alert setting – that will have alerts completely follow comment wordings. If you’re using ‘alert_message’ feature other than ‘comment’, here it should be {{strategy.order.alert_message}} at this setting. If your strategy is limit or stop pending order, Pinescript reference recommends using ‘alert_message’ feature for strategy.entry.  Please read this article: Using ‘comment’ word is still okay for this order fulfill scenario.  Anyway I make below sample codes for stop order scenario and put the message words at ‘alert_message’ part.   The stop price is 1.0 times of atr value – and stoploss and take profit rules follow the previous example. 
strategy(title = “Strategy Alert Test – Stop Order”, overlay = true)

lot = input.int(title = “Trade lot”, defval = 100, minval = 1)
startDate = input.time(title = “Start date”, defval = timestamp(‘2022-01-01’))

bool inDateRange = time >= startDate

rrr = 2.0
atrMultiOpen = 1.0
atrMultiSp = 3.0
atrVal = ta.atr(50)

maShort = ta.sma(close, 50)
maLong = ta.sma(close, 100)
sigBuy = ta.crossover(maShort, maLong)
sigSell = ta.crossunder(maShort, maLong)
plot(maShort, color = color.blue)
plot(maLong, color = color.red)

if sigBuy and inDateRange
        openPr = math.round_to_mintick(close + syminfo.mintick + atrVal * atrMultiOpen)
        slPrice = math.round_to_mintick(openPr – atrVal * atrMultiSp)
        tpPrice = math.round_to_mintick(openPr + atrVal * atrMultiSp * rrr)
        strategy.entry(“buy”, direction = strategy.long, stop = openPr, alert_message = “Buy”)
        strategy.exit(id = “buy exit”, from_entry = “buy”, stop = slPrice, limit = tpPrice, alert_message = “close buy”)
if sigSell and inDateRange
        openPr = math.round_to_mintick(close – syminfo.mintick + atrVal * atrMultiOpen)
        slPrice = math.round_to_mintick(openPr + atrVal * atrMultiSp)
        tpPrice = math.round_to_mintick(openPr – atrVal * atrMultiSp * rrr)
        strategy.entry(“sell”, direction = strategy.short, stop = openPr, alert_message = “Sell”)
        strategy.exit(id = “sell exit”, from_entry = “sell”, stop = slPrice, limit = tpPrice, alert_message = “close sell”)
         As ‘alert_message’ feature is now used at codes, the alert setting should have {{strategy.order.alert_message}} so the exact words will show as alert texts. Partial close If you want to have partial close actions, just add xx% at strategy exit alert text, e.g. ‘close long 50%’. Below is the sample pinescript codes you could use as reference.  At MT4/MT5 copier ea, you don’t need to do any setting. 
strategy(title = “Strategy Partial Close”, overlay = true, calc_on_order_fills = false, initial_capital = 100000, currency = “USD”)

lot = input.int(title = “Trade lot”, defval = 100, minval = 1)
isBuy = input.bool(title = “Buy?”, defval =  true)
isSell = input.bool(title = “Sell?”, defval = true)
startDate = input.time(title = “Start date”, defval = timestamp(‘2022-11-15’))

bool inDateRange = time >= startDate

buySig = close[1] < open[1] and close[0] > open[0]
buyPCSig = close[1] > open[1] and close[0] > open[0]
buyACSig = close[2] > open[2] and close[1] > open[1] and close[0] > open[0]

sellSig = close[1] > open[1] and close[0] < open[0]
sellPCSig = close[1] < open[1] and close[0] < open[0]
sellACSig = close[2] < open[2] and close[1] < open[1] and close[0] < open[0]

var bool _buyPartial = false
var bool _sellPartial = false

if isBuy and buySig and inDateRange and strategy.position_size <= 0
   strategy.entry(id = “buy”, direction = strategy.long, qty = lot, comment = “buy”)
     _buyPartial := true
if isBuy and buyPCSig and inDateRange and strategy.position_size > 0 and _buyPartial
   strategy.close(id = “buy”, qty_percent = 50, comment = “close buy 50%”)
     _buyPartial := false
if isBuy and buyACSig and inDateRange and strategy.position_size > 0
        strategy.close(id = “buy”, comment = “close buy”)
        
if isSell and sellSig and inDateRange and strategy.position_size >= 0
        strategy.entry(id = “sell”, direction = strategy.short, qty = lot, comment = “sell”)
     _sellPartial := true
if isSell and sellPCSig and inDateRange and strategy.position_size < 0 and _sellPartial
        strategy.close(id = “sell”, qty_percent = 50, comment = “close sell 50%”)
     _sellPartial := false
if isSell and sellACSig and inDateRange and strategy.position_size < 0
        strategy.close(id = “sell”, comment = “close sell”)

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *