Python Forum
loop (create variable where name is dependent on another variable)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop (create variable where name is dependent on another variable)
#1
Hi guys, i am new to python. i am coding a buy/sell order instruction for interactive brokers api. Is there a way to simplify the code here?

es_nos  = 0
spy_nos = 0
nq_nos  = 1
qqq_nos = 2
rty_nos = 3
iwm_nos = 4
zb_nos  = 5
tlt_nos = 6

es_buy_sell  = 'buy' if es_nos > 0 else 'sell'
spy_buy_sell = 'buy' if spy_nos > 0 else 'sell'
nq_buy_sell  = 'buy' if nq_nos > 0 else 'sell'
qqq_buy_sell = 'buy' if qqq_nos > 0 else 'sell'
rty_buy_sell = 'buy' if rty_nos > 0 else 'sell'
iwm_buy_sell = 'buy' if iwm_nos > 0 else 'sell'
zb_buy_sell  = 'buy' if zb_nos > 0 else 'sell'
tlt_buy_sell = 'buy' if tlt_nos > 0 else 'sell'
Larz60+ write Aug-05-2022, 10:27 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time please use bbcode tags on future posts.
Reply
#2
The code as shown doesn't do anything, so it's hard to discuss. You've created 8 new variables, but do you get anything from them? How will they be used? Couldn't you just look at the original variable?

Probably I would keep the original information in a collection (a list or a dictionary) and then loop over the elements for whatever action was intended.

share_count = {
    "es": 0,
    "spy": 0,
    "nq": 1,
    "qqq": 2,
    "rty": 3,
    "iwm": 4,
    "zb": 5,
    "tlt": 6,
    }

for product, count in share_count.items():
    # do something with them.
    if count > 0:
        print(f"{count} shares available.  Buy more {product}!")
    else:
        print(f"No {product} around.  SELL!")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 2 129 Yesterday, 10:11 AM
Last Post: Pedroski55
  Commas issue in variable ddahlman 6 380 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 195 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 165 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 247 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  optimum chess endgame with D=3 pieces doesn't give an exact moves_to_mate variable max22 1 248 Mar-21-2024, 09:31 PM
Last Post: max22
  unbounded variable akbarza 3 485 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 627 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 430 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  working directory if using windows path-variable chitarup 2 721 Nov-28-2023, 11:36 PM
Last Post: chitarup

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020