LoginSignup
harukumo7263
@harukumo7263

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

仮想通貨自動売買bot エラー解決のアドバイスお願いします

解決したいこと

pythonで仮想通貨の自動売買運用を目指しておりますが、
売買の実行まではできるのですが、その後、エラーが発生。

エラー解決すべく格闘しておりますが、なかなか解決の糸口が見出せず
行き詰っております。

何卒、解決のアドバイスを宜しくお願い致します。

(使用python) ver3.10.5
(使用エディタ) vscode

発生している問題・エラー

(エラー)TypeError: unsupported operand type(s) for +: 'int' and 'str'

過去10足の最安値3203223円を、直近の価格が3201409円でブレイクしました
現在のアカウント残高は179217.0円です
新規注文に利用可能な証拠金の額は179217.0円です
現在の5期間の平均ボラティリティは13947円です
現在のアカウント残高は179217.0円です
許容リスクから購入できる枚数は最大0.03BTCまでです
2回に分けて0.01BTCずつ注文します
証拠金から購入できる枚数は最大0.16BTCまでです
3201409円あたりに0.01BTCの売りの成行注文を出します
--------------------
{'info': {'child_order_acceptance_id': 'JRF20220815-200251-015824'}, 'id': 'JRF20220815-200251-015824'}
--------------------
Traceback (most recent call last):
  File "C:\Users\beerm\AppData\Local\Programs\Python\Python310\main1-3.py", line 785, in <module>
    flag = entry_signal( data,last_data,flag )
  File "C:\Users\beerm\AppData\Local\Programs\Python\Python310\main1-3.py", line 141, in entry_signal
    price = bitflyer_market( "SELL", lot )
  File "C:\Users\beerm\AppData\Local\Programs\Python\Python310\main1-3.py", line 670, in bitflyer_market
    average_price = bitflyer_check_market_order( order_id, lot )
  File "C:\Users\beerm\AppData\Local\Programs\Python\Python310\main1-3.py", line 693, in bitflyer_check_market_order
    if round(sum(size),2) != lot:
TypeError: unsupported operand type(s) for +: 'int' and 'str'

発生している問題・エラー

# 成行注文の執行状況を確認する関数
def bitflyer_check_market_order(id,lot):
	while True:
		try:
			size = []
			price = []
			
			executions = bitflyer.private_get_getexecutions( params = { "product_code" : "FX_BTC_JPY" })
			for exec in executions:
				if exec["child_order_acceptance_id"] == id:
					size.append( exec["size"] )
					price.append( exec["price"] )
			
			# 全部約定するまで待つ
			if round(sum(size) ,2) != lot:
				time.sleep(20)
				print_log("注文がすべて約定するのを待っています")
			else:
				# 平均価格を計算する
				average_price = round(sum(( price[i]) * (size[i]) for i in (range(len(price)) )) / sum(size))
				print_log("すべての成行注文が執行されました")
				print_log("執行価格は平均 {}円です".format(average_price))
				return average_price
				
		except ccxt.BaseError as e:
			print_log("BitflyerのAPIで問題発生 : " + str(e))
			print_log("20秒待機してやり直します"){\rtf1}

###確認したこと

(エラー693行目)if round(sum(size) ,2) != lot:
size → list 、 lot → Any

0

7Answer

sizeの中に文字列が含まれていないでしょうか?
リストに文字列が含まれている状態でsum関数を使用すると同様のエラーが起きます。

x = [1,2,3,4]
print(sum(x))
#10

x = [1,2,3,'4']
print(sum(x))
#TypeError: unsupported operand type(s) for +: 'int' and 'str'
1

実際に確認してみました。
sizeは文字列のようですので、数値型に変換したうえでリストに追加していってあげれば問題は解決するかもしれません。

image.png

1

float(size)
sizeがリストだと思うので、ここでエラーになっていそうですね。

現状のソースを見た感じ、リストから取り出すときにfloatに変換しようとしているかと思いますが、リストに追加する際に数値に変換してあげた方が適切かと思われます。

if exec["child_order_acceptance_id"] == id:
-    size.append( exec["size"] )
-    price.append( exec["price"] )
+    size.append( float(exec["size"]) )
+    price.append( float(exec["price"]) )
1

