-3

enter image description hereI want to implement buying by entering a certain price in the tradingview strategy, and I can also achieve one-click close all through the Pine Script, this is my code, now the problem is that the buy signal will be displayed normally on the chart, but no alert appears.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version=5
strategy(title="sample", overlay=true)

// Strategy Settings
g_Trade             = "Trading Parameters"
sellNow             = input.bool(title="Close All!", defval=false, inline = "sellNow", group=g_Trade)
buyLineBO           = input.float(title="Entry price", defval=1870, step = 0.01, inline = "Line", group=g_Trade)
useBuyLineTrade     = input.bool(title="Enable entry price", defval=false, group=g_Trade, inline = "Line", tooltip="Whether to enable buy by entering the price")

// Make line variables (to access their previous values later)
var line buyLine = na

if useBuyLineTrade and barstate.islast
    buyLine := line.new(x1=bar_index[10], y1=buyLineBO, x2=bar_index+3, y2=buyLineBO, color=color.red, width=2, extend=extend.none)
if not useBuyLineTrade
    line.delete(id=buyLine)

g_Parameter          = "Strategy Parameters"
i_riskReward1        = input.float(title="RR1", defval=0.6, step = 0.1, inline = "sp", group=g_Parameter)
i_riskPerTrade       = input.float(title="Risk%", defval=1.9, step = 0.1, group=g_Parameter,  inline = "sp") 
i_trailingStopOffset = input.float(title="Stop offset", defval=0.01, step=0.01, inline = "stop",  group=g_Parameter) 

// Save stops & targets
var float t_stop   = na
var float t_target = na
var g_alert        = "Alerts"
entry1_alert_msg   = input.text_area(defval = "", title = "Enable entry price", group = g_alert)

exit1_alert_msg    = input.text_area(defval = "", title = "Stop/Target", group = g_alert)
exit2_alert_msg     = input.text_area(defval = "", title = "One-click Close All", group = g_alert)

buyLineCrossAboveSuccess = ta.crossover(close, buyLineBO) and close > buyLineBO 
// plotshape(strategy.position_size == 0 ? buyLineCrossAboveSuccess : na, style=shape.triangleup, size=size.auto, location=location.abovebar, color= color.yellow)

if useBuyLineTrade and buyLineCrossAboveSuccess and barstate.isrealtime and barstate.isconfirmed and strategy.position_size == 0 
    float positionSize = 0.0
    string stopLabelText = ""
    float longStopDistance = 0.0
    t_stop := low - i_trailingStopOffset
    longStopDistance := close - t_stop // Stop distance
    t_target := close + (longStopDistance * i_riskReward1) 

    positionSize := math.min(math.floor((strategy.equity * (i_riskPerTrade/100)) / (close - t_stop)), math.floor(strategy.equity/close))
    stopLabelText := str.tostring(close, "0.00") + "\n SL%=" + str.tostring(longStopDistance/close*100, "0.00") + "\n S=" + str.tostring(t_stop, "0.00")
    label.new(bar_index, close, "Testing BuyLineTrade")
    strategy.entry(id="Long", direction=strategy.long, qty=positionSize, comment = "CL= " + stopLabelText, alert_message = entry1_alert_msg)
    // alert(entry1_alert_msg, alert.freq_once_per_bar_close)

strategy.exit(id="Long Exit", from_entry="Long",limit=na, stop=t_stop, comment =  "S=" + str.tostring(t_stop, "0.00") + "\nT=" + str.tostring(t_target, "0.00") , alert_message = exit1_alert_msg) 
    // alert(exit1_alert_msg, alert.freq_once_per_bar_close)
// label.new(x=bar_index, y=t_stop, text=str.tostring(t_stop, "0.00"), color=color.white, style=label.style_label_upper_left)

if strategy.position_size > 0 and sellNow and barstate.isrealtime
    //strategy.close(id = "Long", immediately = true, comment = "Close All" + str.tostring(close, "0.00")+ "\n", alert_message = exit2_alert_msg )
    strategy.close_all(immediately = true, comment = "Close All", alert_message = exit2_alert_msg, disable_alert = false)
    //alert(exit2_alert_msg, alert.freq_once_per_bar)

your text I want Tradingview to fire alerts.

Bruce
  • 1
  • 2
  • Did you set the alerts manually? It is not enough to have it in your code. – vitruvius Aug 06 '23 at 19:32
  • Yes, I set the price manually and then initiate the buy through the checkbox, as shown in the screenshot. – Bruce Aug 06 '23 at 19:35
  • My original intention was to add a way to buy by manually entering a price into the current strategy, and then also consider adding an option to manually trigger Close All. – Bruce Aug 06 '23 at 19:40
  • These are just a complement to the existing strategy, which was fully automated. – Bruce Aug 06 '23 at 19:41

0 Answers0