s21jb
s21jb

Reputation: 105

strategy.position_size alerting the wrong value when used with {{strategy.order.alert_message}}

Is there any way to access {{strategy.market_position_size}} from inside pine script to generate a strategy alert in order to creat a POST message? I tried using the pine script variable strategy.position_size inside the script to generate a POST message so that I can use {{strategy.order.alert_message}} in the Create Alert dialog's "Message" box as shown below:

Create Alert dialog box

This is a simple example strategy demonstrating how I'm trying to assemble the POST message as an alert:

//@version=5
// Webhook message test
strategy("Webhook test", overlay=true, initial_capital=1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)
alertID =""
// Limit processing time span
startDate = input.time(title="Start Date", defval=timestamp("30 Jun 2022 06:00:00 GMT-4"))
endDate = input.time(title="End Date", defval=timestamp("01 Jan 2024 06:00:00 GMT-4"))
BetweenDates = startDate <= time and time <= endDate

timeStamp = str.tostring(year, '0000')+str.tostring(month, '00')+str.tostring(dayofmonth, '00')+str.tostring(hour, '00')+str.tostring(minute, '00')
POSTmessage = '"Symbol": "'+syminfo.ticker+'", "timestamp": "'+timeStamp+'"'

EntryCondition = BetweenDates and ta.crossover(close, ta.sma(close,3))
ExitCondition = BetweenDates and ta.crossunder(close,ta.sma(close,3))

if EntryCondition
    alertID := str.tostring(timeStamp+syminfo.ticker)
    POSTmessage := POSTmessage +', "order_id": "'+alertID+', "order_action": "buy"'
if ExitCondition
    POSTmessage := POSTmessage +', "order_id": "'+alertID+', "order_action": "sell"'

//Order execution and alert
strategy.entry(id=alertID, direction=strategy.long, when = EntryCondition, alert_message='{'+POSTmessage+', "order_contratcs": '+str.tostring(strategy.position_size)+'}')
strategy.close(id=alertID, when = ExitCondition, alert_message='{'+POSTmessage+', "order_contratcs": '+str.tostring(strategy.position_size)+'}')
//paste into in [ Creat Alert > Message ] box:
//      {{strategy.order.alert_message}} 

However, when the alert pops up it shows zero quantity in the "order_contratcs" field for strategy.entry executions since the position is flat from the previous strategy.close. It seems that it actually reports {{strategy.prev_market_position_size}} whereas I would like it to report {{strategy.market_position_size}}. Is there any way to do this from inside the script?

Alert pop up on strategy.entry

Upvotes: 0

Views: 981

Answers (1)

vitruvius
vitruvius

Reputation: 21294

You can use the {{strategy.position_size}} placeholder in your alert message.

//Order execution and alert
strategy.entry(id=alertID, direction=strategy.long, when = EntryCondition, alert_message='{'+POSTmessage+', "order_contratcs": '+ '{{strategy.position_size}}'+'}')
strategy.close(id=alertID, when = ExitCondition, alert_message='{'+POSTmessage+', "order_contratcs": '+'{{strategy.position_size}}'+'}')

enter image description here

Upvotes: 1

Related Questions