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
  Loop through all files in a directory? Winfried 10 426 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  for loop not executing Abendrot47 2 228 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 625 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 823 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 498 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 377 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 420 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 450 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 632 Dec-28-2023, 05:14 PM
Last Post: trix
  Need an alternative to brute force optimization loop jmbonni 5 1,191 Dec-07-2023, 12:28 PM
Last Post: RockBlok

Forum Jump:

User Panel Messages

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