I created a tradingview.com strategy that is ready to automate. And I have found that capitalize.ai (cap.ai from here) is able to execute my trades from tradingview (TV from hereon), via an API to Avatrade and Interactive Brokers.
The problem however is limited to webhook alerts from TV to cap.ai.
I enclose my alert and tradelogic code below. Does any one know of why I get 10 (5 SELL and 5 BUY alerts, every time the alert triggers. And cap.ai does not trigger on my alerts/webhooks, which have proper msg and URL).
My code for alerts and tradelogic
// Alert Messages long
ANDstartLongTradeMessage = input.string(title = "Long Entry Webhook Message", defval = "", group = "Long Webhook Messages")
ANDendLongTradeMessage = input.string(title = "Close Long Webhook Message", defval = "", group = "Long Webhook Messages")
// Alert Messages short
ANDstartShortTradeMessage = input.string(title = "Short Entry Webhook Message", defval = "", group = "Short Webhook Messages")
ANDendShortTradeMessage = input.string(title = "Close Short Webhook Message", defval = "", group = "Short Webhook Messages")
// ------------------- Trading signals and Alerts ----------------- See section on alerts above ANDTradelogic
// All orders have the same but dynamic text, that will show the direction of order
alertsyntax = '{{strategy.order.action}} - - - Machine Learning: Logistic Regression (ME v.6 - DynTP & SL) (Close, , 3, 2, 0.0009, 1000, None, 5, 7, 1, 2023, 1, 1, 2029, 6, 0.01):
order {{strategy.order.action}} @ {{strategy.order.contracts}} filled on {{ticker}}. New strategy position is {{strategy.position_size}}'
// Alerts in TV GUI will work with this message: {{ticker}} - {{strategy.order.action}} {{strategy.order.contracts}} @ {{strategy.order.price}}. New strategy position is {{strategy.position_size}} {{strategy.order.alert_message}}
// -------------------------------------------------------------------------
// ---------------- ANDTradelogic Fct ------------------------------ Bruges nu til handler for at minimere antal trades
ANDTradelogic(close) =>
if ANDstartLongTrade
strategy.entry('Long', strategy.long) //
if use_TP
strategy.exit("Exit Long, TP", from_entry="Long", limit=high * (1+(tpPercent/100)))
alert(message=alertsyntax, freq=alert.freq_all) //, freq=alert.freq_once_per_bar_close)
alert(ANDendShortTradeMessage, freq = alert.freq_once_per_bar)
alert(ANDstartLongTradeMessage, freq = alert.freq_once_per_bar)
if ANDstartShortTrade
strategy.entry('Short', strategy.short)
if use_TP
strategy.exit("Exit Short, TP", from_entry="Short", limit=low * (1-(tpPercent/100)))
alert(message=alertsyntax, freq=alert.freq_all) //, freq=alert.freq_once_per_bar_close)
alert(ANDendLongTradeMessage, freq = alert.freq_once_per_bar)
alert(ANDstartShortTradeMessage, freq = alert.freq_once_per_bar)
// Final decision of when to trade
if ANDendShortTrade
strategy.close('Long', 'Long')
alert(message=alertsyntax) //, freq=alert.freq_once_per_bar_close)
alert(ANDendShortTradeMessage, freq = alert.freq_once_per_bar)
if ANDendLongTrade
strategy.close('Short', 'Short')
alert(message=alertsyntax) //, freq=alert.freq_once_per_bar_close)
alert(ANDendLongTradeMessage, freq = alert.freq_once_per_bar)
if window() // if in timewindow of trading/backtest
// Run ANDTradelogic Fct.
ANDTradelogic (close)
I am expecting that cap.ai can use my alerts to dynamically from my TV strategy code, get the direction of the trade (BUY/SELL) and the quantity, which change over time. So a hardcoded cap.ai will not do. This is too constrained.
When I do backtest of this strategy on TV, the backtest properly trades both ways, and the quantity evolves like so: BUY x, Short 2x, buy 2x, short 2x…. etc.
1
Check out the documentation and examples posted on the Capitalise web
Use only box input strings
something like this:
// Alert Messages long
ANDstartLongTradeMessage = input.string(title = "Long Entry Webhook Message", defval = "", group = "Long Webhook Messages")
ANDendLongTradeMessage = input.string(title = "Close Long Webhook Message", defval = "", group = "Long Webhook Messages")
// Alert Messages short
ANDstartShortTradeMessage = input.string(title = "Short Entry Webhook Message", defval = "", group = "Short Webhook Messages")
ANDendShortTradeMessage = input.string(title = "Close Short Webhook Message", defval = "", group = "Short Webhook Messages")
if ANDstartLongTrade
strategy.entry('Long', strategy.long) //
alert(ANDstartLongTradeMessage, freq = alert.freq_once_per_bar)
if ANDendLongTrade
strategy.close('Long', 'Long')
alert(ANDendLongTradeMessage, freq = alert.freq_once_per_bar)
if ANDstartShortTrade
strategy.entry('Short', strategy.short)
alert(ANDstartShortTradeMessage, freq = alert.freq_once_per_bar)
if ANDendShortTrade
strategy.close('Short', 'Short')
alert(ANDendShortTradeMessage, freq = alert.freq_once_per_bar)
4