# TradingView 策略自动化交易：使用 PineScript 实现量化交易与交易所自动下单

By [jtnffmizhcy](https://paragraph.com/@jtnffmizhcy) · 2025-04-01

---

文章概览
----

本文将详细指导您如何将 **TradingView 策略** 与加密货币交易所对接，实现自动化交易。通过本教程，您将学会使用 PineScript 编写量化交易策略，并将其串联至 OKX、Binance、Bybit 等平台进行自动买卖操作。

准备工作
----

在开始之前，您需要完成以下准备：

*   **TradingView Pro 或以上账户**：确保您拥有一个高级账户以解锁更多功能，访问 [TradingView 官网](https://bit.ly/TradingView-Pro) 获取账户。
    
*   **交易策略**：您需要一个可用的交易策略，可以是 TradingView 社区的公开策略、自编策略，或他人分享的开源/闭源代码。
    

教程详解
----

假设您已准备好一个交易策略，下一步是将该策略与交易所对接，实现自动化下单。本教程将以 **TVCBOT 专业版** 的智能对接模式为例，逐步展示操作流程。

### 步骤 1：加载 TradingView 交易策略

首先，您需要一个交易策略代码。以 OKX 的 BTCUSDT.P 交易对（4小时周期）为例，以下是一个简单的 PineScript 示例代码。将代码添加至 TradingView 的 Pine 编辑器，保存并应用到图表中。

> **注意**：此代码仅供学习参考，不保证任何交易结果，您可根据需求自行调整。

pine // This source code is subject to the terms of the Mozilla Public License 2.0 at [https://mozilla.org/MPL/2.0/](https://mozilla.org/MPL/2.0/) // © TVCBOT

//@version=4 strategy("TVCBOT BTC Automated Strategy Example", overlay=true, initial\_capital=5000, pyramiding=0, currency="USD", default\_qty\_type=strategy.percent\_of\_equity, default\_qty\_value=100, commission\_type=strategy.commission.percent, commission\_value=0.1)

// ATR 计算 Atr(p) => atr = 0. Tr = max(high - low, max(abs(high - close\[1\]), abs(low - close\[1\]))) atr := nz(atr\[1\] + (Tr - atr\[1\])/p, Tr)

// TEMA 计算 TEMA(series, length) => if (length > 0) ema1 = ema(series, length) ema2 = ema(ema1, length) ema3 = ema(ema2, length) (3 \* ema1) - (3 \* ema2) + ema3 else na

tradeType = input("LONG", title="What trades should be taken : ", options=\["LONG", "SHORT", "BOTH", "NONE"\])

// 指标设置 source = close trend\_type1 = input("TEMA", title="First Trend Line : ", options=\["LSMA", "TEMA", "EMA", "SMA"\]) trend\_type2 = input("LSMA", title="First Trend Line : ", options=\["LSMA", "TEMA", "EMA", "SMA"\]) trend\_type1\_length = input(25, "Length of the First Trend Line") trend\_type2\_length = input(100, "Length of the Second Trend Line")

leadLine1 = if trend\_type1 == "LSMA" linreg(close, trend\_type1\_length, 0) else if trend\_type1 == "TEMA" TEMA(close, trend\_type1\_length) else if trend\_type1 == "EMA" ema(close, trend\_type1\_length) else sma(close, trend\_type1\_length)

leadLine2 = if trend\_type2 == "LSMA" linreg(close, trend\_type2\_length, 0) else if trend\_type2 == "TEMA" TEMA(close, trend\_type2\_length) else if trend\_type2 == "EMA" ema(close, trend\_type2\_length) else sma(close, trend\_type2\_length)

// 交易条件 entry\_long = crossover(leadLine1, leadLine2) and Trail1\_high < close exit\_long = close < Trail1\_high or crossover(leadLine2, leadLine1) or close < long\_sl\_input\_level

// 回测时间 testStartYear = input(2016, "Backtest Start Year") testPeriodStart = timestamp(testStartYear, 1, 1, 0, 0) testStopYear = input(9999, "Backtest Stop Year") testPeriodStop = timestamp(testStopYear, 12, 31, 0, 0)

testPeriod() => time >= testPeriodStart and time <= testPeriodStop ? true : false

if testPeriod() if tradeType == "LONG" or tradeType == "BOTH" if strategy.position\_size == 0 or strategy.position\_size > 0 strategy.entry("long", strategy.long, when=entry\_long) strategy.close("long", when=exit\_long, comment="close")

将代码加载到图表后，您可以查看回测结果。点击“交易列表”（List of Trades），检查策略表现并调整下单数量。例如，若每次下单 0.01 BTC，可在设置中修改 Order Size 为 0.01，并选择 Contract 单位。

👉 [【点击查看】TradingView 30天 独享 Premium 高级会员账号（完整质保30天售后）](https://bit.ly/TradingView-Pro)

### 步骤 2：连接 TVCBOT 实现自动交易

完成 TradingView 的策略设置后，接下来通过 TVCBOT 连接交易所账户（如 OKX、Binance 等），实现自动化交易。

#### 操作流程

1.  **账户绑定**：确保已在 TVCBOT 专业版中绑定您的交易所账户。
    
2.  **配置交易对**：在 TVCBOT 界面选择 BTC-U 本位永续合约（与 TradingView 图表一致），然后启用“策略对接 - 智能对接”模式。
    
3.  **生成警报**：点击“生成 TradingView 下单警报”，获取消息内容和 Webhook URL。
    
4.  **设置警报**：在 TradingView 中打开警报功能（闹钟图标），粘贴消息和 Webhook URL，保存设置。
    

完成以上步骤后，您的策略将自动运行。若需暂停交易，可在 TradingView 的警报列表中停止相关警报。

总结
--

通过本教程，您已掌握如何使用 **PineScript** 编写量化交易策略，并在 **TradingView** 上结合 TVCBOT 实现与交易所的自动下单对接。无论是新手还是资深交易者，这一方法都能帮助您提升交易效率，抓住市场机会。立即尝试，优化您的交易体验！

---

*Originally published on [jtnffmizhcy](https://paragraph.com/@jtnffmizhcy/tradingview-pinescript)*
