简体   繁体   English

如何在 Python 中循环以下代码?

[英]How to loop the following code in Python?

I am new to Python but I tried to create the following trading strategy but cannot find a way to loop it for different products (trading hours in this case).我是 Python 新手,但我尝试创建以下交易策略,但找不到针对不同产品(在本例中为交易时间)循环的方法。

def strategy(area_code, product, orders, environment):
    order = None
    new_orders = [] 
    Quantity = 5
    price_delta = 0.1

    def process_flex(Plant):

        order = None
        Tur1 = Plant + "1"

        if Tur1Volume > 0:
            if Tur1Price:
                order = None
                if check_if_we_have_order_on_the_market(Plant,Tur1)==0:
                    order =  package.create_sell_order(area_code, Tur1Volume, Quantity, calculate_selling_price(Tur1Price), price_delta, product, environment.current_datetime).with_label((Plant,Tur1))
                if order:
                    new_orders.append(order)
            else:
                order = None

        return 

    process_flex("bla")
    process_flex("blabla")
    process_flex("blablabla")


    return new_orders

This code is only working for one product (1 hour) and does not loop for all 24 products.此代码仅适用于一种产品(1 小时)并且不会对所有 24 种产品循环。 I thought that it could work like this:我认为它可以像这样工作:

    for product in products:
        Plant = ['bla', 'blabla', 'blablabla']
        for i in Plant:
            order = process_flex(Plant)
        return_orders.append(order)

    return return_orders      

Unfortunately, it did not work.不幸的是,它没有奏效。 Do you have any idea on the solution?你对解决方案有什么想法吗?

Thank a lot in advance!非常感谢!

You want to swap Plant to : order = process_flex(i) because i is an element of Plant您想将 Plant 交换为: order = process_flex(i) 因为 i 是 Plant 的一个元素

    for product in products:
        Plant = ['bla', 'blabla', 'blablabla']
        for i in Plant:
            order = process_flex(i)
        return_orders.append(order)

    return return_orders     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM