def calculate_signals(self, event): #print("Event sent to MA strategy: %s" % event) if event.type == 'TICK': pair = event.instrument price = event.bid pd = self.pairs_dict[pair] #print("Pair: %s; Price: %s; Pd: %s" %(pair, price, pd)) if pd['ticks'] == 0: pd["short_sma"] = price pd["long_sma"] = price else: pd["short_sma"] = self.calc_rolling_sma( pd["short_sma"], self.short_window, price) pd["long_sma"] = self.calc_rolling_sma(pd["long_sma"], self.long_window, price) # Only start the strategy when we have crated and accurate short window if pd["ticks"] > self.short_window: #print("Ticks > %s" % self.short_window) #print("Short_MA: %s; Long_MA: %s; Invesed: %s" % ( # pd["short_sma"], pd["long_sma"], pd["invested"])) if pd["short_sma"] > pd["long_sma"] and not pd["invested"]: print("Signal: buy") signal = SignalEvent(pair, "market", "buy", event.time) self.events.put(signal) pd["invested"] = True if pd["short_sma"] < pd["long_sma"] and pd["invested"]: print("Signal: sell") signal = SignalEvent(pair, "market", "sell", event.time) self.events.put(signal) pd["invested"] = False pd["ticks"] += 1
def open(self, symbol, ema_short, ema_long, adx, rsi): if adx[-1] <= 25: return side = check_cross(ema_short, ema_long, shift=0) logger.info('%s@%s param=%s, %0.5f, %0.5f, %0.2f, %0.2f' % ( self.name, symbol, side, ema_short[-1], ema_long[-1], adx[-1], rsi[-1])) event = None if side == OrderSide.BUY and 70 > rsi[-1] > 50: event = SignalEvent(SignalAction.OPEN, self.name, self.version, self.magic_number, symbol, side, trailing_stop=self.trailing_stop, take_profit=self.take_profit, stop_loss=self.stop_loss) self.send_event(event) elif side == OrderSide.SELL and 50 > rsi[-1] > 30: event = SignalEvent(SignalAction.OPEN, self.name, self.version, self.magic_number, symbol, side, trailing_stop=self.trailing_stop, take_profit=self.take_profit, stop_loss=self.stop_loss) self.send_event(event) if event: logger.info('[ORDER_%s] %s@%s %s, param=%0.5f, %0.5f, %0.2f, %0.2f' % ( event.action, event.strategy, event.instrument, event.to_dict(), ema_short[-1], ema_long[-1], adx[-1], rsi[-1])) return side
def calculate_signals(self, tick_event): currency_pair = tick_event.instrument time = tick_event.time if self.ticks % 5 == 0: if self.invested == False: signal = SignalEvent(currency_pair, "market", "buy", time) self.events_queue.put(signal) self.invested = True elif self.invested == True: signal = SignalEvent(currency_pair, "market", "sell", time) self.events_queue.put(signal) self.invested = False self.ticks += 1
def calculate_signals(self, event): if event.type == 'TICK' and event.instrument == self.pairs[0]: if self.ticks % 5 == 0: if self.invested == False: signal = SignalEvent(self.pairs[0], "market", "buy", event.time) self.events.put(signal) self.invested = True else: signal = SignalEvent(self.pairs[0], "market", "sell", event.time) self.events.put(signal) self.invested = False self.ticks += 1
def calculate_signals(self, event): #print("Event pased to strategy: %s" % event) if event.type == 'TICK' and event.instrument == self.pairs[0]: #print("Tick No: %s" % self.ticks) if self.ticks % 5 == 0: #print("Tick No: %s" % self.ticks) if self.invested == False: signal = SignalEvent(self.pairs[0], "market", "buy", event.time) #print("Invested: False; Signal: %s" % signal) self.events.put(signal) #print("Invested: False; self.events: %s" % self.events) self.invested = True else: signal = SignalEvent(self.pairs[0], "market", "sell", event.time) #print("Invested: True; Signal: %s" % signal) self.events.put(signal) #print("Invested: True; self.events: %s" % self.events) self.invested = False self.ticks += 1
def test_event(self): open = SignalEvent(SignalAction.OPEN, HLHBTrendStrategy.name, HLHBTrendStrategy.version, HLHBTrendStrategy.magic_number, instrument='EURUSD', side=OrderSide.BUY, order_type=OrderType.MARKET, stop_loss=30, take_profit=50, trailing_stop=None, percent=None) close = SignalEvent(SignalAction.CLOSE, HLHBTrendStrategy.name, HLHBTrendStrategy.version, HLHBTrendStrategy.magic_number, instrument='EURUSD', side=OrderSide.BUY, percent=0.5) data = json.dumps(open.to_dict()) data2 = json.loads(data) open2 = Event.from_dict(data2) self.assertEqual(open.type, open2.type) for k in open.__dict__.keys(): self.assertEqual(open.__dict__[k], open2.__dict__[k]) data = json.dumps(close.to_dict()) data2 = json.loads(data) close2 = Event.from_dict(data2) self.assertEqual(close.type, close2.type) for k in close.__dict__.keys(): self.assertEqual(close.__dict__[k], close2.__dict__[k])
def calculate_signals(self, event): if event.type == 'TICK': pair = event.instrument price = event.bid pd = self.pairs_dict[pair] if pd["ticks"] == 0: pd["short_sma"] = price pd["long_sma"] = price else: pd["short_sma"] = self.calc_rolling_sma( pd["short_sma"], self.short_window, price) pd["long_sma"] = self.calc_rolling_sma(pd["long_sma"], self.long_window, price) # Only start the strategy when we have created an accurate short window if pd["ticks"] > self.short_window: if pd["short_sma"] > pd["long_sma"] and not pd["invested"]: signal = SignalEvent(pair, "market", "buy", event.time) self.events.put(signal) pd["invested"] = True if pd["short_sma"] < pd["long_sma"] and pd["invested"]: signal = SignalEvent(pair, "market", "sell", event.time) self.events.put(signal) pd["invested"] = False pd["ticks"] += 1
def calculate_signals(self, event): """ For "Buy and Hold" generiamo un singolo segnale per simbolo e quindi nessun segnale aggiuntivo. CiĆ² significa che siamo costantemente LONG sul mercato a partire dalla data di inizializzazione della strategia. Parametri event - Un oggetto MarketEvent. """ 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