This article is participating in Python Theme Month. See the link for details

In recent years, more and more people buy stock funds. Although the fund does not have to keep an eye on the plate, but also from time to time to see how today’s earnings. After all, the yield is positively correlated with the time of the foot bath tonight.

background

We can see the real-time fund trend on various financial websites or apps, but all such websites and apps are not simple enough and generally too eye-catching. When you just take out the mobile phone delectation calculation today earnings, just be seen by the boss, and then the next day because left foot first into the company year-end bonus to you deduct half. Above this kind of circumstance gains not to lose, after all each big boss’s year-end bonus is very considerable.

To solve the channel

It would be great if we could display the trend and yield of the fund in real time on the terminal. After all, we only open the terminal when we are working seriously. Display as little information as possible, preferably only the amount of current returns for all funds. Because ALL I care about is how much money I make, and I don’t really care about the ups and downs of that line.

The implementation process

  • Configure a JSON file with the following format (source code at the end)
{
"000496":10000."004241":60000
}
Copy the code

Key :000496 is the fund code, open your fund generally under the name of the fund value:10000 is the amount to buy the fund

  • Reading json files
# read fund id and corresponding fund
def read_fund_code(self) :
    f = open("fundCode.txt"."r")
    fund_map = json.load(f)
    self.fund_code_map = fund_map
    f.close()
Copy the code
  • Grequests requests all fund movement information concurrently and formats the data into tables using PrettyTable. You can customize the tables to output the information you want

def request_fund_data(self) :
    x = PrettyTable(["code"."name"."Net estimated value"."Profit"])
    x.align["code"] = "|"
    x.padding_width = 0 
    req_list = []
    for code in self.fund_code_map.keys():
            request_url = self.base_url + code + ".js"
            req_list.append(grequests.get(request_url))
    res_list = grequests.map(req_list)
    for res in res_list:
            list = re.findall(r'[(](.*?) [] ', res.text)
            code = json.loads(list[0[])'fundcode']
            name = json.loads(list[0[])'name']
            gszzl = float(json.loads(list[0[])'gszzl'])
            yl = gszzl*float(self.fund_code_map[code])/100.00
            x.add_row([code,name,gszzl,float('%.2f' % yl)])	
    os.system('clear')
    print(x)
Copy the code
  • Timed pull uses apscheduler
def start(self) :
    self.read_fund_code()
    scheduler = BlockingScheduler()
    scheduler.add_job(self.request_fund_data, 'interval', seconds=3)
    scheduler.start()
Copy the code
  • Terminal output example

Program source code

  • Github: project source code
  • Clone can use
  • I wish you all wealth management income beyond salary (hint thumbs up)