File: OVRLY.cpp

package info (click to toggle)
qtstalker 0.26-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,064 kB
  • ctags: 2,375
  • sloc: cpp: 27,066; makefile: 68
file content (295 lines) | stat: -rw-r--r-- 7,161 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 *  Qtstalker stock charter
 *
 *  Copyright (C) 2001-2004 Stefan S. Stratigakos
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 *  USA.
 */

#include "OVRLY.h"
#include "ChartDb.h"
#include <qdatetime.h>
#include <qdict.h>
#include "PrefDialog.h"

OVRLY::OVRLY ()
{
  pluginName = "OVRLY";
  plotFlag = FALSE;
  alertFlag = FALSE;
  setDefaults();

  methodList.append(tr("Compare Price"));
  methodList.append(tr("Compare Performance"));
  methodList.sort();
}

OVRLY::~OVRLY ()
{
}

void OVRLY::setDefaults ()
{
  color.setNamedColor("yellow");
  baseColor.setNamedColor("red");
  lineType = PlotLine::Line;
  baseLineType = PlotLine::Line;
  label = pluginName;
  baseLabel = tr("Base");
  baseSymbol.truncate(0);
  method = tr("Compare Performance");
}

void OVRLY::calculate ()
{
  while (1)
  {
    if (! method.compare(tr("Compare Price")))
    {
      comparePrice();
      break;
    }

    if (! method.compare(tr("Compare Performance")))
    {
      comparePerformance();
      break;
    }

    break;
  }
}

void OVRLY::comparePrice ()
{
  PlotLine *line1 = data->getInput(BarData::Close);
  line1->setColor(color);
  line1->setType(lineType);
  line1->setLabel(label);
  line1->setScaleFlag(TRUE);
  output.append(line1);

  if (baseSymbol.length())
  {
    PlotLine *line2 = getSymbolLine(baseSymbol);
    line2->setColor(baseColor);
    line2->setType(baseLineType);
    line2->setLabel(baseLabel);
    line2->setScaleFlag(TRUE);
    output.append(line2);
  }
}

void OVRLY::comparePerformance ()
{
  if (data->count() < 1)
    return;

  if (baseSymbol.length() == 0)
    return;

  PlotLine *tline = getSymbolLine(baseSymbol);
  if (tline->getSize() < 1)
  {
    delete tline;
    return;
  }

  int line1Loop = data->count() - 1;
  PlotLine *line1 = new PlotLine;
  line1->setColor(color);
  line1->setType(lineType);
  line1->setLabel(label);

  int line2Loop = tline->getSize() - 1;
  PlotLine *line2 = new PlotLine;
  line2->setColor(baseColor);
  line2->setType(baseLineType);
  line2->setLabel(baseLabel);

  while (line1Loop > -1 && line2Loop > -1)
  {
    line1Loop--;
    line2Loop--;
  }
  line1Loop++;
  line2Loop++;
  double line1base = data->getClose(line1Loop);
  double line2base = tline->getData(line2Loop);

  for (; line1Loop < (int) data->count() && line2Loop < tline->getSize(); line1Loop++, line2Loop++)
  {
    line1->append(((data->getClose(line1Loop) - line1base) / line1base) * 100);
    line2->append(((tline->getData(line2Loop) - line2base) / line2base) * 100);
  }

  delete tline;

  output.append(line1);
  output.append(line2);
}

PlotLine * OVRLY::getSymbolLine (QString d)
{
  PlotLine *line = new PlotLine();

  ChartDb *db = new ChartDb;
  if (db->openChart(d))
  {
    delete db;
    return line;
  }
  
  BarDate date = data->getDate(0);
  
  db->setBarCompression(ChartDb::Daily);
  db->setBarRange(99999999);
  BarData *recordList = db->getHistory();

  QDict<Setting> dict;
  int loop;
  for (loop = 0; loop < (int) recordList->count(); loop++)
  {
    Setting *r = new Setting;
    r->setData("Close", QString::number(recordList->getClose(loop)));
    dict.insert(recordList->getDate(loop).getDateTimeString(FALSE), r);
  }

  double val = 0;

  for (loop = 0; loop < (int) data->count(); loop++)
  {
    Setting *r2 = dict[recordList->getDate(loop).getDateTimeString(FALSE)];
    if (! r2)
      line->append(val);
    else
    {
      val = r2->getFloat(tr("Close"));
      line->append(val);
    }
  }

  delete recordList;
  delete db;

  return line;
}

