资源加载中... loading...

均线策略-赢减输加

Author: 13797386711, Date: 2022-07-24 21:29:39
Tags:


import random
def Display():
    position = _C(exchange.GetPosition)#获取持仓订单
    money_all = 0
    if len(position) > 0:
        for i in range(len(position)):
            if position[i]["Type"] == 0:
                Log("持仓方向:买涨",",持仓量:",position[i]["Amount"],",仓位冻结:",position[i]["FrozenAmount"],",持仓均价:",position[i]["Price"],
                    ",盯市盈亏:",position[i]["Profit"],",合约代码:",position[i]["ContractType"],",仓位占用保证金:",position[i]["Margin"])
            elif position[i]["Type"] == 1:
                Log("持仓方向:买空",",持仓量:",position[i]["Amount"],",仓位冻结:",position[i]["FrozenAmount"],",持仓均价:",position[i]["Price"],
                    ",盯市盈亏:",position[i]["Profit"],",合约代码:",position[i]["ContractType"],",仓位占用保证金:",position[i]["Margin"])
            money_all = money_all + position[i]["Margin"]
            
    #获取账户信息  余额  挂单余额
    account = _C(exchange.GetAccount)
    Log("可用余额:", account["Balance"], "  挂单冻结余额:", account["FrozenBalance"])
    money_all = money_all + account["Balance"]
    Log("总权益:",money_all)
        
