skka3134

skka3134

email
telegram

加密貨幣自動交易機器人:示範

  1. 安裝相依套件
sudo /home/skka3134/folder/botTrade/bin/python -m pip install pandas_ta
sudo /home/skka3134/folder/botTrade/bin/python -m pip install schedule
import schedule
import pandas_ta as ta
pd.set_option('display.max_rows',None)
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from  datetime import datetime
import time
  1. 轉換時間戳,to_datetime () 函數用於將時間戳轉換為日期時間。
    df['dt']=pd.to_datetime(df['timestamp'],unit='ms')

image
15. 計算指標,Pandas TA 是一個基於 Pandas 模組開發的,具有上百個技術指標和常用指標的開源模組。RSI 指標正是根據供求平衡的原理,通過測量某一個期間內股價上漲總幅度佔股價變化總幅度平均值的百分比,來評估多空力量的強弱程度,進而提示具體操作的。指數平均數指標 (Exponential Moving Average,EXPMA 或 EMA) ,它也是一種趨向類指標,其構造原理是仍然對價格收盤價進行算術平均,並根據計算結果來進行分析,用於判斷價格未來走勢的變動趨勢。

df['rsi']=ta.rsi(df['close'],length=10)
df['ema']=ta.ema(df['close'],length=200)

image
16. 從我們處理好的數據中獲取最新的部分

last_row_index=len(df.index)-1
latest_rsi=round(df['rsi'].iloc[-1],2)
latest_price=round(df['close'].iloc[-1],2)
latest_ema=round(df['ema'].iloc[-1],2)
latest_ts=df['timestamp'].iloc[-1]
  1. 創建購買條件,如果最新的 Ris 指標小於 20,並且最新的價格大於最新的 ema 指標,那麼就買入 ETH,1 個,1. RSI 值小於 20,超賣,RSI 值大於 80,超買.
long_condition=(latest_rsi<20)and(latest_price>latest_ema)
if long_condition:
    order=exchange.create_market_buy_order('ETH/USDT',1)
closed_orders=exchange.fetch_closed_orders('ETH/USDT',limit=2)
  1. 賣出條件
most_recent_closed_order=closed_orders[-1]
tf_mult=exchange.parse_timeframe('30m')*1000
diff=latest_ts-most_recent_closed_order['timestamp']
last_buy_signal_cnt=int(diff/tf_mult)
exit_condition=(latest_rsi<40)and(last_buy_signal_cnt>10)
if exit_condition:
    order=exchange.create_market_sell_order('ETH/USDT',1)

  1. + 個循環,完整版
import ccxt
import schedule
import pandas as pd
import pandas_ta as ta
pd.set_option('display.max_rows',None)
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from  datetime import datetime
import time


exchange = ccxt.binance({
    'apiKey': '',
    'secret': '',
    'enableRateLimit': True,
})


exchange.load_markets()

entry_rsi=30
exit_rsi=40
symbol='ETH/USDT'
timeframe='30m'
tf_mult=exchange.parse_timeframe(timeframe)*1000


def indicators(data):
    data['rsi']=data.ta.rsi(length=10)
    data['ema']=data.ta.ema(length=200)
    return data

def check_buy_sell_signals(df):
    last_row_index=len(df.index)-1
    latest_rsi=round(df['rsi'].iloc[-1],2)
    latest_price=round(df['close'].iloc[-1],2)
    latest_ema=round(df['ema'].iloc[-1],2)
    latest_ts=df['timestamp'].iloc[-1]

    long_condition=(latest_rsi<30)and(latest_price>latest_ema)
    if long_condition:
        order=exchange.create_market_buy_order('ETH/USDT',1)
    closed_orders=exchange.fetch_closed_orders('ETH/USDT',limit=2)

    most_recent_closed_order=closed_orders[-1]
    diff=latest_ts-most_recent_closed_order['timestamp']
    last_buy_signal_cnt=int(diff/tf_mult)

    exit_condition=(latest_rsi<40)and(last_buy_signal_cnt>10)
    if exit-exit_condition:
        order=exchange.create_market_sell_order('ETH/USDT',1)

def run_bot():
    bars=exchange.fetch_ohlcv('SOL/USDT',timeframe='30m',limit=200)
    df=pd.DataFrame(bars[:],columns=['timestamp','open','high','low','close','volume'])
    df['dt']=pd.to_datetime(df['timestamp'],unit='ms')
    df=indicators(df).tail(30)
    check_buy_sell_signals(df)

schedule.every(10).seconds.do(run_bot)

while True:
    schedule.run_pending()
    time.sleep(1)
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。