สร้าง Bot Trade Crypto ไม่ยากอย่างที่คิดด้วย Binance API

NUTHDANAI WANGPRATHAM
QUANT I LOVE U
Published in
2 min readDec 24, 2022

ในตลาด Crypto ที่มีความผันผวนสูงอีกทั้งตลาดยังเปิดให้ซื้อขายตลอด 24 ชั่วโมง การลงทุนในตลาด Crypto จึงมีความท้าทายอย่างมากสำหรับมนุษย์แต่ไม่ใช่สำหรับ Robot trade

Binance API

Binance API คือเครื่องมือในการดึงข้อมูลและส่งคำสั่ง API ของ Binance ช่วยให้นักพัฒนาสามารถเข้าถึงแพลตฟอร์มการแลกเปลี่ยนโดยใช้โปรโตคอล HTTP API เหล่านี้สามารถใช้เพื่อดึงข้อมูลตลาด จัดการคำสั่งซื้อ และดำเนินการอื่นๆ

หากต้องการใช้ API ของ Binance จะต้องสร้างบัญชี Binance และรับคีย์ API จากนั้นคุณสามารถใช้คีย์เหล่านี้เพื่อตรวจสอบคำขอ API ของคุณและเข้าถึงคุณลักษณะและฟังก์ชันของ Exchange ได้ สิ่งสำคัญคือต้องรักษาคีย์ API ของคุณให้ปลอดภัยและปฏิบัติตามแนวทางปฏิบัติที่ดีที่สุดเมื่อใช้ API ของ Binance เพื่อรับรองความปลอดภัยของบัญชีและการเทรด

import binance
from binance.enums import *
import pybacktest
import backtrader as bt
import tensorflow as tf
import numpy as np

เราสามารถดึงข้อมูลด้วย code ดังต่อไปนี้

import requests
# Set the API endpoint URL
api_url = "https://api.binance.com/api/v3/ticker/price"
# Set the symbol for the cryptocurrency you want to retrieve data for
symbol = "BTCUSDT"
# Send a GET request to the API endpoint
response = requests.get(api_url, params={"symbol": symbol})
# If the request was successful, print the price data
if response.status_code == 200:
data = response.json()
print(f"Current price of {symbol}: {data['price']}")
else:
print(f"Error retrieving price data for {symbol}")

# Data Extraction from Binance API
client = binance.Client("API_KEY", "API_SECRET")
historical_data = client.futures.klines(symbol="BTCUSDT", interval=KLINE_INTERVAL_1MINUTE)
real_time_data = client.futures.ticker(symbol="BTCUSDT")

ที่นี้เราจะสร้างกลยุทธเทรดง่ายๆ โดยหากผลตอบแทนมีแนวโน้มสูงขึ้นให้ซื้อและผลตอบแทนมีแนวโน้มตำ่ลงให้ขาย สำหรับใครที่อยากดูกลยุทธ์อื่นๆคลิ๊กได้เลย

สร้าง Bot Trade Crypto ไม่ยากอย่างที่คิดด้วย Binance API
# Momentum Algorithmic Trading Strategy with ML
data['position'] = np.sign(data['returns'].rolling(3).mean())
data['strategy'] = data['position'].shift(1) * data['returns']
data[['returns', 'strategy']].dropna().cumsum().apply(np.exp).plot();

และเราก็ใช้ระบบส่งคำสั่งได้โดย code ดังต่อไปนี้

# Order Management System
def create_order(symbol, side, order_type, quantity):
try:
order = client.futures.order_limit(
symbol=symbol,
side=side,
type=order_type,
quantity=quantity,
price='0.00'
)
print(f'Order successful: {order}')
except Exception as e:
print(f'Error creating order: {e}')

# Backtesting System
strategy = bt.Strategy()

และเราก็สามารถหาค่า nav. เพื่อไปทำ backtesting

# backtesting trading strategy
def trade(df):
# Initialize the holding and cash
holding = 0
cash = 1000

# Iterate over the dataframe
for index, row in df.iterrows():
# Check if we have any holding
if holding == 0:
# Check if the momentum is positive
if row["momentum"] > 0:
# Buy the stock
holding = cash / row["close"]
cash = 0
else:
# Sell the stock
cash = holding * row["close"]
holding = 0

# Return the final cash balance
return cash

ข้อควรระวัง

โดยทั่วไปไม่ใช่ความคิดที่ดีที่จะไว้วางใจและพึ่งพา “หุ่นยนต์” หรือระบบอัตโนมัติสำหรับการซื้อขายบนแพลตฟอร์มเช่น Binance หรือการแลกเปลี่ยนสกุลเงินดิจิตอลอื่น ๆ มีโอกาสขาดทุนเสมอ และสิ่งสำคัญคือต้องทำการวิจัยอย่างถี่ถ้วนและทำความเข้าใจกับความเสี่ยงที่เกี่ยวข้องก่อนที่จะทำการซื้อขายใดๆ

--

--

NUTHDANAI WANGPRATHAM
QUANT I LOVE U

I am a learner and have a multipotential life. You can contact me at nutdnuy@gmail.com