Loading ...

1btc版,空头解锁策略 , 最大仓位约2倍 Fmex,后市看空

Author: 有朋-FMex-高频排序, Date: 2019-12-15 04:11:25
Tags:

FMex排序挖矿空头版本代码使用说明。(注意api地址)(微信:ying5737) (预计日缓慢跌幅1%以上,赚币赚矿,反之亏损明显) 保证金市场风险巨大,你可能随时面临100%损失。或有不明bug100%损失,概不负责。

原理:盘口下单随机成交/ 先持仓SHORT1-1000u。// - 检测现有订单是否超过界限,如果超过了就立马取消该订单/ - 检测是否成交形成了仓位,如果大于仓位xxu,则减仓到既定仓位以下/

几种情况: 全局挂单:为区别市商策略,特定定义远端排序为挂单。参数可调。 市商: 最大多头持仓,若大于该仓位,约每6s减仓位. 最大空头持仓,若大于该仓位,约每6s减仓位. 大于1u多仓启动减多仓策略,挂单为shor为主 大于空头仓位,启动减空仓策略,挂单为long为主 正常持仓

//参数中的备注描述,仅供参考,另可加挡位 风险自负/参数可调,微信:ying5737


exchange.IO("base", Url)//保证金市场风险巨大,你可能随时面临100%损失。或有不明bug100%损失,概不负责。
//实盘地址:https://api.fmex.com 测试网https://api.fmextest.net//注意:默认近端排序不启动(给手动平仓预留空间),多头版先持仓long1u-1000u,空头版先持仓short1u-1000u.用来激活近端排序.
var ordersInfo = {//(联系微信:ying5737)策略仅限个人使用,若用于商业传播,请提前联系
    buyId: 0,    buyPrice: 0,    sellId: 0,    sellPrice: 0,    minPrice: 0,    maxPrice: 0
}
var depthInfo = {
    asks: [],
    bids: []
}
function getTicker(symbol) {
    url = "/v2/market/ticker/" + symbol;
    data = _C(exchange.IO,"api", "GET", url);
    return data.data;
}    
function getAccounts() {
    data = _C(exchange.IO,"api", "GET", "/v3/contracts/accounts")
    return data.data;
}
function createOrderPrice(body) {
    parameter = "symbol=" + body.symbol + "&type=" + body.type + "&direction=" + body.direction + "&post_only=" + body.post_only +  "&price=" + body.price + "&quantity=" + body.quantity + "&affiliate_code=" + body.affiliate_code;   
    resultData = _C(exchange.IO,"api", "POST", "/v3/contracts/orders", parameter)
    return resultData;
}
function createOrder(body) {
    parameter = "symbol=" + body.symbol + "&type=" + body.type + "&direction=" + body.direction + "&quantity=" + body.quantity + "&affiliate_code=" + body.affiliate_code;   
    resultData = _C(exchange.IO,"api", "POST", "/v3/contracts/orders", parameter)    
    return resultData;
}
function getOrders() {
    resultData = _C(exchange.IO,"api", "GET", "/v3/contracts/orders/open");
    return resultData.data
}
function cancelOrder(id) {
    resultData = _C(exchange.IO,"api", "POST", "/v3/contracts/orders/"  + "/cancel");//+ id 
    return resultData;    
}
function getPosition() {
    resultData = _C(exchange.IO,"api", "GET", "/v3/broker/auth/contracts/positions");
    return resultData.data;
}
function getMatches(id) {
    resultData = _C(exchange.IO,"api", "GET", "/v3/contracts/orders/" + id + "/matches");
    return resultData.data;
}
function getCandles(resolution, symbol) {
    resultData = _C(exchange.IO,"api", "GET", "/v2/market/candles/" + resolution + "/" + symbol);
    return resultData.data;
}
function cleanOrders(lastPrice) {
    ordersInfo = getOrders();
    orders = ordersInfo.results;
    for (var i = 0; i < orders.length; i++) {
        diffPercent = Math.abs(lastPrice - orders[i].price) / lastPrice * 100
        if (diffPercent < g_minPercent || diffPercent > g_maxPercent) {
            orderInfo = cancelOrder(orders[i].id);         
            Log("取消订单");
        }
    }
}
function getToOrderPrice(basePrice, direct) {
    return parseInt(basePrice * (direct * (g_minPercent + Math.random() * (g_maxPercent - g_minPercent)) / 100 + 1))
}
function cleanPosition() {
    res = getPosition();    
    res.results.forEach(function(it) {
        if (it.symbol == 'BTCUSD_P') {
            if (it.quantity) {
                if (it.quantity > g_maxHoldingLong && it.direction.toUpperCase() == 'LONG') { 
                    data = createOrder({
                        symbol: "BTCUSD_P",
                        type: "MARKET",
                        direction: it.direction.toUpperCase() == 'LONG' ? "SHORT" : "LONG",
                        quantity: 80,   
                        affiliate_code: "9y40d8"
                    })
                    Log("LONG超最大仓位,减仓");
                }
                if (it.quantity > g_maxHoldingShort && it.direction.toUpperCase() == 'SHORT') {
                    data = createOrder({
                        symbol: "BTCUSD_P",
                        type: "MARKET",
                        direction: it.direction.toUpperCase() == 'LONG' ? "SHORT" : "LONG",
                        quantity: 80,     
                        affiliate_code: "9y40d8"
                    })
                    Log("SHORT超最大仓位,减仓");
                }
            }
        }
    });
}
function guadan() {
    tickerInfo = getTicker('BTCUSD_P');
    ticker = tickerInfo.ticker;
    
    var lastPrice = ticker[0]; 
    Log("lastPrice: "+lastPrice);
    buyPrice = getToOrderPrice(lastPrice, -1);
    sellPrice = getToOrderPrice(lastPrice, 1);
      
    LongOrderInfo = createOrderPrice({
        symbol: "BTCUSD_P",
        type: "LIMIT",
        direction: "LONG",
        post_only: true,
        price: buyPrice,
        quantity: g_perAmount,
        affiliate_code: "9y40d8"
    })
    Log("挂单LONG" );
    shortOrderInfo = createOrderPrice({
        symbol: "BTCUSD_P",
        type: "LIMIT",
        direction: "SHORT",
        post_only: true,
        price: sellPrice,
        quantity: g_perAmount,
        affiliate_code: "9y40d8"
    })
    Log("挂单SHORT" );  
    cleanOrders(lastPrice)
    cleanPosition();
}
function shishang() {
    Log("市商/排序挖矿**********************************");
    tickerInfo = getTicker('BTCUSD_P');
    ticker = tickerInfo.ticker;
    Log("市商/排序挖矿ticker:");
    lastPrice = ticker[0] 
    buyPrice = ticker[2] 
    sellPrice = ticker[4] 
    Log("lastPrice:"+lastPrice+":buyPrice:"+buyPrice+":sellPrice:" + sellPrice);
    if (lastPrice == buyPrice) {
        sellPrice = (buyPrice + 0.5).toFixed(1)
    }
    if (lastPrice == sellPrice) {
        buyPrice = (sellPrice - 0.5).toFixed(1)
    }
    Log("buyPrice:"+buyPrice+":sellPrice:" + sellPrice);
    // const LastK7 = k[7]; // 最近第九根 
    res = getPosition();
    Log("市商/排序挖矿Position:" + JSON.stringify(res));
    res.results.forEach(function(it) {
        
        if (it.symbol == 'BTCUSD_P') {
            if (it.quantity) {
                if (it.quantity > sp_baseAmountLong && it.direction.toUpperCase() == 'LONG') {//多仓过多,改变挂单档位,short3-9
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 1.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖3" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 2,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖4" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 2.5,
                        quantity: sp_perAmount,
                       affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖5" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 3,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖6" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 3.5,
                        quantity: sp_perAmount,  
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖7" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 4,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 4.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖9" );
                } else if (it.quantity > sp_baseAmountShort && it.direction.toUpperCase() == 'SHORT') {//空仓short仓位过多,改变挂单档位,long2-6
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买6" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 1,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买2" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 1.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买3" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 2,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买4" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 2.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买5" );
                } else {//正常持仓状态,short2-7              long6789    ,10  
                     order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice - 1.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖3" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 2,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖4" );
                         order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 1,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖2" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 2.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖5" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 3,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖6" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 3.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT卖7" );
                     order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买6" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买7" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 4,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 4.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买9" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG买10" );
                  
                }
            }
        }
    })
}
/********************END 市商/排序挖矿***************************************************************************************************/
// 模板初始化时调用

function init() {
    // 过滤常见错误
    if (EnableErrorFilter) {
        SetErrorFilter("502:|503:|tcp|character|connection|unexpected|network|timeout|WSARecv|Connect|GetAddr|no such|reset|http|received|EOF|reused");
    }
    // 重定义需要容错的函数
    var names = ApiList.split(',');
    _.each(exchanges, function(e) {
        _.each(names, function(name) {
            if (typeof(e[name]) !== 'function') {
                throw "尝试容错 " + name + " 失败, 请确认存在此API并且输入正确.";
            }
            var old = e[name];
            e[name] = function() {
                var r;
                while (!(r = old.apply(this, Array.prototype.slice.call(arguments)))) {
                    if (Debug) {
                        Log(e.GetLabel(), name, "调用失败", RetryInterval, "毫秒后重试...");
                    }
                    Sleep(RetryInterval);
                }
                return r;
            };
        });
    });
    Log("容错机制开启", names);
    
}

function main() {
   
    while(true){
        
        guadan();
        Sleep(Intervel * 1000)       
        shishang()
        Sleep(mainIntervel * 1000)
    }    
}//FMex排序挖矿多头版本代码使用说明。(注意api地址)联系微信:ying5737)

More