Crafting and Implementing Alert Messages in PineScript for IBKR Bracket Orders
Background
Before proceeding with this chapter, it's essential to have read and understood the chapter titled “Defining Alert Message Format for TBOT”. This will provide the foundational knowledge about the Alert Message Format.
Use Case
For PineScript Users wanting to utilize strategy.exitlong
with ProfitTake and StopLoss settings, this chapter provides instructions on creating a message using the makeWebhookJson()
Note: There's a nuanced difference between how PineScript's strategy functions operate versus the workings of the IBKR API. In PineScript, if you sequentially invoke strategy.entry
followed by strategy.exit
, the PineScript Engine will delay strategy.exit
until strategy.entry
is executed.
if longEntryCond
...
strategy.entry("Enter Long #1", strategy.long)
strategy.exit("Exit Long#A", from_entry="Enter Long #1",
stop=longStopPrice, limit=longLimitPrice)
However, when crafting an alert message, you don't have to dispatch two separate messages. For an alert corresponding to strategy.entry
, you can utilize the following:
// TBOT: creates a bracket IBKR order
msg = tv.makeWebhookJson(webhookKey=webhookKey, direction='strategy.entrylong',
qty=qty, exitLimit=longLimitPrice, exitStop=longStopPrice, orderRef='unique7')
strategy.entry("Enter Long #1", strategy.long, alert_message=msg)
There are two crucial points to consider:
The central takeaway from that chapter is the flexibility you have in setting prices which allows the definition of various order types like market order, limit order, bracket order, attached orders, and so on.
To design a bracket order for strategy.exitlong
, follow:
msg = tv.makeWebhookJson(webhookKey=webhookKey, direction='strategy.exitlong',
qty=qty, exitLimit=longLimitPrice, exitStop=longStopPrice, orderRef='unique7')
strategy.exit("Exit Long#B", from_entry="Enter Long #1",
stop=longStopPrice,limit=longLimitPrice, alert_message=msg)
For further details and procedures, I highly recommend referring to the comprehensive Udemy course available here:Link to Udemy Course.