当方、Python経験一週間以内のド素人です。
ccxtを使用し、Binanceにて自動売買プログラムを実行しようとしました。
(API key と Secret keyはマスクしてます)
import time import pandas as pd import ccxt import talib as ta import requests exchange = ccxt.binance() exchange.apiKey = 'XXXXX' exchange.secret = 'XXXXX' ### カスタマイズ項目 ### crypto = 'SOL' # 取引する仮想通貨 currency = 'BUSD' # BUSD建て interval = 60*1 # チャート足(60*5なら 5分足) duration = 20 # 移動平均のサイズ trading_amount = 500 # 一度の売買で使う金額(単位=currency) dif = 0.8 # 購入時からの差額 ### カスタマイズ項目ここまで ### symbol = crypto + '/' + currency position = 0 # 自動売買中に持っているポジション last_rate = 0 # 最後に約定したレート(単位=currency) df = pd.DataFrame() while True: time.sleep(interval) ticker_info = exchange.fetch_ticker(symbol) df = pd.concat([df, pd.DataFrame({'price': [ticker_info['last']]})], ignore_index=True) if len(df) < duration: continue df['upper'], df['middle'], df['lower'] = ta.BBANDS(df['price'], timeperiod=duration, nbdevup=2, nbdevdn=2, matype=0) if position: #売却 #購入時からの差額が、difで設定した金額より大きくなった場合 if ticker_info['last'] > last_rate + dif or ticker_info['last'] < last_rate - dif: exchange.create_order(symbol=symbol, type='market', side='sell', amount=position) position = 0 else: #購入 ticker_balance = exchange.fetch_balance() if float(ticker_balance[currency]['free']) > trading_amount and df['price'].iloc[-1] < df['lower'].iloc[-1]: amount = round(trading_amount / float(ticker_info['last']), 5) exchange.create_order(symbol=symbol, type='market', side='buy', amount=amount) position = amount last_rate = ticker_info['last'] df = df.iloc[1:, :]
上記のコードで購入はできたのですが、売却時に下記エラーが出てしまいます。
解決方法を教えて頂けないでしょうか。(こちらも一部マスクしてます)
Traceback (most recent call last): File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\base\exchange.py", line 655, in fetch response.raise_for_status() File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py", line 960, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.binance.com/api/v3/order During handling of the above exception, another exception occurred: Traceback (most recent call last): File "G:\XXXXX\teratail_test.py", line 40, in <module> exchange.create_order(symbol=symbol, type='market', side='sell', amount=position) File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\binance.py", line 2788, in create_order response = getattr(self, method)(self.extend(request, params)) File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\base\exchange.py", line 495, in inner return entry(_self, **inner_kwargs) File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\binance.py", line 5070, in request response = self.fetch2(path, api, method, params, headers, body, config, context) File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\base\exchange.py", line 542, in fetch2 return self.fetch(request['url'], request['method'], request['headers'], request['body']) File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\base\exchange.py", line 671, in fetch skip_further_error_handling = self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body) File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\binance.py", line 5026, in handle_errors self.throw_exactly_matched_exception(self.exceptions['exact'], message, self.id + ' ' + message) File "C:\Users\XXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ccxt\base\exchange.py", line 560, in throw_exactly_matched_exception raise exact[string](message) ccxt.base.errors.InsufficientFunds: binance Account has insufficient balance for requested action.
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。