Skip to content

Trading Guides

🚀 Getting Started with TradingView Integration

Section titled “🚀 Getting Started with TradingView Integration”
  • How to set up webhook connections
  • Understanding the Algo Trade Analytics webhook format
  • Creating your first automated strategy
  • Testing and debugging your setup

Algo Trade Analytics uses webhooks to receive trading signals from TradingView. When your strategy generates a signal, TradingView sends a webhook to Algo Trade Analytics, which then executes the trade on Alpaca.

The Process:

TradingView Signal → Algo Trade Analytics Webhook → Alpaca Execution

Here’s a complete Pine Script example that implements the Algo Trade Analytics webhook format:

//@version=6
strategy("Algo Trade Analytics Basic Strategy", overlay=true)
// Webhook configuration
authKey = "your_auth_key_here"
ticker = syminfo.ticker
// Simple moving average strategy
length = input.int(20, "MA Length")
ma = ta.sma(close, length)
// Entry conditions
longCondition = ta.crossover(close, ma)
shortCondition = ta.crossunder(close, ma)
// Create webhook messages
longMessage = '{"ticker":"' + ticker +
'","direction":"long","auth_key":"' + authKey +
'","orderType":"market","qty":100}'
shortMessage = '{"ticker":"' + ticker +
'","direction":"short","auth_key":"' + authKey +
'","orderType":"market","qty":100}'
// Execute trades with webhooks
if longCondition
strategy.entry("Long", strategy.long, alert_message=longMessage)
if shortCondition
strategy.entry("Short", strategy.short, alert_message=shortMessage)
  • Test your setup with paper trading first
  • Monitor the webhook logs for successful connections
  • Explore advanced order types and risk management
  • Check out our advanced strategy examples

📝 Pine Script Fundamentals for Algo Trade Analytics

Section titled “📝 Pine Script Fundamentals for Algo Trade Analytics”

Algo Trade Analytics uses a specific webhook format. Here are the key fields:

  • ticker - Stock symbol
  • direction - “long” or “short”
  • auth_key - Authentication key
  • qty - Share quantity
  • orderType - Order type
  • stopPrice - Stop price
  • limitPrice - Limit price
//@version=6
strategy("Advanced Algo Trade Analytics Strategy", overlay=true)
// Configuration
authKey = input.string("your_auth_key", "Auth Key")
baseQty = input.int(100, "Base Quantity")
riskPercent = input.float(2.0, "Risk Percent", minval=0.1, maxval=10.0)
// Indicators
ema20 = ta.ema(close, 20)
ema50 = ta.ema(close, 50)
rsi = ta.rsi(close, 14)
// Entry conditions
longCondition = ta.crossover(ema20, ema50) and rsi < 70
shortCondition = ta.crossunder(ema20, ema50) and rsi > 30
// Risk management
stopLossPercent = 2.0
takeProfitPercent = 4.0
// Calculate position size based on risk
stopLoss = longCondition ? close * (1 - stopLossPercent/100) :
shortCondition ? close * (1 + stopLossPercent/100) : na
accountEquity = 100000 // Replace with your account size
riskAmount = accountEquity * (riskPercent / 100)
stopDistance = math.abs(close - stopLoss)
qty = math.floor(riskAmount / stopDistance)
// Webhook messages with stop losses
longMessage = '{"ticker":"' + syminfo.ticker +
'","direction":"long","auth_key":"' + authKey +
'","orderType":"stop_limit","qty":' + str.tostring(qty) +
',"stopPrice":' + str.tostring(stopLoss) + '}'
if longCondition
strategy.entry("Long", strategy.long, qty=qty, alert_message=longMessage)
strategy.exit("Exit Long", "Long", stop=stopLoss, limit=close * (1 + takeProfitPercent/100))

🛡️ Risk Management and Position Sizing

Section titled “🛡️ Risk Management and Position Sizing”

Risk the same dollar amount on each trade:

riskAmount = 500 // $500 per trade
stopDistance = math.abs(close - stopLoss)
qty = math.floor(riskAmount / stopDistance)

Risk a percentage of account equity:

riskPercent = 2.0 // 2% of account
accountEquity = 100000
riskAmount = accountEquity * (riskPercent / 100)
qty = math.floor(riskAmount / stopDistance)
  • Never risk more than 1-2% of account per trade
  • Always use stop losses
  • Size positions based on stop distance
  • Consider correlation between positions
  • Have a maximum daily/weekly loss limit
  • Test all strategies with paper trading first
  1. Backtest in TradingView

    • Test your strategy logic with historical data
  2. Validate Webhook Format

    • Use Algo Trade Analytics’ webhook tester
  3. Paper Trading

    • Test with live data but simulated trades
  4. Small Position Live Test

    • Start with minimal position sizes
  • Validates JSON format
  • Tests authentication
  • Simulates trade execution
  • See all webhook attempts
  • Check response codes
  • Review error messages
  • Always test with paper trading first
  • Use version control for your Pine Script code
  • Keep detailed logs of your testing process
  • Test edge cases and error conditions
  • Monitor performance metrics continuously
{
"ticker": "AAPL",
"direction": "long",
"auth_key": "your_key",
"orderType": "market",
"qty": 100
}
{
"ticker": "AAPL",
"direction": "long",
"auth_key": "your_key",
"orderType": "limit",
"limitPrice": 150.00,
"qty": 100
}
{
"ticker": "AAPL",
"direction": "short",
"auth_key": "your_key",
"orderType": "stop",
"stopPrice": 145.00,
"qty": 100
}
{
"ticker": "AAPL",
"direction": "short",
"auth_key": "your_key",
"orderType": "stop_limit",
"stopPrice": 145.00,
"limitPrice": 144.50,
"qty": 100
}
//@version=6
strategy("Mean Reversion", overlay=true)
// Bollinger Bands mean reversion
length = 20
mult = 2.0
basis = ta.sma(close, length)
dev = mult * ta.stdev(close, length)
upper = basis + dev
lower = basis - dev
// Entry conditions
longCondition = close < lower
shortCondition = close > upper
// Exit conditions
longExit = close > basis
shortExit = close < basis
// Webhook implementation
authKey = "your_auth_key"
longMessage = '{"ticker":"' + syminfo.ticker + '","direction":"long","auth_key":"' + authKey + '"}'
shortMessage = '{"ticker":"' + syminfo.ticker + '","direction":"short","auth_key":"' + authKey + '"}'
if longCondition
strategy.entry("Long", strategy.long, alert_message=longMessage)
if longExit
strategy.close("Long")
//@version=6
strategy("Breakout Strategy", overlay=true)
// Donchian Channel breakout
length = 20
upper = ta.highest(high, length)
lower = ta.lowest(low, length)
// Entry conditions
longCondition = close > upper[1]
shortCondition = close < lower[1]
// Webhook messages
authKey = "your_auth_key"
qty = 100
longMessage = '{"ticker":"' + syminfo.ticker +
'","direction":"long","auth_key":"' + authKey +
'","qty":' + str.tostring(qty) + '}'
if longCondition
strategy.entry("Long", strategy.long, alert_message=longMessage)
  • Use limit orders when possible
  • Avoid trading during low liquidity periods
  • Consider market impact for large positions
  • Monitor bid-ask spreads
  • Set realistic limit prices
  • Use stop-limit orders instead of stop orders
  • Consider time-in-force parameters
  • Monitor order execution quality
  • Set up real-time performance alerts
  • Monitor drawdown levels
  • Track hit rates and slippage
  • Review execution quality regularly