PHP Application to Estimate SZ162411 Net Value

Aug 18, 2015
As my CSR Stock is turning into cash soon, I am considering to use the USD to trade XOP while using my other CNY to trade SZ162411 together. I was watching stock quotations on Yahoo and Sina everyday, and always need to click on the calculator application to convert the price between XOP and SZ162411, soon I got bored.
Later I thought of my first PHP application 5 years ago, and decided to write my second PHP application. It was planned to put all the usual stock quotations together, and to estimate SZ162411 net value based on XOP, ^SPSIOP and USDCNY exchange rate. Today the first version is released, and I am writing this record of programming details for future reference.
Thanks abkoooo for Sina realtime US stock data format.
Using Sina realtime source for US and Chinese stocks, oil futures and forex: http://hq.sinajs.cn/list=gb_xop,sz162411,hf_CL,USDCNY
At first I found that fopen will always fail to open those urls, maybe my Yahoo Web Hosting does not support allow_url_fopen. I searched and found curl to solve this problem, copied some curl code to my url_get_contents function with name similar as file_get_contents.
To optimize web response time, I used 2 files gb_xop.txt and sz162411.txt to save last updated stock data. The following are checked:

  1. New request in the same minute using data in original files directly.
  2. Using gb_xop.txt for US stock data after US market closed.
  3. Using sz162411.txt for Chinese stock data after Chinese market closed.

Similarly, oil future data is stored in file hf_cl.txt, and USDCNY forex data in usdcny.txt.

SMA

Aug 20, 2015
To set up my own trading rules, and to avoid following the market all night, I plan to trade XOP simply on Simple Moving Average (SMA) values. This version added the premium of current trading price comparing with XOP estimation of SZ162411 net value, and the days in past 100 trading days did the estimated SMA value fitted in the trading range.
The historic data of XOP only need to update once a day, using Yahoo stock historic data: https://finance.yahoo.com/quote/XOP/history
The official fund net value only need to update once a day too. Using file f_162411.txt for official SZ162411 net value from http://hq.sinajs.cn/list=f_162411, as I do not konw when the data will be updated, I update it once an hour stupidly.
Using debug file debug.txt for temp data checking.

Mobile Detect

Aug 21, 2015
After released the link of this tool, I checked the data of Google Analytics yesterday. During the 3 days there are 584 visits from 289 different IP address. Unlike the usual Palmmicro visitors, 1/3 of the tool visitors used mobile phone. So I added the UI optimization for mobile phone in a hurry.
Using http://mobiledetect.net/ to detect if it is a mobile phone visit. Following the developer's advice, I copied the code github and put it separately in /php/class/Mobile_Detect.php.

Expansion

Aug 27, 2015
The best way to organize the source code is to write more similar QDII net value estimation software. I bought 2 HK ETF related QDII with recent stock market crash and added them. Although I only watched US market crash, I still added BOSERA S&P 500 net value tool for possible future usage.

Stock Transaction

Sep 13, 2015
After login, user can now input related stock transaction record. And we make SZ162411 and XOP arbitrage analysis based on those record.
The input and handling of stock transaction record is in file /woody/res/php/_edittransactionform.php and /woody/res/php/_submittransaction.php. Visual C++ coded Woody's Web Tool is modified to generate _submitXXX.php file automatically when insert copy of a _editXXXform.php file.

Unified Display

Jan 26, 2016
Add date display in related price time items following the advice from oldwain. The original version omitted date display because I thougth it was obvious. And it is obvious too that not every one is as familar with both USA and China time as I am.
Although it is a small separating data and display change, I can not help myself to mofidy a lot of code. The original StockReference class is now a base class for FutureReference and ForexReference. In this way, the display is unified.

参考数据
代码价格涨跌日期时间名称
SZ1624110.2850.35%2020-11-2715:00华宝油气-华宝标普油气上游股票(QDII-LOF)A

Period Three Implies Chaos

Feb 26, 2016
SZ162411 is trading more than 10% higher than its net value recently, the max premium as high as 17%, so the XOP and SZ162411 arbitrage is not possible now.
Continuing to organzie the similar code, added MysqlReference and MyStockReference class. FutureReference and ForexReference are now extended from MysqlReference, same as the new MyStockReference. We called MySQL related function in MysqlReference class, put history and calibration database operation in the same place.

US Daylight Saving Time

March 14, 2016
A bug is found as US enter daylight saving time: date_default_timezone_set('EST') is not considering daylight saving, need to use date_default_timezone_set('America/New_York') or date_default_timezone_set('EDT').

Gold ETF

