Python Forum
loop ( 2 inputs for each loop) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: loop ( 2 inputs for each loop) (/thread-37898.html)



loop ( 2 inputs for each loop) - brianhclo - Aug-05-2022

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)



RE: loop ( 2 inputs for each loop) - rob101 - Aug-05-2022

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])



RE: loop ( 2 inputs for each loop) - deanhystad - Aug-05-2022

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



RE: loop ( 2 inputs for each loop) - rob101 - Aug-05-2022

My, quote unquote, "poor solution", was intended to answer the OPs question, without redefining it.


RE: loop ( 2 inputs for each loop) - deanhystad - Aug-05-2022

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.


RE: loop ( 2 inputs for each loop) - Gribouillis - Aug-05-2022

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.


RE: loop ( 2 inputs for each loop) - rob101 - Aug-05-2022

(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.