def pair_entry_rule(contract_group: ContractGroup, i: int, timestamps: np.ndarray, indicators: SimpleNamespace, signal: np.ndarray, account: Account, strategy_context: StrategyContextType) -> Sequence[Order]: timestamp = timestamps[i] assert(math.isclose(account.position(contract_group, timestamp), 0)) signal_value = signal[i] risk_percent = 0.1 orders = [] symbol = contract_group.name contract = contract_group.get_contract(symbol) if contract is None: contract = Contract.create(symbol, contract_group=contract_group) curr_equity = account.equity(timestamp) order_qty = np.round(curr_equity * risk_percent / indicators.c[i] * np.sign(signal_value)) print(f'order_qty: {order_qty} curr_equity: {curr_equity} timestamp: {timestamp}' f' risk_percent: {risk_percent} indicator: {indicators.c[i]} signal_value: {signal_value}') reason_code = ReasonCode.ENTER_LONG if order_qty > 0 else ReasonCode.ENTER_SHORT orders.append(MarketOrder(contract, timestamp, order_qty, reason_code=reason_code)) return orders
def pair_entry_rule(contract_group, i, timestamps, indicators, signal, account, strategy_context): timestamp = timestamps[i] assert (math.isclose(account.position(contract_group, timestamp), 0)) signal_value = signal[i] risk_percent = 0.1 orders = [] symbol = contract_group.name contract = contract_group.get_contract(symbol) if contract is None: contract = Contract.create(symbol, contract_group=contract_group) # if we don't already have a position, check if we should enter a trade #if math.isclose(curr_pos, 0): curr_equity = account.equity(timestamp) order_qty = np.round(curr_equity * risk_percent / indicators.c[i] * np.sign(signal_value)) trigger_price = indicators.c[i] print(f'order_qty: {order_qty} curr_equity: {curr_equity} timestamp: {timestamp}' + \ f' risk_percent: {risk_percent} indicator: {indicators.c[i]} signal_value: {signal_value}') reason_code = ReasonCode.ENTER_LONG if order_qty > 0 else ReasonCode.ENTER_SHORT orders.append( MarketOrder(contract, timestamp, order_qty, reason_code=reason_code)) return orders
def test_account(): #if __name__ == "__main__": from pyqstrat.pq_types import Contract, ContractGroup, Trade from pyqstrat.orders import MarketOrder import math def get_close_price(contract, timestamps, idx, strategy_context): if contract.symbol == "IBM": price = idx + 10.1 elif contract.symbol == "MSFT": price = idx + 15.3 else: raise Exception(f'unknown contract: {contract}') return price ContractGroup.clear() Contract.clear() ibm_cg = ContractGroup.create('IBM') msft_cg = ContractGroup.create('MSFT') ibm_contract = Contract.create('IBM', contract_group = ibm_cg) msft_contract = Contract.create('MSFT', contract_group = msft_cg) timestamps = np.array(['2018-01-01 09:00', '2018-01-02 08:00', '2018-01-02 09:00', '2018-01-05 13:35'], dtype = 'M8[m]') account = Account([ibm_cg, msft_cg], timestamps, get_close_price, None) #account = Account([Contract(symbol)], timestamps, get_close_price) trade_1 = Trade(ibm_contract, np.datetime64('2018-01-02 08:00'), 10, 10.1, commission = 0.01, order = MarketOrder(ibm_contract, np.datetime64('2018-01-01 09:00'), 10)) trade_2 = Trade(ibm_contract, np.datetime64('2018-01-02 09:00'), -20, 15.1, commission = 0.02, order = MarketOrder(ibm_contract, np.datetime64('2018-01-01 09:00'), -20)) trade_3 = Trade(msft_contract, timestamps[1], 20, 13.2, commission = 0.04, order = MarketOrder(msft_contract, timestamps[1], 15)) trade_4 = Trade(msft_contract, timestamps[2], 20, 16.2, commission = 0.05, order = MarketOrder(msft_contract, timestamps[2], 20)) account.add_trades([trade_1, trade_2, trade_3, trade_4]) account.calc(np.datetime64('2018-01-05 13:35')) assert(len(account.df_trades()) == 4) assert(len(account.df_pnl()) == 6) assert(np.allclose(np.array([9.99, 61.96, 79.97, 103.91, 69.97, 143.91]), account.df_pnl().net_pnl.values, rtol = 0)) assert(np.allclose(np.array([10, 20, -10, 40, -10, 40]), account.df_pnl().position.values, rtol = 0)) assert(np.allclose(np.array([1000000. , 1000183.88, 1000213.88]), account.df_account_pnl().equity.values, rtol = 0))
def pair_exit_rule(contract_group, i, timestamps, indicators, signal, account, strategy_context): timestamp = timestamps[i] curr_pos = account.position(contract_group, timestamp) assert(not math.isclose(curr_pos, 0)) signal_value = signal[i] orders = [] symbol = contract_group.name contract = contract_group.get_contract(symbol) if contract is None: contract = Contract.create(symbol, contract_group = contract_group) if (curr_pos > 0 and signal_value == -1) or (curr_pos < 0 and signal_value == 1): order_qty = -curr_pos reason_code = ReasonCode.EXIT_LONG if order_qty < 0 else ReasonCode.EXIT_SHORT orders.append(MarketOrder(contract, timestamp, order_qty, reason_code = reason_code)) return orders
def pair_exit_rule(contract_group: ContractGroup, i: int, timestamps: np.ndarray, indicators: SimpleNamespace, signal: np.ndarray, account: Account, strategy_context: StrategyContextType) -> Sequence[Order]: timestamp = timestamps[i] curr_pos = account.position(contract_group, timestamp) assert(not math.isclose(curr_pos, 0)) signal_value = signal[i] orders = [] symbol = contract_group.name contract = contract_group.get_contract(symbol) if contract is None: contract = Contract.create(symbol, contract_group=contract_group) if (curr_pos > 0 and signal_value == -1) or (curr_pos < 0 and signal_value == 1): order_qty = -curr_pos reason_code = ReasonCode.EXIT_LONG if order_qty < 0 else ReasonCode.EXIT_SHORT orders.append(MarketOrder(contract, timestamp, order_qty, reason_code=reason_code)) return orders
from pyqstrat.orders import MarketOrder import math def get_close_price(contract, timestamps, idx, strategy_context): if contract.symbol == "IBM": price = idx + 10.1 elif contract.symbol == "MSFT": price = idx + 15.3 else: raise Exception(f'unknown contract: {contract}') return price ibm_cg = ContractGroup.create('IBM') msft_cg = ContractGroup.create('MSFT') ibm_contract = Contract.create('IBM', contract_group=ibm_cg) msft_contract = Contract.create('MSFT', contract_group=msft_cg) timestamps = np.array([ '2018-01-01 09:00', '2018-01-02 08:00', '2018-01-02 09:00', '2018-01-05 13:35' ], dtype='M8[m]') account = Account([ibm_cg, msft_cg], timestamps, get_close_price, None) #account = Account([Contract(symbol)], timestamps, get_close_price) trade_1 = Trade(ibm_contract, np.datetime64('2018-01-02 08:00'), 10, 10.1, commission=0.01, order=MarketOrder(ibm_contract, np.datetime64('2018-01-01 09:00'), 10))