|
How to get the stock real data in database by django? |
Posted by sword on Feb-12-2011 11:54 |
|
Hi,I want to use django and Chartdirector make a stock chart,and I import the stock data
to database ,include stock number,timestamps,opendata,highdata,lowdata,etc.but I don't
know how to get them,and the Chartdirector documentation was use rantable,how can I
get the real data in my database?
Thank you. |
Re: How to get the stock real data in database by django? |
Posted by Peter Kwan on Feb-12-2011 15:03 |
|
Hi sword,
First, you would need to read data from your database to Python variables. You should obtain 6 data series (6 Python arrays). They are an array of timeStamps, an array of high prices, an array of low prices, an array of open prices, an array of close prices and an array of volume. You would need to refer to your database and djando documentation on how to read data from database to Python variables.
After you have read the data from the database to Python variables, you can pass those variables to ChartDirector to create the chart. The code is like that in the sample code:
c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays)
Hope this can help.
Regards
Peter Kwan |
Re: How to get the stock real data in database by django? |
Posted by sword on Feb-12-2011 18:57 |
|
Thanks,Peter.
I already import the data to my database,and in django,I use these code get the data:
def chartdir(request,number):
noOfDays = 100
extraDays = 30
stockdata = Stockhq.objects.all()
timeStamps = Stockhq.objects.values_list('timeStamps', flat=True)[:100]
highData = Stockhq.objects.values_list('highData', flat=True)[:100]
lowData = Stockhq.objects.values_list('lowData', flat=True)[:100]
openData = Stockhq.objects.values_list('openData', flat=True)[:100]
closeData = Stockhq.objects.values_list('closeData', flat=True)[:100]
volData = Stockhq.objects.values_list('volData', flat=True)[:100]
c = FinanceChart(650)
c.addTitle("Finance Chart Demonstration")
c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays)
c.addMainChart(350)
c.addExpMovingAvg(7, 0x000000)
c.addExpMovingAvg(14, 0xff0000)
c.addExpMovingAvg(25, 0x0000ff)
c.addCandleStick(0xff0000, 0x00ff00)
c.addBollingerBand(13, 1.618, 0x9999ff, 0xc06666ffL)
c.addVolBars(75, 0x99ff99, 0xff9999, 0x808080)
c.addMACD(150, 26, 12, 9, 0x0000ff, 0xff00ff, 0x008000)
# Output the chart
c.makeChart("finance.png")
return HttpResponse(c.makeChart2(PNG), 'image/png')
return response
and I test in shell,I can see the data just like:
In [120]: Stockhq.objects.values_list('highData', flat=True)[:100]
Out[120]: [u'67.41', u'66.4', u'66.07', u'65.74', u'66.42', u'65.76', u'65.43', u'65.1',
u'64.77', u'64.45', u'64.13', u'63.81', u'63.49', u'63.2', u'62.85', u'62.54', u'62.23', u'61.92',
u'61.62', u'61.3', u'60.99', u'60.39', u'60.09', u'60.39', u'60.99', u'61.29', u'60.6', u'61.91',
u'62.22', u'62.53', u'62.84', u'63.15', u'63.47', u'63.79', u'64.11', u'64.43', u'64.75',
u'65.07', u'65.4', u'65.07', u'64.74', u'64.42', u'64.1', u'63.36', u'63.04', u'62.72', u'62.41',
u'62.11', u'61.8', u'61.49', u'61.49', u'61.49', u'61.49', u'61.49', u'61.49', u'61.49', u'61.49',
u'61.49', u'49', u'48.76', u'48.52', u'48.28', u'48.04', u'47.8', u'47.56', u'47.56', u'47.08',
u'46.84', u'46.84', u'46.38', u'46.15', u'45.92', u'45.69', u'45.46', u'45.46', u'45', u'44.78',
u'44.56', u'44.34', u'44.34', u'43.9', u'43.68', u'43.46', u'43.24', u'43.02', u'43.02', u'42.59',
u'42.38', u'42.17', u'41.96', u'41.75', u'41.54', u'41.33', u'41.12', u'40.91', u'40.71',
u'40.51', u'40.31', u'40.11', u'39.91']
but they can't make the chart in website.
I don't konw why. |
Re: How to get the stock real data in database by django? |
Posted by sword on Feb-14-2011 01:32 |
|
yep,I know,I make a mistake,I set the model's field is wrong,and I set the correct
format,but I can't get the date data correct,and I test by the random data like the
source in help doc,the chart can work.so??what can I do?
the model code:
class Stock(models.Model):
"""
A stock with its number and name
"""
number = models.CharField(_('number'), max_length=50,unique=False)
name = models.CharField(_('name'), max_length=50,unique=False)
date_added = models.DateTimeField(_('date added'), default=datetime.now,
editable=False)
is_public = models.BooleanField(_('is public'), default=True, help_text=_('Public
patient will be displayed in the default views.'))
def __unicode__(self):
return self.number
def get_absolute_url(self):
return ("stock_details", [self.pk])
get_absolute_url = models.permalink(get_absolute_url)
class Stockhq(models.Model):
"""
A stock with its details
"""
stock = models.ForeignKey(Stock,default=1)
number = models.CharField(_('number'), max_length=8,unique=False)
timeStamps = models.DateField(_('timeStamps'), max_length=10,unique=False)
openData = models.FloatField(_('openData '), unique=False)
highData = models.FloatField(_('highData'), unique=False)
lowData = models.FloatField(_('lowData'), unique=False)
closeData = models.FloatField(_('closeData '), unique=False)
volData = models.IntegerField(_('volData '), max_length=500,unique=False)
moneyData = models.IntegerField(_('moneyData '),
max_length=500,unique=False)
date_added = models.DateTimeField(_('date added'), default=datetime.now,
editable=False)
is_public = models.BooleanField(_('is public'), default=True, help_text=_('Public
patient will be displayed in the default views.'))
def __unicode__(self):
return self.number
def get_absolute_url(self):
return ("stock_details", [self.pk])
get_absolute_url = models.permalink(get_absolute_url)
class Meta:
ordering = ('-timeStamps', )
and the view.py code:
def chartdir(request):
noOfDays = 100
extraDays = 30
queryset = Stockhq.objects.order_by('-timeStamps')[:100]
timeStamps = list(map(lambda Stockhq: Stockhq.timeStamps.timetuple(),
queryset))
highData = list(Stockhq.objects.order_by('-timeStamps').values_list('highData',
flat=True)[:100])
lowData = list(Stockhq.objects.order_by('-timeStamps').values_list('lowData',
flat=True)[:100])
openData = list(Stockhq.objects.order_by('-timeStamps').values_list('openData',
flat=True)[:100])
closeData = list(Stockhq.objects.order_by('-timeStamps').values_list('closeData',
flat=True)[:100])
volData = list(Stockhq.objects.order_by('-timeStamps').values_list('volData',
flat=True)[:100])
.....
I don't know the correct format of the timeStamps . |
Re: How to get the stock real data in database by django? |
Posted by Peter Kwan on Feb-15-2011 00:55 |
|
Hi sword,
You may use chartTime or chartTime2 to create date/time in ChartDirector. (You may look for "Date Representation" in the ChartDirector documentation index for more information.) For example:
#assume timeStamps contains date/time elements in tuples format
timeStamps[i] = map(lambda t: chartTime(t[0], t[1], t[2], t[3], t[4], t[5]), timeStamps)
Hope this can help.
Regards
Peter Kwan |
Re: How to get the stock real data in database by django? |
Posted by sword on Feb-15-2011 11:58 |
|
Thanks Peter,I know what's wrong with the data,the timedata I import into database is the iso format,and django read it as the format like that: datetime.date(2011,2,11),and I use the toordinal.() make the data format as a long integer,now,it work,I can see the correct chart in my test websit.
Thank you for your help. |
|