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:

💡
When using Bracket Orders (ProfitTake and StopLoss), it's vital to specify 'strategy.entrylong', exitLimit, and exitStop to determine the kind of order you intend to employ.
💡
In case of any uncertainties, revisiting the chapter “Defining Alert Message Format for TBOT” is advised.

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.

Subscribe to TBOT on TradingBoat

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe