Write a stock analysis app from No to use (i)

Source: Internet
Author: User

Let's dig a hole for myself.


I want to write something?

One: Can browse the current relevant information, as well as the market index real-time update.

Second: Add the optional stock, you can view the stock chart, related information and basic data.

Third: Through the server-side definition of relevant indicators and common strategies, mobile phones can directly add the defined technical indicators and policies for the combination, and then on the server side to get results, mobile phone side view.


Project address:Https://github.com/youerning/pstock


The technology used:

Nodejs:socket.io

Golang

Javascript:angularjs,chartjs

Css.

Python:tushare,pyalgotrade,tornado,flask

Package: Ionic

and preview the demo for two days.

650) this.width=650; "Src=" Https://s5.51cto.com/wyfs02/M01/9A/60/wKioL1lU21bQGX12AADR-YWIVUc817.png-wh_ 500x0-wm_3-wmp_4-s_851602259.png "style=" height:400px;width:230px;float:left; "title=" Preview1.png "alt=" Wkiol1lu21bqgx12aadr-ywivuc817.png-wh_50 "/>650) this.width=650;" Src= "https://s3.51cto.com/wyfs02/M02/9A/60/ Wkiol1lu21etcf7waaazoczkoa4391.png-wh_500x0-wm_3-wmp_4-s_923876792.png "style=" float:none;height:400px;width : 230px; "title=" Preview2.png "alt=" wkiol1lu21etcf7waaazoczkoa4391.png-wh_50 "/>650" this.width=650; "src=" https ://s4.51cto.com/wyfs02/m02/9a/60/wkiom1lu21ngdn7qaabayxoembs877.png-wh_500x0-wm_3-wmp_4-s_4128043257.png "style = "float:none;height:400px;width:230px;" title= "Preview3.png" alt= "Wkiom1lu21ngdn7qaabayxoembs877.png-wh_50"/>

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/9A/60/wKioL1lU21yDE5RHAABcM4LbGYM181.png-wh_500x0-wm_ 3-wmp_4-s_1359733200.png "style=" float:none;height:400px;width:230px; "title=" Preview4.png "alt=" Wkiol1lu21yde5rhaabcm4lbgym181.png-wh_50 "/>


Article directory:

    • One: Layout

    • Part Two: Detail description

    • Three: Get the data

    • Four: Drawing

    • Five: Writing strategy//Waiting for pits

    • Six: Optimization details//Waiting for pits

    • Seven: Landscaping, finishing//Waiting for pits

    • Note: In order to make the article not too lengthy, the code details may be reduced, the details refer to the project source code:


A

1. Environment Construction Reference: Write an OPS app from scratch (i)


2. Create a project

Ionic start Pstock Blank

3. Write the index.html.

<ion-tabs class= "Tabs-icon-top" ><!--home--><ion-tab title= "Home" icon= "Ion-home" href= "#/home" >< Ion-nav-view name= "Tab-home" ></ion-nav-view></ion-tab><!--optional--><ion-tab title= "optional" icon= " Ion-person-add "href=" #/user "><ion-nav-view name=" Tab-user "></ion-nav-view></ion-tab><!- -Backtesting--><ion-tab title= "backtesting" icon= "Ion-clock" href= "#/backtest" ><ion-nav-view name= "Tab-backtest" >< /ion-nav-view></ion-tab></ion-tabs>


4. Create the appropriate template file with the following structure

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9A/60/wKioL1lU3qDzdV6fAABJzR37VWQ807.png-wh_500x0-wm_ 3-wmp_4-s_3077122795.png "style=" height:400px;width:230px; "title=" Clipboard.png "alt=" Wkiol1lu3qdzdv6faabjzr37vwq807.png-wh_50 "/>


5. Create a route

App. Config (function ($stateProvider, $urlRouterProvider, $ionicConfigProvider) {$ionicConfigProvider. tabs.position (' bottom '); $stateProvider. State ("Home", {URL: "/", views:{"Tab-home": {controller: "Homectrl", Templateurl: "Tpls /home.html "}}});

At this point, the basic structure is determined.


Two

1. Pull up, pull down.

Drag the screen up and down to refresh the data and load the data the JavaScript components in Ionic are already available, so you can use them directly.

The code is as follows:

<ion-refresherpulling-text= "Pull to refresh ..." on-refresh= "Loadnewer ()" ></ion-refresher><a class= " Item Item-thumbnail-left "ng-repeat=" N News track by n.item_id "ng-click=" OpenLink (n.article_url) ">

Then define the specified execution function in the appropriate controller loadnewer (), Loadolder ()


2. Save the data of the optional stock.

Since there is no plan to place the stock of your choice on the server side, the data should be stored locally, i.e. Localstorage

$scope. Usercode = Angular.fromjson (window.localstorage["Usercode"] | | "{}"), function persist () {window.localstorage["usercode"] = Angular.tojson ($scope. usercode)};


Three

1. Access to news data

In the domestic acquisition of data time is very sad things, why sad not to say, of course, you can climb, but that is too elegant.

Here's our headline News today (today's headline isn't an API that's not publicly available?) )

First we open the following today's headline website

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/9A/60/wKiom1lU38nxiyKMAAISoG_S-cM776.png-wh_500x0-wm_ 3-wmp_4-s_963988901.png "style=" Float:none; "title=" Image2017-06-29-18-06-001.png "alt=" wKiom1lU38nxiyKMAAISoG_ S-cm776.png-wh_50 "/>

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/9A/60/wKiom1lU38vReJKCAABd6qf5Fvo834.png-wh_500x0-wm_ 3-wmp_4-s_1624648514.png "style=" Float:none; "title=" Image2017-06-29-18-06-002.png "alt=" Wkiom1lu38vrejkcaabd6qf5fvo834.png-wh_50 "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Then the data appeared, that is, there is a lot of urine, in fact, there are many sites also like this, we can try their own.

Reference:

Https://github.com/iMeiji/Toutiao/wiki/%E4%BB%8A%E6%97%A5%E5%A4%B4%E6%9D%A1Api%E5%88%86%E6%9E%90


2. Get Stock Data

Here with Tushare, of course, you can also use other APIs.

Reference: Http://tushare.org/trading.html#id2


3. Policy Data (Waiting for pits ... )

Run Pyalgotrade policy.

In fact, directly with Tushare data will be error, but, that is, less a adj Close, add a field will not be so rare ...


4.server Terminal code

#coding: utf8from flask import flaskfrom flask import response,  request, abortimport urlparseimport requestsimport jsonimport tushare as  Tsfrom random import randintfrom bs4 import beautifulsoupimport pandas  as pd# import sys# reload (SYS) # sys.setdefaultencoding (' Utf-8 ') app =  Flask (__name__) # sinaapi =  "http://hq.sinajs.cn/list=" detailurl =  "/HTTP/ stockpage.10jqka.com.cn/%s/company/"toutiao = " http://www.toutiao.com/api/article/recent/?source=2 &category=%s&as=a105177907376a5&cp=5797c7865ad54e1&count=5&offset=0&_=%s "def  Getuseragent (): useragent = ["mozilla/5.0  (compatible, msie 10.0, windows nt ,  digext) "," mozilla/4.0  (COMPATIBLE,&NBSP;MSIE&NBSP;7.0,&NBSP;WINDOWS&NBSP;NT&NBSP;5.1,&NBSP;360SE) " , "mozilla/4.0  (Compatible, msie 8.0, windows nt 6.0, trident/4.0) "," mozilla/5.0  (compatible, MSIE  9.0, windows nt 6.1, trident/5.0, "," opera/9.80  (Windows nt 6.1, u,  en)  presto/2.8.131 version/11.11 "," mozilla/4.0  (compatible, msie 7.0,  windows nt 5.1, tencenttraveler 4.0) "," mozilla/5.0  (windows, u, windows  nt 6.1, en-us)  AppleWebKit/534.50  (Khtml, like gecko)  version/5.1  safari/534.50 "," mozilla/5.0  (MACINTOSH,&NBSP;INTEL&NBSP;MAC&NBSP;OS&NBSP;X&NBSP;10_7_0)   applewebkit/535.11  (Khtml, like gecko)  chrome/17.0.963.56 safari/535.11 "," Mozilla/5.0   (macintosh, u, intel mac os x 10_6_8, en-us)  AppleWebKit/534.50   (Khtml, like gecko)  version/5.1 safari/534.50 "," mozilla/5.0  (Linux, U,  android 3.0, en-us,&nbsP xoom build/hri39)  AppleWebKit/534.13  (Khtml, like gecko)  version/4.0 safari /534.13 "," mozilla/5.0  (ipad, u, cpu os 4_3_3 like mac os x,  En-US)  AppleWebKit/533.17.9  (Khtml, like gecko)  Version/5.0.2 Mobile/8J2  safari/6533.18.5 "," mozilla/4.0  (compatible, msie 7.0, windows nt 5.1,  Trident/4.0, se 2.x metasr 1.0, se 2.x metasr 1.0, .net clr  2.0.50727, se 2.x metasr 1.0) "," mozilla/5.0  (iphone, u, cpu  iphone os 4_3_3 like mac os x, en-us)  AppleWebKit/533.17.9  (khtml ,  like gecko)  version/5.0.2 mobile/8j2 safari/6533.18.5 "," MQQBrowser/26 Mozilla/ 5.0  (linux, u, android 2.3.7, zh-cn, mb200 build/grj22, cyanogenmod-7)  applewebkit/533.1  (Khtml, like gecko)  version/4.0 mobile safari/533.1 "]return userAgent[ Randint (0,len (useragent)-1)] @app. Route ("/<app>/",  methods=["GET", "POST"]) Def index (APP): headers = {"User-agent":  getuseragent ()}code = request.args["code"]data = { }error =  "" if app ==  "Now":#  get current price Code = code.split (",") df =  ts.get_realtime_quotes (Code) Ret = df.to_json () elif app ==  "Stock":#  Get Stock history Data Df = ts.get_hist_data (code) Df = df.sort_index () df["Date"] =  Df.indexdf.index = range (Len (df.index)) Ret = df.to_json () elif app ==  " Detail ":#  get stock basic data #  Company name #  Region #  Company Profile #  Business Scope Ret = {}url = detailurl  % codepage = requests.get (url, headers=headers) Soup = beautifulsoup ( page.content,  "Html.parser") name = Soup.select ("Td span") [0].textbussines = soup.select ("Td span") [3].textregion =  Soup.select ("Td span") [1].textintro = soup.select ("P.tip.lh24") [ -2].text[:-3]ret["Name"] =  nameret["Bussines"] = bussinesret["Region"] = regionret["Intro"] =  introelif app ==  "BT": ret = [{"status": "OK"}]elif app ==  "News": #   Reverse proxy today's headline catelog = request.args["Catelog"]time = request.args["Now"]url =  toutiao %  (catelog, time) page = requests.get (url, headers=headers) ret =  [{"status": "OK"}]else:ret =  "" error =  "Incorrect url" try:data["Data"] =  json.loads (ret) except exception as e:data["Data"] = retdata["error"] =  error# print dataresp = response (Json.dumps (data)) If error:abort (500) resp.headers["Content-type"] =  "APPlication/json; charset=utf-8 "resp.headers[" Access-control-allow-origin "] = " * "return  respif __name__ ==  "__main__": App.run (port=80,debug=true, host= "0.0.0.0")

