def onMarketDataUpdate(self, market, code, md): time_info = md.timestamp.split('_') if self.last_data != time_info[0]: self.last_data = time_info[0] if self.last_price: self.close_data.pop(0) self.close_data.append(self.last_price) macd, macdsignal, mcadhist = talib.MACD(numpy.array( self.close_data), fastperiod=12, slowperiod=26, signalperiod=9) if self.current_holding and macd[-1] < macdsignal[-1] < 0: order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.bidPrice1, 100, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.current_holding = False if not self.current_holding and macd[-1] > macdsignal[-1] > 0: order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.askPrice1, 100, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.current_holding = True self.last_price = float(md.lastPrice)
def onMarketDataUpdate(self, market, code, md): time_info = md.timestamp.split('_') if 120000 <= int(time_info[1]) <= 130000: return if self.last_data != time_info[0]: self.last_data = time_info[0] self.close_data = [] if len(self.close_data) >= self.data_number: self.close_data.pop(0) self.close_data.append(md.lastPrice) if len(self.close_data) >= self.slow_period: macd, macdsignal, mcadhist = talib.MACD( numpy.array(self.close_data), fastperiod=self.fast_period, slowperiod=self.slow_period, signalperiod=9) # Sell stocks if self.current_holding and macd[-1] < macdsignal[-1] < 0: print macd[-1], macdsignal[-1], self.close_data order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.bidPrice1, 100, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.current_holding = False # Buy all the holdings if not self.current_holding and macd[-1] > macdsignal[-1] > 0: print macd[-1], macdsignal[-1], self.close_data order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.askPrice1, 100, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.current_holding = True if self.current_holding and md.timestamp.split('_')[1][:4] == '1600': order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.bidPrice1, 100, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.current_holding = False
def long_security(self, timestamp, code, price, volume): order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, int(volume), "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.hold_volume += int(volume) self.current_capital -= int(volume) * price
def long_securities(self, strategy, code, timestamp, price): if strategy == TREND: print "%s transaction" % strategy volume = int(self.current_capital[strategy] / price) else: volume0 = int(self.total_capital[strategy] * self.first_buy_factor / price) if not volume0: return n = round(math.log(1 - self.hold_volume[strategy] / volume0 * (1 - self.volume_factor), self.volume_factor)) volume = int(volume0 * self.volume_factor ** (n + 1)) if price <= self.current_capital[MEAN] < volume * price: volume = self.current_capital[MEAN] / price elif self.current_capital[MEAN] < price: # print "%s, current money is %s, cannot buy" % (timestamp, self.current_capital) volume = 0 if volume: order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, volume, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.current_capital[strategy] -= volume * price # print "Buy: %s, current total capital is %s" % (volume, self.total_capital) self.hold_volume[strategy] += volume self.hold_price[strategy] += volume * price
def short_security(self, timestamp, code, price): order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, self.hold_volume, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.current_capital += self.hold_volume * price self.total_capital = self.current_capital self.hold_volume = 0
def short_security(self, timestamp, code, price): volume = self.stock_info[code][HOLD_VOLUME] order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, volume, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.stock_info[code][HOLD_VOLUME] -= volume
def short_security(self, timestamp, code, price): if self.stock_info[code][RSI] > self.rsi_sell_bound: volume = self.stock_info[code][HOLD_VOLUME] \ if self.stock_info[code][HOLD_VOLUME] < self.stock_info[code][TOTAL_CAPITAL] / price / 10 \ else self.stock_info[code][HOLD_VOLUME] / 3 else: volume = self.stock_info[code][HOLD_VOLUME] order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, volume, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.stock_info[code][HOLD_VOLUME] -= volume
def long_security(self, timestamp, code, price): volume0 = int(self.stock_info[code][TOTAL_CAPITAL] / 10 / price) n = round( math.log( 1 - self.stock_info[code][HOLD_VOLUME] / volume0 * (1 - self.volume_factor), self.volume_factor)) volume = int(volume0 * self.volume_factor**(n + 1)) if price <= self.stock_info[code][CURRENT_CAPITAL] < volume * price: volume = int(self.stock_info[code][CURRENT_CAPITAL] / price) if volume and self.stock_info[code][CURRENT_CAPITAL] > volume * price: order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, int(volume), "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.stock_info[code][HOLD_VOLUME] += int(volume)
def short_security(self, timestamp, code, price): if self.rsix[-1] > self.rsi_sell_bound and self.ppo[-1] > self.ppo_sell_threshold: volume = self.buy_volume if self.buy_volume < self.total_capital / price / 10 else self.buy_volume / 3 else: volume = self.buy_volume print "stime: %s, price: %s, ma: %.2f, std: %.4f, rsi1: %.4f, rsi2: %.4f" % (timestamp.split('_')[0], price, self.mean_average, self.standard_dev, self.rsix[-1], self.rsix[-2]) order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, volume, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.buy_volume -= volume
def long_security(self, timestamp, code, price): volume0 = int(self.total_capital / 10 / price) n = round(math.log(1 - self.buy_volume / volume0 * (1 - self.volume_factor), self.volume_factor)) volume = int(volume0 * self.volume_factor ** (n + 1)) if price <= self.current_capital < volume * price: volume = int(self.current_capital / price) if volume and self.current_capital > volume * price: order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, int(volume), "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.buy_volume += int(volume) print "btime: %s, price: %s, ma: %.2f, std: %.4f, rsi1: %.4f, rsi2: %.4f" % (timestamp.split('_')[0], price, self.mean_average, self.standard_dev, self.rsix[-1], self.rsix[-2])
def short_securities(self, strategy, code, timestamp, price): print "%s transaction" % strategy order = cashAlgoAPI.Order(timestamp, 'SEHK', code, str(self.cnt), price, self.hold_volume[strategy], "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 sell_capital = self.hold_volume[strategy] * price interest = sell_capital - self.hold_price[strategy] total_capital = self.total_capital[MEAN] + self.total_capital[TREND] + interest current_mean_factor = float(self.total_capital[MEAN]) / (self.total_capital[TREND] + self.total_capital[MEAN]) self.total_capital[MEAN] = total_capital * current_mean_factor self.total_capital[TREND] = total_capital * (1 - current_mean_factor) self.current_capital[strategy] += sell_capital if self.current_capital[strategy] > self.total_capital[strategy]: difference = self.current_capital[strategy] - self.total_capital[strategy] other_strategy = MEAN if strategy == TREND else TREND self.current_capital[strategy] -= difference self.current_capital[other_strategy] += difference print "Sell: %s, current total capital is %s" % (self.hold_volume[strategy], self.total_capital) self.hold_volume[strategy] = 0 self.hold_price[strategy] = 0
def onMarketDataUpdate(self, market, code, md): # The following time is not allowed to trade. Only trade from 9:30 am to 12:00 am, and from 13:00 to 16:00 time_info = md.timestamp.split('_') if int(time_info[1][:4]) not in (range(930, 1201) + range(1300, 1601)): return # For open price record last day close price if time_info[0] != self.last_date: self.last_date = time_info[0] if len(self.close_price) > max(self.rsi_period, self.move_average_day) + 1: self.close_price.pop(0) if len(self.close_price_MACD) > max(self.macd_slow_period, self.macd_signal_period, self.macd_fast_period) + 1: self.close_price_MACD.pop(0) # print self.close_price_MACD # print self.close_price if self.last_price: # print 'date: %s\t%s' % (md.timestamp, self.last_price) self.close_price.append(self.last_price) self.close_price_MACD.append(self.last_price) if len(self.close_price) < max(self.rsi_period, self.move_average_day): self.last_price = md.lastPrice return self.mean_average = talib.MA(numpy.array(self.close_price), self.move_average_day)[-1] self.standard_dev = talib.STDDEV(numpy.array(self.close_price), self.move_average_day)[-1] if len(self.close_price) < max(self.rsi_period, self.move_average_day): self.last_price = md.lastPrice return # rsi6 = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=6)[-1] # rsi9 = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=9)[-1] # rsi14 = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=15)[-1] rsix = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=self.rsi_period)[-1] macd, macdsignal, macdhist = talib.MACD( numpy.array(self.close_price_MACD), fastperiod=self.macd_fast_period, slowperiod=self.macd_slow_period, signalperiod=self.macd_signal_period) macdDiff1 = macd[-1] macdDiff2 = macd[-1] - macdsignal[-1] onSig = 0 if md.lastPrice + self.std_factor * self.standard_dev < self.mean_average and rsix < self.rsi_buy_bound and macdDiff1 > -0.25: # if rsix < 50: # print "Buying point appear rsi%s: %s; macdDiff1 = %s; macdDiff2 %s" % (self.rsi_period, rsix, macdDiff1, macdDiff2) # Increase the buying volume so that we will buy more at second time. volume0 = int(self.total_capital / 10 / md.lastPrice) n = round( math.log( 1 - self.buy_volume / volume0 * (1 - self.volume_factor), self.volume_factor)) volume = volume0 * self.volume_factor**(n + 1) if md.lastPrice <= self.current_capital < volume * md.lastPrice: volume = self.current_capital / md.lastPrice if self.current_capital > volume * md.lastPrice: order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.askPrice1, int(volume), "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.buy_volume += int(volume) onSig = 1 # self.current_capital -= int(volume) * md.askPrice1 # print "Buy: %s %s" % (volume, self.total_capital) if macdDiff1 > -0 and macdDiff2 > -0.2: # if rsix < 50: # print "Buying point appear rsi%s: %s; macdDiff1 = %s; macdDiff2 %s" % (self.rsi_period, rsix, macdDiff1, macdDiff2) # Increase the buying volume so that we will buy more at second time. if onSig == 1: print "also bought" volume0 = int(self.total_capital / 10 / md.lastPrice) n = round( math.log( 1 - self.buy_volume / volume0 * (1 - self.volume_factor), self.volume_factor)) volume = volume0 * self.volume_factor**(n + 1) if md.lastPrice <= self.current_capital < volume * md.lastPrice: volume = self.current_capital / md.lastPrice if self.current_capital > volume * md.lastPrice: order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.askPrice1, int(volume), "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.buy_volume += int(volume) onSig = 1 # self.current_capital -= int(volume) * md.askPrice1 # print "Buy: %s %s" % (volume, self.total_capital) if int(10 * md.lastPrice) >= int( self.mean_average * 10 ) and self.buy_volume and rsix > self.rsi_sell_bound and onSig == 0: # print "Selling point appear rsi%s: %s" % (self.rsi_period, rsix) order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.bidPrice1, self.buy_volume, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 # self.current_capital += self.buy_volume * md.bidPrice1 # self.total_capital = self.current_capital # print "Sell: %s %s" % (self.buy_volume, self.total_capital) self.buy_volume = 0 if int(10 * md.lastPrice) >= int( self.mean_average * 10) and self.buy_volume and ( macdDiff1 < 0.2 or macdDiff2 < 0) and onSig == 0: # print "Selling point appear macd: macdDiff1 = %s; macdDiff2 %s" % (macdDiff1, macdDiff2) order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.bidPrice1, self.buy_volume, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 # self.current_capital += self.buy_volume * md.bidPrice1 # self.total_capital = self.current_capital # print "Sell: %s %s" % (self.buy_volume, self.total_capital) self.buy_volume = 0 self.last_price = md.lastPrice
def onMarketDataUpdate(self, market, code, md): # The following time is not allowed to trade. Only trade from 9:30 am to 12:00 am, and from 13:00 to 16:00 time_info = md.timestamp.split('_') if int(time_info[1][:4]) not in (range(930, 1201) + range(1300, 1601)): return # For open price record last day close price if time_info[0] != self.last_date: self.last_date = time_info[0] if self.last_price: self.close_price.pop(0) self.close_price.append(self.last_price) self.mean_average = talib.MA(numpy.array(self.close_price), self.move_average_day)[-1] self.standard_dev = talib.STDDEV(numpy.array(self.close_price), self.move_average_day)[-1] # in case some bad value if md.lastPrice < self.mean_average / 10: md.lastPrice *= 100 rsi6 = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=6)[-1] rsi9 = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=9)[-1] rsi14 = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=15)[-1] rsix = talib.RSI(numpy.array(self.close_price + [md.lastPrice]), timeperiod=self.move_average_day)[-1] if md.lastPrice + self.factor * self.standard_dev < self.mean_average: if rsix < 50: print "Buying point appear rsi6: %s, rsi9: %s, rsi14: %s, rsi%s: %s" % ( rsi6, rsi9, rsi14, self.move_average_day, rsix) # Increase the buying volume so that we will buy more at second time. volume0 = int(self.total_capital / 10 / md.lastPrice) n = round( math.log( 1 - self.buy_volume / volume0 * (1 - self.volume_factor), self.volume_factor)) volume = volume0 * self.volume_factor**(n + 1) if md.lastPrice <= self.current_capital < volume * md.lastPrice: volume = self.current_capital / md.lastPrice if self.current_capital > volume * md.lastPrice: order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.askPrice1, int(volume), "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.buy_volume += int(volume) # self.current_capital -= int(volume) * md.askPrice1 # print "Buy: %s %s" % (volume, self.total_capital) if int(10 * md.lastPrice) >= int(self.mean_average * 10): if self.buy_volume: print "Selling point appear rsi6: %s, rsi9: %s, rsi14: %s, rsi%s: %s" % ( rsi6, rsi9, rsi14, self.move_average_day, rsix) order = cashAlgoAPI.Order(md.timestamp, 'SEHK', code, str(self.cnt), md.bidPrice1, self.buy_volume, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 # self.current_capital += self.buy_volume * md.bidPrice1 # self.total_capital = self.current_capital # print "Sell: %s %s" % (self.buy_volume, self.total_capital) self.buy_volume = 0 self.last_price = md.lastPrice
def onOHLCFeed(self, of): # transfer data md = cashAlgoAPI.MarketData([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) md.timestamp = of.timestamp md.market = of.market md.productCode = str(of.productCode) md.lastPrice = of.close md.askPrice1 = of.close md.bidPrice1 = of.close #md.lastVolume = 1 #Get daily price if (md.timestamp[9:15] == "160000"): # when receive a stock x data, do: # 1. store current_data_x # 2. predict theoretical price of stock y, ln(PriceY_t) = Gamma*ln(PriceX_t) + Mu # 3. wait next stock y data if (md.productCode == self.stock_code_x): #print "receive a x" self.current_data_x = md # self.y_price_ln_predict = self.gamma * math.log(self.current_data_x.lastPrice, math.e) + self.mu self.y_price_ln_predict = self.gamma * math.log( self.current_data_x.lastPrice, math.e) return # when receive a stock y data, do: # 1. store current_data_y # 2. check strategy status: # 1) standing by to trade # a. check if could do trading [y_price_ln_diff >= delta or <= -delta] # b. if could, do trade # 2) waiting to clear # a. check if two stocks match # b. if matching, do clearing if (md.productCode == self.stock_code_y): #print "receive a y" self.current_data_y = md y_price_ln_actual = math.log( float(self.current_data_y.lastPrice), math.e) #y_price_ln_diff = y_price_ln_actual - self.y_price_ln_predict diff = y_price_ln_actual - self.y_price_ln_predict print(diff) y_price_ln_diff = (diff - self.mean) / self.std print(y_price_ln_diff) #moving window of price diff #self.movingWindow(self.list, self.mins, diff) if (self.mins == 0): self.list.pop(0) else: self.mins = self.mins - 1 self.list.append(y_price_ln_diff) avr = sum(self.list) / len(self.list) #self.movingWindow1(self.sma, self.sma_mins, avr) if (self.sma_mins == 0): self.sma.pop(0) else: self.sma_mins = self.sma_mins - 1 self.sma.append(avr) if (len(self.sma) > 1): if ((self.sma[1] - self.sma[0]) * y_price_ln_diff < 0): self.signal = 1 #if(md.timestamp[9:13] == "0930"): print y_price_ln_diff if (self.position_x == 0 and self.position_y == 0): #print "check if could trade" if (y_price_ln_diff >= self.delta and self.signal == 1): print md.timestamp, y_price_ln_diff, "do trade long x, short y." self.cnt += 1 self.volume_hold_y = self.initialCapital / self.current_data_y.lastPrice self.volume_hold_x = self.initialCapital * self.gamma / self.current_data_x.lastPrice self.position_y = -1 self.position_x = 1 order1 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_y, str(self.cnt), self.current_data_y.lastPrice, self.volume_hold_y, "open", 2, "insert", "market_order", "today") self.cnt += 1 order2 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_x, str(self.cnt), self.current_data_x.lastPrice, self.volume_hold_x, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order1) self.mgr.insertOrder(order2) elif (y_price_ln_diff <= -self.delta and self.signal == 1): print md.timestamp, y_price_ln_diff, "do trade short x, long y." self.cnt += 1 self.volume_hold_y = self.initialCapital / self.current_data_y.lastPrice self.volume_hold_x = self.initialCapital * self.gamma / self.current_data_x.lastPrice self.position_y = 1 self.position_x = -1 order1 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_y, str(self.cnt), self.current_data_y.lastPrice, self.volume_hold_y, "open", 1, "insert", "market_order", "today") self.cnt += 1 order2 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_x, str(self.cnt), self.current_data_x.lastPrice, self.volume_hold_x, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order1) self.mgr.insertOrder(order2) else: return else: #cut loss if (self.position_x == 1 and y_price_ln_diff >= self.cut_loss): print md.timestamp, y_price_ln_diff, "do clear." self.cnt += 1 self.position_y = 0 self.position_x = 0 order1 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_y, str(self.cnt), self.current_data_y.lastPrice, self.volume_hold_y, "open", 1, "insert", "market_order", "today") self.cnt += 1 order2 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_x, str(self.cnt), self.current_data_x.lastPrice, self.volume_hold_x, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order1) self.mgr.insertOrder(order2) #close win if (self.position_x == 1 and y_price_ln_diff <= self.close): print md.timestamp, y_price_ln_diff, "do clear." self.cnt += 1 self.position_y = 0 self.position_x = 0 order1 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_y, str(self.cnt), self.current_data_y.lastPrice, self.volume_hold_y, "open", 1, "insert", "market_order", "today") self.cnt += 1 order2 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_x, str(self.cnt), self.current_data_x.lastPrice, self.volume_hold_x, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order1) self.mgr.insertOrder(order2) #cut loss if (self.position_x == -1 and y_price_ln_diff <= self.cut_loss * -1): print md.timestamp, y_price_ln_diff, "do clear." self.cnt += 1 self.position_y = 0 self.position_x = 0 order1 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_y, str(self.cnt), self.current_data_y.lastPrice, self.volume_hold_y, "open", 2, "insert", "market_order", "today") self.cnt += 1 order2 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_x, str(self.cnt), self.current_data_x.lastPrice, self.volume_hold_x, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order1) self.mgr.insertOrder(order2) if (self.position_x == -1 and y_price_ln_diff >= self.close * -1): print md.timestamp, y_price_ln_diff, "do clear." self.cnt += 1 self.position_y = 0 self.position_x = 0 order1 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_y, str(self.cnt), self.current_data_y.lastPrice, self.volume_hold_y, "open", 2, "insert", "market_order", "today") self.cnt += 1 order2 = cashAlgoAPI.Order( md.timestamp, "SEHK", self.stock_code_x, str(self.cnt), self.current_data_x.lastPrice, self.volume_hold_x, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order1) self.mgr.insertOrder(order2) else: return ''' # Open Long Position if md.lastPrice>=max(self.list) and self.range>=self.minrange and self.range<=self.maxrange and self.position==0 and not max(self.list)==999999 and not md.timestamp[9:11]=="16": order = cashAlgoAPI.Order(md.timestamp, "HKFE", md.productCode, str(self.cnt), md.askPrice1, 1, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) print "Place an BUY order at %s" %md.timestamp + "Price at %s" %md.askPrice1 self.cnt+=1 self.position=1 self.target=self.range self.openPrice=md.askPrice1 # Open Short Position if md.lastPrice<=min(self.list) and self.range>=self.minrange and self.range<=self.maxrange and self.position==0 and not max(self.list)==999999 and not md.timestamp[9:11]=="16": order = cashAlgoAPI.Order(md.timestamp, "HKFE", md.productCode, str(self.cnt), md.bidPrice1, 1, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) print "Place an SELL order at %s" %md.timestamp + "Price at %s" %md.bidPrice1 self.cnt+=1 self.position=-1 self.target=self.range self.openPrice=md.bidPrice1 # Win close position (Long) if self.position==1 and md.lastPrice>=(self.openPrice+self.target): order = cashAlgoAPI.Order(md.timestamp, "HKFE", md.productCode, str(self.cnt), md.bidPrice1, 1, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) print "Place an SELL order at %s" %md.timestamp + "Price at %s, (Win)" %md.bidPrice1 self.cnt+=1 self.position=0 # Win close position (Short) if self.position==-1 and md.lastPrice<=(self.openPrice-self.target): order = cashAlgoAPI.Order(md.timestamp, "HKFE", md.productCode, str(self.cnt), md.askPrice1, 1, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) print "Place an BUY order at %s" %md.timestamp + "Price at %s, (Win)" %md.askPrice1 self.cnt+=1 self.position=0 # Cut loss position (Long) if self.position==1 and md.lastPrice<=min(self.list): order = cashAlgoAPI.Order(md.timestamp, "HKFE", md.productCode, str(self.cnt), md.bidPrice1, 1, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) print "Place an SELL order at %s" %md.timestamp + "Price at %s, (Cut)" %md.bidPrice1 self.cnt+=1 self.position=0 # Cut loss position (Short) if self.position==-1 and md.lastPrice>=max(self.list): order = cashAlgoAPI.Order(md.timestamp, "HKFE", md.productCode, str(self.cnt), md.askPrice1, 1, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) print "Place an BUY order at %s" %md.timestamp + "Price at %s, (Cut)" %md.askPrice1 self.cnt+=1 self.position=0 # Dayend cut (Long) if self.position==1 and md.timestamp[9:13]=="1614": order = cashAlgoAPI.Order(md.timestamp, "HKFE", md.productCode, str(self.cnt), md.bidPrice1, 1, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) print "Place an SELL order at %s" %md.timestamp + "Price at %s, (Dayend Cut)" %md.bidPrice1 self.cnt+=1 self.position=0 # Dayend cut (Short) if self.position==-1 and md.timestamp[9:13]=="1614": order = cashAlgoAPI.Order(md.timestamp, "HKFE", md.productCode, str(self.cnt), md.askPrice1, 1, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) print "Place an BUY order at %s" %md.timestamp + "Price at %s, (Dayend Cut)" %md.askPrice1 self.cnt+=1 self.position=0 ''' return
def onMarketDataUpdate(self, market, code, md): if int(md.timestamp[9:11]) not in range(9, 12) + range(13, 17): return if self.last_date != md.timestamp[0:8]: self.last_date = md.timestamp[0:8] self.list = [] self.position = 0 self.target = 0 self.open_price = 0 if 1 <= md.lastPrice < 999999: if len(self.list) >= self.min: self.list.pop(0) self.list.append(md.lastPrice) max_price = max(self.list) min_price = min(self.list) self.range = max_price - min_price if self.max_range >= self.range >= self.min_range and self.position == 0: if md.lastPrice >= max_price: order = cashAlgoAPI.Order(md.timestamp, md.market, md.productCode, str(self.cnt), md.askPrice1, 1, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.position = 1 self.target = self.range self.open_price = md.askPrice1 if md.lastPrice <= min_price: order = cashAlgoAPI.Order(md.timestamp, md.market, md.productCode, str(self.cnt), md.bidPrice1, 1, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.position = -1 self.target = self.range self.open_price = md.bidPrice1 elif self.position == 1 and (md.lastPrice >= (self.open_price + self.target) or md.lastPrice <= min_price or md.timestamp[9:13] == "1614"): order = cashAlgoAPI.Order(md.timestamp, md.market, md.productCode, str(self.cnt), md.bidPrice1, 1, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.position -= 1 elif self.position == -1 and (md.lastPrice <= (self.open_price - self.target) or md.lastPrice >= max_price or md.timestamp[9:13] == "1614"): order = cashAlgoAPI.Order(md.timestamp, md.market, md.productCode, str(self.cnt), md.askPrice1, 1, "open", 1, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 self.position += 1
def onMarketDataUpdate(self, market, code, md): time_info = md.timestamp.split('_') if int(time_info[1]) not in (range(93000, 120000) + range(130000, 155959)): return # For open price if time_info[0] != self.security_info[code][LAST_DATE]: # print "open price of %s is %s" % (code, md.lastPrice) self.open_execute = False print "mean reversion price of code %s: %s" % ( code, self.security_info[code][MEAN_AVERAGE].price) if len(self.open_price) == self.security_num: self.open_price.clear() self.security_info[code][LAST_DATE] = time_info[0] self.security_info[code][LAST_LOW_PRICE] = self.security_info[ code][LOW_PRICE] self.security_info[code][LOW_PRICE] = md.lastPrice # mark how many stocks have been dealt self.open_price[code] = md.askPrice1 # print "yesterday's close price:%f" % self.today_low[code] bog_rate = self.open_price[code] - self.security_info[code][ LAST_LOW_PRICE] # print "open price of %s is %s and its bog rate: %s" % (code, md.lastPrice, bog_rate) if bog_rate > 0 and self.open_price[code] > self.security_info[ code][MEAN_AVERAGE].price: self.bog_array[ code] = bog_rate / self.security_info[code][LAST_LOW_PRICE] # The open bid strategy if len(self.open_price) == self.security_num and not self.open_execute: sorted_array = collections.OrderedDict( sorted(self.bog_array.items(), key=lambda t: t[1], reverse=False)) self.bog_array.clear() # print "array %s" % str(sorted_array) execute_number = 0 while execute_number < self.execute_number: if sorted_array: buy_code = sorted_array.popitem(True) order = cashAlgoAPI.Order(md.timestamp, 'SEHK', buy_code[0], str(self.cnt), self.open_price[buy_code[0]], self.volume, "open", 1, "insert", "market_order", "today") # print "Place an BUY order at %s" % of.timestamp self.mgr.insertOrder(order) self.cnt += 1 execute_number += 1 self.bog_array[buy_code[0]] = None else: break self.open_execute = True # At the end of everyday sell all the product if code in self.bog_array and ( time_info[1][:4] == '1559' or float(md.bidPrice1) >= self.factor_threshold * float(self.open_price[code])): del self.bog_array[code] order = cashAlgoAPI.Order(md.timestamp, market, code, str(self.cnt), md.bidPrice1, self.volume, "open", 2, "insert", "market_order", "today") self.mgr.insertOrder(order) self.cnt += 1 if time_info[1][:4] == '1559': self.security_info[code][MEAN_AVERAGE].update(md)