Posts: 2
Threads: 2
Joined: Aug 2022
Aug-05-2022, 07:31 AM
(This post was last modified: Aug-05-2022, 10:28 AM by Larz60+.)
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.
Posts: 453
Threads: 16
Joined: Jun 2022
Aug-05-2022, 07:51 AM
(This post was last modified: Aug-05-2022, 07:52 AM by rob101.)
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
Posts: 6,799
Threads: 20
Joined: Feb 2020
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
Posts: 453
Threads: 16
Joined: Jun 2022
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
Posts: 6,799
Threads: 20
Joined: Feb 2020
Aug-05-2022, 03:02 PM
(This post was last modified: Aug-05-2022, 03:15 PM by deanhystad.)
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.
Posts: 4,790
Threads: 76
Joined: Jan 2018
Aug-05-2022, 03:10 PM
(This post was last modified: Aug-05-2022, 03:11 PM by Gribouillis.)
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.
Posts: 453
Threads: 16
Joined: Jun 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.
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
|