5.client Terminal code

$http. Get (sURL). Success (function (RESP) {$scope. Labelsline = Object.values (resp.data.date); $scope. Seriesline = ["Ma5 "," ma10 "," ma20 "," close "]; $scope. dataline = [Object.values (RESP.DATA.MA5), Object.values (RESP.DATA.MA10), Object.values (RESP.DATA.MA20), Object.values (Resp.data.close)]; $scope. optionsline = {title: {display:true,text: " Trend Chart "},elements: {point:{radius:0}},xaxis: {display:true,axislabel: ' X Axis ', rotatelabels:90}};


Four

With Echarts or CHARTJS, in fact, there is no technical content ... Main Search API.

But it seems that there is a problem with the mobile phone, may be too large or incompatible with the data, to be found ...


5,6,7 to Pits


Ask yourself a self-answer:

Q: Obviously useless golang,socket.io,tornado, for Mao in the technology used to write out.

A: I conceived it, but I haven't finished it yet.


Q: It's not a good thing to write a web.

A: Finished writing the app will naturally write the web ...


PostScript: It is worth saying things, as if not imagined in the so simple, expected to be three days to finish writing, before the next article, I should first write Pyalgotrade source interpretation.

If you feel good, and have something to gain, please I have a cup of tea

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9A/60/wKioL1lU4MXwELckAADg-gB3Tsc583.jpg-wh_500x0-wm_ 3-wmp_4-s_2152181634.jpg "style=" float:none;height:270px;width:200px; "title=" Picture _20170629183733.jpg "alt=" Wkiol1lu4mxwelckaadg-gb3tsc583.jpg-wh_50 "/>650) this.width=650;" Src= "https://s3.51cto.com/wyfs02/M02/9A/60/ Wkiom1lu4mqg8rxiaadzypnx0fu518.jpg-wh_500x0-wm_3-wmp_4-s_1288684929.jpg "style=" float:none;height:270px;width : 200px; "title=" Picture _20170629183740.jpg "alt=" Wkiom1lu4mqg8rxiaadzypnx0fu518.jpg-wh_50 "/>


This article is from the "Ear Note" blog, be sure to keep this source http://youerning.blog.51cto.com/10513771/1943259

Write a stock analysis app from No to use (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.