コード例 #1
0
ファイル: base.py プロジェクト: MatthieuPerrot/nurse
	def emit(self, signal, signal_data=None):
		'''
   Emits a signal and associated signal data if provided. For instance, 
   signal data can help identifying between which states of a context a 
   transition is happening.

   Parameters:
   
   signal : any pickable object
   signal_data : any pickable object

   See Also:

   :func:`nurse.state_machine.change_state`

		'''

		try:
			async_connections = self._async_connections[signal]
		except KeyError:
			async_connections = []
		async_connections += self._async_connections['__all__']
		try:
			sync_connections = self._sync_connections[signal]
		except KeyError:
			sync_connections = []
		sync_connections += self._sync_connections['__all__']

		# batch active slots :
		#   done in 2 passes to avoid domino effect if some observers
		#   is_receiving_events status change after a first observer
		#   receive a signal.
		connections = [(receiver, slot) \
			for (receiver, slot) in sync_connections
			if receiver.is_receiving_events()]
		for (receiver, slot) in connections:
			event = SignalEvent(self, receiver, slot,
						signal, signal_data)
			event.start()

		connections = [(receiver, slot) \
			for (receiver, slot) in async_connections
			if receiver.is_receiving_events()]

		from config import Config # FIXME : move somewhere else
		event_loop = Config.get_event_loop()
		for (receiver, slot) in connections:
			event = SignalEvent(self, receiver, slot,
						signal, signal_data)
			event_loop.add_event(event)
コード例 #2
0
    def close_all_positions(self, data):
        """Closes all open positions without any checks."""

        datestamp = dt.datetime.strptime(data[0]['Time'], '%d-%m-%Y %H:%M:%S')

        # close active buy
        if self.active_long:
            signal = SignalEvent(self.symbol, datestamp, 'EXIT', 'SELL')
            self.event_queue.put(signal)
            self.active_long = False

        # close active sell
        elif self.active_short:
            signal = SignalEvent(self.symbol, datestamp, 'EXIT', 'BUY')
            self.event_queue.put(signal)
            self.active_short = False
コード例 #3
0
    def check_for_tp(self, data):
        """Checks to see if position take profit hit."""

        datestamp = dt.datetime.strptime(data[0]['Time'], '%d-%m-%Y %H:%M:%S')

        # active buy take profit conditions
        if self.active_long & (data[0]['High'] >= self.vwap):
            signal = SignalEvent(self.symbol, datestamp, 'EXIT', 'SELL')
            self.event_queue.put(signal)
            self.active_long = False

        # active sell take profit conditions
        elif self.active_short & (data[0]['Low'] <= self.vwap):
            signal = SignalEvent(self.symbol, datestamp, 'EXIT', 'BUY')
            self.event_queue.put(signal)
            self.active_short = False
コード例 #4
0
 def calculate_signals(self, event):
     if isinstance(event, MarketEvent):
         for symbol in self.symbol_list:
             data = self.data.get_latest_data(symbol)[0]
             if data is not None and len(data) > 0:
                 signal = SignalEvent(symbol, data[1], 'LONG')
                 self.event_queue.put(signal)
                 self.bought[symbol] = True
コード例 #5
0
    def check_for_sl(self, position, data):
        """Checks to see if position stop loss hit."""

        datestamp = dt.datetime.strptime(data[0]['Time'], '%d-%m-%Y %H:%M:%S')
        buy_sl = position['Open Price'] - 0.0020  # fixed 20 pip sl
        sell_sl = position['Open Price'] + 0.0020  # fixed 20 pip sl

        # active buy stop loss conditions
        if self.active_long & (data[0]['Low'] <= buy_sl):
            signal = SignalEvent(self.symbol, datestamp, 'EXIT', 'SELL')
            self.event_queue.put(signal)
            self.active_long = False

        # active sell stop loss conditions
        elif self.active_short & (data[0]['High'] >= sell_sl):
            signal = SignalEvent(self.symbol, datestamp, 'EXIT', 'BUY')
            self.event_queue.put(signal)
            self.active_short = False
コード例 #6
0
    def check_for_open(self, data):
        """Checks to see if a position should be opened."""

        datestamp = dt.datetime.strptime(data[0]['Time'], '%d-%m-%Y %H:%M:%S')
        lb = self.vwap - 0.00300
        ub = self.vwap + 0.00300

        if not self.active_long and not self.active_short:
            # open buy conditions
            if (self.bars_under_lb() < 1) & (data[0]['Low'] <= lb):
                signal = SignalEvent(self.symbol, datestamp, 'LONG', 'BUY')
                self.event_queue.put(signal)
                self.active_long = True

            # open sell conditions
            elif (self.bars_over_ub() < 1) & (data[0]['High'] >= ub):
                signal = SignalEvent(self.symbol, datestamp, 'SHORT', 'SELL')
                self.event_queue.put(signal)
                self.active_short = True
コード例 #7
0
 def calculate_signals(self, event):
     #Generate a single LONG signal per symbol when the first marketevent occurs
     
     if isinstance(event, MarketEvent):
         for s in self.symbol_list:
             data = self.data.get_latest_data(s)
             if data is not None and len(data)>0:
                 if self.bought[s] == False:
                     signal = SignalEvent(s, data[0][1],   #datetime
                                    'LONG')
                 
                     self.event_queue.put(signal)
                     self.bought[s] = True
コード例 #8
0
ファイル: Strategy.py プロジェクト: chowdaniel/Code
    def calculateSignal(self, dataHandler):

        if self.mean == None:
            return

        XELp = dataHandler.getLatestValue(self, "XEL", "Adj Close")
        CMSp = dataHandler.getLatestValue(self, "CMS", "Adj Close")

        res = CMSp - self.beta * XELp

        date = dataHandler.getDate()

        if res > self.band1:
            signal = SignalEvent("XEL", date, 1, 1)
            self.event.append(signal)
            signal = SignalEvent("CMS", date, -1, 1)
            self.event.append(signal)
        elif res < self.band2:
            signal = SignalEvent("XEL", date, -1, 1)
            self.event.append(signal)
            signal = SignalEvent("CMS", date, 1, 1)
            self.event.append(signal)
コード例 #9
0
ファイル: strategy.py プロジェクト: mikimaus78/trading-py
    def calculate_signals(self, event: MarketEvent) -> None:
        """
        For the MarketEvent, updates the event queue and sets True.
        For "Buy and Hold" strategy one signal per symbol is generated.
        Which means that we constantly LONG the market since the init.

        Args:
            event: MarketEvent object per symbol.

        Todo:
            Understand if other events are suitable.
        """
        if event.type == "MARKET":
            for s in self.symbol_list:
                # Get the last bar for the symbol?
                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)
                        signal = SignalEvent(bars[0][0], bars[0][1], 'LONG')
                        self.events.put(signal)
                        self.bought[s] = True