Loading ...

商品期货预定开仓后平仓机器人

Author: 小草, Date: 2020-02-29 19:49:37
Tags: Commodity-futures

如题,可设定买入价格,买入成功后自动挂卖出价格卖出.

插件可以在交易终端一键启动,不收取费用,方便手动交易。详细介绍:https://www.fmz.com/digest-topic/5051


/*backtest
start: 2020-01-30 00:00:00
end: 2020-02-28 00:00:00
period: 5m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["Direction",0]]
*/


var buy = false
var trade_amount = 0
function main(){
    while(true){
        if(exchange.IO("status")){
            exchange.SetContractType(Contract)
            if(!buy){
                buy = true
                if(Direction == 0){
                    exchange.SetDirection('buy')
                    exchange.Buy(Open_Price, Amount)
                }else{
                    exchange.SetDirection('sell')
                    exchange.Sell(Open_Price, Amount)
                }
            }
            var pos = exchange.GetPosition()
            if(pos && pos.length > 0){
                for(var i=0;i<pos.length;i++){
                    if(pos[i].ContractType == Contract && pos[i].Type == Direction && pos[i].Amount-pos[i].FrozenAmount>0){
                        var cover_amount = math.min(Amount-trade_amount, pos[i].Amount-pos[i].FrozenAmount)
                        if(cover_amount >= 1){
                            trade_amount += cover_amount
                            if(Direction == 0){
                                exchange.SetDirection('closebuy_today')
                                exchange.Sell(Close_Price, cover_amount)
                            }else{
                                exchange.SetDirection('closesell_today')
                                exchange.Buy(Close_Price, cover_amount)
                            }
                        }
                    }
                }
            }
        } else {
            LogStatus(_D(), "未连接CTP !")
            Sleep(10000)
        }
        if(trade_amount >= Amount){
            Log('任务完成')
            return
        }
        Sleep(1000)
    }
}


Related

More