我正在尝试构建一个脚本来模拟出售股票的策略。考虑到随着时间的推移对股票价格的假设,其目的是以更高的价格出售越来越多的股票。定期(每周)创建多个卖单,并保持打开状态,直到它们以高于限价的价格成交(限价是我愿意卖出股票的价格)。每个卖单都有不同的限价,因此在更高的价格下,更多的订单被执行,更多的股票被卖出。
我的方法是使用列表来反映每周的价格假设,并使用列表列表来反映每周下达的订单。我的意图是每周迭代订单列表并“填充”满足以下条件的订单:
- 他们的限价低于该周的价格假设
- 他们还没有被卖掉
这是脚本的简化版本
orders = [] # initalize an empty list of orders. Append orders to this list each week.
number_of_weeks = 4 # number of weeks to simulate
weekly_order_template = [[100, 5, "", ""],[150, 10, "", ""]] # these are the orders that will be added each week (2 in this example) and each order includes the number of shares, the limit price, the sale price (if sold), the sale week (if sold).
# create a list to store the weekly price assumptions
weekly_price = [] # init a list to store weekly prices
price = 4.90
price_increment = .05
for weeks in range(0,number_of_weeks):
price = price + price_increment
weekly_price.append(price)
# each week, add this week's orders to the orders list and then compare all orders in the list to see which should be sold. Update the orders list elements to reflect sales.
for week in range(0,number_of_weeks):
print "****This is WEEK ", week, "****"
this_weeks_price = weekly_price[week]
print "This week's price: ", this_weeks_price
for order in weekly_order_template: # add this week's orders to the orders list
orders.append(order)
for order in orders: # iterate over the orders list and update orders that are sold
if (order[2] == "") and (order[1] < this_weeks_price):
order[2] = this_weeks_price
order[3] = week
print "All orders to date: ", orders
这个脚本不工作。在这些订单应该存在之前,它是“销售”订单。例如,这是第四周的输出:
****This is WEEK 3 ****
This week's price: 5.1
All orders to date: [[100, 5, 5.05, 2], [150, 10, '', ''], [100, 5, 5.05, 2], [150, 10,'', ''], [100, 5, 5.05, 2], [150, 10, '', ''], [100, 5, 5.05, 2], [150, 10, '', '']]
为什么第七个元素(第 3 周的第一个订单)以前一周的价格而不是当时的 5.10 美元的价格“出售”?(注意 - “第 3 周”指的是第四周,因为我使用第 0 周作为第一周)