Exemplo n.º 1
0
    def cross(self, ticker, signal_a, signal_b):
        if self.current_date == self.portfolio.start_day:
            return 'no_cross'

        prev_day = self.portfolio.get_prev_day()
        today = self.current_date

        signal_a_0 = dm.get_value(
            ticker, signal_a, prev_day,
            self.portfolio.trader.confirmation_indicators_table, 1)
        signal_a_1 = dm.get_value(
            ticker, signal_a, today,
            self.portfolio.trader.confirmation_indicators_table, 1)

        # signal_a_0 = self.portfolio.get_value(ticker, signal_a, date = prev_day)
        # signal_a_1 = self.portfolio.get_value(ticker, signal_a, date = today)

        # if signal_b != 0:
        #     signal_b_0 = self.portfolio.get_value(ticker, signal_b, date=prev_day)
        #     signal_b_1 = self.portfolio.get_value(ticker, signal_b, date = today)
        # else:
        signal_b_0 = 0
        signal_b_1 = 0

        if (signal_a_1 >= signal_b_1) and (signal_a_0 < signal_b_0):
            return 'up'
        elif (signal_a_0 > signal_b_0) and (signal_a_1 <= signal_b_1):
            return 'down'
        else:
            return 'no_cross'
Exemplo n.º 2
0
 def buy_signal(stock_ticker):
     confirmation_value = dm.get_value(
         stock_ticker, 'total_score', self.current_date,
         self.portfolio.trader.confirmation_indicators_table, 1)
     if confirmation_value >= confirmation_total_buy_limit:
         return True
     else:
         return False
Exemplo n.º 3
0
    def calc_net_worth(self):
        cash = self.current_cash
        current_stocks = self.get_current_stocks()
        total_net_worth = 0 + cash

        for stock in current_stocks:
            price = dm.get_value(stock, self.price_field, self.current_day, self.dataset, 1)
            stock_total_cost = price * self.get_num_shares(stock)
            total_net_worth = total_net_worth + stock_total_cost

        return total_net_worth
Exemplo n.º 4
0
    def modular_strategy(self, params):
        # entry_indicator_name = params['entry_indicator'] + str(int(params['entry_indicator_period']))
        # volume_total_buy_limit = params['volume_total_buy_limit']
        confirmation_total_buy_limit = params['confirmation_total_buy_limit']

        close = self.price_field
        order = None

        def buy_signal(stock_ticker):
            confirmation_value = dm.get_value(
                stock_ticker, 'total_score', self.current_date,
                self.portfolio.trader.confirmation_indicators_table, 1)
            if confirmation_value >= confirmation_total_buy_limit:
                return True
            else:
                return False

        def sell_signal(price, ticker, order):
            sell_dict = {'flag': False, 'exit_type': None}
            stop_loss = self.stop_loss(price, order)
            trailing_stop = self.trailing_stop_loss(ticker, params)

            if stop_loss or trailing_stop:
                if stop_loss:
                    sell_dict['exit_type'] = 'stop_loss'
                elif trailing_stop:
                    sell_dict['exit_type'] = 'trailing_stop'
                sell_dict['flag'] = True
                return sell_dict
            else:
                return sell_dict

        def scale_out_signal(price, order):
            if order.state() == 'open':
                take_profit = self.take_profit(price, order)
                if take_profit:
                    return True
            else:
                return False

        for ticker in self.tradeable_tickers:
            price = dm.get_value(ticker, close, self.current_date,
                                 self.dataset, 1)
            if not self.is_stock_in_portfolio(ticker):
                if buy_signal(ticker):
                    self.add_buy_order(ticker)
            else:
                order = self.portfolio.get_open_order(ticker)
                sell_dict = sell_signal(price, ticker, order)
                if sell_dict['flag']:
                    self.add_sell_order(ticker, sell_dict['exit_type'])
                elif scale_out_signal(price, order):
                    self.add_scale_out_order(ticker)
Exemplo n.º 5
0
 def trailing_stop_loss(self, ticker, params):
     open_orders = self.portfolio.open_orders
     value = params['trailing_stop_parameter']
     stop_type = params['trailing_stop_type']
     if stop_type == 'percentage':
         for order in open_orders:
             if order.stock == ticker:
                 max_price = order.max_price
                 price = dm.get_value(ticker, self.price_field,
                                      self.current_date, self.dataset, 1)
                 loss = (max_price - price) / max_price
                 if loss >= value:
                     return True
                 else:
                     return False
         raise Exception(
             'trailing_stop_loss() - Ticker is not in open_orders!! Ticker['
             + str(ticker) + '] not available!!')
     elif stop_type.startswith('atr'):
         for order in open_orders:
             if order.stock == ticker:
                 atr = dm.get_value(ticker, stop_type, self.current_date,
                                    self.dataset, 1)
                 max_price = order.max_price
                 price = dm.get_value(ticker, self.price_field,
                                      self.current_date, self.dataset, 1)
                 loss = (max_price - price)
                 if loss >= atr * value:
                     return True
                 else:
                     return False
         raise Exception(
             'trailing_stop_loss() - Ticker is not in open_orders!! Ticker['
             + str(ticker) + '] not available!!')
     else:
         raise Exception('Trailing stop type: ' +
                         str(params['trailing_stop_type']) +
                         ' doesnt exit!')
Exemplo n.º 6
0
 def initial_stop_loss(self, ticker, value):
     #value = 0.2 means 20% stop loss
     open_orders = self.portfolio.open_orders
     for order in open_orders:
         if order['stock'] == ticker:
             buy_price = order['price']
             price = dm.get_value(ticker, self.price_field,
                                  self.current_date, self.dataset, 1)
             loss = (buy_price - price) / buy_price
             if loss >= value:
                 return True
             else:
                 return False
     raise Exception(
         'Initial_stop_loss - Ticker is not in open_orders!! Ticker[' +
         str(ticker) + '] not available!!')
Exemplo n.º 7
0
 def get_value(self, ticker,field, date = None):
     if date is None:
         date = self.current_day
     value = dm.get_value(ticker, field, date, self.dataset,  1)
     return value
Exemplo n.º 8
0
 def get_current_price(self, ticker):
     price = dm.get_value(ticker, self.price_field, self.current_day, self.dataset,  1)
     return price