00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "Index.h"
00023 #include "Config.h"
00024 #include "DbPlugin.h"
00025 #include <qdir.h>
00026
00027
00028 Index::Index ()
00029 {
00030 }
00031
00032 Index::~Index ()
00033 {
00034 }
00035
00036 void Index::getHistory (BarData *barData, QDateTime &startDate, QString &il,
00037 int barRange, BarData::BarLength barLength)
00038 {
00039 QStringList l = QStringList::split(":", il, FALSE);
00040 if (! l.count())
00041 return;
00042
00043 QDict<Bar> lookup;
00044 lookup.setAutoDelete(TRUE);
00045 int loop;
00046 int count = 0;
00047 for (loop = 0; loop < (int) l.count(); loop = loop + 2)
00048 {
00049
00050
00051 float weight = l[loop + 1].toFloat();
00052 if (weight == 0)
00053 weight = 1;
00054
00055 loadIndexData(l[loop], lookup, startDate, weight, barRange, barLength);
00056 count++;
00057 }
00058
00059 l.clear();
00060 QString s;
00061 QDictIterator<Bar> it(lookup);
00062 for (; it.current(); ++it)
00063 {
00064 Bar *r = it.current();
00065 if (r->getOI() == count)
00066 {
00067 r->setOpen(r->getOpen() / count);
00068 r->setHigh(r->getHigh() / count);
00069 r->setLow(r->getLow() / count);
00070 r->setClose(r->getClose() / count);
00071
00072 if (r->getOpen() > r->getHigh())
00073 r->setHigh(r->getOpen());
00074 if (r->getOpen() < r->getLow())
00075 r->setLow(r->getOpen());
00076
00077 if (r->getClose() > r->getHigh())
00078 r->setHigh(r->getClose());
00079 if (r->getClose() < r->getLow())
00080 r->setLow(r->getClose());
00081
00082 r->getDateTimeString(FALSE, s);
00083 l.append(s);
00084 }
00085 else
00086 lookup.remove(it.currentKey());
00087 }
00088
00089 l.sort();
00090 for (loop = l.count() - 1; loop > -1; loop--)
00091 {
00092 Bar *r = lookup.find(l[loop]);
00093 if (r)
00094 {
00095 QDateTime dt;
00096 r->getDate(dt);
00097 Bar tr;
00098 tr.setDate(dt);
00099 tr.setOpen(r->getOpen());
00100 tr.setHigh(r->getHigh());
00101 tr.setLow(r->getLow());
00102 tr.setClose(r->getClose());
00103 barData->prepend(tr);
00104 }
00105 }
00106
00107
00108 }
00109
00110 void Index::loadIndexData (QString &symbol, QDict<Bar> &lookup, QDateTime &startDate, float weight,
00111 int barRange, BarData::BarLength barLength)
00112 {
00113 DbPlugin db;
00114 if (db.openChart(symbol))
00115 {
00116 qDebug("DbPlugin::getIndexHistory: cannot open symbol chart");
00117 db.close();
00118 return;
00119 }
00120
00121 BarData *bar = new BarData(symbol);
00122 bar->setBarLength(barLength);
00123 db.setBarRange(barRange);
00124 db.getHistory(bar, startDate);
00125 db.close();
00126
00127 int loop;
00128 for (loop = 0; loop < (int) bar->count(); loop++)
00129 {
00130 QDateTime dt;
00131 bar->getDate(loop, dt);
00132 QString s = dt.toString("yyyyMMddhhmmss");
00133 Bar *r = lookup.find(s);
00134 if (! r)
00135 {
00136 r = new Bar;
00137 r->setDate(dt);
00138 r->setOpen(bar->getOpen(loop) * weight);
00139 r->setHigh(bar->getHigh(loop) * weight);
00140 r->setLow(bar->getLow(loop) * weight);
00141 r->setClose(bar->getClose(loop) * weight);
00142 r->setOI(1);
00143 r->getDateTimeString(FALSE, s);
00144 lookup.insert(s, r);
00145 }
00146 else
00147 {
00148 r->setOpen(r->getOpen() + (bar->getOpen(loop) * weight));
00149 r->setHigh(r->getHigh() + (bar->getHigh(loop) * weight));
00150 r->setLow(r->getLow() + (bar->getLow(loop) * weight));
00151 r->setClose(r->getClose() + (bar->getClose(loop) * weight));
00152 r->setOI((int) r->getOI() + 1);
00153 }
00154 }
00155
00156 delete bar;
00157 db.close();
00158 }
00159