March 25, 2016
As Gold future GC is not trading on Easter holiday, I get the chance to adjust the net value estimation for Chinese Gold ETF.
Different users have been suggesting to add estimation value in the fund history table of SZ162411. Except for not willing to show my possible error directly, I did not add it because the change is realtime, and I don't know when to record it, after US market close or Chinese market close?
In the QDII code, the variable for estimation value was originally in _QdiiAccount class. With my new _GoldSilverAccount class again having the same class member of FundReference class, naturally I moved the estimation value to FundReference class. And when estimaation value and net value variable are put together, the data structure leads my mind again, suddenly I realized that it is most reasonable to record the estimation value in the same time when the current day net value is recorded!
The average page view of my net value pages in a normal trading day is around 1000 now, the max day is nearly 1700. I have been optimizing my software for more page views in the future. As the SMA caculation only need to be done once in a day, it is natural to save the result instead of calculate it with every page view. I have been thinking about it but never did anything, until I finished the adjustment of 7 gold ETF, I realized that the SMA of GLD need to be calculated in 8 pages including the GC gold future page. I feel I can not stand it any more.
Based on the experience of finding Mobile-Detect code on internet, I underestimated the efforts to find a ready php class for config file read and write. An easy found one cost 5 USD, it says to support file and mysql for configuration at the same time, as I do not like too many tables in mysql and only interested in file config, I think 5 USD may be a waste of money. As last I found the free INIFile. The class only support config read and write on an existing file, still as a PHP new programmer, I spent a few hours to modify it and finally made it works with my pages.

Sina Realtime HK Stock Data

Apr 23, 2016
With the help of rdcheju, now using Sina realtime HK stock data(http://hq.sinajs.cn/list=rt_hk02828) to replace original Sina Stock Data which has 15 minutes delay.
On last Thursday the total page view of my net value pages was over 2200, this encouraged me to do more page speed optimization.

The Most Stupid Bug in Recent Years

May 15, 2016
Last week USDCNY was rising significantly again, and a new bug of SZ162411 net value estimation floated on water. On Friday, May 13, my estimation was about 1% higher than the official data. I began to check the problem yesterday, found that the last automatic calibration was done on the evening of May 12, after the official net value of May 11 data released. And the automatic calibration on May 13 was not done yet. In other words, the calibration was one day late for quite some time now, but when USDCNY had little change, the problem was ignored.
It was easy to find the bug, I used the simplest way to fix the estimation bug caused by continue Chinese and USA market close after Chinese New Year. Because only Hongkong QDII would have the chance of official QDII net value newer than ETF, I rewrote part of the code by checking if it was HK QDII, and modified an actually unrelated code, what should be $iHours = STOCK_HOUR_END + ($this->usdhkd_ref ? 0 : 24); was written as $iHours = STOCK_HOUR_END + ($this->usdhkd_ref) ? 0 : 24;
But this bug made me feel very bad, this time I can not say I am still a 6 years new PHP programmer. As a proud 25 years C programmer, this is also a very stupid bug in C language!

T+1 Estimation with Current CL Factor in

Aug 18, 2016
Many people's Excel sheet has this one, so I added it too. Here is the difference between offical estimation, fair estimation and realtime estimation of SZ162411 net value.

代码净值官方估值溢价参考估值溢价实时估值溢价
SZ1624110.2920.288-0.94%0.288-0.94%0.288-1.1%

Est Factor Offical Est Fair Est Realtime Est
T day trading ^SPSIOP XOP XOP
T+1 day CL future N N Y
USDCNY Reference Rate T day T+1 day T+1 day

How to Precisely Estimate the Net Value of SZ162411

Sep 27, 2016
Added 95% position for all LOF net value estimation.

Automatic and manual calibration history

Oct 6, 2016
SZ162411 calibration history.

Crazy T+1 Moving Average

March 27, 2018

XOP 2020-11-27数据牛熊分数: 3 历史价格记录
均线估值溢价T+1估值溢价SZ162411估值T+1估值
5日58.23-0.6%58.74-1.46%0.290.292
10日54.775.67%55.174.92%0.2730.275
20日49.4916.95%49.916%0.2480.25
布林上轨63.23-8.46%66.44-12.88%0.3130.328
布林下轨35.7561.9%33.3673.52%0.1830.172
5周49.8516.11%52.939.35%0.250.265
10周46.125.55%46.6624.04%0.2320.235
20周48.1420.24%47.920.83%0.2420.241
周布林上轨60.05-3.62%62.61-7.55%0.2980.31
周布林下轨36.2259.8%33.1974.37%0.1860.171
5月46.1825.34%44.5829.84%0.2330.225
10月48.1720.16%46.6923.96%0.2420.235
20月71.32-18.85%68.66-15.69%0.3520.339
月布林上轨127.94-54.76%131.83-56.1%0.6190.637
月布林下轨14.71293.45%5.48956.75%0.0840.04
小牛熊分界48.1320.25%48.0820.37%0.2420.242
牛熊分界54.376.45%54.226.74%0.2710.271

No comments for this page yet. All Comment

More options? Please login or register account.