def ExePolicy(self, code): df_source = self._getUserStrategy() if len(df_source) == 0: return d = [] bHaveCode = False for i in range(len(df_source)): strategy_code = df_source.irow(i)['code'] strategy_id = df_source.irow(i)['id'] try: self._checkImport(strategy_code) #self._log("run here") exec(strategy_code) if initialize(None).find(code) >= 0: bHaveCode = True break except Exception as e: s = str(e) d.append({ "strategy_id": strategy_id, "title": code, "code": s, "t": datetime.datetime.now() }) #一分钟内同一个策略不能提交 if (d[-1]["t"] - self.pre_post_runlog[strategy_id] ).total_seconds() > 60: agl.LOG(str(e)) basesign.PostRunLog(d) self.pre_post_runlog[strategy_id] = d[-1]['t'] continue if bHaveCode == False: return live_data = LiveData() df_hisdat = live_data.getHisdat(code) df_fenshi = live_data.getFenshi(code) if len(df_fenshi) == 0: return #return data = { "code": code, "fenshi": df_fenshi, "hisdat": df_hisdat, 'name': stock.GetCodeName(code), 'price': float(df_fenshi.tail(1)['p']) } # 取历史redis分时 df_fenshi_1min = df_fenshi.resample("1min") df_fenshi_1min_pre = fenshi_redis.getOneMinFenshiFromRedis(code) if df_fenshi_1min_pre is None: agl.LOG("分时历史取值失败" + code) else: df_fenshi_1min = pd.concat([df_fenshi_1min_pre, df_fenshi_1min]) df_fenshi_1min = df_fenshi_1min.dropna() closes = np.array(df_fenshi_1min['p']) data['rsi'] = stock.RSI(closes)[-1] # BOLL df_fenshi_5min = df_fenshi_1min.resample('5min') upper, middle, lower = stock.BOLL(np.array(df_fenshi_5min['p'])) data['boll_up'] = upper[-1] data['boll_mid'] = middle[-1] data['boll_down'] = lower[-1] # ATR atr = stock.ATR(df_fenshi_5min['p'], df_fenshi_5min['p'], df_fenshi_5min['p']) data['atr'] = atr[-1] #执行用户的代码 data['msglist'] = [] for i in range(len(df_source)): strategy_code = df_source.irow(i)['code'] #strategy_code = strategy_code.replace('\n','') strategy_id = df_source.irow(i)['id'] agl.LOG(str(strategy_id)) data['strategy_id'] = strategy_id try: self._checkImport(strategy_code) exec(strategy_code) if initialize(None).find(code) < 0: continue sign = handle_data(self, data) s = "run sussessful" if sign: s += " have sign" agl.LOG(s) except Exception as e: s = str(e) #s += '\n' s += traceback.format_exc() d.append({ "strategy_id": strategy_id, "title": code, "code": s, "t": datetime.datetime.now() }) #所有用户的策略执行完毕后再提交 agl.LOG('start post') basesign.PostRunLog(d) if len(data['msglist']) > 0: agl.LOG(data['msglist']) basesign.PostMsgList(data['msglist']) agl.LOG('end post')
def showBoll(closes): """显示布林线""" upper, middle, lower = stock.BOLL(closes) ui.DrawTs(pl, closes, high=upper, low=lower)