def update_stock_id(request): StockType = [2, 4] cnt = 0 for i in xrange(0, len(StockType)): url = "http://isin.twse.com.tw/isin/C_public.jsp?strMode=" + str( StockType[i]) webcode = urllib.urlopen(url) if webcode.code != 200: return HttpResponse("Update failed") req = urllib2.Request(url) response = urllib2.urlopen(req) soup = BeautifulSoup(response, from_encoding="big-5") datas = soup.find('tr') while (datas.next_sibling): data = datas.next_sibling.td.next try: if data.next.next_sibling.next_sibling.next_sibling.next_sibling.string.split()[0] == 'ESVUFR' or\ data.next.next_sibling.next_sibling.next_sibling.next_sibling.string.split()[0] == 'ESVTFR': symbol, name = data.split() name = name.encode('utf8') print symbol listing_date = datetime.datetime.strptime( data.next.next_sibling.string.split()[0], "%Y/%m/%d").date() market_type = data.next.next_sibling.next_sibling.string.split( )[0] company_type = data.next.next_sibling.next_sibling.next_sibling.string.split( )[0] company_type = company_type.encode('utf8') stockid = StockId(symbol=symbol, name=name, market_type=market_type, company_type=company_type, listing_date=listing_date) stockid.save() cnt += 1 datas = datas.next_sibling except: datas = datas.next_sibling updateManagement = UpdateManagement(name="stockID", last_update_date=datetime.date.today(), last_data_date=datetime.date.today(), notes="There is " + str(cnt) + " stockIds") updateManagement.save() json_obj = json.dumps({ "updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": updateManagement.last_data_date.strftime("%y-%m-%d"), "notes": updateManagement.notes }) return HttpResponse(json_obj, content_type="application/json")
def update_price(request): stockids = StockId.objects.all() symbol_cnt = 0; today = datetime.today() last_monday = today - timedelta(days=today.weekday()) if 'date' in request.GET: date = request.GET['date'] last_monday = datetime.strptime(date, '%Y-%m-%d') for stockid in stockids: print ('start update {0} history price'.format(stockid.symbol)) lastest_price_date = WeekPrice.objects.filter(symbol=stockid.symbol).aggregate(Max('date')) if lastest_price_date['date__max'] == None: pass elif last_monday.date() <= lastest_price_date['date__max']: continue url = 'http://jsjustweb.jihsun.com.tw/Z/ZC/ZCW/czkc1.djbcd?a=' + stockid.symbol + '&b=W&c=2880&E=1&ver=5' response = urllib.request.urlopen(url) datas = response.read().split() if (len(datas) >=6): dates = datas[0].split(b',') opens = datas[1].split(b',') highs = datas[2].split(b',') lows = datas[3].split(b',') closes = datas[4].split(b',') volumes = datas[5].split(b',') else: continue cnt = 0 for i in range(len(dates)): priceObj = WeekPrice() priceObj.surrogate_key = stockid.symbol + '_' + str(dates[i].decode('utf-8')).replace('/','-') priceObj.date = datetime.strptime(str(dates[i], 'utf-8'), '%Y/%m/%d').date() priceObj.symbol = stockid.symbol priceObj.open_price = Decimal(str(opens[i], 'utf-8')) priceObj.high_price = Decimal(str(highs[i], 'utf-8')) priceObj.low_price = Decimal(str(lows[i], 'utf-8')) priceObj.close_price = Decimal(str(closes[i], 'utf-8')) priceObj.volume = Decimal(str(volumes[i], 'utf-8')) priceObj.save() cnt = cnt + 1 symbol_cnt = symbol_cnt + 1 print ('update {0} history price, there has {1} datas'.format(stockid.symbol, cnt)) #lastDate = WeekPrice.objects.all().aggregate(Max('date'))['date__max'] lastDateDataCnt = WeekPrice.objects.filter(date__gte=last_monday).count() updateManagement = UpdateManagement(name='wp', last_update_date = datetime.today(), last_data_date = last_monday, notes = "There is " + str(lastDateDataCnt) + " week price after " + last_monday.strftime("%Y-%m-%d")) updateManagement.save() json_obj = json.dumps({'dataDate': last_monday.strftime("%Y-%m-%d"), 'notes': 'update ' + str(lastDateDataCnt) + ' data in ' + last_monday.strftime("%Y-%m-%d")}) return HttpResponse(json_obj, content_type="application/json")
def update_stock_id(request): StockType = [2, 4] cnt = 0 for i in xrange(0, len(StockType)): url = "http://isin.twse.com.tw/isin/C_public.jsp?strMode=" + str(StockType[i]) webcode = urllib.urlopen(url) if webcode.code != 200: return HttpResponse("Update failed") req = urllib2.Request(url) response = urllib2.urlopen(req) soup = BeautifulSoup(response, from_encoding="big-5") datas = soup.find('tr') while(datas.next_sibling): data = datas.next_sibling.td.next try: if data.next.next_sibling.next_sibling.next_sibling.next_sibling.string.split()[0] == 'ESVUFR' or\ data.next.next_sibling.next_sibling.next_sibling.next_sibling.string.split()[0] == 'ESVTFR': symbol,name = data.split() name = name.encode('utf8') print symbol listing_date = datetime.datetime.strptime(data.next.next_sibling.string.split()[0], "%Y/%m/%d").date() market_type = data.next.next_sibling.next_sibling.string.split()[0] company_type = data.next.next_sibling.next_sibling.next_sibling.string.split()[0] company_type = company_type.encode('utf8') stockid = StockId(symbol = symbol, name = name, market_type = market_type, company_type = company_type, listing_date = listing_date) stockid.save() cnt += 1 datas = datas.next_sibling except: datas = datas.next_sibling updateManagement = UpdateManagement(name = "stockID", last_update_date = datetime.date.today(), last_data_date = datetime.date.today(), notes="There is " + str(cnt) + " stockIds") updateManagement.save() json_obj = json.dumps({"updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": updateManagement.last_data_date.strftime("%y-%m-%d"), "notes": updateManagement.notes}) return HttpResponse(json_obj, content_type="application/json")
def update_month_revenue(request): today = datetime.date.today() year = today.year month = today.month if month == 1: year = year - 1 month = 12 else: month = month - 1 if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_month = date.split('-') year = int(str_year) month = int(str_month) except: json_obj = json.dumps({"notes": "please input correct date 'yyyy-mm'"}) return HttpResponse(json_obj, content_type="application/json") market = ['otc', 'sii'] updateCnt = 0 for i in range(len(market)): for j in [0, 1]: # url example http://mops.twse.com.tw/t21/sii/t21sc03_99_1.html if year >= 2015: url = "http://mops.twse.com.tw/nas/t21/" + market[i] + "/t21sc03_" + str(year-1911) + "_" + str(month) + "_" + str(j) +".html" elif year == 2014 and month >= 11: url = "http://mops.twse.com.tw/nas/t21/" + market[i] + "/t21sc03_" + str(year-1911) + "_" + str(month) + "_" + str(j) +".html" else: url = "http://mops.twse.com.tw/t21/" + market[i] + "/t21sc03_" + str(year-1911) + "_" + str(month) + "_" + str(j) + ".html" req = urllib2.Request(url) try: response = urllib2.urlopen(req) except URLError as e: if hasattr(e, 'reason'): json_obj = json.dumps({"notes": "Reason: " + str(e.reason)}) return HttpResponse(json_obj, content_type="application/json") elif hasattr(e, 'code'): json_obj = json.dumps({"notes": "Error code:" + e.code}) return HttpResponse(json_obj, content_type="application/json") soup = BeautifulSoup(response, from_encoding="utf-8") datas = soup.find_all('td', {'align':'center'}) for data in datas: if data.string: if data.string != '-': revenue = MonthRevenue() revenue.surrogate_key = data.string + "_" + str(year) + str(month).zfill(2) revenue.year = year revenue.month = month revenue.date = datetime.date(year, month, 1) revenue.symbol = data.string revenue_data = data.next_sibling.next_sibling if is_decimal(st_to_decimal(revenue_data.string)): revenue.revenue = revenue_data.string.strip().replace(',', '') last_year_revenue_data = revenue_data.next_sibling.next_sibling if is_decimal(last_year_revenue_data.string.strip().replace(',', '')): revenue.last_year_revenue = last_year_revenue_data.string.strip().replace(',', '') month_growth_rate_data = last_year_revenue_data.next_sibling if is_decimal(month_growth_rate_data.string.strip().replace(',', '')): revenue.month_growth_rate = month_growth_rate_data.string.strip().replace(',', '') year_growth_rate_data = month_growth_rate_data.next_sibling if is_decimal(year_growth_rate_data.string.strip().replace(',', '')): revenue.year_growth_rate = year_growth_rate_data.string.strip().replace(',', '') acc_revenue_data = year_growth_rate_data.next_sibling if is_decimal(acc_revenue_data.string.strip().replace(',', '')): revenue.acc_revenue = acc_revenue_data.string.strip().replace(',', '') last_acc_revenue_data = acc_revenue_data.next_sibling if is_decimal(last_acc_revenue_data.string.strip().replace(',', '')): revenue.last_acc_revenue = last_acc_revenue_data.string.strip().replace(',', '') acc_year_growth_rate_data = last_acc_revenue_data.next_sibling if is_decimal(acc_year_growth_rate_data.string.strip().replace(',', '')): revenue.acc_year_growth_rate = acc_year_growth_rate_data.string.strip().replace(',', '') print (revenue.symbol) updateCnt = updateCnt + 1 revenue.save() print ("update %d symbol") %updateCnt cnt = MonthRevenue.objects.filter(year=year, month=month).count() lastDate = MonthRevenue.objects.all().aggregate(Max('date'))['date__max'] lastDateDataCnt = MonthRevenue.objects.filter(date=lastDate).count() updateManagement = UpdateManagement(name = "mr", last_update_date = datetime.date.today(), last_data_date = lastDate, notes="There is " + str(lastDateDataCnt) + " datas") updateManagement.save() json_obj = json.dumps({"name": updateManagement.name, "updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": lastDate.strftime("%y-%m-%d"), "notes": "Update " + str(cnt) + " month revenue on " + str(year) + "-" + str(month)}) return HttpResponse(json_obj, content_type="application/json")
def update_season_revenue(request): if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_season = date.split('-') year = int(str_year) season = int(str_season) except: json_obj = json.dumps({"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") else: json_obj = json.dumps({"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") else: json_obj = json.dumps({"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") if season == 1: startMonth = 1 elif season == 2: startMonth = 4 elif season == 3: startMonth = 7 elif season == 4: startMonth = 10 else: json_obj = json.dumps({"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") firtMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth).values_list('symbol', flat=True) secondMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth+1).values_list('symbol', flat=True) thirdMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth+2).values_list('symbol', flat=True) firstMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth) secondMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth+1) thirdMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth+2) date = datetime.date(year, startMonth, 1) lastYear, lastSeason = last_season(date) lastSeasonRevenues = SeasonRevenue.objects.filter(year=lastYear, season=lastSeason) symbols = list(set(firtMonthStockIds).intersection(set(secondMonthStockIds)).intersection(set(thirdMonthStockIds))) for symbol in symbols: print symbol revenue = SeasonRevenue() revenue.surrogate_key = symbol + '_' + str(year) + str(season).zfill(2) revenue.year = year revenue.season = season revenue.date = date revenue.symbol = symbol try: revenue.revenue = firstMonthRevenues.get(symbol=symbol).revenue +\ secondMonthRevenues.get(symbol=symbol).revenue +\ thirdMonthRevenues.get(symbol=symbol).revenue revenue.last_year_revenue = firstMonthRevenues.get(symbol=symbol).last_year_revenue +\ secondMonthRevenues.get(symbol=symbol).last_year_revenue +\ thirdMonthRevenues.get(symbol=symbol).last_year_revenue if revenue.last_year_revenue > 0: revenue.year_growth_rate = revenue.revenue / revenue.last_year_revenue * 100 - 100 if lastSeasonRevenues.filter(symbol=symbol): last_season_revenue = lastSeasonRevenues.get(symbol=symbol).revenue if last_season_revenue > 0: revenue.season_growth_rate = revenue.revenue / last_season_revenue * 100 - 100 revenue.acc_revenue = thirdMonthRevenues.get(symbol=symbol).acc_revenue revenue.acc_year_growth_rate = thirdMonthRevenues.get(symbol=symbol).acc_year_growth_rate revenue.save() except: pass cnt = SeasonRevenue.objects.filter(year=year, season=season).count() lastDate = SeasonRevenue.objects.all().aggregate(Max('date'))['date__max'] if lastDate == None: json_obj = json.dumps({"notes": "There is no data in SeasonRevenue"}) return HttpResponse(json_obj, content_type="application/json") lastDateDataCnt = SeasonRevenue.objects.filter(date=lastDate).count() updateManagement = UpdateManagement(name = "sr", last_update_date = datetime.date.today(), last_data_date = lastDate, notes="There is " + str(lastDateDataCnt) + " datas") updateManagement.save() json_obj = json.dumps({"name": updateManagement.name, "updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": lastDate.strftime("%y-%m-%d"), "notes": "Update " + str(cnt) + " season revenue on " + str(year) + "-" + str(season)}) return HttpResponse(json_obj, content_type="application/json")
def update_wawa_value_line(request): if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_season = date.split('-') year = int(str_year) season = int(str_season) except: return HttpResponse( "please input correct season 'year-season'") else: return HttpResponse("please input correct season 'year-season'") else: return HttpResponse("please input correct season 'year-season'") symbols = WatchList.objects.values_list('symbol', flat=True).distinct() for symbol in symbols: print("start update " + symbol + "'s value line date:" + str_year + "-" + str_season) value_line = WawaValueLine() value_line.surrogate_key = symbol + '_' + str_year value_line.symbol = symbol value_line.year = year value_line.season = season value_line.date = util.season_to_date(year, season) if not SeasonFinancialRatio.objects.filter( symbol=symbol, year=year, season=season): print(symbol + "'s sfr is empty date:" + str_year + "-" + str_season) continue sfrs = SeasonFinancialRatio.objects.filter( symbol=symbol).order_by('-date') if (len(sfrs) >= 4): value_line.last_year_eps = sfrs[0].earnings_per_share + sfrs[1].earnings_per_share + \ sfrs[2].earnings_per_share + sfrs[3].earnings_per_share else: continue yfrs = YearFinancialRatio.objects.filter( symbol=symbol).order_by('-date') len_yfrs = len(yfrs) value_line.future_eps_growth = Decimal(0.01) if (len_yfrs >= 6): if (yfrs[5].earnings_per_share > 0): if (yfrs[0].earnings_per_share > yfrs[5].earnings_per_share): value_line.future_eps_growth = ( yfrs[0].earnings_per_share / yfrs[5].earnings_per_share)**(Decimal(1) / 5) - 1 elif (yfrs[1].earnings_per_share > yfrs[5].earnings_per_share): value_line.future_eps_growth = ( yfrs[1].earnings_per_share / yfrs[5].earnings_per_share)**(Decimal(1) / 4) - 1 elif (yfrs[4].earnings_per_share > 0): if (yfrs[0].earnings_per_share > yfrs[4].earnings_per_share): value_line.future_eps_growth = ( yfrs[0].earnings_per_share / yfrs[4].earnings_per_share)**(Decimal(1) / 4) - 1 elif (yfrs[1].earnings_per_share > yfrs[4].earnings_per_share): value_line.future_eps_growth = ( yfrs[1].earnings_per_share / yfrs[4].earnings_per_share)**(Decimal(1) / 3) - 1 elif (len_yfrs <= 1): value_line.future_eps_growth = Decimal(0.01) else: if (yfrs[len_yfrs - 1].earnings_per_share > 0): if (yfrs[0].earnings_per_share > yfrs[len_yfrs - 1].earnings_per_share): value_line.future_eps_growth = ( yfrs[0].earnings_per_share / yfrs[len_yfrs - 1].earnings_per_share)**( Decimal(1) / (len_yfrs - 1)) - 1 elif (yfrs[1].earnings_per_share > yfrs[len_yfrs - 1].earnings_per_share): value_line.future_eps_growth = ( yfrs[1].earnings_per_share / yfrs[len_yfrs - 1].earnings_per_share)**( Decimal(1) / (len_yfrs - 2)) - 1 elif (yfrs[len_yfrs - 2].earnings_per_share > 0): if (yfrs[0].earnings_per_share > yfrs[len_yfrs - 2].earnings_per_share): value_line.future_eps_growth = ( yfrs[0].earnings_per_share / yfrs[len_yfrs - 2].earnings_per_share)**( Decimal(1) / (len_yfrs - 2)) - 1 elif (yfrs[1].earnings_per_share > yfrs[len_yfrs - 2].earnings_per_share): value_line.future_eps_growth = ( yfrs[1].earnings_per_share / yfrs[len_yfrs - 2].earnings_per_share)**( Decimal(1) / (len_yfrs - 3)) - 1 avg_pes = AvgPE.objects.filter(symbol=symbol).order_by('-year') if (len(avg_pes) >= 6): value_line.past_pe = (avg_pes[0].pe + avg_pes[1].pe + avg_pes[2].pe + \ avg_pes[3].pe + avg_pes[4].pe) / 5 elif (len(avg_pes) > 0): total_pe = 0 for avg_pe in avg_pes: total_pe += avg_pe.pe value_line.past_pe = total_pe / (len(avg_pes)) else: value_line.past_pe = 0 if value_line.past_pe > 1000: value_line.past_pe = 0 if (value_line.future_eps_growth > 0.3): value_line.future_eps_growth = Decimal(0.3) value_line.estimate_eps = value_line.last_year_eps * ( Decimal(value_line.future_eps_growth + 1)**10) value_line.estimate_future_price = value_line.estimate_eps * value_line.past_pe value_line.estimate_price = value_line.estimate_future_price / ( Decimal(1.1)**10) value_line.hold_price = value_line.estimate_price * Decimal(0.8) dividends = Dividend.objects.filter(symbol=symbol, year__gte=year - 5) total_dividend = 0 if (len(dividends) > 0): for dividend in dividends: total_dividend += dividend.total_dividends value_line.avg_dividend = total_dividend / len(dividends) else: value_line.avg_dividend = 0 value_line.low_price = 16 * value_line.avg_dividend value_line.high_price = 32 * value_line.avg_dividend one_year_dividend = Dividend.objects.filter( symbol=symbol).order_by('-date') if (len(one_year_dividend) > 0): value_line.one_low_price = 16 * one_year_dividend[0].total_dividends else: value_line.one_low_price = 0 value_line.recovery_years = 100 if (value_line.future_eps_growth > 0): eps_growth = value_line.future_eps_growth + 1 total_value = 0 for i in range(1, 15): total_value += value_line.last_year_eps * Decimal(eps_growth** i) if total_value > value_line.hold_price: value_line.recovery_years = i break try: value_line.save() except: print(symbol + "'s value line save error") cnt = WawaValueLine.objects.filter(year=year, season=season).count() print('There is ' + str(cnt) + ' datas') lastDate = WawaValueLine.objects.all().aggregate(Max('date'))['date__max'] lastDateDataCnt = WawaValueLine.objects.filter(date=lastDate).count() updateManagement = UpdateManagement( name='waValue', last_update_date=datetime.date.today(), last_data_date=lastDate, notes="There is " + str(lastDateDataCnt) + " wawa_value_line in " + lastDate.strftime("%Y-%m-%d")) updateManagement.save() json_obj = json.dumps({ 'dataDate': lastDate.strftime("%Y-%m-%d"), 'notes': 'update ' + str(cnt) + ' data in ' + str(year) + '-' + str(season) }) return HttpResponse(json_obj, content_type="application/json")
def update_wawa_growth_power(request): print('start update wawa growth power') if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_season = date.split('-') year = int(str_year) season = int(str_season) except: return HttpResponse( "please input correct season 'year-season'") else: return HttpResponse("please input correct season 'year-season'") else: return HttpResponse("please input correct season 'year-season'") stockids = WatchList.objects.values_list('symbol', flat=True).distinct() for stockid in stockids: #print("start " + stockid + "'s wawa growth power date:" + str_year + "-" + str_season) wawa_growth = WawaGrowthPower() wawa_growth.symbol = stockid wawa_growth.year = year wawa_growth.season = season wawa_growth.date = util.season_to_date(year, season) wawa_growth.surrogate_key = stockid + '_' + str(year) + str( season).zfill(2) if not SeasonFinancialRatio.objects.filter( symbol=stockid, year=year, season=season): print(stockid + "'s sfr is empty date:" + str_year + "-" + str_season) continue if not SeasonFinancialRatio.objects.filter( symbol=stockid, year=year - 1, season=season): print(stockid + "'s sfr is empty date:" + str(year - 1) + "-" + str_season) continue if not YearFinancialRatio.objects.filter(symbol=stockid, year=year - 1): print(stockid + "'s yfr is empty year:" + str(year - 1)) continue if season == 1: financial_ratio = SeasonFinancialRatio.objects.get(symbol=stockid, year=year, season=season) wawa_growth.season_eps = financial_ratio.earnings_per_share wawa_growth.estimate_eps = wawa_growth.season_eps * 4 wawa_growth.last_year_season_eps = SeasonFinancialRatio.objects.get( symbol=stockid, year=year - 1, season=season).earnings_per_share wawa_growth.last_year_eps = YearFinancialRatio.objects.get( symbol=stockid, year=year - 1).earnings_per_share if not wawa_growth.last_year_eps: continue wawa_growth.estimate_growth_rate = wawa_growth.estimate_eps / wawa_growth.last_year_eps - 1 if (wawa_growth.estimate_growth_rate > 0.4): wawa_growth.estimate_growth_rate = Decimal(0.4) wawa_growth.reasonable_price = wawa_growth.estimate_growth_rate * Decimal( 66) * wawa_growth.last_year_eps wawa_growth.save() elif season == 2: financial_ratio1 = SeasonFinancialRatio.objects.get(symbol=stockid, year=year, season=season - 1) financial_ratio2 = SeasonFinancialRatio.objects.get(symbol=stockid, year=year, season=season) wawa_growth.season_eps = financial_ratio2.earnings_per_share wawa_growth.estimate_eps = ( financial_ratio1.earnings_per_share + financial_ratio2.earnings_per_share) * 2 wawa_growth.last_year_season_eps = SeasonFinancialRatio.objects.get( symbol=stockid, year=year - 1, season=season).earnings_per_share wawa_growth.last_year_eps = YearFinancialRatio.objects.get( symbol=stockid, year=year - 1).earnings_per_share if not wawa_growth.last_year_eps: continue wawa_growth.estimate_growth_rate = wawa_growth.estimate_eps / wawa_growth.last_year_eps - 1 if (wawa_growth.estimate_growth_rate > 0.4): wawa_growth.estimate_growth_rate = Decimal(0.4) wawa_growth.reasonable_price = wawa_growth.estimate_growth_rate * Decimal( 66) * wawa_growth.last_year_eps wawa_growth.save() elif season == 3: financial_ratio1 = SeasonFinancialRatio.objects.get(symbol=stockid, year=year, season=season - 2) financial_ratio2 = SeasonFinancialRatio.objects.get(symbol=stockid, year=year, season=season - 1) financial_ratio3 = SeasonFinancialRatio.objects.get(symbol=stockid, year=year, season=season) wawa_growth.season_eps = financial_ratio3.earnings_per_share wawa_growth.estimate_eps = ( financial_ratio1.earnings_per_share + financial_ratio2.earnings_per_share + financial_ratio3.earnings_per_share) * 4 / 3 wawa_growth.last_year_season_eps = SeasonFinancialRatio.objects.get( symbol=stockid, year=year - 1, season=season).earnings_per_share wawa_growth.last_year_eps = YearFinancialRatio.objects.get( symbol=stockid, year=year - 1).earnings_per_share if not wawa_growth.last_year_eps: continue wawa_growth.estimate_growth_rate = wawa_growth.estimate_eps / wawa_growth.last_year_eps - 1 if (wawa_growth.estimate_growth_rate > 0.4): wawa_growth.estimate_growth_rate = Decimal(0.4) wawa_growth.reasonable_price = wawa_growth.estimate_growth_rate * Decimal( 66) * wawa_growth.last_year_eps wawa_growth.save() elif season == 4: try: financial_ratio1 = SeasonFinancialRatio.objects.get( symbol=stockid, year=year, season=season - 3) financial_ratio2 = SeasonFinancialRatio.objects.get( symbol=stockid, year=year, season=season - 2) financial_ratio3 = SeasonFinancialRatio.objects.get( symbol=stockid, year=year, season=season - 1) financial_ratio4 = SeasonFinancialRatio.objects.get( symbol=stockid, year=year, season=season) wawa_growth.season_eps = financial_ratio4.earnings_per_share wawa_growth.estimate_eps = financial_ratio1.earnings_per_share + financial_ratio2.earnings_per_share + \ financial_ratio3.earnings_per_share + financial_ratio4.earnings_per_share wawa_growth.last_year_season_eps = SeasonFinancialRatio.objects.get( symbol=stockid, year=year - 1, season=season).earnings_per_share wawa_growth.last_year_eps = YearFinancialRatio.objects.get( symbol=stockid, year=year - 1).earnings_per_share except: continue if not wawa_growth.last_year_eps: continue wawa_growth.estimate_growth_rate = wawa_growth.estimate_eps / wawa_growth.last_year_eps - 1 if (wawa_growth.estimate_growth_rate > 0.4): wawa_growth.estimate_growth_rate = Decimal(0.4) wawa_growth.reasonable_price = wawa_growth.estimate_growth_rate * Decimal( 66) * wawa_growth.estimate_eps wawa_growth.save() #print("update " + stockid + "'s wawa growth power date:" + str_year + "-" + str_season) cnt = WawaGrowthPower.objects.filter(year=year, season=season).count() print('There is ' + str(cnt) + ' datas') lastDate = WawaGrowthPower.objects.all().aggregate( Max('date'))['date__max'] lastDateDataCnt = WawaGrowthPower.objects.filter(date=lastDate).count() updateManagement = UpdateManagement( name='waGrowth', last_update_date=datetime.date.today(), last_data_date=lastDate, notes="There is " + str(lastDateDataCnt) + " wawa_growth in " + lastDate.strftime("%Y-%m-%d")) updateManagement.save() json_obj = json.dumps({ 'dataDate': lastDate.strftime("%Y-%m-%d"), 'notes': 'update ' + str(cnt) + ' data in ' + str(year) + '-' + str(season) }) return HttpResponse(json_obj, content_type="application/json")
def update_vk_growth_power(request): print('start update vk growth power') if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_season = date.split('-') year = int(str_year) season = int(str_season) except: return HttpResponse( "please input correct season 'year-season'") else: return HttpResponse("please input correct season 'year-season'") else: return HttpResponse("please input correct season 'year-season'") stockids = WatchList.objects.values_list('symbol', flat=True).distinct() for stockid in stockids: vk_growth = VKGrowthPower() vk_growth.symbol = stockid vk_growth.year = year vk_growth.season = season vk_growth.date = util.season_to_date(year, season) vk_growth.surrogate_key = stockid + '_' + str(year) + str( season).zfill(2) if not SeasonFinancialRatio.objects.filter( symbol=stockid, year=year, season=season): print(stockid + "'s sfr is empty date:" + str_year + "-" + str_season) continue financial_ratios = SeasonFinancialRatio.objects.filter( symbol=stockid).order_by('-date') if (len(financial_ratios) >= 8): financial_ratio = financial_ratios[0] financial_ratio1 = financial_ratios[1] financial_ratio2 = financial_ratios[2] financial_ratio3 = financial_ratios[3] financial_ratio4 = financial_ratios[4] financial_ratio5 = financial_ratios[5] financial_ratio6 = financial_ratios[6] financial_ratio7 = financial_ratios[7] if not financial_ratio.earnings_per_share or not financial_ratio1.earnings_per_share or \ not financial_ratio2.earnings_per_share or not financial_ratio3.earnings_per_share or \ not financial_ratio4.earnings_per_share or not financial_ratio5.earnings_per_share or \ not financial_ratio6.earnings_per_share or not financial_ratio7.earnings_per_share: continue vk_growth.season_eps = financial_ratio.earnings_per_share vk_growth.estimate_eps = financial_ratio.earnings_per_share + financial_ratio1.earnings_per_share + \ financial_ratio2.earnings_per_share + financial_ratio3.earnings_per_share vk_growth.last_year_season_eps = financial_ratio4.earnings_per_share vk_growth.last_year_eps = financial_ratio4.earnings_per_share + financial_ratio5.earnings_per_share + \ financial_ratio6.earnings_per_share + financial_ratio7.earnings_per_share vk_growth.estimate_growth_rate = vk_growth.estimate_eps / vk_growth.last_year_eps - 1 if (vk_growth.estimate_growth_rate > 0.4): vk_growth.estimate_growth_rate = Decimal(0.4) vk_growth.reasonable_price = vk_growth.estimate_growth_rate * 66 * vk_growth.estimate_eps vk_growth.save() print("update " + stockid + "'s vk growth power date:" + str_year + "-" + str_season) else: print(stockid + "'s data not enough to update vk growth power") cnt = VKGrowthPower.objects.filter(year=year, season=season).count() print('There is ' + str(cnt) + ' datas') lastDate = VKGrowthPower.objects.all().aggregate(Max('date'))['date__max'] lastDateDataCnt = VKGrowthPower.objects.filter(date=lastDate).count() updateManagement = UpdateManagement( name='vkGrowth', last_update_date=datetime.date.today(), last_data_date=lastDate, notes="There is " + str(lastDateDataCnt) + " vk_growth in " + lastDate.strftime("%Y-%m-%d")) updateManagement.save() json_obj = json.dumps({ 'dataDate': lastDate.strftime("%Y-%m-%d"), 'notes': 'update ' + str(cnt) + ' data in ' + str(year) + '-' + str(season) }) return HttpResponse(json_obj, content_type="application/json")
tds = tr.find_all('td') if len(tds) == 7: if tds[5].string == 'ESVUFR' or tds[5].string == 'ESVTFR': symbol, name = tds[0].string.split() symbol = symbol.strip() name = name.strip() listing_date = datetime.datetime.strptime(tds[2].string.strip(), "%Y/%m/%d").date() company_type = tds[4].string.strip() stockid = StockId(symbol = symbol, name = name, market_type = market, company_type = company_type, listing_date = listing_date) if symbol is not None: stockid.save() cnt += 1 print symbol updateManagement = UpdateManagement(name = "stockID", last_update_date = datetime.date.today(), last_data_date = datetime.date.today(), notes="There is " + str(cnt) + " stockIds") updateManagement.save() json_obj = json.dumps({"updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": updateManagement.last_data_date.strftime("%y-%m-%d"), "notes": updateManagement.notes}) return HttpResponse(json_obj, content_type="application/json") def test_month_revenue(request): lastDate = MonthRevenue.objects.all().aggregate(Max('date'))['date__max'] return HttpResponse(lastDate['date__max']) def update_month_revenue(request): today = datetime.date.today() year, month = last_month(today) if "date" in request.GET: date = request.GET["date"] try:
def update_pivotal_state(request): stock_ids = StockId.objects.all() for stock_id in stock_ids: cnt = 0 pivotal_point_count = PivotalPoint.objects.filter(symbol=stock_id.symbol).count() print("start update {0} pivotal".format(stock_id.symbol)) if pivotal_point_count < 10: stock_prices = WeekPrice.objects.filter(symbol=stock_id.symbol).order_by('date') if stock_prices.count() == 0: print ("update {0} pivotal error, there is no price data".format(stock_id)) continue pivotal_state = InitPivotalState(date=stock_prices[0].date.strftime('%Y-%m-%d'), price=0, symbol=stock_id.symbol, prev_state='init_pivotal_state', upward_trend=0 ,\ downward_trend=0, natural_reaction=0, natural_rally=0, secondary_rally=0, secondary_reaction=0) for stock_price in stock_prices: cnt += 1 pivotal_state = pivotal_state.next(stock_price.close_price, stock_price.date.strftime('%Y-%m-%d')) pivotal_state.save_to_db() print ('update {0} pivotal state, there has {1} datas'.format(stock_id.symbol, cnt)) else: pivotal_state = PivotalPoint.objects.filter(symbol=stock_id.symbol).order_by("-date")[9] stock_prices = WeekPrice.objects.filter(symbol=stock_id.symbol, date__gte=pivotal_state.date).order_by("date") if (pivotal_state.date != stock_prices[0].date): print ("update {0} pivotal error date is not the same".format(stock_id)) else: if pivotal_state.state == INIT_PIVOTAL_STATE: pivotal_state = InitPivotalState(date=pivotal_state.date.strftime('%Y-%m-%d'), price=pivotal_state.price, symbol=stock_id.symbol, prev_state=pivotal_state.prev_state, \ upward_trend=pivotal_state.upward_trend_point , downward_trend=pivotal_state.downward_trend_point, natural_reaction=pivotal_state.natural_reaction_point, \ natural_rally=pivotal_state.natural_rally_point, secondary_rally=pivotal_state.secondary_rally_point, secondary_reaction=pivotal_state.secondary_reaction_point) elif pivotal_state.state == UPWARD_TREND_STATE: pivotal_state = UpwardTrendState(date=pivotal_state.date.strftime('%Y-%m-%d'), price=pivotal_state.price, symbol=stock_id.symbol, prev_state=pivotal_state.prev_state, \ upward_trend=pivotal_state.upward_trend_point , downward_trend=pivotal_state.downward_trend_point, natural_reaction=pivotal_state.natural_reaction_point, \ natural_rally=pivotal_state.natural_rally_point, secondary_rally=pivotal_state.secondary_rally_point, secondary_reaction=pivotal_state.secondary_reaction_point) elif pivotal_state.state == DOWNWARD_TREND_STATE: pivotal_state = DownwardTrendState(date=pivotal_state.date.strftime('%Y-%m-%d'), price=pivotal_state.price, symbol=stock_id.symbol, prev_state=pivotal_state.prev_state, \ upward_trend=pivotal_state.upward_trend_point , downward_trend=pivotal_state.downward_trend_point, natural_reaction=pivotal_state.natural_reaction_point, \ natural_rally=pivotal_state.natural_rally_point, secondary_rally=pivotal_state.secondary_rally_point, secondary_reaction=pivotal_state.secondary_reaction_point) elif pivotal_state.state == NATURAL_RALLY_STATE: pivotal_state = NaturalRallyState(date=pivotal_state.date.strftime('%Y-%m-%d'), price=pivotal_state.price, symbol=stock_id.symbol, prev_state=pivotal_state.prev_state, \ upward_trend=pivotal_state.upward_trend_point , downward_trend=pivotal_state.downward_trend_point, natural_reaction=pivotal_state.natural_reaction_point, \ natural_rally=pivotal_state.natural_rally_point, secondary_rally=pivotal_state.secondary_rally_point, secondary_reaction=pivotal_state.secondary_reaction_point) elif pivotal_state.state == NATURAL_REACTION_STATE: pivotal_state = NaturalReactionState(date=pivotal_state.date.strftime('%Y-%m-%d'), price=pivotal_state.price, symbol=stock_id.symbol, prev_state=pivotal_state.prev_state, \ upward_trend=pivotal_state.upward_trend_point , downward_trend=pivotal_state.downward_trend_point, natural_reaction=pivotal_state.natural_reaction_point, \ natural_rally=pivotal_state.natural_rally_point, secondary_rally=pivotal_state.secondary_rally_point, secondary_reaction=pivotal_state.secondary_reaction_point) elif pivotal_state.state == SECONDARY_RALLY_STATE: pivotal_state = SecondaryRallyState(date=pivotal_state.date.strftime('%Y-%m-%d'), price=pivotal_state.price, symbol=stock_id.symbol, prev_state=pivotal_state.prev_state, \ upward_trend=pivotal_state.upward_trend_point , downward_trend=pivotal_state.downward_trend_point, natural_reaction=pivotal_state.natural_reaction_point, \ natural_rally=pivotal_state.natural_rally_point, secondary_rally=pivotal_state.secondary_rally_point, secondary_reaction=pivotal_state.secondary_reaction_point) elif pivotal_state.state == SECONDARY_REACTION_STATE: pivotal_state = SecondaryReactionState(date=pivotal_state.date.strftime('%Y-%m-%d'), price=pivotal_state.price, symbol=stock_id.symbol, prev_state=pivotal_state.prev_state, \ upward_trend=pivotal_state.upward_trend_point , downward_trend=pivotal_state.downward_trend_point, natural_reaction=pivotal_state.natural_reaction_point, \ natural_rally=pivotal_state.natural_rally_point, secondary_rally=pivotal_state.secondary_rally_point, secondary_reaction=pivotal_state.secondary_reaction_point) else: print ("update {0} pivotal error: can't find state".format(stock_id)) for stock_price in stock_prices: if (stock_price.date != pivotal_state.date): pivotal_state = pivotal_state.next(stock_price.close_price, stock_price.date.strftime('%Y-%m-%d')) pivotal_state.save_to_db() # print ('update {0} pivotal state, there has {1} datas'.format(stock_id.symbol, cnt)) cnt += 1 if cnt != 11: print ('update {0} pivotal state, there has {1} datas'.format(stock_id.symbol, cnt)) lastDate = PivotalPoint.objects.all().aggregate(Max('date'))['date__max'] lastDateDataCnt = PivotalPoint.objects.filter(date=lastDate).count() updateManagement = UpdateManagement(name='kp', last_update_date = datetime.today(), last_data_date = lastDate, notes = "There is " + str(lastDateDataCnt) + " pivotal point in " + lastDate.strftime("%Y-%m-%d")) updateManagement.save() json_obj = json.dumps({'dataDate': lastDate.strftime("%Y-%m-%d"), 'notes': 'update ' + str(lastDateDataCnt) + ' data in ' + lastDate.strftime("%Y-%m-%d")}) return HttpResponse(json_obj, content_type="application/json")
def update_season_revenue(request): if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_season = date.split('-') year = int(str_year) season = int(str_season) except: json_obj = json.dumps( {"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") else: json_obj = json.dumps( {"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") else: json_obj = json.dumps( {"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") if season == 1: startMonth = 1 elif season == 2: startMonth = 4 elif season == 3: startMonth = 7 elif season == 4: startMonth = 10 else: json_obj = json.dumps( {"notes": "please input correct season 'year-season'"}) return HttpResponse(json_obj, content_type="application/json") firtMonthStockIds = MonthRevenue.objects.filter( year=year, month=startMonth).values_list('symbol', flat=True) secondMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth + 1).values_list('symbol', flat=True) thirdMonthStockIds = MonthRevenue.objects.filter(year=year, month=startMonth + 2).values_list('symbol', flat=True) firstMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth) secondMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth + 1) thirdMonthRevenues = MonthRevenue.objects.filter(year=year, month=startMonth + 2) date = datetime.date(year, startMonth, 1) lastYear, lastSeason = last_season(date) lastSeasonRevenues = SeasonRevenue.objects.filter(year=lastYear, season=lastSeason) symbols = list( set(firtMonthStockIds).intersection( set(secondMonthStockIds)).intersection(set(thirdMonthStockIds))) for symbol in symbols: print symbol revenue = SeasonRevenue() revenue.surrogate_key = symbol + '_' + str(year) + str(season).zfill(2) revenue.year = year revenue.season = season revenue.date = date revenue.symbol = symbol try: revenue.revenue = firstMonthRevenues.get(symbol=symbol).revenue +\ secondMonthRevenues.get(symbol=symbol).revenue +\ thirdMonthRevenues.get(symbol=symbol).revenue revenue.last_year_revenue = firstMonthRevenues.get(symbol=symbol).last_year_revenue +\ secondMonthRevenues.get(symbol=symbol).last_year_revenue +\ thirdMonthRevenues.get(symbol=symbol).last_year_revenue if revenue.last_year_revenue > 0: revenue.year_growth_rate = revenue.revenue / revenue.last_year_revenue * 100 - 100 if lastSeasonRevenues.filter(symbol=symbol): last_season_revenue = lastSeasonRevenues.get( symbol=symbol).revenue if last_season_revenue > 0: revenue.season_growth_rate = revenue.revenue / last_season_revenue * 100 - 100 revenue.acc_revenue = thirdMonthRevenues.get( symbol=symbol).acc_revenue revenue.acc_year_growth_rate = thirdMonthRevenues.get( symbol=symbol).acc_year_growth_rate revenue.save() except: pass cnt = SeasonRevenue.objects.filter(year=year, season=season).count() lastDate = SeasonRevenue.objects.all().aggregate(Max('date'))['date__max'] if lastDate == None: json_obj = json.dumps({"notes": "There is no data in SeasonRevenue"}) return HttpResponse(json_obj, content_type="application/json") lastDateDataCnt = SeasonRevenue.objects.filter(date=lastDate).count() updateManagement = UpdateManagement(name="sr", last_update_date=datetime.date.today(), last_data_date=lastDate, notes="There is " + str(lastDateDataCnt) + " datas") updateManagement.save() json_obj = json.dumps({ "name": updateManagement.name, "updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": lastDate.strftime("%y-%m-%d"), "notes": "Update " + str(cnt) + " season revenue on " + str(year) + "-" + str(season) }) return HttpResponse(json_obj, content_type="application/json")
def update_month_revenue(request): today = datetime.date.today() year = today.year month = today.month if month == 1: year = year - 1 month = 12 else: month = month - 1 if 'date' in request.GET: date = request.GET['date'] if date != '': try: str_year, str_month = date.split('-') year = int(str_year) month = int(str_month) except: json_obj = json.dumps( {"notes": "please input correct date 'yyyy-mm'"}) return HttpResponse(json_obj, content_type="application/json") market = ['otc', 'sii'] updateCnt = 0 for i in range(len(market)): for j in [0, 1]: # url example http://mops.twse.com.tw/t21/sii/t21sc03_99_1.html if year >= 2015: url = "http://mops.twse.com.tw/nas/t21/" + market[ i] + "/t21sc03_" + str(year - 1911) + "_" + str( month) + "_" + str(j) + ".html" elif year == 2014 and month >= 11: url = "http://mops.twse.com.tw/nas/t21/" + market[ i] + "/t21sc03_" + str(year - 1911) + "_" + str( month) + "_" + str(j) + ".html" else: url = "http://mops.twse.com.tw/t21/" + market[ i] + "/t21sc03_" + str(year - 1911) + "_" + str( month) + "_" + str(j) + ".html" req = urllib2.Request(url) try: response = urllib2.urlopen(req) except URLError as e: if hasattr(e, 'reason'): json_obj = json.dumps( {"notes": "Reason: " + str(e.reason)}) return HttpResponse(json_obj, content_type="application/json") elif hasattr(e, 'code'): json_obj = json.dumps({"notes": "Error code:" + e.code}) return HttpResponse(json_obj, content_type="application/json") soup = BeautifulSoup(response, from_encoding="utf-8") datas = soup.find_all('td', {'align': 'center'}) for data in datas: if data.string: if data.string != '-': revenue = MonthRevenue() revenue.surrogate_key = data.string + "_" + str( year) + str(month).zfill(2) revenue.year = year revenue.month = month revenue.date = datetime.date(year, month, 1) revenue.symbol = data.string revenue_data = data.next_sibling.next_sibling if is_decimal(st_to_decimal(revenue_data.string)): revenue.revenue = revenue_data.string.strip( ).replace(',', '') last_year_revenue_data = revenue_data.next_sibling.next_sibling if is_decimal( last_year_revenue_data.string.strip().replace( ',', '')): revenue.last_year_revenue = last_year_revenue_data.string.strip( ).replace(',', '') month_growth_rate_data = last_year_revenue_data.next_sibling if is_decimal( month_growth_rate_data.string.strip().replace( ',', '')): revenue.month_growth_rate = month_growth_rate_data.string.strip( ).replace(',', '') year_growth_rate_data = month_growth_rate_data.next_sibling if is_decimal( year_growth_rate_data.string.strip().replace( ',', '')): revenue.year_growth_rate = year_growth_rate_data.string.strip( ).replace(',', '') acc_revenue_data = year_growth_rate_data.next_sibling if is_decimal(acc_revenue_data.string.strip().replace( ',', '')): revenue.acc_revenue = acc_revenue_data.string.strip( ).replace(',', '') last_acc_revenue_data = acc_revenue_data.next_sibling if is_decimal( last_acc_revenue_data.string.strip().replace( ',', '')): revenue.last_acc_revenue = last_acc_revenue_data.string.strip( ).replace(',', '') acc_year_growth_rate_data = last_acc_revenue_data.next_sibling if is_decimal(acc_year_growth_rate_data.string.strip(). replace(',', '')): revenue.acc_year_growth_rate = acc_year_growth_rate_data.string.strip( ).replace(',', '') print(revenue.symbol) updateCnt = updateCnt + 1 revenue.save() print("update %d symbol") % updateCnt cnt = MonthRevenue.objects.filter(year=year, month=month).count() lastDate = MonthRevenue.objects.all().aggregate(Max('date'))['date__max'] lastDateDataCnt = MonthRevenue.objects.filter(date=lastDate).count() updateManagement = UpdateManagement(name="mr", last_update_date=datetime.date.today(), last_data_date=lastDate, notes="There is " + str(lastDateDataCnt) + " datas") updateManagement.save() json_obj = json.dumps({ "name": updateManagement.name, "updateDate": updateManagement.last_update_date.strftime("%y-%m-%d"), "dataDate": lastDate.strftime("%y-%m-%d"), "notes": "Update " + str(cnt) + " month revenue on " + str(year) + "-" + str(month) }) return HttpResponse(json_obj, content_type="application/json")