Try   HackMD

使用FTX量化空間定投BTC

tags: 加密貨幣

設定每15分鐘自動買0.0001個BTC

在FTX開一個子帳戶

先開一個子帳戶專門來做BTC定投,比較好計算成本與收益

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

子帳戶名稱隨意輸入個「btc-dca」

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

切換至這個子帳戶後,點上方的選單,選「量化空間」

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

每個子帳戶的量化空間都是分開的

點「建立新策略」

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定下單條件

或是點Import Rule,輸入Import string:

{
    "name": "buy BTC",
    "condition": "balance(\"USD\")>100 and price(\"BTC/USD\")<45000",
    "actions": [
        {
            "ioc": false,
            "side": "buy",
            "size": "0.0001",
            "type": "place",
            "market": "BTC/USD",
            "postOnly": true,
            "orderType": "limit",
            "limitPrice": "bid_price(\"BTC/USD\")",
            "reduceOnly": false,
            "replaceExisting": true,
            "cancelIfNoLongerTriggered": false
        },
        {
            "type": "pause",
            "allRules": false,
            "duration": {
                "unit": "minute",
                "value": "15"
            },
            "cancelOrders": false
        }
    ]
}

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

名稱隨意寫個 buy BTC
條件不限的話就輸入 true
想要在45000以下才買的話可以輸入 price("BTC/USD")<=45000

要限制只買到0.1顆BTC的話,可以加上 and balance("BTC")<=0.1
balance("BTC") 代表這個子帳號有多少BTC現貨的意思

若想要買到 USD 剩下 100 就停止的話,加上 and balance("USD")>100

例如想要價格跌到45000以下才買,買到最多0.1顆BTC,且買到USD剩下100為止,輸入:
price("BTC/USD")<=45000 and balance("BTC")<=0.1 and balance("USD")>100

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定訂單數量與價格

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

執行邏輯,使用「下自定義訂單」
選「限價委託」「買入」「BTC/USD」
如果有質押25FTT,使用限價委託可以免手續費

訂單數量,輸入每次想買幾顆BTC
要買0.0001顆的話輸入「0.0001」,如果幣價是$45000的話,買0.0001顆就是$4.5
若要用10usd來買,輸入「10/price("BTC/USD")」
注意BTC最小下單金額是0.0001,設定小於0.0001的話會下單失敗

限價,要掛買單第一筆的話,輸入「bid_price("btc/USD")」
勾「Post Only」可限定只要用掛單買入,如果掛單時設定的價位忽然變賣單價了,就會取消這筆交易,避免使用市價買入而被抽手續費

勾「取消並下新訂單」,自動改掛單到買單第一筆,每次執行這個策略時,若之前掛的買單因為上漲沒買到,就取消該訂單,重新掛到新的買單第一筆

使用限價委託,要在掛單後BTC價格再下來一點才會買到,如果掛單後BTC價格就漲上去了那就會沒買到,如果要確保可以買到的話,可以改用市價委託,但會有手續費且可能會滑價

設定多久執行一次

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

點增加執行邏輯,Type選「暫停策略」
設定想要每隔多久再執行一次,例如每15分鐘執行一次

如果沒有設定暫停策略的話,會每15秒執行一次

輸入好後點「保存並運行」就會開始執行了

計算平均購買價格

想要計算平均購買價格的話,因為量化空間只會幫你掛單,不會知道什麼時候成交,所以只能依照花了多少USD,以及共買了多少BTC來計算
平均購買價格 = (一開始投入的USD - 剩下的USD) / 買了多少BTC

建立變數

新增一個策略,專門用來新增變數

先新增變數 average_price,用來記錄平均購買價格
Import string:

{
    "name": "set variable",
    "condition": "true",
    "actions": [
        {
            "name": "average_price",
            "type": "setVariable",
            "value": "0"
        },
        {
            "type": "pause",
            "allRules": false,
            "duration": {
                "unit": "hour",
                "value": "5000"
            },
            "cancelOrders": false
        }
    ]
}

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