int OVRLY::indicatorPrefDialog ()
{
  PrefDialog *dialog = new PrefDialog();
  dialog->setCaption(tr("OVRLY Indicator"));
  
  dialog->createPage (tr("Base Symbol"));
  dialog->addColorItem(tr("Base Color"), tr("Base Symbol"), baseColor);
  dialog->addComboItem(tr("Base Line Type"), tr("Base Symbol"), lineTypes, baseLineType);
  dialog->addTextItem(tr("Base Label"), tr("Base Symbol"), baseLabel);
  dialog->addComboItem(tr("Method"), tr("Base Symbol"), methodList, method);
  dialog->addSymbolItem(tr("Base Symbol"), tr("Base Symbol"), dataPath, baseSymbol);
  
  dialog->createPage (tr("Current Symbol"));
  dialog->addColorItem(tr("Color"), tr("Current Symbol"), color);
  dialog->addComboItem(tr("Line Type"), tr("Current Symbol"), lineTypes, lineType);
  dialog->addTextItem(tr("Label"), tr("Current Symbol"), label);
  
  int rc = dialog->exec();
  
  if (rc == QDialog::Accepted)
  {
    color = dialog->getColor(tr("Color"));
    baseColor = dialog->getColor(tr("Base Color"));
    lineType = (PlotLine::LineType) dialog->getComboIndex(tr("Line Type"));
    baseLineType = (PlotLine::LineType) dialog->getComboIndex(tr("Base Line Type"));
    label = dialog->getText(tr("Label"));
    baseLabel = dialog->getText(tr("Base Label"));
    method = dialog->getCombo(tr("Method"));
    baseSymbol = dialog->getSymbol(tr("Base Symbol"));
    rc = TRUE;
  }
  else
    rc = FALSE;
  
  delete dialog;
  return rc;
}

void OVRLY::loadIndicatorSettings (QString file)
{
  setDefaults();
  
  QDict<QString> dict = loadFile(file);
  if (! dict.count())
    return;
  
  QString *s = dict["color"];
  if (s)
    color.setNamedColor(s->left(s->length()));
    
  s = dict["baseColor"];
  if (s)
    baseColor.setNamedColor(s->left(s->length()));
  
  s = dict["lineType"];
  if (s)
    lineType = (PlotLine::LineType) s->left(s->length()).toInt();

  s = dict["baseLineType"];
  if (s)
    baseLineType = (PlotLine::LineType) s->left(s->length()).toInt();
  
  s = dict["label"];
  if (s)
    label = s->left(s->length());
      
  s = dict["baseLabel"];
  if (s)
    baseLabel = s->left(s->length());
  
  s = dict["method"];
  if (s)
    method = s->left(s->length());
    
  s = dict["baseSymbol"];
  if (s)
    baseSymbol = s->left(s->length());
}

void OVRLY::saveIndicatorSettings (QString file)
{
  QDict<QString>dict;
  dict.setAutoDelete(TRUE);

  dict.replace("color", new QString(color.name()));
  dict.replace("baseColor", new QString(baseColor.name()));
  dict.replace("lineType", new QString(QString::number(lineType)));
  dict.replace("baseLineType", new QString(QString::number(baseLineType)));
  dict.replace("label", new QString(label));
  dict.replace("baseLabel", new QString(baseLabel));
  dict.replace("method", new QString(method));
  dict.replace("baseSymbol", new QString(baseSymbol));
  dict.replace("plugin", new QString(pluginName));

  saveFile(file, dict);
}

Plugin * create ()
{
  OVRLY *o = new OVRLY;
  return ((Plugin *) o);
}