Python Forum
How to pick random entries of stock prices?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pick random entries of stock prices?
#1
Dear Python Users,

Please, can you help me with the following issue. I downloaded a stock price from google. Sorted the prices. What I need to do now is to pick 8 prices starting at the beginning of the consecutive months (the closest date, since not all times stocks trade on the 1st of the month). Say, if the randomly assigned starting month is January then the 2nd price is picked from February, 3rd from March, etc. This should be random. Once, the prices are picked they should be allocated to excel file. I attach the code I have below. Please, advise me this issue

import pandas as pd
import datetime
#import pandas.io.data as web 
import pandas_datareader.data as web
from matplotlib import style
style.use('ggplot')

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime.today().strftime("%m/%d/%Y")

stock = web.DataReader("AMZN", "google", start, end)
stock1 = stock['Close']#pick the column - close price
          
stock1 = stock1.resample('BMS', how='first') #sort the data following the calendar dates
Reply
#2
Something like that?
victor ~ $ ptpython
>>> import random, datetime

>>> months = {datetime.date(2017, month, 1).strftime('%B'): month for month in range(1, 13)}

>>> carousel = iter(months)

>>> num = months[random.choice(list(months.keys()))]

>>> for i in range(num - 1):
...     next(carousel)

>>> while True:
...     try:
...         print(months[next(carousel)])
...     except StopIteration:
...         break
10
11
12
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pick a line in a plot with matplotlib Romain 3 5,627 Feb-28-2023, 11:56 AM
Last Post: get2sid
  Learning indexing with python, pick dates dervast 1 1,749 Jul-11-2019, 07:29 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020