Example #1
0
def create_stock(request):
    code = request.GET.get('code')
    amount = request.GET.get('amount')
    stock_tag = request.GET.get('tag')
    print 'code:{},amount:{},tag:{}'.format(code, amount, stock_tag)
    stock = Stock(code, amount)
    stock.save()
    # insert_stock(stock)
    add_tag(code, 'top100')
    return render_to_response('portfolio/index.html')
Example #2
0
def create_stock(request):
    code = request.GET.get('code')
    amount = request.GET.get('amount')
    stock_tag = request.GET.get('tag')
    print 'code:{},amount:{},tag:{}'.format(code, amount, stock_tag)
    stock = Stock(code, amount)
    stock.save()
    # insert_stock(stock)
    add_tag(code, 'top100')
    return render_to_response('portfolio/index.html')
Example #3
0
def stock_list():
    import tushare as ts
    df = ts.get_stock_basics()
    stocks = df.index.tolist()
    print((len(stocks)))
    print(stocks)
    for stock in stocks:
        s = Stock()
        s.code = stock
        s.save()
Example #4
0
def getCSVHistorialData(code='600276',
                        save=True,
                        beginDate='',
                        endDate=str(date.today())):
    from lxml import etree
    #yahoo stock ticker need post-fix ".SS" for Shanghai,'.SZ' for shenzheng
    if len(code) == 9:
        code2 = code
    elif code.startswith('6'):
        code2 = code + ".SS"
    else:
        code2 = code + ".SZ"

    begin = beginDate.split('-')
    end = endDate.split('-')
    print 'begin:{} end:{}'.format(begin, end)
    period = '&d=' + (
        str)(int(end[1]) - 1) + '&e=' + end[2] + '&f=' + end[0] + '&a=' + (
            str)(int(begin[1]) - 1) + '&b=' + begin[2] + '&c=' + begin[0]
    url = 'http://ichart.finance.yahoo.com/table.csv?s=' + code2 + period
    logger.debug(url)

    #check whether data is update to latest
    #from stocktrace.dao.stockdao import findLastUpdate
    #from stocktrace.dao.stockdao import findOldestUpdate
    lastStock = findLastUpdate(code)
    oldestStock = findOldestUpdate(code)

    page = parse(url).getroot()
    if page is None:
        logger.error('Fail to download history data for:{}'.format(url))
    logger.debug(page)
    result = etree.tostring(page)
    #print result
    lines = result.split('\n')

    from stocktrace.stock import Stock
    historyDatas = []

    for a in lines:
        if a.find('html') != -1:
            continue
        #print etree.tostring(tree)

        datas = a.split(',')
        #print datas
        stock = Stock(code)
        stock.date = datas[0]
        stock.high = float(datas[2])
        stock.low = float(datas[3])
        stock.open_price = float(datas[1])
        stock.close = float(datas[4])
        stock.volume = float(datas[5])

        isNewData = True
        if lastStock is not None:
            isNewData = (stock.date > lastStock['date']) or (
                stock.date < oldestStock['date'])
        #print stock.date+'***isNewData***'+str(isNewData)
        if isNewData and save:
            # saveStock(stock)
            stock.save()
        #print stock
        historyDatas.append(stock)
    historyDatas.sort(key=lambda item: item.date, reverse=True)

    if len(historyDatas) == 0:
        logger.warning("No data downloaded for " + code)
    else:
        # update_week52(code)
        logger.info(
            str(len(historyDatas)) + " history Data downloaded for " + code)
Example #5
0
def getCSVHistorialData(code='600276', save=True, beginDate='', endDate =str(date.today())):
    from lxml import etree
    #yahoo stock ticker need post-fix ".SS" for Shanghai,'.SZ' for shenzheng
    if len(code) == 9:
        code2 = code
    elif code.startswith('6'):
        code2 = code +".SS"
    else:
        code2 = code +".SZ"

    begin = beginDate.split('-')
    end = endDate.split('-')
    print 'begin:{} end:{}'.format(begin, end)
    period = '&d='+(str)(int(end[1])-1)+'&e='+end[2]+'&f='+end[0]+'&a='+(str)(int(begin[1])-1)+'&b='+begin[2]+'&c='+begin[0]    
    url = 'http://ichart.finance.yahoo.com/table.csv?s='+code2+period
    logger.debug(url)
    
    #check whether data is update to latest
    #from stocktrace.dao.stockdao import findLastUpdate
    #from stocktrace.dao.stockdao import findOldestUpdate
    lastStock = findLastUpdate(code)
    oldestStock = findOldestUpdate(code)
            
    page = parse(url).getroot()
    if page is None:
        logger.error('Fail to download history data for:{}'.format(url))
    logger.debug(page)
    result = etree.tostring(page)
    #print result
    lines = result.split('\n')
    
    from stocktrace.stock import Stock
    historyDatas = []

    for a in lines:  
        if a.find('html')!= -1:
            continue        
        #print etree.tostring(tree) 

        datas = a.split(',')
        #print datas
        stock = Stock(code)           
        stock.date = datas[0]
        stock.high = float(datas[2])
        stock.low = float(datas[3])  
        stock.open_price = float(datas[1])
        stock.close = float(datas[4])
        stock.volume = float(datas[5])
        
        isNewData = True
        if lastStock is not None:            
            isNewData = (stock.date > lastStock['date']) or (stock.date < oldestStock['date'])
        #print stock.date+'***isNewData***'+str(isNewData)
        if isNewData and save:  
            # saveStock(stock)
            stock.save()
        #print stock    
        historyDatas.append(stock) 
    historyDatas.sort(key=lambda item: item.date, reverse=True)
    
    if len(historyDatas) == 0:
        logger.warning("No data downloaded for "+code)
    else:
        # update_week52(code)
        logger.info(str(len(historyDatas))+" history Data downloaded for "+code)