July 3, 2014

esProc Helps to Analyze Stock Bullish Trend

Here, we use esProc to analyze stock bullish trend. There is a variety of specific algorithms, from which, one will be illustrated the whole process in detail. Other cases can be solved in the similar codes. Specific requirements are:

Among the stocks which have kept going up for N days, what is the proportion of those that continue to rise for another day.

The key step to solve this problem is to compute the maximal days when each stock kept rising (hereinafter referred to as Rising Days). Divide the number of stocks where the Rising Days is greater than or equal to n +1 by the number of stocks where the Rising Days is greater than or equal to n days, this is the rate we evaluate.

Stock data must include three fields: Transaction day,Closing price and Stock codes. Therefore, the data source may like this:

Stock        Date          Price
120089 2009-01-01 00:00:00 50.24
120123 2009-01-01 00:00:00 10.35
120136 2009-01-01 00:00:00 43.37
120141 2009-01-01 00:00:00 41.86
120170 2009-01-01 00:00:00 194.63
120243 2009-01-01 00:00:00 15.75
120319 2009-01-01 00:00:00 1.36
120343 2009-01-01 00:00:00 20.95
120344 2009-01-01 00:00:00 232.38
120355 2009-01-01 00:00:00 31.6
120414 2009-01-01 00:00:00 244.32
120439 2009-01-01 00:00:00 181.36
120484 2009-01-01 00:00:00 6.27
120528 2009-01-01 00:00:00 43.86
120533 2009-01-01 00:00:00 227.91
120584 2009-01-01 00:00:00 25.12
120607 2009-01-01 00:00:00 36.36
120626 2009-01-01 00:00:00 29.36
120630 2009-01-01 00:00:00 29.77
120642 2009-01-01 00:00:00 30.98


Computing the maximum of Rising Days is a natural idea: The closing price of each stock is sorted by Transaction dates, to compare each closing price against previous one in sequence. If greater, then the Rising Days adds 1; if not greater, the Rising Days is reset to 0.


We found that programming idea of esProc is just a nature idea. Therefore, the code written in esProc is more intelligible, and easy to maintain. Specific code is shown as follows:




Description:
1. In cell A1, the input value of n is 2 days. If this is an external call, you can also pass it through the parameters.

2. In cell A2,import the source data from txt file. If necessary, you can also import it from the database or hdfs.

3. In cellA3, group the data by stock IDs. After grouping, each group corresponds to one stock.

4. Create a blank Table sequence used to save the Rising Days for each stock.

5. In A5,loop through stock groups one by one, that is, make a loop for each stock once. Loop body is from B6 to B10.

6. In B6, get stock ID of the first record in this group; B7 will set up an initial value 0 for Rising Days.
7. B8 is to sort data in this group by transaction dates.

8. B9 is making a loo pone by one within this group to determine if the closing price is greater than previous one, B7 will be added 1; otherwise B7 is reset to 0.

9. B10 is to add computed stock ID and Rising Days to A4. The loop is ended.

10. In A11, divide the number of stocks where the Rising Days is greater than or equal to n +1in A4 by the number of stocks where the Rising Days is greater than or equal to n days, this is the rate we evaluate.

As we can see from the above codes, those in line with the natural ideas can diminish the difference between thinking characteristics of the human brain and codes, thus to reduce the difficulties of programming and maintaining codes, improve efficiency and reduce the costs.

It should be noted here that by the use of IDE (Integrated Development Environment) that esProc enables, you can easily debug the codes to further improve development efficiency. For example: You can execute each step through cell B8, the results can be observed, as shown below:



After the programmersare familiar with esProc ideas, they will be able to write more concise codes, as shown below: