6. 安装依赖
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
14. 转换 timestamp,to_datetime () 函数用于将时间戳转换为日期时间。
df['dt']=pd.to_datetime(df['timestamp'],unit='ms')
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)
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]
17. 创建购买条件,如果最新的 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)
18. 卖出条件
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)
19.+ 个循环,完整版
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)