Python Forum
loop ( 2 inputs for each loop)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop ( 2 inputs for each loop)
#1
Hi guys, anyways I can use a loop to do this?
thanks.

ib.placeOrder(es, es_order)
ib.placeOrder(nq, nq_order)
ib.placeOrder(rty, rty_order)
ib.placeOrder(zb, zb_order)
ib.placeOrder(spy, spy_order)
ib.placeOrder(qqq, qqq_order)
ib.placeOrder(iwm, iwm_order)
ib.placeOrder(tlt, tlt_order)
Larz60+ write Aug-05-2022, 10:28 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 ime. Please use bbcode tags on future posts.
Reply
#2
How about:

def place_order(sym, order):
    ib.placeOrder(sym, order)

order_list = [(es, es_order),(nq, nq_order),(rty, rty_order)]

for order in order_list:
    place_order(order[0],order[1])
Gribouillis likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
You cannot loop over a bunch of variables. To loop, or iterate, you need a variable that is iterable. rob101 takes your variables and creates iterable variables (lists), but this is a poor solution. A better solution is for the application to not have es or spy or tlt, but to have a list of stocks. The code below defines a Stock class that is used to keep information associated with a stock. It creates a portfolio which is a list of Stock items.
import dataclasses

class Stock(dataclasses.dataclass):
    symbol: str
    shares: int
    order: int

portfolio = [
    Stock("ES", 0, 0),
    Stock("SPY", 0, 1),
    Stock("NQ", 1, 3)
]

for stock in portfolio:
    if stock.order > 0:
        ib.placeOrder(stock.symbol, stock.order)
        stock.order = 0
Reply
#4
My, quote unquote, "poor solution", was intended to answer the OPs question, without redefining it.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
Looking at it now, that is a bit harsh. Sorry.

Your solution is a good solution for an underlaying problem. Fix the underlaying problem and your code is not needed. Would you write code like that for your use? Of course not. You would organize your data to make it easy to work with.

In another thread brianhclo mentions being new to programming. In that post as well as this, the code exhibits a problem common to new programmers; Using tons of variables instead of lists, dictionaries or classes when you have related information.
Reply
#6
Tim Peters Wrote:Simple is better than complex.
Remembering the time when I was a Python newbie, I tend to agree with @rod101 here. When you learn the language, you need solutions to recurring small problems like «how do I replace these lines with a loop?». The question is not about architecture or design, it is rather about the basic usage of the semantic structure of the language. Also it is more funny to learn by small steps.
rob101 likes this post
Reply
#7
(Aug-05-2022, 03:02 PM)deanhystad Wrote: Looking at it now, that is a bit harsh. Sorry.

Your solution is a good solution for an underlaying problem. Fix the underlaying problem and your code is not needed. Would you write code like that for your use? Of course not. You would organize your data to make it easy to work with.

In another thread brianhclo mentions being new to programming. In that post as well as this, the code exhibits a problem common to new programmers; Using tons of variables instead of lists, dictionaries or classes when you have related information.

Accepted, thank you.

Yes, I get that he seems to be new at this (as we all once were) which is why I tried to use code that he would be able (most likely) to understand; in this case Tuples within a List, which is a concept that is a little beyond 'beginner' (IMHO), but not so far advanced as the concept of a Class.

Your skills are advanced, I can see that and I'm sure you're an asset to this Forum.

Peace.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning data on button click by buttons created by a loop bradells 3 347 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  in c# create a loop counting from 0 to 5, consecutively Frankd 19 2,264 Apr-01-2025, 12:46 PM
Last Post: Frankd
  really new to python want to know how to do a loop pentopdmj 6 1,622 Mar-09-2025, 12:59 PM
Last Post: snippsat
  knowing for loop position in a list medic5678 4 701 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Run this once instead of a loop, do I need the 'calibration' steps? duckredbeard 2 732 Jan-28-2025, 04:55 PM
Last Post: duckredbeard
  Error loop with Chatgpt sportak12 0 517 Jan-14-2025, 12:04 PM
Last Post: sportak12
  How to convert while loop to for loop in my code? tatahuft 4 834 Dec-21-2024, 07:59 AM
Last Post: snippsat
  How to get keep information in a loop ginod 4 908 Dec-11-2024, 01:32 AM
Last Post: deanhystad
  Regarding The For Loop Hudjefa 5 1,490 Nov-15-2024, 01:02 PM
Last Post: deanhystad
  For Loop beginner agoldav 2 736 Nov-05-2024, 12:51 AM
Last Post: agoldav

Forum Jump:

User Panel Messages

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