PINE教學案例:12年200倍的雙均綫TradingView交易策略與自動交易教學/量化交易教學

本文内容

    本文内容:PINE教學案例:12年200倍的雙均綫TradingView交易策略與自動交易教學/量化交易教學;

    本文程式代碼僅做教學用途,不構成任何投資建議,加密行業風險高,請謹慎操作。

準備工作

    1. 你需要1個 TradingView Pro 以上的賬戶: https://www.tradingview.com/

    2. 你需要使用 TradingView 專業版賬戶:TVCBOT專業版交易機器人

    3. 你需要將本文提供的策略案例程式代碼添加到 TradingView 中

教學開始

   請將以下的交易策略程式演示代碼添加到 TradingView 中,我們在tradingview中選擇 BITSTAMP:BTCUSD 交易對,切換爲4H周期,我們要將這個代碼添加到 tradingview 的pine編輯器中,保存后添加到圖表。(請注意,這份代碼僅供學習與參考,我們不對代碼任何時候的結果提供保證,你可以自行修改)

// This source code is subject to the terms of the GNU GENERAL PUBLIC LICENSE 3.0 at https://www.gnu.org/licenses/gpl-3.0.html
// @ blockplus
// 代碼來源:https://www.tvcbot.com/knowledgebase/72/PINE教學案例12年200倍的雙均綫TradingView交易策略與自動交易教學or量化交易教學.html

//@version=5
strategy("TVCBOT Strategy Example (SMA Crossing)", 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)

// 兩根SMA均綫
trend_type1_length=input.int(25, "MA1长度")
trend_type2_length=input.int(100, "MA2长度")
ma1 = ta.wma(close,trend_type1_length)
ma2 = ta.wma(close,trend_type2_length)

// 把SMA畫出來
p3 = plot(ma1, color= color.new(#00ff00, 0), title="SMA1", linewidth = 1)
p4 = plot(ma2, color= color.new(#ff0000, 0), title="SMA2", linewidth = 1)
fill(p3, p4, color = ma1 > ma2 ? color.new(#00ff00, 50):color.new(#ff0000, 50))

// 參數設定
long_sl_input = input.float(5, title='止损(%)', step=0.1)/100
long_sl_input_level = strategy.position_avg_price * (1 - long_sl_input)
multiplier = input.float(3.5, "SL Mutiplier", minval=1, step=0.1)
ATR_period=input.int(8,"ATR长度", minval=1, step=1)

// 入場條件: ma1上穿ma2 且 close在atr下軌前高之上;
// 出場條件:close在atr下軌前高之下 或 close<固定止損價格
ATRTrail = 0.0
ATRTrail := close - multiplier * ta.atr(ATR_period)
ATRTrailHigh = ta.highest(ATRTrail, 50)
atrd = plot(ATRTrail, color= color.new(#ffffff, 0), title="ATR", linewidth = 1)
fill(p3, atrd, color = color.new(#ffffff, 75))
entry_long=ta.crossover(ma1,ma2) and ATRTrailHigh < close
exit_long = close < ATRTrailHigh or close < long_sl_input_level

// 入場與出場
if entry_long
    strategy.entry("long", strategy.long)
if exit_long
    strategy.close("long", comment="close" )

   添加到圖表后,我們會看到如下界面(請注意不同人添加這份代碼回測信息可能不一樣):

   接下來,我們點 List of Trades(交易列表),可看到如下界面:

    看到 Contracts 這一欄了嗎?這是我們要改的。我們點鬧鐘旁邊的設定,修改 Order Size 為你要下單的BTC數量,比如你每次要做多0.01個BTC,就輸入0.01,右邊選擇 Contract 表示用幣的數量下單,然後點OK即可。

 

連接 TVCBOT 開始進行自動交易

    在上面一個步驟中,我們已經完成了tradingview的交易設定,下一步我們連接 TVCBOT 開始自動交易。

    假設你已經在 TVCBOT 專業版上連接了你的 OKX/Binance/bybit 等賬戶(如果沒有請查看TVCBOT知識庫教程網頁)。

    接下來打開你的 TVCBOT 機器人界面,按照如下界面選擇,選擇你的交易賬戶后,交易對選擇 BTC-U本位永續!!請注意!我故意選錯了,是希望你記住不要選錯,因爲我們的 tradingview 圖表用的是 BTC,所以這裏要選擇 BTC-U本位永續,請不要選錯,然後選擇策略對接,智能對接。其他的都按照我的圖片設定,如果你不明白意思,TVCBOT網頁上都會告訴你。

tvcbot tradingview bot

    按照上述設定后,點擊生成 TradingView 下單警報后,出現以下界面:

    在 TradingView 找到鬧鐘圖標,添加快訊警報,按下面的圖片添加警報即可。

    主要有兩步:

     一:填寫消息

     二:填寫 Webhook URL

下面是點 notification(通知)后粘貼 webhook url

粘貼完成后,警報創建完成,就可以自動交易了。如果你要停止自動交易,請在tradingview右側的快訊列表停止你所添加的快訊。

这个答案有用吗? 7 个用户发现这个很有用 (7 张投票)