import numpy as npimport pandas as pdimport talibdef initContextInfo MarketPosition = ContextInfoMarketPosition = MarketPosition # 初始化持仓 ContextInfoset_universeContextInfostockcode + + ContextInfom
def order_shares(stockcode, shares, price_type, price, ContextInfo, accountID): if shares == 0: return order_price = 0 if price_type == 'fix': order_price = price else: order_price = 0 order_ref = 'order' + str(ContextInfo.barpos) if shares > 0: order_side = 'BUY' else: order_side = 'SELL' shares = abs(shares) if shares % 100 > 0: shares = shares - shares % 100 order = create_trade_order(order_ref, accountID, stockcode, order_side, shares, order_price, 'STOCK') send_order(order)
def create_trade_order(order_ref, accountID, stockcode, order_side, shares, order_price, order_type): order = TradeOrder() order.m_strRef = order_ref order.m_strAccountID = accountID order.m_strInstrumentID = stockcode order.m_strExchangeID = 'SZSE' order.m_nVolume = shares order.m_nPrice = order_price if order_side == 'BUY': order.m_nOffset = 'OPEN' else: order.m_nOffset = 'CLOSE' order.m_nOrderType = 'LIMIT' order.m_nTimeCondition = 'GTC' order.m_nVolumeCondition = 'ANY' order.m_nOrderStatus = 'WORKING' order.m_nOrderSide = order_side order.m_strOrderType = 'STOCK' return order
def send_order(order): order.m_strOrderID = str(np.random.randint(1000000, 9999999)) order.m_strEntrustTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S') order.m_strOrderStatus = 'SUCCESS' insert_trade_order_data(order)
def timetag_to_datetime(timetag, date_format): return datetime.strptime(str(timetag), '%Y%m%d%H%M%S').strftime(date_format)
原文地址: https://www.cveoy.top/t/topic/bnVV 著作权归作者所有。请勿转载和采集!