- 依存関係のインストール
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
- タイムスタンプの変換、to_datetime () 関数はタイムスタンプを日付と時刻に変換するために使用されます。
df['dt']=pd.to_datetime(df['timestamp'],unit='ms')
15. インジケータの計算、Pandas TA は、Pandas モジュールに基づいて開発されたオープンソースモジュールで、100 以上のテクニカルインジケータと一般的なインジケータを備えています。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]
- 購入条件の作成、最新の RSI 指標が 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)
- 売却条件
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)
- ループ、完全版
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)