00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "VFI.h"
00023 #include "PrefDialog.h"
00024 #include <qdict.h>
00025 #include <qobject.h>
00026 #include <math.h>
00027
00028 VFI::VFI ()
00029 {
00030 pluginName = "VFI";
00031 helpFile = "vfi.html";
00032
00033 colorLabel = "color";
00034 lineTypeLabel = "lineType";
00035 labelLabel = "label";
00036 periodLabel = "period";
00037 smoothingLabel = "smoothing";
00038 maTypeLabel = "maType";
00039 pluginLabel = "plugin";
00040
00041 formatList.append(FormatMAType);
00042 formatList.append(FormatInteger);
00043 formatList.append(FormatInteger);
00044
00045 setDefaults();
00046 }
00047
00048 VFI::~VFI ()
00049 {
00050 }
00051
00052 void VFI::setDefaults ()
00053 {
00054 color.setNamedColor("red");
00055 lineType = PlotLine::Line;
00056 label = pluginName;
00057 smoothing = 3;
00058 maType = 1;
00059 period = 100;
00060 }
00061
00062 Indicator * VFI::calculate ()
00063 {
00064 Indicator *output = new Indicator;
00065 output->setDateFlag(dateFlag);
00066 output->setLogScale(logScale);
00067 output->addLine(getVFI());
00068 return output;
00069 }
00070
00071 PlotLine * VFI::getVFI ()
00072 {
00073 PlotLine *vfi = new PlotLine();
00074 int loop;
00075 for (loop = period; loop < (int) data->count(); loop++)
00076 {
00077 double inter = 0.0;
00078 double sma_vol = 0.0;
00079 int i;
00080 double close = data->getClose(loop-period);
00081 double high = data->getHigh(loop-period);
00082 double low = data->getLow(loop-period);
00083 double typical = (high+low+close)/3.0;
00084 for(i=loop-period+1; i<=loop; i++) {
00085 double ytypical = typical;
00086 close = data->getClose(i);
00087 high = data->getHigh(i);
00088 low = data->getLow(i);
00089 typical = (high+low+close)/3.0;
00090 double delta = (log(typical) - log(ytypical));
00091 inter += delta*delta;
00092 sma_vol += data->getVolume(i);
00093 }
00094 inter = 0.2*sqrt(inter/(double)period)*typical;
00095 sma_vol /= (double)period;
00096
00097 close = data->getClose(loop-period);
00098 high = data->getHigh(loop-period);
00099 low = data->getLow(loop-period);
00100 typical = (high+low+close)/3.0;
00101 double t = 0;
00102 for(i=loop-period+1; i<=loop; i++) {
00103 double ytypical = typical;
00104 double volume = data->getVolume(i);
00105 close = data->getClose(i);
00106 high = data->getHigh(i);
00107 low = data->getLow(i);
00108 typical = (high+low+close)/3.0;
00109
00110 if (typical > ytypical+inter)
00111 t = t + log(1.0 + volume/sma_vol);
00112 else
00113 {
00114 if (typical < ytypical-inter)
00115 t = t - log(1.0 + volume/sma_vol);
00116 }
00117 }
00118
00119 vfi->append(t);
00120 }
00121
00122 PlotLine *pl = 0;
00123 if (smoothing > 1)
00124 {
00125 PlotLine *ma = getMA(vfi, maType, smoothing);
00126 ma->setColor(color);
00127 ma->setType(lineType);
00128 ma->setLabel(label);
00129 pl = ma;
00130 delete vfi;
00131 }
00132 else
00133 {
00134 vfi->setColor(color);
00135 vfi->setType(lineType);
00136 vfi->setLabel(label);
00137 pl = vfi;
00138 }
00139
00140 return pl;
00141 }
00142
00143 int VFI::indicatorPrefDialog (QWidget *w)
00144 {
00145 QString pl = QObject::tr("Parms");
00146 QString cl = QObject::tr("Color");
00147 QString ll = QObject::tr("Label");
00148 QString ltl = QObject::tr("Line Type");
00149 QString perl = QObject::tr("Period");
00150 QString sl = QObject::tr("Smoothing");
00151 QString stl = QObject::tr("Smoothing Type");
00152
00153 PrefDialog *dialog = new PrefDialog(w);
00154 dialog->setCaption(QObject::tr("VFI Indicator"));
00155 dialog->createPage (pl);
00156 dialog->setHelpFile(helpFile);
00157 dialog->addColorItem(cl, pl, color);
00158 dialog->addComboItem(ltl, pl, lineTypes, lineType);
00159 dialog->addTextItem(ll, pl, label);
00160 dialog->addIntItem(perl, pl, period, 1, 99999999);
00161 dialog->addIntItem(sl, pl, smoothing, 0, 99999999);
00162 QStringList l;
00163 getMATypes(l);
00164 dialog->addComboItem(stl, pl, l, maType);
00165
00166 int rc = dialog->exec();
00167
00168 if (rc == QDialog::Accepted)
00169 {
00170 dialog->getColor(cl, color);
00171 lineType = (PlotLine::LineType) dialog->getComboIndex(ltl);
00172 dialog->getText(ll, label);
00173 period = dialog->getInt(perl);
00174 smoothing = dialog->getInt(sl);
00175 maType = dialog->getComboIndex(stl);
00176 rc = TRUE;
00177 }
00178 else
00179 rc = FALSE;
00180
00181 delete dialog;
00182 return rc;
00183 }
00184
00185 void VFI::setIndicatorSettings (Setting &dict)
00186 {
00187 setDefaults();
00188
00189 if (! dict.count())
00190 return;
00191
00192 QString s;
00193 dict.getData(colorLabel, s);
00194 if (s.length())
00195 color.setNamedColor(s);
00196
00197 dict.getData(lineTypeLabel, s);
00198 if (s.length())
00199 lineType = (PlotLine::LineType) s.toInt();
00200
00201 dict.getData(labelLabel, s);
00202 if (s.length())
00203 label = s;
00204
00205 dict.getData(periodLabel, s);
00206 if (s.length())
00207 period = s.toInt();
00208
00209 dict.getData(smoothingLabel, s);
00210 if (s.length())
00211 smoothing = s.toInt();
00212
00213 dict.getData(maTypeLabel, s);
00214 if (s.length())
00215 maType = s.toInt();
00216
00217 }
00218
00219 void VFI::getIndicatorSettings (Setting &dict)
00220 {
00221 QString ts = color.name();
00222 dict.setData(colorLabel, ts);
00223 ts = QString::number(lineType);
00224 dict.setData(lineTypeLabel, ts);
00225 dict.setData(labelLabel, label);
00226 ts = QString::number(period);
00227 dict.setData(periodLabel, ts);
00228 ts = QString::number(smoothing);
00229 dict.setData(smoothingLabel, ts);
00230 ts = QString::number(maType);
00231 dict.setData(maTypeLabel, ts);
00232 dict.setData(pluginLabel, pluginName);
00233 }
00234
00235 PlotLine * VFI::calculateCustom (QString &p, QPtrList<PlotLine> &d)
00236 {
00237
00238
00239 if (checkFormat(p, d, 3, 3))
00240 return 0;
00241
00242 QStringList mal;
00243 getMATypes(mal);
00244 maType = mal.findIndex(formatStringList[0]);
00245 period = formatStringList[1].toInt();
00246 smoothing = formatStringList[2].toInt();
00247
00248 return getVFI();
00249 }
00250
00251 void VFI::formatDialog (QStringList &, QString &rv, QString &rs)
00252 {
00253 rs.truncate(0);
00254 rv.truncate(0);
00255 QString pl = QObject::tr("Parms");
00256 QString vnl = QObject::tr("Variable Name");
00257 QString perl = QObject::tr("Period");
00258 QString sl = QObject::tr("Smoothing");
00259 QString stl = QObject::tr("Smoothing Type");
00260 PrefDialog *dialog = new PrefDialog(0);
00261 dialog->setCaption(QObject::tr("VFI Format"));
00262 dialog->createPage (pl);
00263 dialog->setHelpFile(helpFile);
00264
00265
00266
00267 QString s;
00268 QStringList l;
00269 getMATypes(l);
00270 dialog->addTextItem(vnl, pl, s);
00271 dialog->addComboItem(stl, pl, l, maType);
00272 dialog->addIntItem(perl, pl, period, 1, 99999999);
00273 dialog->addIntItem(sl, pl, smoothing, 0, 99999999);
00274
00275 int rc = dialog->exec();
00276
00277 if (rc == QDialog::Accepted)
00278 {
00279 dialog->getText(vnl, rv);
00280
00281 dialog->getCombo(stl, rs);
00282
00283 int t = dialog->getInt(perl);
00284 rs.append("," + QString::number(t));
00285
00286 t = dialog->getInt(sl);
00287 rs.append("," + QString::number(t));
00288 }
00289
00290 delete dialog;
00291 }
00292