top of page

陳兆庚量化交易程式
Kenavada Autotrade System

免費資源
Free Resource

這是我們過去編程系統的重要資源和核心技術,如果各位程式開發者覺得有用, 歡迎隨便複製使用: 

(1)市場選擇:

Market_Type = OpenHKTradeContext #香港正股
Market_Type =  OpenUSTradeContext #美國正股
Market_Type = OpenHKCCTradeContext #A 股通市场
Market_Type = OpenCNTradeContext    #A 股市场
Market_Type = OpenFutureTradeContext #期货市场

 

 

(2)開倉資源:

2.1 買入:

def do_buy(buy_price, buy_qty, scode):
    position_df = get_position_df()
    code_df = position_df.query(f'code=="{scode}"').query('position_side=="LONG"')
    if code_df.empty:
        qty = 0
    else:
        qty = code_df['qty'].values[0]
    if qty >= 0:
        trd_ctx = Market_Type(host='127.0.0.1', port=11111)
        print(trd_ctx.unlock_trade(pwd_unlock))
        print(trd_ctx.place_order(price=buy_price, qty=buy_qty, code=scode, trd_side=TrdSide.BUY))
        trd_ctx.close()

2.2 賣出:

def do_sell(sell_price, sell_qty, scode):
    position_df = get_position_df()
    code_df = position_df.query(f'code=="{scode}"').query('position_side=="LONG"')
    if code_df.empty:
        qty = 0
    else:
        qty = code_df['qty'].values[0]
    if qty > 0:
        trd_ctx = Market_Type(host='127.0.0.1', port=11111)
        print(trd_ctx.unlock_trade(pwd_unlock))
        print(trd_ctx.place_order(price=sell_price, qty=sell_qty, code=scode, trd_side=TrdSide.SELL))
        trd_ctx.close()​

2.3 沽空平倉:

def do_sell_back(buy_price, scode):
    """买"""
    position_df = get_position_df()
    code_df = position_df.query(f'code=="{scode}"').query('position_side=="SHORT"')
    if code_df.empty:
        qty = 0
    else:
        qty = code_df['qty'].values[0]
    if qty < 0:
        buy_qty = abs(qty)
        trd_ctx = OpenFutureTradeContext(host='127.0.0.1', port=11111)
        print(trd_ctx.unlock_trade(pwd_unlock))
        print(trd_ctx.place_order(price=buy_price, qty=buy_qty, code=scode, trd_side=TrdSide.BUY))
        trd_ctx.close()
        time.sleep(sleep_time)

2.4 買入平倉:

def do_buy_back(sell_price, scode):
    position_df = get_position_df()
    code_df = position_df.query(f'code=="{scode}"').query('position_side=="LONG"')
    if code_df.empty:
        qty = 0
    else:
        qty = code_df['qty'].values[0]
    if qty > 0:
        sell_qty = abs(qty)
        trd_ctx = OpenFutureTradeContext(host='127.0.0.1', port=11111)
        print(trd_ctx.unlock_trade(pwd_unlock))
        print(trd_ctx.place_order(price=sell_price, qty=sell_qty, code=scode, trd_side=TrdSide.SELL))
        trd_ctx.close()
        time.sleep(sleep_time)

 

​​

​​

(3)報價:​

​def get_now_price(scode):
    quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
    ret_sub, err_message = quote_ctx.subscribe([scode], [SubType.QUOTE], subscribe_push=False)
    # 先订阅 K 线类型。订阅成功后 FutuOpenD 将持续收到服务器的推送,False 代表暂时不需要推送给脚本
    if ret_sub == RET_OK:  # 订阅成功
        ret, data = quote_ctx.get_stock_quote([scode])  # 获取订阅股票报价的实时数据
        if ret == RET_OK:
            pass
    else:
        print('subscription failed', err_message)
    quote_ctx.close()  # 关闭当条连接,FutuOpenD 会在1分钟后自动取消相应股票相应类型的订阅
    return data['last_price'].values[0]

(4) 檢查倉位:

 

 

def get_position_df():
    """获取持仓"""
    trd_ctx = OpenFutureTradeContext(host='127.0.0.1', port=11111)
    trd_ctx.unlock_trade(pwd_unlock)
    data = trd_ctx.position_list_query()
    trd_ctx.close()
    return data[1]

bottom of page