简体   繁体   中英

Pine Script Strategy Alert - How to add to my script?

I'm trying to figure out how to add an alert for "buy" and "sell" on my strategy script. I want to be able to select buy or sell in the alert window. I need to use the message area to send a message to a 3commas bot. So I can't use the message area in the alert window. Is there a way I can input the alerts in the Trading View alert popup to show buy and sell, and not use the message area? As shown in the picture?

TV ALERT

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Hawkreign

//@version=5
strategy(title="SGA Alert", overlay=true, default_qty_type = strategy.cash, default_qty_value = 100)

// Input options
src = input.source(close, title = "SGA" )

// Long and Short Conditions on the SGA
long = ta.rising(src, 1)
short = ta.falling(src, 1)

// Long and Short Memory
longmem = false
shortmem = false

// Memory Conditions
longmem := long ? true : short ? false : longmem[1]
shortmem := short ? true : long ? false : shortmem[1]

// Entry and Exit Variables
buy = long and not (longmem[1])
sell = short and not (shortmem[1])

// Long and Short Background color
bgcolor(buy ? color(color.rgb(11, 238, 18)): na)
bgcolor(sell ? color(color.rgb(134, 7, 7)): na)

// Long Entry and Exit
if (buy)
    strategy.entry("buy", strategy.long)
    strategy.close("sell")

// Short Entry and Exit
if (sell)
    strategy.entry("sell", strategy.short)
    strategy.close("buy")

plot(src)

If you're looking to write your own message for different orders, you have few options:

  • Use the alert_message parameter of the order function. This is an optional parameter which replaces the {{strategy.order.alert_message}} placeholder when it is used in the "Create Alert" dialog box's "Message" field.
  • Use the alert function.

Here is an example for alert_message parameter:

// Long Entry and Exit
if (buy)
    strategy.entry("buy", strategy.long, alert_message = "Let's go long!!!")
    strategy.close("sell", alert_message = "Let's close short!!!")

// Short Entry and Exit
if (sell)
    strategy.entry("sell", strategy.short, alert_message = "Let's go Short!!!")
    strategy.close("buy", alert_message = "Let's close long!!!")

When using this method, don't forget to set the {{strategy.order.alert_message}} placeholder in the message field of the "Create Alert" dialog box.

Here is an example for alert function:

if (buy)
    strategy.entry("buy", strategy.long)
    strategy.close("sell")
    alert("Close all shorts and buy long!!!")
    
// Short Entry and Exit
if (sell)
    strategy.entry("sell", strategy.short)
    strategy.close("buy")
    alert("Close all longs and buy short!!!")

When using this method, you'll need to change to your strategy in the "Condition" box of the "Create Alert" and then choose "Order fills and alert() function calls" or "Alert() function calls only"

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