Beispiel #1
0
 def create_single_order(self, q_event):
     """
     Get a SignalEvent and enter accordingly, but also get rid of the 
     other holdings.
     """
     self.updated_list[q_event.get_ticker()] = {
         'ticker': q_event.get_ticker(),
         'direction': q_event.get_direction(),
         'candle': q_event.get_candle()
     }
     # if we're at the last signal event, process it and update the update_list
     if isinstance(self.events[0], event.SignalEvent) and len(
             self.events) == 1:
         # for each pair in the updated list, check if it's already in holdings
         # if the pair is in holidngs, do nothing.
         # if the pair is not in holdings,
         with open("holdings.csv", 'r', newline='') as file:
             reader = csv.reader(file)
             for row in reader:
                 #print(type(ast.literal_eval(row[2])))
                 self.holdings[row[0]] = {
                     "ticker": row[0],
                     "direction": int(row[1]),
                     "candle": ast.literal_eval(
                         row[2]),  # convert row[2] string to dict
                     "quantity": int(row[3]),
                     "price": float(row[4]),
                     "pip_value": float(row[5]),
                     "margin": float(row[6]),
                     "stop_loss": float(row[7]),
                     "take_profit": float(row[8])
                 }
         for pair in self.updated_list:
             if self.updated_list[pair]['ticker'] not in self.holdings:
                 # enter pairs not currently holding:
                 self.events.append(
                     event.OrderEvent(
                         direction=self.updated_list[pair]['direction'],
                         datetime=self.updated_list[pair]['candle'],
                         ticker=self.updated_list[pair]['ticker'],
                         quantity=1000))
         for h in self.holdings:
             if h not in self.updated_list:
                 # leave pairs that aren't in updated list
                 self.events.append(
                     event.OrderEvent(
                         direction=1
                         if self.holdings[h]['direction'] == -1 else -1,
                         datetime=self.holdings[h]
                         ['candle'],  # BIG ISSUE: this needs to be the current price, not the price entered at.
                         ticker=self.holdings[h]['ticker'],
                         quantity=self.holdings[h]['quantity']))
         self.updated_list = {}
Beispiel #2
0
 def create_order(self, q_event):
     """
     Append an OrderEvent to the queue (typically after receiving a SignalEvent 
     from a strategy object, or after a MarketEvent hits a stoploss or takeprofit). 
     """
     self.events.append(
         event.OrderEvent(direction=q_event.get_direction(),
                          datetime=q_event.get_candle(),
                          ticker=q_event.get_ticker(),
                          quantity=1000))
Beispiel #3
0
 def create_single_order(self, q_event):
     """
     Get a SignalEvent and enter accordingly, but also get rid of the 
     other holdings.
     """
     # if we're at the last signal event, process it and update the update_list
     if isinstance(self.events[0], event.SignalEvent) and len(
             self.events) == 1:
         self.updated_list[q_event.get_ticker()] = {
             'ticker': q_event.get_ticker(),
             'direction': q_event.get_direction(),
             'candle': q_event.get_candle()
         }
         # for each pair in the updated list, check if it's already in holdings
         # if the pair is in holidngs, do nothing.
         # if the pair is not in holdings,
         for pair in self.updated_list:
             if self.updated_list[pair]['ticker'] not in self.holdings:
                 # enter pairs not currently holding:
                 self.events.append(
                     event.OrderEvent(
                         direction=self.updated_list[pair]['direction'],
                         datetime=self.updated_list[pair]['candle'],
                         ticker=self.updated_list[pair]['ticker'],
                         quantity=1000))
         for h in self.holdings:
             if self.holdings[h]['ticker'] not in self.updated_list:
                 # leave pairs that aren't in updated list
                 self.events.append(
                     event.OrderEvent(
                         direction=1
                         if self.holdings[h]['direction'] == -1 else -1,
                         datetime=self.holdings[h]['candle'],
                         ticker=self.holdings[h]['ticker'],
                         quantity=self.holdings[h]['quantity']))
         self.updated_list = {}
     else:
         self.updated_list[q_event.get_ticker()] = {
             'ticker': q_event.get_ticker(),
             'direction': q_event.get_direction(),
             'candle': q_event.get_candle()
         }
Beispiel #4
0
 def create_close_order(self,
                        ticker,
                        direction,
                        datetime,
                        price,
                        quantity=1000):
     """For takeprofit / stoploss caused OrderEvents. """
     print(ticker, 'closed')
     self.events.append(
         event.OrderEvent(direction=direction * -1,
                          datetime=datetime,
                          ticker=ticker,
                          price=price,
                          quantity=quantity))
Beispiel #5
0
 def create_order(self, q_event):
     """
     Get a SignalEvent and enter accordingly, but also get rid of the 
     other holdigns.
     """
     if q_event.get_ticker() not in self.holdings:
         #create order to get rid of old holdings
         if self.holdings:
             for h in self.holdings:
                 print('holding', h)
                 self.events.append(event.OrderEvent(
                     direction=1 if self.holdings[h]['direction']<0 else -1, 
                     datetime=self.holdings[h]['candle'],
                     ticker=self.holdings[h]['ticker'], 
                     quantity=self.holdings[h]['quantity']
                 ))
         # create order for new holding
         self.events.append(event.OrderEvent(
             direction=q_event.get_direction(), 
             datetime= q_event.get_candle(),
             ticker=q_event.get_ticker(), 
             quantity=40000
         ))
Beispiel #6
0
    def create_order(self, q_event):
        """
        Append an OrderEvent to the queue (typically after receiving a SignalEvent 
        from a strategy object, or after a MarketEvent hits a stoploss or takeprofit). 
        """
        with open("example.csv", 'w', newline='') as file:
            writer = csv.writer(file)
            writer.writerow([
                q_event.get_direction(),
                q_event.get_candle(),
                q_event.get_ticker()
            ])

        self.events.append(
            event.OrderEvent(direction=q_event.get_direction(),
                             datetime=q_event.get_candle(),
                             ticker=q_event.get_ticker(),
                             quantity=1000))
Beispiel #7
0
 def create_order(self, q_event):
     self.events.append(
         event.OrderEvent(direction=q_event.get_direction(),
                          datetime=q_event.get_datetime(),
                          ticker=q_event.get_ticker(),
                          quantity=1000))