1 /**
2 *  Copyright Mikael H�gdahl - triyana@users.sourceforge.net
3 *
4 *  This source is distributed under the terms of the Q Public License version 1.0,
5 *  created by Trolltech (www.trolltech.com).
6 */
7 
8 #ifndef MHPrice_H
9 #define MHPrice_H
10 
11 #include "MHString.h"
12 #include "MHVector.h"
13 
14 
15 
16 /**
17 *  A class for storing chart data points or other numerical
18 *  data that needs to be stored in a time serie.
19 *  There are also some static utility methods to convert time series of the points.
20 */
21 class MHPrice : public MH {
22 public:
23     char                    aDate[9];
24     double                  aHigh;
25     double                  aLow;
26     double                  aClose;
27     double                  aOpen;
28     double                  aVol;
29 
30     enum {
31                             MVG_ATR               =  14,
32                             MVG_BOLLINGER         =  20,
33                             MVG_ELONG             =  16,
34                             MVG_ESHORT            =  8,
35                             MVG_LONG              =  16,
36                             MVG_MACD_ESMA         =   9,
37                             MVG_MACD_LONG         =  26,
38                             MVG_MACD_SHORT        =  12,
39                             MVG_MOMENTUM          =  10,
40                             MVG_RSI               =  14,
41                             MVG_SHORT             =   8,
42                             MVG_STD               =  20,
43                             MVG_STOCHASTICS_K     =  14,
44                             MVG_STOCHASTICS_SMA_K =   1,
45                             MVG_STOCHASTICS_SMA_D =   3,
46 
47                             BUY                   =  1,
48                             NEUTRAL               =  0,
49                             SELL                  = -1,
50     };
51 
52     static const double     MIN_VALUE;
53     static const double     MAX_VALUE;
54     static const double     ZERO_VALUE;
55 
MHPrice()56                             MHPrice () {aHigh = aLow = aClose = aOpen = aVol = 0.0; *aDate = '\0';}
57                             MHPrice (const MHPrice&);
58                             MHPrice (const char* d, double h);
59                             MHPrice (double h, double c);
60                             MHPrice (const char* d, double h, double l, double c, double o, double v);
61 
62     MHPrice&                operator=(const MHPrice&);
63     bool                    operator==(const MHPrice& x) {return (strcmp (aDate, x.aDate) == 0) ? true : false;}
64     bool                    operator==(const char* s) {return (strcmp (aDate, s) == 0) ? true : false;}
65     bool                    operator!=(const MHPrice& x) {return (strcmp (aDate, x.aDate) != 0) ? true : false;}
66     bool                    operator>(const MHPrice& x) {return (strcmp (aDate, x.aDate) > 0) ? true : false;}
67     bool                    operator>(const char* s) {return (strcmp (aDate, s) > 0) ? true : false;}
68     bool                    operator<(const MHPrice& x) {return (strcmp (aDate, x.aDate) < 0) ? true : false;}
69     bool                    operator<(const char* s){return (strcmp (aDate, s) < 0) ? true : false;}
70     bool                    operator<=(const MHPrice& x) {return (strcmp (aDate, x.aDate) < 1) ? true : false;}
71     bool                    operator<=(const char* x) {return (strcmp (aDate, x) < 1) ? true : false;}
72     bool                    operator>=(const MHPrice& x) {return (strcmp (aDate, x.aDate) >= 0) ? true : false;}
73     bool                    operator>=(const char* x) {return (strcmp (aDate, x) >= 0) ? true : false;}
74 
75     void                    Adjust (double price, double volume = -1);
Class()76     const char*             Class () {return "MHPrice";}
77     int                     Compare (const MH* o, int type);
SetDate(const char * val)78     void                    SetDate (const char* val) {strncpy (aDate, val, 8); aDate[8] = '\0';}
SetDate(const MHString * val)79     void                    SetDate (const MHString* val) {strncpy (aDate, val->Get(), 8); aDate[8] = '\0';}
80     void                    Validate ();
81 
82     static void             ATR (MHVector* in, MHVector*out, int nMvg);
83     static void             Bollinger (MHVector* in, MHVector* mvg, MHVector* upper, MHVector* lower, int nDays);
84     static void             Clear (MHVector*);
85     static void             Copy (MHVector* in, MHVector* out, const char* pStartDate = 0, const char* pStopDate = 0);
86     static void             CopyPointers (MHVector* in, MHVector* out);
87     static void             Cut (MHVector* in, MHVector* out);
88     static void             Day2Week (MHVector* in, MHVector* out, int nWeekDay);
89     static void             Diff (MHVector* in1, MHVector* in2, MHVector* out, MHVector* out2 = 0);
90     static void             ExponentialMovingAverage (MHVector* in, MHVector* out, int nMvg);
91     static void             MACD (MHVector* in, MHVector* macd1, MHVector* macd2, int emaShort, int emaLong, int emaMacd);
92     static void             Momentum (MHVector* in, MHVector*out, MHVector* zero, int nMvg);
93     static void             MovingAverage (MHVector* in, MHVector* out, int nMvg);
94     static void             MovingAverageSignals (MHVector* in_short, MHVector* in_long, MHVector* out);
95     static void             Normalise (MHVector* in, MHVector* out, double nOrigo);
96     static void             Print (MHVector* in);
97     static void             RSI (MHVector* in, MHVector*out, int nMvg);
98     static void             RSISignals (MHVector* in, MHVector* out, double low = 30.0, double high = 70.0);
99     static bool             Save (const char* pFilename, MHVector* in);
100     static void             StdDev (MHVector* in, MHVector* out, int nDays);
101     static void             Stochastics (MHVector* in, MHVector* kout, MHVector* dout, int k, int sma, int d);
102     static void             Stochastics (MHVector* in, MHVector* kout, MHVector* dout, MHVector* out20, MHVector* out80, int k, int sma, int d);
103     static void             Volume (MHVector* in, MHVector* out);
104 
105 private:
compare(MHPrice * price)106     int                     compare (MHPrice* price) {return strcmp(aDate, price->aDate);}
compare(MHString * string)107     int                     compare (MHString* string) {return strcmp(aDate, string->Get());}
108 };
109 
110 #endif
111