【问题标题】:condition parameter adjustment条件参数调整
【发布时间】:2016-05-31 10:25:02
【问题描述】:

我尝试在python中使用'IF'来实现可以根据一些股票交易自动调整'IF'中的参数值的算法。

if self.sellcount==0 and int(time.time())-self.programstarttime>600:
     if cur_sum/total_sum>0.15:
           Other Code
else:
      if cur_sum/total_sum>0.35:
           Other Code

我尝试实现如果我的算法在 10 分钟内没有卖出任何股票,算法可以自动将条件从 0.35 更改为 0.15。但是,上面的代码在卖出一次股票后会从 0.15 变为 0.35。我希望代码在卖出一次股票后保持 0.15。

【问题讨论】:

    标签: python algorithm trading


    【解决方案1】:

    我想从免责声明开始,要小心,股票交易不是那么容易,使用简单的算法可能会损失很多钱(就像使用复杂的算法一样)

    不过,这也是一个很好的例子,可以帮助您了解如何在 Python 中随时间推移运行程序并了解条件逻辑。

    您需要了解一些基本结构。第一个概念是,要在程序中不断跟踪时间,您可能希望将代码置于无限循环中。这将使您的编程一直按照您的意愿进行,直到您完成为止。可以这样完成:

    while True:
    

    现在您已完成此设置,我们只需要跟踪时间即可。这可以通过设置一个变量并根据迭代之间的等待时间递增它来轻松完成。但是,我们仍然需要跟踪时间。 Python 在 time 模块中实现了一个不错的睡眠功能。此函数会导致您的程序暂停您希望的几秒钟,然后继续执行其余代码。

    from time import sleep
    last_sold_stock_time = 0
    wait_time = 1
    
    while True:
    
         # <Condition Code goes here>
    
    
        # This is in seconds
        sleep(wait_time)
        # Keep track of how much time has passed.
        last_sold_stock_time += wait_time
    

    现在,您只需要根据时间更改您的条件值。完整的代码最终可能看起来像这样:

    from time import sleep
    
    # The number of seconds since last bought a stock, assumes start is 0
    last_sold_stock_time = 0
    
    # This is in seconds
    wait_time = 1
    
    # ten minutes in seconds
    ten_minutes = 600
    while True:
        # Figure out these values however you do
        cur_sum = 0
        total_sum = 1
    
        if last_sold_stock_time <= ten_minutes:
            condition_value = 0.35
        else:
            condition_value = 0.15
    
        if cur_sum/total_sum > condition_value:
            # Do something
            pass
    
        sleep(wait_time)
        last_sold_stock_time += wait_time
    

    【讨论】:

    • 感谢您的回复。这是我大学课程作业的一部分,所以我不会使用这个算法进行真实交易。
    猜你喜欢
    • 2016-11-20
    • 2016-01-15
    • 2014-04-03
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多