Trading Guides
🚀 Getting Started with TradingView Integration
Section titled “🚀 Getting Started with TradingView Integration”What You’ll Learn
Section titled “What You’ll Learn”- How to set up webhook connections
- Understanding the Algo Trade Analytics webhook format
- Creating your first automated strategy
- Testing and debugging your setup
Step 1: Understanding the Basics
Section titled “Step 1: Understanding the Basics”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 ExecutionStep 2: Basic Pine Script Example
Section titled “Step 2: Basic Pine Script Example”Here’s a complete Pine Script example that implements the Algo Trade Analytics webhook format:
//@version=6strategy("Algo Trade Analytics Basic Strategy", overlay=true)
// Webhook configurationauthKey = "your_auth_key_here"ticker = syminfo.ticker
// Simple moving average strategylength = input.int(20, "MA Length")ma = ta.sma(close, length)
// Entry conditionslongCondition = ta.crossover(close, ma)shortCondition = ta.crossunder(close, ma)
// Create webhook messageslongMessage = '{"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 webhooksif longCondition strategy.entry("Long", strategy.long, alert_message=longMessage)if shortCondition strategy.entry("Short", strategy.short, alert_message=shortMessage)Next Steps
Section titled “Next Steps”- 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 Webhook Format
Section titled “Algo Trade Analytics Webhook Format”Algo Trade Analytics uses a specific webhook format. Here are the key fields:
Required Fields
Section titled “Required Fields”ticker- Stock symboldirection- “long” or “short”auth_key- Authentication key
Optional Fields
Section titled “Optional Fields”qty- Share quantityorderType- Order typestopPrice- Stop pricelimitPrice- Limit price
Advanced Pine Script Example
Section titled “Advanced Pine Script Example”//@version=6strategy("Advanced Algo Trade Analytics Strategy", overlay=true)
// ConfigurationauthKey = 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)
// Indicatorsema20 = ta.ema(close, 20)ema50 = ta.ema(close, 50)rsi = ta.rsi(close, 14)
// Entry conditionslongCondition = ta.crossover(ema20, ema50) and rsi < 70shortCondition = ta.crossunder(ema20, ema50) and rsi > 30
// Risk managementstopLossPercent = 2.0takeProfitPercent = 4.0
// Calculate position size based on riskstopLoss = longCondition ? close * (1 - stopLossPercent/100) : shortCondition ? close * (1 + stopLossPercent/100) : na
accountEquity = 100000 // Replace with your account sizeriskAmount = accountEquity * (riskPercent / 100)stopDistance = math.abs(close - stopLoss)qty = math.floor(riskAmount / stopDistance)
// Webhook messages with stop losseslongMessage = '{"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”Position Sizing Methods
Section titled “Position Sizing Methods”Fixed Dollar Risk
Section titled “Fixed Dollar Risk”Risk the same dollar amount on each trade:
riskAmount = 500 // $500 per tradestopDistance = math.abs(close - stopLoss)qty = math.floor(riskAmount / stopDistance)Percentage Risk
Section titled “Percentage Risk”Risk a percentage of account equity:
riskPercent = 2.0 // 2% of accountaccountEquity = 100000riskAmount = accountEquity * (riskPercent / 100)qty = math.floor(riskAmount / stopDistance)Risk Management Checklist
Section titled “Risk Management Checklist”- 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
🔧 Testing and Debugging Strategies
Section titled “🔧 Testing and Debugging Strategies”Testing Workflow
Section titled “Testing Workflow”-
Backtest in TradingView
- Test your strategy logic with historical data
-
Validate Webhook Format
- Use Algo Trade Analytics’ webhook tester
-
Paper Trading
- Test with live data but simulated trades
-
Small Position Live Test
- Start with minimal position sizes
Webhook Testing Tools
Section titled “Webhook Testing Tools”Built-in Tester
Section titled “Built-in Tester”- Validates JSON format
- Tests authentication
- Simulates trade execution
Webhook Logs
Section titled “Webhook Logs”- See all webhook attempts
- Check response codes
- Review error messages
Best Practices
Section titled “Best Practices”- 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
📊 Advanced Order Types
Section titled “📊 Advanced Order Types”Market Orders
Section titled “Market Orders”{ "ticker": "AAPL", "direction": "long", "auth_key": "your_key", "orderType": "market", "qty": 100}Limit Orders
Section titled “Limit Orders”{ "ticker": "AAPL", "direction": "long", "auth_key": "your_key", "orderType": "limit", "limitPrice": 150.00, "qty": 100}Stop-Loss Orders
Section titled “Stop-Loss Orders”{ "ticker": "AAPL", "direction": "short", "auth_key": "your_key", "orderType": "stop", "stopPrice": 145.00, "qty": 100}Stop-Limit Orders
Section titled “Stop-Limit Orders”{ "ticker": "AAPL", "direction": "short", "auth_key": "your_key", "orderType": "stop_limit", "stopPrice": 145.00, "limitPrice": 144.50, "qty": 100}🎯 Strategy Examples
Section titled “🎯 Strategy Examples”Mean Reversion Strategy
Section titled “Mean Reversion Strategy”//@version=6strategy("Mean Reversion", overlay=true)
// Bollinger Bands mean reversionlength = 20mult = 2.0basis = ta.sma(close, length)dev = mult * ta.stdev(close, length)upper = basis + devlower = basis - dev
// Entry conditionslongCondition = close < lowershortCondition = close > upper
// Exit conditionslongExit = close > basisshortExit = close < basis
// Webhook implementationauthKey = "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")Breakout Strategy
Section titled “Breakout Strategy”//@version=6strategy("Breakout Strategy", overlay=true)
// Donchian Channel breakoutlength = 20upper = ta.highest(high, length)lower = ta.lowest(low, length)
// Entry conditionslongCondition = close > upper[1]shortCondition = close < lower[1]
// Webhook messagesauthKey = "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)📈 Performance Optimization
Section titled “📈 Performance Optimization”Reducing Slippage
Section titled “Reducing Slippage”- Use limit orders when possible
- Avoid trading during low liquidity periods
- Consider market impact for large positions
- Monitor bid-ask spreads
Improving Fill Rates
Section titled “Improving Fill Rates”- Set realistic limit prices
- Use stop-limit orders instead of stop orders
- Consider time-in-force parameters
- Monitor order execution quality
Monitoring and Alerts
Section titled “Monitoring and Alerts”- Set up real-time performance alerts
- Monitor drawdown levels
- Track hit rates and slippage
- Review execution quality regularly
🔗 Related Resources
Section titled “🔗 Related Resources”- Knowledge Base - FAQ and troubleshooting
- Algo Trade Analytics Webhook API - Complete API specification
- Integration Guide - Technical implementation
- Risk Management - Advanced risk control