def JsonDataToFinance(data): financeData = None for i in range(1, 5): if i == 1: finance = data['finance1'] announcement = data['announcement1'] elif i == 2: finance = data['finance2'] announcement = data['announcement2'] elif i == 3: finance = data['finance3'] announcement = data['announcement3'] elif i == 4: finance = data['finance4'] announcement = data['announcement4'] if not finance or not announcement or not JSComplierHelper.IsNumber( announcement['year']) or not JSComplierHelper.IsNumber( announcement['quarter']): continue date = data['date'] if financeData: # 如果存在1天公布多个报告期数据 只取最新的一个公告期数据 if financeData.Announcement['year'] < announcement['year']: financeData = FinanceData(date, finance=finance, announcement=announcement) else: financeData = FinanceData(date, finance=finance, announcement=announcement) return financeData
def CalculateDividendYield(self, cashData, marketValue): if not marketValue: return [] dataLen, cashLen = len(self.Data), len(cashData) dividendYield = [] j = 0 for i in range(dataLen): day = self.Data.Data[i] market = marketValue.MarketValue[i] if not day or not market: continue item = SingleData() item.Date = day.Date item.Value = 0 if (j + 1 < cashLen): if (cashData[j].Date < day.Date and cashData[j + 1].Date <= day.Date): j += 1 i -= 1 if (j < cashLen): cash = cashData[j] endDate = cash.Date + 10000 # 1年有效 if (day.Date >= cash.Date and day.Date <= endDate and JSComplierHelper.IsDivideNumber(market.Value) and JSComplierHelper.IsNumber(cash.Value)): item.Value = cash.Value / market.Value * 100 dividendYield.append(item) return dividendYield
def GetFittingData2(self, overlayData, defaultValue): dataLen = len(self.Data) result = JSComplierHelper.CreateArray(dataLen) i, j = 0, 0 while i < dataLen: date = self.Data[i].Date if j >= len(overlayData): result[i] = SingleData() result[i].Date = date result[i].Value = defaultValue i += 1 continue overlayDate = overlayData[j].Date if overlayDate == date: item = SingleData() item.Date = overlayData[j].Date item.Value = overlayData[j].Value result[i] = item j += 1 i += 1 elif overlayDate < date: j += 1 else: result[i] = SingleData() result[i].Date = date result[i].Value = defaultValue i += 1 return result
def GetFittingMarketValueData(self, financeData): dataLen = len(self.Data) financeLen = len(financeData) result = JSComplierHelper.CreateArray(dataLen) i, j = 0, 0 while i < dataLen: date = self.Data[i].Date price = self.Data[i].Close if j + 1 < financeLen: if financeData[j].Date < date and financeData[j + 1].Date <= date: j += 1 continue item = SingleData() item.Date = date item.Value = financeData[j].Value * price # 市值计算 收盘价*股数 item.TestDate = financeData[j].Date # 财务日期 调试用 result[i] = item i += 1 return result
def GetFittingFinanceData(self, financeData): dataLen = len(self.Data) financeLen = len(financeData) result = JSComplierHelper.CreateArray(dataLen) i, j = 0, 0 while i < dataLen: date = self.Data[i].Date if j + 1 < financeLen: if financeData[j].Date < date and financeData[j + 1].Date <= date: j += 1 continue item = SingleData() item.Date = date if j < financeLen: item.Value = financeData[j].Value item.TestData = financeData[j].Date # 财务日期 调试用 else: item.Value = None item.TestData = None result[i] = item i += 1 return result
def Get(self, name): if not self.Finance: return None if not JSComplierHelper.IsJsonNumber(self.Finance, name): return None value = self.Finance[name] return value
def GetValue(self): dataLen = len(self.Data) result = JSComplierHelper.CreateArray(dataLen) for i in range(dataLen): if self.Data[i] and self.Data[i].Value != None: result[i] = self.Data[i].Value return result
def RecvStockDayData(self, recvData, jobID): stocks = recvData['stock'] if not stocks or len(stocks) != 1: return aryStockDay = stocks[0]['stockday'] bFinanceData = False # 是否是定期的财务数据 bMarketValue = False # 是否计算市值 aryData = [] for item in aryStockDay: indexData = SingleData() indexData.Date = item['date'] if jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_CAPITAL_DATA: capitalData = item['capital'] if not capitalData or not JSComplierHelper.IsJsonNumber( capitalData, 'a'): continue indexData.Value = capitalData['a'] / 100 # 流通股本(手) bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_TOTAL_EQUITY_DATA: capitalData = item['capital'] if not capitalData or not JSComplierHelper.IsJsonNumber( capitalData, 'total'): continue indexData.Value = capitalData['total'] / 10000 #总股本(万股) bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_FLOW_EQUITY_DATA: capitalData = item['capital'] if not capitalData or not JSComplierHelper.IsJsonNumber( capitalData, 'a'): continue indexData.Value = capitalData['a'] / 10000 # 流通股本(万股) bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_FLOW_MARKETVALUE_DATA: #流通市值 capitalData = item['capital'] if not capitalData or not JSComplierHelper.IsJsonNumber( capitalData, 'a'): continue indexData.Value = capitalData['a'] # 流通股本 bMarketValue = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_MARKETVALUE_DATA: # 总市值 capitalData = item['capital'] if not capitalData or not JSComplierHelper.IsJsonNumber( capitalData, 'total'): continue indexData.Value = capitalData['total'] # 总股本 bMarketValue = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_EXCHANGE_DATA: # 换手率 capitalData = item['capital'] if not financeData or not JSComplierHelper.IsJsonNumber( capitalData, 'a'): continue indexData.Value = capitalData['a'] # 流通股本 bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_PER_U_PROFIT_DATA: if not self.GetFinanceValue(item, 'peruprofit', indexData): # 每股未分配利润 continue bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_PER_NETASSET_DATA: if not self.GetFinanceValue(item, 'pernetasset', indexData): # 每股净资产 continue bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_PER_C_RESERVE_DATA: if not self.GetFinanceValue(item, 'percreserve', indexData): # 每股资本公积金 continue bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_PER_S_EARNING_DATA: if not self.GetFinanceValue(item, 'persearning', indexData): # 每股收益 continue bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_PER_S_EARNING2_DATA: financeData = JSSymbolData.JsonDataToFinance(item) if not financeData or not financeData.Finance or not financeData.Announcement: continue if not JSComplierHelper.IsJsonNumber( financeData.Finance, 'persearning' ) or not JSComplierHelper.IsJsonDivideNumber( financeData.Announcement, 'quarter'): continue indexData.Value = financeData.Finance[ 'persearning'] / financeData.Announcement[ 'quarter'] * 4 # 每股收益(折算为全年收益) 报告期每股收益/报告期*4 bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_N_PROFIT_DATA: if not self.GetFinanceValue(item, 'nprofit', indexData): # 净利润 continue bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_AL_RATIO_DATA: if not self.GetFinanceValue(item, 'alratio', indexData): # 资产负债率 continue bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_PROFIT_YOY_DATA: if not self.GetFinanceValue(item, 'profityoy', indexData): # 净利润同比增长率 continue bFinanceData = True elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_DIVIDEND_YIELD_DATA: if not self.GetFinanceValue(item, 'quarter4', indexData): # 净利润同比增长率 continue else: continue aryData.append(indexData) if bFinanceData: aryFixedData = self.SourceData.GetFittingFinanceData(aryData) elif bMarketValue: aryFixedData = self.SourceData.GetFittingMarketValueData( aryData) # 总市值用不复权的价格计算 if jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_MARKETVALUE_DATA: self.MarketValue = aryFixedData # 总市值保存下 算其他数据可能要用 elif jobID == JS_EXECUTE_JOB_ID.JOB_DOWNLOAD_DIVIDEND_YIELD_DATA: # 股息率TTM:过去4个季度现金分红总额/总市值 * 100% aryFixedData = self.SourceData.CalculateDividendYield( aryData, self.MarketValue) else: aryFixedData = self.SourceData.GetFittingData(aryData) bindData = ChartData(data=aryFixedData) bindData.Period = self.Period if bindData.Period > 0: # 周期数据 periodData = bindData.GetPeriodSingleData(bindData.Period) bindData.Data = periodData data = bindData.GetValue() self.FinanceData[jobID] = data
def RunAST(self): # 预定义的变量 for item in self.Arguments: self.VarTable[item.Name] = item.Value if not self.AST or not self.AST.Body: self.ThrowError() for item in self.AST.Body: self.VisitNode(item) if item.Type == Syntax.ExpressionStatement and item.Expression: if item.Expression.Type == Syntax.AssignmentExpression and item.Expression.Operator == ':' and item.Expression.Left: assignmentItem = item.Expression varName = assignmentItem.Left.Name outVar = self.VarTable[varName] if not isinstance(outVar, list): outVar = self.SingleDataToArrayData(outVar) self.OutVarTable.append( OutVariable(name=varName, data=outVar, type=0)) elif (item.Expression.Type == Syntax.CallExpression): callItem = item.Expression if (JSDraw.IsDrawFunction(callItem.Callee.Name)): draw = callItem.Draw draw.Name = callItem.Callee.Name self.OutVarTable.append( OutVariable(name=draw.Name, draw=draw, type=1)) elif (item.Expression.Type == Syntax.SequenceExpression): varName = None draw = None color = None lineWidth = None colorStick = False pointDot = False circleDot = False lineStick = False stick = False volStick = False isShow = True isExData = False for itemExpression in item.Expression.Expression: if (itemExpression.Type == Syntax.AssignmentExpression and itemExpression.Operator == ':' and itemExpression.Left): varName = itemExpression.Left.Name varValue = self.VarTable[varName] if not JSComplierHelper.IsArray(varValue): varValue = self.SingleDataToArrayData(varValue) self.VarTable[ varName] = varValue # 把单数值转化成数组放到变量表里 elif (itemExpression.Type == Syntax.Identifier): value = itemExpression.Name if (value == 'COLORSTICK'): colorStick = True elif (value == 'POINTDOT'): pointDot = True elif (value == 'CIRCLEDOT'): circleDot = True elif (value == 'LINESTICK'): lineStick = True elif (value == 'STICK'): stick = True elif (value == 'VOLSTICK'): volStick = True elif (value.find('COLOR') == 0): color = value elif (value.find('LINETHICK') == 0): lineWidth = value elif (value.find('NODRAW') == 0): isShow = False elif (value.find('EXDATA') == 0): isExData = True # 扩展数据, 不显示再图形里面 elif (itemExpression.Type == Syntax.Literal): #常量 aryValue = self.SingleDataToArrayData( itemExpression.Value) varName = str(itemExpression.Value) self.VarTable[varName, aryValue] # 把常量放到变量表里 elif (itemExpression.Type == Syntax.CallExpression and JSDraw.IsDrawFunction( itemExpression.Callee.Name)): draw = itemExpression.Draw draw.Name = itemExpression.Callee.Name if (pointDot and varName): # 圆点 outVar = self.VarTable[varName] if (not JSComplierHelper.IsArray(outVar)): outVar = self.SingleDataToArrayData(outVar) value = OutVariable(name=varName, data=outVar, type=3) value.Radius = 2 if color: value.Color = color if lineWidth: value.LineWidth = lineWidth self.OutVarTable.append(value) elif (circleDot and varName): # 圆点 outVar = self.VarTable[varName] if not JSComplierHelper.IsArray(outVar): outVar = self.SingleDataToArrayData(outVar) value = OutVariable(name=varName, data=outVar, type=3) value.Radius = 1.3 if color: value.Color = color if lineWidth: value.LineWidth = lineWidth self.OutVarTable.append(value) elif (lineStick and varName): # LINESTICK 同时画出柱状线和指标线 outVar = self.VarTable[varName] value = OutVariable(name=varName, data=outVar, type=4) if color: value.Color = color if lineWidth: value.LineWidth = lineWidth self.OutVarTable.append(value) elif (stick and varName): # STICK 画柱状线 outVar = self.VarTable[varName] value = OutVariable(name=varName, data=outVar, type=5) if color: value.Color = color if lineWidth: value.LineWidth = lineWidth self.OutVarTable.append(value) elif (volStick and varName): # VOLSTICK 画彩色柱状线 outVar = self.VarTable[varName] value = OutVariable(name=varName, data=outVar, type=6) if color: value.Color = color self.OutVarTable.append(value) elif (varName and color): outVar = self.VarTable[varName] if not JSComplierHelper.IsArray(outVar): outVar = self.SingleDataToArrayData(outVar) value = OutVariable(name=varName, data=outVar, type=0) value.Color = color if (lineWidth): value.LineWidth = lineWidth if (isShow == False): value.IsShow = False if (isExData == True): value.IsExData = True self.OutVarTable.append(value) elif (draw and color): value = OutVariable(name=draw.Name, data=draw, type=1) value.Color = color self.OutVarTable.append(value) elif (colorStick and varName ): # CYW: SUM(VAR4,10)/10000, COLORSTICK; 画上下柱子 outVar = self.VarTable[varName] value = OutVariable(name=varName, data=outVar, type=2) value.Color = color self.OutVarTable.append(value) elif (varName): outVar = self.VarTable[varName] value = OutVariable(name=varName, data=outVar, type=0) if color: value.Color = color if lineWidth: value.LineWidth = lineWidth if isShow == False: value.IsShow = False if isExData == True: value.IsExData = True self.OutVarTable.append(value) return self.OutVarTable
def GetTime(self): result = JSComplierHelper.CreateArray(len(self.Data)) for i in range(len(self.Data)): result[i] = self.Data[i].Time return result