-1

I am very unexperienced with Python but I managed to build a full code for what I need. However, what I believe to be my now last step is to solve this issue i am having with the following message: TypeError: 'NoneType' object is not subscriptable the issue here is I am having a key on the JSON file which ceases to have the particular data I am looking for in the other coins. How can i solve this? or drop the corresponding array?

Can you give me any suggestion? :(

def sell_coins():
 
  last_price = get_price()
  for coin in coins_bought:
    if coins_bought[coin]["status"]=="FILLED":
 
      TP = float(coins_bought[coin]["bought_at"]) + (float(coins_bought[coin]["bought_at"]) * TAKE_PROFIT) / 100
      SL = float(coins_bought[coin]["bought_at"]) - (float(coins_bought[coin]["bought_at"]) * STOP_LOSS) / 100

      if float(last_price[coin]["lastPrice"]) > TP or float(last_price[coin]["lastPrice"]) < SL:
        print("TP or SL reached selling", {coins_bought[coin]["volume"]}, {coin})
        sell_limit = client.futures_create_order(
        symbol=coin,
        side="SELL",
        type="MARKET",
        quantity=coins_bought[coin]["volume"])

      else: 
        coins_bought[coin] = None
        with open(coins_bought_file_path, "w") as file:
          json.dump(coins_bought, file, indent=4)
        print("No additional coin to buy or to sell")
            

JSON File: (This is the problem is I am setting this to null/none { "SFPUSDT": null }

HaloBoySCP
  • 17
  • 2
  • What is `thershold’’’`? It looks like the end of a documentation comment, where's the beginning? – Barmar Jun 02 '22 at 00:39
  • You have two definitions of the same function. – Barmar Jun 02 '22 at 00:41
  • 1
    Welcome to Stack Overflow. Please read [ask] and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and [mre], and keep in mind that this is not a discussion forum. Try to ask your question in clear, complete, distinct English sentences, and avoid conversational language. – Karl Knechtel Jun 02 '22 at 00:41
  • 1
    you are setting `coins_bought[coin] = None` in some places, however you're also iterating over `coins_bought` and accessing `coins_bought[coin]["status"]` for example - therein lies the issue I suppose. – rv.kvetch Jun 02 '22 at 00:41
  • Please post the full traceback of the error. – Barmar Jun 02 '22 at 00:41
  • @Barmar I think those are just errors in pasting the code into the question. – Karl Knechtel Jun 02 '22 at 00:41
  • 1
    Perhaps you should remove the `None` entries from the dictionary before saving to the JSON file. – Barmar Jun 02 '22 at 00:42
  • i guess adding something like `if coins_bought[coin] is None: continue` at the top of both for loops would be a quick fix for now – rv.kvetch Jun 02 '22 at 00:43

1 Answers1

0

I think you got the error because you're trying to access a key in coins_bought while the key is None. You can reproduce the error by running the code like

coins_bought = {}
coins_bought["SFPUSDT"] = None
if coins_bought[coin]["status"]=="FILLED":
    print("FILLED")

I think one way to workaround this is to add a check if the key is None before checking the condition coins_bought[coin]["status"]=="FILLED"

if (coins_bought[coin] is not None) and (coins_bought[coin]["status"]=="FILLED"):
    ....
tax evader
  • 2,082
  • 1
  • 7
  • 9