def main():
    num = 0
    mode = exchange.IO("mode", 0)
    if mode is None:
        raise Exception("切换模式失败,请更新到最新托管者!")
    while not exchange.IO("status"):
        Sleep(3000)
        LogStatus("正在等待与交易服务器连接," + _D())
        
    #返回交易所名称
    exchange_name = _C(exchange.GetName)
    Log("交易所名称:",exchange_name)
    #返回期货还是股票
    currency_name = _C(exchange.GetCurrency)
    if currency_name == "STOCK" or currency_name == "STOCK_CNY":
        Log("当前交易对象为:股票证券")
    elif currency_name == "FUTURES" or currency_name == "FUTURES_CNY":
        Log("当前交易对象为:商品期货")
    
    unlock_flg = 0#锁仓标志
    display_flg = 0#显示仓位标志位
    trad_direction = 0#下单状态  0未下单 1买涨  -1买跌
    suodin_flg = 0#锁仓标志
    
    Price_num = 0;
    sell_price_num = sell_price_num_config;

    #设置合约代码
    if moni_flg == 1:
        Log("交易状态:实盘")
        type = exchange.SetContractType("AP210")
        Log(type)
    else:
        Log("交易状态:模拟盘")
        type = exchange.SetContractType("AP888")
        Log(type)
        
    while 1:
        num = num + 1
        if num > 1000000:
            num = 0
        if num % 100 == 0:
            display_flg = 1
        now_time = _D()#获取当前时间zz
        if int(now_time[11:13]) >= 9 and int(now_time[11:13]) <= 14:
            r = exchange.GetRecords(PERIOD_M1)
            ticker = _C(exchange.GetTicker)#获取行情
            if trad_direction == 0:#未交易
                #获取随机值
                rand_num = random.randint(0,1)
                #进入买空切入点
                if rand_num == 1 :
                    exchange.SetDirection("buy")#买涨
                    if moni_flg == 1:
                        exchange.Buy(-1,trad_lots)#下单
                    else:
                        exchange.Buy(ticker["Last"] + 1,trad_lots)#下单
                    trad_direction = 1
                    Log("买涨")
                elif rand_num == 0:
                    exchange.SetDirection("sell")#买空
                    if moni_flg == 1:
                        exchange.Sell(-1,trad_lots)#下单
                    else:
                        exchange.Sell(ticker["Last"] - 1,trad_lots)#下单
                    trad_direction = -1
                    Log("买空")

            elif trad_direction == 1:
                position = _C(exchange.GetPosition)#获取持仓订单
                
                if len(position) > 0:
                    Price_num = ticker["Last"] - position[0]["Price"]
                    Log("Price_num = :",Price_num);
                    if Price_num >= sell_price_num:
                        Display()
                        #符合平仓条件
                        exchange.SetDirection("closebuy")#设置买入平仓
                        if moni_flg == 1:
                            exchange.Sell(-1,trad_lots)#平仓
                        else:
                            exchange.Sell(ticker["Last"] - 1,trad_lots)#平仓
                        trad_direction = 0
                        Log("买涨平仓")
                        display_flg = 1
                        sell_price_num = sell_price_num - sell_price_tiao;
                        if sell_price_num < sell_price_tiao:
                            sell_price_num = sell_price_tiao;
                        Log("当前点位:",sell_price_num);
                    elif Price_num <= -sell_price_num:
                        Display()
                        #符合平仓条件
                        exchange.SetDirection("closebuy")#设置买入平仓
                        if moni_flg == 1:
                            exchange.Sell(-1,trad_lots)#平仓
                        else:
                            exchange.Sell(ticker["Last"] - 1,trad_lots)#平仓
                        trad_direction = 0
                        Log("买涨平仓")
                        display_flg = 1
                        sell_price_num = sell_price_num + sell_price_tiao;
                        if sell_price_num > sell_price_max:
                            sell_price_num = sell_price_max;
                        Log("当前点位:",sell_price_num);
                # if int(now_time[11:13]) == 14 and int(now_time[14:16]) > 40 and suodin_flg == 0:
                #     Display()
                #     exchange.SetDirection("sell")#买空
                #     if moni_flg == 1:
                #         exchange.Sell(-1,trad_lots)#下单
                #     else:
                #         exchange.Sell(ticker["Last"] - 1,trad_lots)#平仓
                    
                #     Log("隔夜锁仓")
                #     suodin_flg = 1
                # elif int(now_time[11:13]) >= 9 and int(now_time[11:13]) <= 12 and int(now_time[14:16]) > 20 and suodin_flg == 1:
                #     Display()
                #     exchange.SetDirection("closesell")#设置卖出平仓
                #     if moni_flg == 1:
                #         exchange.Buy(-1,trad_lots)#平仓
                #     else:
                #         exchange.Buy(ticker["Last"] + 1,trad_lots)#平仓
                #     Log("仓位解锁")
                #     suodin_flg = 0
            elif trad_direction == -1:
                position = _C(exchange.GetPosition)#获取持仓订单
                if len(position) > 0:
                    Price_num = position[0]["Price"] - ticker["Last"];
                    if Price_num >= sell_price_num:
                        Display()
                        #符合平仓条件
                        exchange.SetDirection("closesell")#设置卖出平仓
                        if moni_flg == 1:
                            exchange.Buy(-1,trad_lots)#平仓
                        else:
                            exchange.Buy(ticker["Last"] + 1,trad_lots)#平仓
                        trad_direction = 0
                        Log("买空平仓")
                        display_flg = 1
                        sell_price_num = sell_price_num - sell_price_tiao;
                        if sell_price_num < sell_price_tiao:
                            sell_price_num = sell_price_tiao;
                        Log("当前点位:",sell_price_num);
                    elif Price_num <= -sell_price_num:
                        Display()
                        #符合平仓条件
                        exchange.SetDirection("closesell")#设置卖出平仓
                        if moni_flg == 1:
                            exchange.Buy(-1,trad_lots)#平仓
                        else:
                            exchange.Buy(ticker["Last"] + 1,trad_lots)#平仓
                        trad_direction = 0
                        Log("买空平仓")
                        sell_price_num = sell_price_num + sell_price_tiao;
                        if sell_price_num > sell_price_max:
                            sell_price_num = sell_price_max;
                        Log("当前点位:",sell_price_num);

                # if int(now_time[11:13]) == 14 and int(now_time[14:16]) > 40 and suodin_flg == 0:
                #     exchange.SetDirection("buy")#买涨
                #     if moni_flg == 1:
                #         exchange.Buy(-1,trad_lots)#下单
                #     else:
                #         exchange.Buy(ticker["Last"] + 1,trad_lots)#下单
                #     Log("隔夜锁仓")
                #     suodin_flg = -1
                # elif int(now_time[11:13]) >= 9 and int(now_time[11:13]) <= 12 and int(now_time[14:16]) > 20 and suodin_flg == -1:
                #     exchange.SetDirection("closebuy")#设置买入平仓
                #     if moni_flg == 1:
                #         exchange.Sell(-1,trad_lots)#平仓
                #     else:
                #         exchange.Sell(ticker["Last"] - 1,trad_lots)#平仓
                    
                #     Log("仓位解锁")
                #     suodin_flg = 0
            if num % 1200 == 0:
                Log("运行中",num)
                Display()
        else:
            if num % 120 == 0:
                Log("等待开盘",num)
                
        Sleep(500)



更多内容