'This part is the \'secret-sauce\' where actual trades takes place.
My take is that great experience, good portfolio construction,
and together with robust backtesting will make your strategy viable.
GOOD PORTFOLIO CONTRUCTION CAN SAVE YOU FROM BAD RESEARCH,
BUT BAD PORTFOLIO CONSTRUCTION CANNOT SAVE YOU FROM GREAT RESEARCH
This trade logic uses volatility ratio and beta as our indicators.
- volatility ratio > 1 :: uptrend, volatility ratio < 1 :: downtrend
- beta is calculated as: mean(price A) / mean(price B)
We use the assumption that prive levels will mean-revert.
Expected price A = beta x price B
Consider other methods of identifying our trade logic:
- current trend
- current regime
- detect structural breaks'

def __perform_trade_logic(self):

    volatility_ratio = self.strategy_params.get_volatility_ratio()
    (is_up_trend, is_down_trend) = ((volatility_ratio > 1), (volatility_ratio < 1))
    (is_overbought, is_oversold) = self.__is_overbought_or_oversold()
    (is_buy_signal, is_sell_signal) = ((is_up_trend and is_oversold), (is_down_trend and is_overbought))
    symbol_a = self.symbols[0]
    position = self.stocks_data[symbol_a].position
    (is_position_closed, is_short, is_long) = ((position == 0), (position < 0), (position > 0))
    (upnl, rpnl) = self.__calculate_pnls()
    signal_text = ('BUY' if is_buy_signal else ('SELL' if is_sell_signal else 'NONE'))
    console_output = ('\r[%s]    signal=%s,    position=%s    UPnL=%s    RPnL=%s\r' % (dt.datetime.now(), signal_text, position, upnl, rpnl))
    sys.stdout.write(console_output)
    sys.stdout.flush()
    if (is_position_closed and is_sell_signal):
        print '=================================='
        print 'OPEN    SHORT    POSIITON:    SELL    A    BUY    B'
        print '=================================='
        self.__place_spread_order((- self.trade_qty))
    elif (is_position_closed and is_buy_signal):
        print '=================================='
        print 'OPEN    LONG    POSIITON:    BUY    A    SELL    B'
        print '=================================='
        self.__place_spread_order(self.trade_qty)
    elif (is_short and is_buy_signal):
        print '=================================='
        print 'CLOSE    SHORT    POSITION:    BUY    A    SELL    B'
        print '=================================='
        self.__place_spread_order(self.trade_qty)
    elif (is_long and is_sell_signal):
        print '=================================='
        print 'CLOSE    LONG    POSITION:    SELL    A    BUY    B'
        print '=================================='
        self.__place_spread_order((- self.trade_qty))
