Reputation: 13
I'm trying to build a script to simulate a strategy for selling stock. The intention is to sell an increasing number of shares at higher prices given assumptions about the stock price over time. Multiple sell orders are created at regular intervals (weekly) and remain open until they are filled at a price above their limit price (a limit price is the price above which I am willing to sell stock). Each sell order has a different limit price, so that at higher prices more orders are filled and more stock is sold.
My approach has been to use a list to reflect the weekly price assumptions and a list of lists to reflect the orders that are placed each week. My intention is to iterate over the orders list each week and "fill" the orders that meet the following conditions:
Here's a simplified version of the script
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 script isn't working. It's "selling" orders before these orders should exist. For example, this is the output for the fourth week:
****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, '', '']]
Why is the seventh element (the first order of Week 3) being "sold" at the prior week's price rather than at the then current price of $5.10? (Note - "WEEK 3" refers to the fourth week since I'm using week 0 as the first week)
Upvotes: 1
Views: 556
Reputation: 10477
Change the line
orders.append(order)
to
orders.append(list(order))
The issue is that you need to create a copy of the order from weekly_order_template
(which is what list(order)
does) rather than simply referring to the order template, so that when you change the orders later (in the for order in orders:
loop) you are changing individual copies of the order template, not the order template itself.
Upvotes: 0
Reputation: 20654
Python uses "reference semantics", in other words, it never copies something unless you tell it to do so explicitly.
The problem is with this line:
orders.append(order)
It appends object referred to by order
to the list, and then the next week it appends the very same object again. What you should be doing is appending a copy of it:
orders.append(list(order))
Upvotes: 1