Comments

  1. @harukumo7263

    Questioner
    何度もご回答ありがとうございます。

    早速、教えて頂いたコードに書き換えて、実行してみたところ、
    書き換えたところでエラーが出ました(汗)

    括弧の種類が違う?とのエラーのように出ており、括弧の使い方に
    問題あるように感じられず、???状態です。

    []を()に変えても、的を得てない状態です。

    度々申し訳ございませんが、ご教授お願い致します。



    (変更後)
    # ポジション情報を取得する関数
    def bitflyer_check_positions():
    failed = 0
    while True:
    try:
    size = []
    price = []
    positions = bitflyer.private_get_getpositions( params = { "product_code" : "FX_BTC_JPY" })
    if not positions:
    #print_log("現在ポジションは存在しません")
    return 0,0,None
    for pos in positions:
    size.append( float(exec["size"]) )
    price.append( float(exec["price"]) )
    side = pos["side"]

    # 平均建値を計算する
    average_price = round(sum( price[i] * size[i] for i in range(len(price)) ) / sum(size))
    sum_size = round(sum(size),2)

    (エラー)
    File "c:\Users\beerm\AppData\Local\Programs\Python\Python310\819.py", line 735, in bitflyer_check_positions
    size.append( float(exec["size"]) )
    TypeError: 'builtin_function_or_method' object is not subscriptable
  2. for exec in executions:
     ↓
    for pos in positions:

    ループ用の変数が「exec」から「pos」になっているようですので、

    size.append( float(pos["size"]) )
    ではないでしょうか?
  3. @harukumo7263

    Questioner
    ご回答ありがとうございます!

    確かにご指摘いただいた箇所をコピペでして凡ミスしてました(汗)。

    下記で打ち直したところ、

    size.append( float( pos["size"] ))
    price.append( float(pos["price"] ))


    時間: 2022/08/19 14:00 高値: 3110665 安値: 3103137 終値: 3104527
    --5秒待機--
    ---実行開始---
    時間: 2022/08/19 14:30 高値: 3110354 安値: 3103014 終値: 3109066
    把握していないポジションが見つかりました
    反映の遅延でないことを確認するため様子を見ています



    エラーが無くなり、前進出来ました。

    色々ご丁寧に教えて頂き、ありがとうございました。
  4. 解決したようでよかったです!

ご回答ありがとうございます!
なんとなく、sizeの中に文字が入っていそうな雰囲気は感じてましたが、何処で紛れ込んできているか、迷走中です(汗)

0

なんとなく、sizeの中に文字が入っていそうな雰囲気は感じてましたが、何処で紛れ込んできているか、迷走中です(汗)

sum関数に入る直前のsizeの中身やexecutionsの中身はどのようになっていますでしょうか?
そのあたりの中身を確認すれば、原因つかめそうな気がします。

0

アドバイスありがとうございます!
テストコードを参考に、私自身も確認したところ
sizeがstr型でした。同様にpriceもstr型が判明。
オーダー履歴を見た際、見た目で数字=intやfrortと思い込んでいました。
少し前進出来そうです。有難うございましたm(_ _)m

0

度々の質問で申し訳ありません。

先のエラーについては、floatを入れて改善したのですが、次に違うエラーが
発生しました。

先と同様に「TypeError: unsupported operand type(s) for +: 'int' and 'str'」が出たので
priceとsizeを読みだしたところ str型が分かりfloatで数値化を図って見ました。(変更後)

(変更前)

平均建値を計算する

(740行目)
average_price = round(sum( price[i] * size[i] for i in range(len(price)) ) / sum(size))

(変更後)

平均建値を計算する

(740行目)
average_price = round(sum(float(price[i]) *float(size[i]) for i in range(len(price)) / sum(float(size))))

File "c:\Users\beerm\AppData\Local\Programs\Python\Python310\8.py", line 740, in bitflyer_check_positions
average_price = round(sum(float(price[i]) *float(size[i]) for i in range(len(price)) / sum(float(size))))
TypeError: float() argument must be a string or a real number, not 'list'

エラーを読むと floatに入れるのは listで無く 文字か数値と出ているのかと
思いますが、floatの付け方に誤りがあるのでしょうか?

ご教授お願いします。

0

Your answer might help someone💌