Loading ...

简易反向跟单机器人

Author: pormogao, Date: 2021-11-24 13:52:09
Tags:

由于多数散户在合约市场的表现欠佳,时长出现账面浮亏,可以通过与自己做对手盘达到避险甚至盈利的效果; 开始时不能存在持仓,只支持单次开仓平仓。


buy = False
def main():
    global buy
    for i in range(len(exchanges)):
        exchanges[i].SetContractType("swap")
        exchanges[i].SetMarginLevel(lever)
        position = exchanges[i].GetPosition()
        if len(position)>0:
            Log('当前不能有持仓')
            exit()
    while True:
        try:
            position = exchanges[0].GetPosition()
            pos = exchanges[1].GetPosition()
            if len(position)==1 and buy == False:
                #多仓时开空
                if position[0]["Type"]==0:
                    exchanges[1].SetDirection("sell")
                    exchanges[1].Sell(-1, kc)
                    buy = True
                #空仓时开多 
                elif position[0]["Type"]==1:
                    exchanges[1].SetDirection("buy")
                    exchanges[1].Buy(-1, kc)
                    buy = True
                    
            elif len(position)==0 and buy == True:
                if len(pos)>0:
                    if pos[0]["Type"]==0:
                        exchanges[1].SetDirection("closebuy")
                        exchanges[1].Sell(-1, kc)
                        buy = False
                    elif pos[0]["Type"]==1:
                        exchanges[1].SetDirection("closesell")
                        exchanges[1].Buy(-1, kc)
                        buy = False
            Sleep(1000)
        except:
            Log("运行出现错误,停顿三秒")
            Sleep(3000)

More