Rule-Based Expert System Dressler 1272 Pine Circle Bellefonte, PA 16823 Abstract systems – they do not need to learn how to recognize In this paper, I propose a design for a rule-based expert patterns. This means that even with a very short stock system that attempts to maximize profits while playing a history, we can begin our investments because the ability to simulated stock market which, unlike in the real market, recognize patterns is already built into the system as part of actually contains embedded patterns within the stock price its expertise. data. This expert system will use linear regression analysis Given the problem on hand and the advantages of the in conjunction with the Stochastic Oscillator to predict rule-based expert system, this paper will explore a variety future stock prices. Using these predictions, the expert of possible solutions, assess their pertinence, narrow down system will predict which stocks are most likely to yield the an approach, and then finally lay out the details for a system highest profit and invest accordingly. that will solve the problem. 1. Introduction 2. Prior Work Number crunching is the digital computer's finest (and Before one can understand the design for the system to perhaps its only) trait. Not long after its invention, the solve the stock market problem, one must first understand stock market quickly became a target of this useful ability. some of the other systems which preceded it. It is after all Many techniques have since been refined to sift through the their work which has brought to light the solution which incomprehensibly large amounts of data needed to predict will be mentioned here later. First, this paper will introduce stock prices. More and more sophisticated systems are the applicable pieces of other solutions that contribute to being built as people realize that predicting a future stock the design of the overall system. Then, the system design price is not as simple as looking at a graph of the data and as a whole shall be described, denoting how the pieces are plotting the next point. The truth is that there are countless integrated together. economic factors that influence the real stock price and they are not always obvious . 2.1 Rule Standardization But let us instead suppose for a moment that pattern were truly embedded within the stock price data In developing a stock trading expert system, a large part rather than being interdependent upon external economic of the challenge is choosing which rules to use, especially factors. Then, if provided a complete history of closing when the rules may come from very different prediction stock prices, perhaps the next future stock price could environments. Then, the knowledge engineer must then accurately be predicted. It seems natural to use the number- tackle the problem of standardizing these rules in such a cruncher’s ability to do the job, but how exactly might such way they may all be compared with one another in a single a system be built and operate? rule-base. Since several very different components will be incorporated into the system, standardizing the rules In addition to there being patterns in the stock becomes very important. price data, let us also assume that we have $25,000 and that One particular system used 350 trading rules where each we know in advance how many days we have to attempt to rule was manipulated to return -1 to indicate to sell, 0 to make a profit. Suppose also that we are provided with a indicate to do nothing, and 1 to indicate to buy  Many fixed number of fabricated stocks (with no prior history) of its other rules returned values falling between -1 and 1 to and are provided on a daily basis with each stock’s closing indicate some degree of confidence in favor of one price. Additionally, at the end of each day, we also have direction or the other . The Stochastic Oscillator, Ease the option to buy or sell stocks, where each trade costs $5. of Movement, and Relative Strength Index indicators were Given these rules, how should we then proceed to maximize each independently refined to return values appropriate to our profit? the [-1, 1] scale and were subsequently incorporated into A rule-based expert system is a natural choice. It the rule-base . So, even though the system incorporated allows for a system to incorporate expert-level knowledge four different indicators, each of the four indicators were without having to learn anything. Many rival systems standardized such that the rules produced values ranging would implement neural networks or genetic algorithms, from -1 to 1. but given the particulars of our experimental situation, rule- Scaling the output using a methodology such as this is based system have an enormous advantage over these other critical. It puts all of the rules on the same playing field and allows other rules to generically deal with each of the point in time to sell. Conversely, if the MEL moves below systems and allow for such rules to depend on one another’s the MEL, then the current price is undervalued compared to output in a consistent way even though the rules may have the real price and so the price will likely increase, thereby been derived from very different environments. This indicating a good opportunity to buy. concept will play a key role in the composition of the Applying indicators such as these seems fairly straight system designed for playing the make-believe stock market forward, but representing such indicators as a rule-set for an and its pertinence will soon become clear. expert system is more problematic. One system which uses Granville’s Law relies on the graph overlaying the MAL 2.2 Using Regression Analysis and MEL together. Once graphed, the rule representation is accomplished using a sort of simple grammar to describe Note that the make-believe stock market problem very how a stock’s moving average line (MAL) compares to its different from the real stock market problem. In the make- end price line (MEL) and also to describe their trends as a believe stock market problem, stock prices are not whole . For example mal(d(2),o,i(1),b), would mean influenced by external factors such as quarterly earnings, “The MAL and MEL are decreasing at a rate of 2 (where projected profits, trading traffic, general market trends, and the MAL is over the MEL) and then increasing at a rate of 1 dividend times. The problem is therefore no longer finding (where the MAL is below the MEL)” . Once the stock the pattern between stocks prices, the value of the company, price is represented, rules can be applied such as the and the surrounding economy. The patterns are instead following: embedded directly within the provided stock-price data. One challenge in creating such a stock trading system is IF mal(d(-),o, c, -, i(3), b) THEN buy therefore finding rules that rely solely on stock price in order to determine a trend and the momentum in a stock This form of rule representation is fairly straight forward price . and performed fairly well, but this representation is also In one case, an expert system used the last 30 S&P index more complicated to implement in comparison to other values fitted to a formula using a linear regression analysis methods that predict the stock price using similar technique to quantify what the overall market might be techniques such as the Stochastic Oscillator indicator [1, 2] doing . In the case of the make-believe stock market, (an indicator very similar to Granville’s Law  and stock prices are not interdependent. However, this presents frequently called the KD-line ). Like this particular an interesting direction – trend analysis using regression application of Granville’s Law, the Stochastic Oscillator [1, analysis . A formula derived using regression analysis 2] uses a moving average line to compute when to buy and could fit very well to data that actually has embedded sell. However, one problem with techniques such as these patterns. In fact, such a formula would fit flawlessly in is if, for example, five stock prices that fall on a line and are cases where the stock price pattern were in fact linear. steadily increasing, the Stochastic Oscillator indicator [1, 2] Since regression analysis is a powerful instrument for will continually signal to the system to sell, even though predicting trends in stock data, it will be used on a per- common sense might say to buy because of the upwards stock bases and ultimately be the most important piece of trend. Indicators such as the Stochastic Oscillator indicator the system designed to tackle the make-believe stock [1, 2] clearly do not recognize trends in the stock price data market problem. (hence the reason why the Stochastic Oscillator [1, 2] is mentioned here under the category of momentum analysis 2.3 Applying a Moving Average Line  and not under trend analysis). The indicator instead In addition to trend analysis, momentum analysis is assume there are no trends and always predicts that the another important measurement of stock price data. stock price will return towards the average. It therefore Momentum analysis measures the rate of change in the only takes advantage of very recent changes in stock price, stock prices . Some systems which use momentum something which regression analysis is not as nimble with. analysis as a primary means for judging which stocks to buy This technique shall therefore be applied when designing and sell use indicators such as Granville’s Law  and the final system in the hopes that it will compensate for Stochastic Oscillator indicator  as their expertise at when regression analysis cannot find a good fit for the stock predicting when to buy, sell, and hold a stock. These price data, meaning there is too much fluctuation in price. indicators [4,2] rely heavily upon comparing the current stock price to a moving average line (MAL). The idea is that when current stock price (MEL) moves above the 3 A Note on other Approaches moving average line (MAL), then the current stock price is Before diving into the design of the system to solve the overvalued and will likely fall, thereby indicating a good make-believe stock market problem, it is worth briefly noting a few hybrid approaches and why they are shares: regression analysis , and a moving average lines. unnecessary for solving the problem at hand. The set of rules created from applying these two systems One hybrid system uses genetic algorithms in will be tailored to return values between -10 and 10 conjunction with an expert system to select the good rules (inclusive)  to indicate whether to sell or buy shares using historical data . Instead of manually tweaking the respectively. Then, the antecedents of the final rule-set will system’s underlying rule-base over time, all possible rules depend on the values returned. These rules will decide are available in a rule-base . From this rule-base, subset whether to buy, hold, or sell the stock. rule-bases are selected and incorporated iteratively into system . Each new rule set is then analyzed for 4.1 Using Regression Analysis as a Contributing performance against the historical data . Using this Solution evolutionary system, rule subsets are both created and tested programmatically . The advantage here is that It seems reasonable to believe that predicting the next stock prices is as simple as finding a formula to fit the line there is no ambiguity as to what which rules need to be connecting all of the daily points comprising the stock added or removed to improve the performance. Using brute price, and then extrapolating the next value. Such a force, the system continues test for a superior rule-subset. technique is called regression analysis and will be without a The above approach may be a bit radical in that each rule doubt an incredibly powerful tool for predicting stock is either accepted or rejected as being part of the system - prices  in the case of the make-believe stock market there is no middle ground. An entirely different where patterns are nested within the data. genetic/rule-based hybrid approach assigned weights to each rule denoting their importance [2, 5]. The resulting weighted rule-set was referred to as an expert and were 4.1.1 Complications generated using a genetic algorithm . At runtime, the Regression analysis, however, is not as simply applied as system would consult various experts and judge each it appears. When using high-order polynomial curves in expert’s performance based on certain criteria . Once an regression analysis, the resulting formula tends to have a lot expert best suited to the stocks available at the time was of unnecessary curvature, making extrapolation particularly selected, the expert was used to pick which stocks to buy difficult. In figure 1, regression analysis is used to produce and sell . a formula satisfying the three points falling on a line. The When genetic algorithms are hybridized with expert result is a line that fits each of the three points very well, systems, the goal is frequently to programmatically generate but the values plummet towards negative infinity outside sub-rule-sets and evaluate sub-set’s performance. The beyond the range of the last value. In the case of the make- advantage here is that the computer does all the number believe stock market problem, a prediction of the stock crunching rather than tweaking the rule base by hand. The price after the third point using this mechanism would result disadvantage (based on the previous two approaches) is that in the system desperately wanting to sell when it should these genetic systems do not manipulate the rules clearly buy. This is a well documented phenomenon known themselves in an intelligent way. If a specific rule is as “Runge’s Phenomenon” . And although there are undervalued or uninformed, there is nothing in the system techniques to mitigate it such as the Chebysheve’s Nodes to improve the rule itself or create completely new ones. and the use of spline curves , solving such complexities The system can only improve its performance by weakening reside outside the scope and the limitations of time placed a rule’s importance or by removing it entirely. upon this project. Instead, different techniques for applying Systems such as these are useful when there are lots of regression analysis will be used. rules and the rules are for the most part independent of one another. In the case of these two systems, the rules are based on a large number of factors external to just the stock price data. For smaller sets of rules such as that used in the final design of the system discussed later, it may very well be just as effective to tweak the rule-set by hand in order to optimize it since manual adjustments will presumably be far more intelligent. 4 The System Design The system that will be built to tackle the imaginary stock market problem will use two primary techniques to recognize good opportunities for buying and selling stock regression, then the parabolic test should not be performed because A. the pattern has already been determined and B. the data could not be properly extrapolated since the curve may plummet towards positive or negative infinity just beyond the end of the graph. Each of the most recent 50, 20, 10, and 5 regressions will each have a separate error factor associated with the fit for that particular number of points. The details concerning how this plays into whether or not to buy, sell, or hold a stock is once again dictated by the rule-set. 4.2 Using the MAL as a Contributing Solution When regression analysis does not provide an adequate fit to the points comprising the stock price history, then rules will be used that incorporate the use of a moving Figure 1. Using high-order polynomials to represent average line. In the design of this particular system, the three linear points. Stochastic Oscillator indicator will be used to forecast whether to buy, sell, or hold a stock just as they have been in prior rule-based expert systems . 4.1.2 Using Linear and Hyperbolic Regression Since the both the math driving this indicator and the Analysis details of its application in other environments lie beyond One way of circumventing Runge’s Phenomenon is to the scope of this document, the summary of its workings simply avoid the production of higher level degree expressed here will be brief. Like the application of polynomial functions when using regression analysis. If a Granville’s law , the Stochastic Oscillator indicator  linear regression technique were used instead of a uses moving averages to compute what to do with a stock. polynomial regression, then the fit would be flawless, rather The comparison is done using two oscillators, namely a fast than being close to flawless. However, points falling on a and a slow oscillator . The independent variable of the perfect line should be considered a rare event – especially fast oscillator is computed using the following formula: when the number of points is large. The far more likely (Today’s price – Low)/(High – Low); where Today’s price is the current price of the stock, the Low is the lowest price circumstance would be when the linear regression analysis over a defined time period (usually two weeks) and the yields a poor fitting line for the data. For example, it High is the highest price over the time period . The should be expected that if the graph of the stock data resulting dependent variable is usually called K . The happens to take the form of a bell curve, it will not fill well slow oscillator is where the moving average is factored into to line. In such a case, the expert system’s rules will likely account . The slow oscillator’s dependant variable is reject the data, which is why a variety of fits will be computed by using the moving average the value of the fast attempted by the system and their acceptance will be oscillator over the past few days (which is usually three) quantified by the expert system’s rules. . The computed dependent variable here is usually Since it is highly unlikely that linear regression analysis called D . The buy action is trigger when K crosses will produce a flawlessly fitting line, other regression up/beyond/through D. The sell action is triggered when K analyses will be used as well. In addition to fitting all of crosses down/past/through D . Pseudo rules will the points for a stock, the system will additionally test the therefore reflect the following spirit : most recent 50, 20, 10, and 5 points (when applicable) for the stock. The significance of these tests will be dictated by If K is smaller than D then sell the rule-base, which will be explained in more detail in a later section. If K is larger than D then buy Since linear regression analysis will only work well when If K and D are small, then buy the data falls more or less on a line, the system will also test If K and D are large, then sell for a fit when points fall more or less on a parabola (y = Ax^0 + Bx^1 + Cx^2). Once again, in addition to fitting all of the points for a stock, the system will additionally test for Once again, this mechanism should help take the most recent 50, 20, 10, and 5 points for the stock. Once advantage of daily fluctuations which the regression again, the significance of the results will be judged by the analysis would consider to be too erroneous to be put to rule-base. Note that if there is a perfect fit for the linear good use. K K 4.3 Applying the Rules Where (comparison_op) can be any one of the It is usually clear when certain rules should signal to following values: buy, and when certain rules should signal to sell. However, such rules should take into account the degree of how good < a sell or buy is so as to quantify the amount that should be > bought or sold. This why nearly all such rules in the system Where (LVL) is can be as follows: will return values between -1 and 1 (inclusive) where -1 means buy¸0 means hold, and 1 means sell . A value ranging from -1 to 1 inclusive. K D 4.3.1 Regression Analysis Rules In the case of regression analysis, the confidence in the fit (100% - Error%) will be multiplied by the predicted 4.3.3 Applying the System’s Rules percent loss or gain for the following day. This will yield a For each stock, the system will attempt to execute each product denoting the goodness of the overall fit. This value rule in the database. For each tested stock, a single average will also be scaled to the [-1, 1] system. Rules representing value between -1 and 1 (inclusive) denoting the general regression analysis will therefore be expressed in the goodness of the stock will be stored into the system’s following format: memory. Once all of the stocks have been processed, the stocks will be sorted according to their goodness value . if (FIT)(comparison_op)(LVL) then (LVL) Those stocks already owned with the lowest values that surpass a certain hardcoded threshold will be sold and those stocks with the highest goodness value beyond a certain Where (FIT) can be any one of the following values: threshold will be purchased to replace them . The word 50_DAY_LINEAR_FIT replace is used here because the system will naturally want 20_DAY_ LINEAR_FIT to avoid unnecessary 10_DAY_ LINEAR_FIT 5 Concluding Remarks 5_DAY_ LINEAR_FIT Most research that has been done using expert systems 50_DAY_PARABOLIC_FIT to solve the stock market problem has done so using lots of 20_DAY_ PARABOLIC_FIT rules acquired from many different indicators. These 10_DAY_ PARABOLIC_FIT indicators do not rely exclusively on the stock price and so oftentimes require complex hybrid systems to gather and 5_DAY_ PARABOLIC_FIT decipher the vast amounts of information available to help Where (comparison_op) can be any one of the make a smart decision regarding stock trades. The make- following values: believe stock market problem presented here is therefore a < very interesting version of the real stock market, where real trades can and should be based on stock-price data alone. > Although the system has not been built yet, since the system Where (LVL) is a value ranging from -1 to 1 inclusive. incorporates both trend analysis via regression analysis and momentum analysis  via the Stochastic Oscillator indicator [1, 2, 6], it should perform well given the particular data it has been provided with to make its decision whether to buy, sell, or hold stock in the make- believe stock market. transactions and so keep only a few stocks at any one point in time. 4.3.2 Stochastic Oscillator Rules if (KD)(comparison_op)(LVL) then (LVL) Where (KD) can be any one of the following values: K_AND_D 3. Conger., S., and Loch, K.D. (eds.). Ethics and computer use. Commun. ACM 38, 12 (entire issue). 4. Mackay, W.E. Ethics, lies and videotape, in REFERENCES Proceedings of CHI '95 (Denver CO, May 1995), ACM 1. Anderson, R.E. Social impacts of computing: Codes of Press, 138-145. professional ethics. Social Science Computing Review 5. Schwartz, M., and Task Force on Bias-Free Language. 10, 2 (Winter 1992), 453-469. Guidelines for Bias-Free Writing. Indiana University 2. CHI Conference Publications Format. Available at Press, Bloomington IN, 1995. http://www.acm.org/sigchi/chipubform/.