Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:58:43+00:00 2026-06-09T09:58:43+00:00

I’m trying to build a script to simulate a strategy for selling stock. The

  • 0

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:

  • They have limit prices below the price assumption for that week
  • They have not already been sold

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:58:45+00:00Added an answer on June 9, 2026 at 9:58 am

    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))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.