def calculate_signals(self, event): """ For "Buy and Hold" we generate a single signal per symbol and then no additional signals. This means we are constantly long the market from the date of strategy initialisation. Parameters event - A MarketEvent object. """ if event.type == 'MARKET': for s in self.symbol_list: bars = self.bars.get_latest_bars(s, N=200) if bars is not None and bars != [] and len(bars) > 199: hist_data = pd.DataFrame(bars).as_matrix()[:,3].tolist() pred = self.generate_prediction(hist_data) if self.bought[s] == False: if pred == 1: # (Symbol, Datetime, Type = LONG, SHORT or EXIT) signal = SignalEvent(bars[0][0], bars[0][1], 'LONG') self.events.put(signal) self.bought[s] = True # if pred == -1: ## # (Symbol, Datetime, Type = LONG, SHORT or EXIT) signal = SignalEvent(bars[0][0], bars[0][1], 'SHORT') # self.events.put(signal) # self.bought[s] = True else: if pred == -1: signal = SignalEvent(bars[0][0], bars[0][1], 'EXIT') self.events.put(signal) self.bought[s] = False
def generate_signal(self, event): if event.type == 'MARKET': for symbol in self.symbol_list: bars = self.data_handler.get_latest_bars_values(symbol, "close", N=0) if bars is not None: sema = talib.EMA(bars, self.short_window)[-1] self.sema[symbol].append(sema) lema = talib.EMA(bars, self.long_window)[-1] self.lema[symbol].append(lema) dt = self.data_handler.get_latest_bar_datetime(symbol) if len(self.sema[symbol] ) > 1 and self.sema[symbol][-2] != None: if sema > lema and self.sema[symbol][-2] < self.lema[ symbol][-2] and self.bought[symbol] == "OUT": signal = SignalEvent(symbol, dt, 'LONG') self.events.put(signal) self.bought[symbol] = 'LONG' elif sema < lema and self.sema[symbol][-2] > self.lema[ symbol][-2] and self.bought[symbol] == "LONG": signal = SignalEvent(symbol, dt, 'EXIT') self.events.put(signal) self.bought[symbol] = 'OUT'
def calculate_signals(self, event): """ For "Buy and Hold" we generate a single signal per symbol and then no additional signals. This means we are constantly long the market from the date of strategy initialisation. Parameters event - A MarketEvent object. """ if event.type == 'MARKET': for s in self.symbol_list: bars = self.bars.get_latest_bars(s, N=1) if bars is not None and bars != []: if self.bought[s] == False: # (Symbol, Datetime, Type = LONG, SHORT or EXIT) signal = SignalEvent(bars[0][0], bars[0][1], 'LONG') self.events.put(signal) self.bought[s] = True
def _order_quantity(self, symbol, dt, price, adj_factor, quantity, direction_str, signal_type=None): """ Parameters ---------- direction: {1: 多头;0:空头} action:{1:开仓;0:平仓} """ if direction_str.lower() == 'long': direction = 1 action = 1 if quantity > 0 else -1 elif direction_str.lower() == 'short': direction = -1 action = -1 if quantity > 0 else 1 else: raise ValueError('请输入正确的下单参数!') adj_price = price * adj_factor # 触发signal事件 signal_event = SignalEvent(dt, symbol, direction, action, abs(quantity), price, adj_price, adj_factor, signal_type, margin_rate=1, multiplier=1) self.event.put(signal_event)
def calculate_signals(self, event): """ For "Buy and Hold" we generate a single signal per symbol and then no additional signals. This means we are constantly long the market from the date of strategy initialisation. Parameters event - A MarketEvent object. """ if event.type == 'MARKET': for s in self.symbol_list: bars = self.bars.get_latest_bars(s, N=200) if bars is not None and bars != [] and len(bars) > 10: hist_data = pd.DataFrame(bars).as_matrix()[:, 3].tolist() pred = self.generate_prediction(hist_data, 30, 1e-4) if self.debug == 1: print 'Predicting p=', pred if pred == -1: if self.short[s] == False: signal = SignalEvent(bars[0][0], bars[0][1], 'SHORT') self.events.put(signal) self.short[s] = True if self.bought[s] == True: signal = SignalEvent(bars[0][0], bars[0][1], 'EXIT') self.events.put(signal) self.bought[s] = False if pred == 1: if self.short[s] == True: signal = SignalEvent(bars[0][0], bars[0][1], 'EXIT') self.events.put(signal) self.short[s] = False if self.bought[s] == False: signal = SignalEvent(bars[0][0], bars[0][1], 'LONG') self.events.put(signal) self.bought[s] = True #if self.bought[s] == False: # signal = SignalEvent(bars[0][0], bars[0][1], 'SHORT') # self.events.put(signal) # self.bought[s] = True #if self.debug == 1: # print 'Going short!' #if pred == 1: #if self.bought == True: # signal = SignalEvent(bars[0][0], bars[0][1], 'EXIT') # self.events.put(signal) # self.bought[s] = False # if pred == 1: # if self.bought == False: # signal = SignalEvent(bars[0][0], bars[0][1], 'LONG') # self.events.put(signal) # self.bought[s] = True # elif self.short == True: # signal = SignalEvent(bars[0][0], bars[0][1], 'EXIT') # self.events.put(signal) # self.short[s] = False # elif pred == -1: # if self.bought == True: # signal = SignalEvent(bars[0][0], bars[0][1], 'EXIT') # self.events.put(signal) # self.bought[s] = False # elif self.short == False: # signal = SignalEvent(bars[0][0], bars[0][1], 'SHORT') # self.events.put(signal) # self.short[s] = True """