策略名稱輸入「set variable」
條件輸入「true」
Type選「設定變量」
變量名稱輸入「average_price」
變量值輸入「0」

點「增加執行邏輯」

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Type選「暫停策略」
時間選5000天,因為沒有永遠暫停的選項所以隨便設一個很久的時間就好

點「保存並運行」,過一會就會在變量那邊看到 average_price 了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

再建立一個變數 investment,用來記錄總投資額
直接修改 set_variable 策略,將變量名稱改為 investment,變量值改為這個子帳戶放的USD數量,點「保存並運行」,過一會就會看到有兩個變數了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

計算平均購買價格

新增一個策略 count_average_price
Import string:

{
    "name": "count_average_price",
    "condition": "balance(\"BTC\")>0",
    "actions": [
        {
            "name": "average_price",
            "type": "setVariable",
            "value": "(get_variable(\"investment\") - balance(\"USD\")) / balance(\"BTC\")"
        },
        {
            "type": "pause",
            "allRules": false,
            "duration": {
                "unit": "minute",
                "value": "15"
            },
            "cancelOrders": false
        }
    ]
}

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

條件輸入 balance("BTC")>0
有買了BTC後才開始計算,也避免計算時產生除以0的錯誤

執行邏輯使用「設定變量」,變量名稱「average_price」
變量值「(get_variable("investment") - balance("USD")) / balance("BTC")」
其中使用 get_variable("investment") 取得之前設定的投資額變數的值
(get_variable("investment") - balance("USD")) 代表已花了多少 USD

增加執行邏輯

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用暫停策略,每15分鐘再計算一次就好了,避免日誌上產生一堆執行記錄

點「保存並運行」

在變量這邊,就會看到計算好的平均購買成本了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如果之後有轉更多USD進來這個子帳戶,只要點 investment 變數右邊的編輯符號,將值改成新的投資額即可

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

將均價設為購買條件

有計算好的平均購買價後,就可以用 get_variable 取得均價,把下單條件改為現價低於均價才下單
price("BTC/USD") < get_variable("average_price")

不過這個下單條件不一定是好的,例如買了一點後就漲到高於均價線的話,定投就會停止了,可以視情況將購買條件再改為自己設的購買價

也可以另外再開一個策略,設定高於均價時就定時賣一點,在均價以上賣的話也可以讓均價變低,但要記得限制賣出的數量,以免一下就賣光光失去了定投的意義

將移動平均線設為購買條件

例如想要以8小時的移動平均線來當購買條件
可以在條件加上
price("BTC/USD") < average_price("BTC/USD",480)
其中480是8*60=480分鐘的意思

要在FTX的15分圖表上顯示8小時移動平均線的話
點「+指標」

搜尋MA

長度輸入32 (32*15=480)

設定跌愈多買愈多

有算出均價後,若想要跌得離均價愈多時,下單的數量就設定多一點,讓均價可以比較快降下來,例如現價為均價跌1%時,下單數量就乘2,跌2%就乘3,以此類推,可將訂單數量設為:

0.0001 * (1+( (get_variable("average_price")-price("BTC/USD")) / get_variable("average_price") ) * 100)
或是
0.0001 * (1+(1-price("BTC/USD")/get_variable("average_price") ) * 100)

意思就是,最小訂單價 * (1+ ((均價 - 現價) / 均價) * 100 )
= 最小訂單價 * (1+ ((1 - 現價 / 均價) * 100 )
其中 ((均價 - 現價) / 均價) * 100 為下跌的百分比,要再+1避免只跌0.9%時,最小訂單數量 * 0.9 會無法下單

如果覺得這樣會一次買太多錢不夠的話,可以把後面的100改小一點,或是在條件加上限制最多買入多少BTC

或是以移動平均線來設定,離8小時MA愈多時買愈多
0.0001 * (1+(1-price("BTC/USD")/average_price("BTC/USD",480)) ) * 100)