Loading ...

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

Author: 古力十段, Date: 2019-12-14 04:20:15
Tags:

FMex排序挖矿多头版本代码使用说明

//保证金市场风险巨大,你可能随时面临100%损失。或有不明bug100%损失,概不负责。 //(若考虑持仓做空或震荡,可对应灵活修改参数和代码挡位) //目前最大仓位(转向计算),建议0.5-3倍以内。若0.1B,3倍杠杆,转向仓位约2700,最大仓位3200,调到10倍杠杆,则转向仓位为9000u,最大仓位10000u,单量可以调整为300-500u //实盘地址:https://api.fmex.com 测试网https://api.fmextest.net//注意:首先手动持仓long1-1000u。 //(联系微信:ying5737) 策略原理:

图片仅供参考 https://wx1.sinaimg.cn/mw690/c5775633ly1gaajdxk8a8j20u10f4dhx.jpg

盘口下单随机成交/该策略默认倾向于持有多头仓位/ 注意:先持仓long1u-1000u。// - 检测现有订单是否超过界限,如果超过了就立马取消该订单/ - 检测是否成交形成了仓位,如果大于仓位xxu,则减仓到既定仓位以下/

几种挂单情况:

全局挂单: 为区别市商策略,特定定义远端排序为挂单。参数可调,目前在8档左右。

市商(盘口近端挂单): 最大多头持仓自定义,若大于该仓位,约每6s减仓位,直到小于自定义参数. 最大空头持仓自定义,若大于该仓位,约每6s减仓位,直到小于自定义参数. 大于多仓启动减多仓策略,挂单为short为主:成交空头仓位 大于1u空头仓位,启动减空头仓位策略,挂单为long:极易成交多单 正常持仓(盘口波动随机成交多单)

//参数中的备注描述,仅供参考,另可加挡位参考如下:

order = createOrderPrice({ symbol: “BTCUSD_P”, type: “LIMIT”, direction: “SHORT”,//选择多空,short或long post_only: true, price: lastPrice + 5.5,//数字可调0.5或0.5的倍数 quantity: sp_perAmount, affiliate_code: “9y40d8” })

风险自负/参数可调,微信:ying5737 优化方向:加入均线或K线对比来确定方向,挡位优化,增加自定义单量等/


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" );  
    Sleep(1000 * Intervel);
    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') {
                     order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 1,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    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" );
                           Log("市商/排序LONG6" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG7" );     
                        order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 3.5,
                        quantity: sp_perAmount,
                        affiliate_code:"9y40d8"
                    })
                    Log("市商/排序SHORT7" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 4,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 4.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT9" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 3.5,
                        quantity: sp_perAmount,
                        affiliate_code:"9y40d8"
                    })
                    Log("市商/排序SHORT7" );
                } else if (it.quantity > sp_baseAmountShort && it.direction.toUpperCase() == 'SHORT' ) {//空仓short仓位过多,
                    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" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 3.5,
                        quantity: sp_perAmount,
                        affiliate_code:"9y40d8"
                    })
                    Log("市商/排序SHORT7" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 4,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 4.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT9" );
                } else {//正常持仓状态,  
                 // if (lastPrice > 11000 || lastPrice < 12000) {
                     order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 1.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG3" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 2,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG4" );
                         order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 1,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG2" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 2.5,
                        quantity: sp_perAmount,
                        affiliate_code:"9y40d8"
                    })
                    Log("市商/排序LONG5" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG6" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG7" );     
                        order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 3.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 4,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 4.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG9" );     
                        order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "LONG",
                        post_only: true,
                        price: lastPrice - 5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序LONG10" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 3.5,
                        quantity: sp_perAmount,
                        affiliate_code:"9y40d8"
                    })
                    Log("市商/排序SHORT10" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 4,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT8" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 4.5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT9" );
                    order = createOrderPrice({
                        symbol: "BTCUSD_P",
                        type: "LIMIT",
                        direction: "SHORT",
                        post_only: true,
                        price: lastPrice + 5,
                        quantity: sp_perAmount,
                        affiliate_code: "9y40d8"
                    })
                    Log("市商/排序SHORT10" );
                   
               //   }
                }
            }
        }
    })
}
/********************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){
        //onTick();
        shishang();
        Sleep(mainIntervel * 1000)
        guadan();
        Sleep(Intervel * 1000)       
        
    }    
}//FMex排序挖矿多头版本代码使用说明。(注意api地址)微信:ying5737)

More

古力十段 优化排序远端,到第8档左右。参数为0.06/

古力十段 优化排序远端,到第8档左右。