# 期货反手加倍算法

Author: Zero, Date: 2015-01-02 15:52:17
Tags: Martingale

796期货反手加倍算法, 先开空或多, 也可以指定失败了继续上次方向, 如果失败了就按指定倍数加仓, 策略有风险, 仅限学习使用.

```
var OrgAccount = null;
var Counter = {s : 0, f: 0};
var LastProfit = 0;
var AllProfit = 0;
var _Failed = 0;

function StripOrders(e, orderId) {
var order = null;
if (typeof(orderId) == 'undefined') {
orderId = null;
}
while (true) {
var dropped = 0;
var orders = _C(e.GetOrders);
for (var i = 0; i < orders.length; i++) {
if (orders[i].Id == orderId) {
order = orders[i];
} else {
var extra = "";
if (orders[i].DealAmount > 0) {
extra = "成交: " + orders[i].DealAmount;
} else {
extra = "未成交";
}
e.CancelOrder(orders[i].Id, orders[i].Type == ORDER_TYPE_BUY ? "买单" : "卖单", extra);
dropped++;
}
}
if (dropped == 0) {
break;
}
Sleep(300);
}
return order;
}

var preMsg = "";
function GetAccount(e, waitFrozen) {
if (typeof(waitFrozen) == 'undefined') {
waitFrozen = false;
}
var account = null;
while (true) {
account = _C(e.GetAccount);
if (!waitFrozen || account.FrozenStocks < MinStock) {
break;
}
Log("发现账户有冻结的钱或币", account);
}
Sleep(Interval);
}
msg = "成功: " + Counter.s + " 次, 失败: " + Counter.f + " 次, 当前账户 币: " + account.Stocks;
if (account.FrozenStocks > 0) {
msg += " 冻结的币: " + account.FrozenStocks;
}

if (msg != preMsg) {
preMsg = msg;
LogStatus(msg, "#ff0000");
}
return account;
}

function GetPosition(e, orderType) {
var positions = _C(e.GetPosition);
if (typeof(orderType) == 'undefined') {
return positions;
}
for (var i = 0; i < positions.length; i++) {
if (positions[i].Type == orderType) {
return positions[i];
}
}
return null;
}

function GetTicker(e) {
while (true) {
var ticker = _C(e.GetTicker);
if (ticker.Buy > 0 && ticker.Sell > 0 && ticker.Sell > ticker.Buy) {
return ticker;
}
Sleep(100);
}
}
var nowPosition = initPosition;
var orderId = null;
var prePrice = 0;
var dealAmount = 0;
var diffMoney = 0;
var isFirst = true;
while (true) {
var account = _C(e.GetAccount);
var ticker = GetTicker(e);
tradePrice = _N((mode == 0 ? ticker.Sell : ticker.Buy) + slidePrice, 4);
} else {
tradePrice = _N((mode == 0 ? ticker.Buy : ticker.Sell) - slidePrice, 4);
}
if (orderId == null) {
if (isFirst) {
isFirst = false;
} else {
}
dealAmount = _N((nowPosition ? nowPosition.Amount : 0) - (initPosition ? initPosition.Amount : 0), 6);
var doAmount = Math.min(tradeAmount - dealAmount, account.Stocks * MarginLevel, 4);
if (doAmount < MinStock) {
break;
}
} else {
if (mode == 0 || Math.abs(tradePrice - prePrice) > maxSpace) {
orderId = null;
}
var order = StripOrders(exchange, orderId);
if (order == null) {
orderId = null;
}
}
Sleep(retryDelay);
}

if (dealAmount <= 0) {
return null;
}

return nowPosition;
}

function coverFutures(e, orderType) {
var coverAmount = 0;
while (true) {
var positions = _C(e.GetPosition);
var ticker = GetTicker(e);
var found = 0;
for (var i = 0; i < positions.length; i++) {
if (positions[i].Type == orderType) {
if (coverAmount == 0) {
coverAmount = positions[i].Amount;
}
} else {
e.SetDirection("closesell");
}
found++;
}
}
if (found == 0) {
break;
}
Sleep(2000);
StripOrders(e);
}
return coverAmount;
}

function loop(pos) {
if (typeof(pos) == 'undefined' || !pos) {
if (!pos) {
throw "出师不利, 开仓失败";
} else {
}
} else {
}
var holdPrice = pos.Price;
var holdAmount = pos.Amount;

var reversePrice = 0;
var coverPrice = 0;
var canOpen = true;

reversePrice = _N(holdPrice * (1 - StopLoss), 4);
coverPrice = _N(holdPrice * (1 + StopProfit), 4);
} else {
reversePrice = _N(holdPrice * (1 + StopLoss), 4);
coverPrice = _N(holdPrice * (1 - StopProfit), 4);
}

var coverId = null;
var msg = "持仓价: " + holdPrice + " 止损价: " + reversePrice;

for (var i = 0; i < 10; i++) {
if (coverId) {
break;
}
coverId = exchange.Sell(coverPrice, holdAmount, msg);
} else {
exchange.SetDirection("closesell");
}

Sleep(Interval);
}

if (!coverId) {
StripOrders(exchange);
throw "下单失败";
}

while (true) {
Sleep(Interval);
var ticker = GetTicker(exchange);
if ((tradeType == ORDER_TYPE_BUY && ticker.Last < reversePrice) || (tradeType == ORDER_TYPE_SELL && ticker.Last > reversePrice)) {
StripOrders(exchange);
if (_Failed >= MaxLoss) {
Counter.f++;
Log("超过最大失败次数", MaxLoss);
break;
}
var reverseAmount = _N(coverAmount * ReverseRate, 4);

var account = GetAccount(exchange, true);
if (_N(account.Stocks * MarginLevel, 4) < reverseAmount) {
Log("没有币反手开仓, 需要开仓: ", reverseAmount, "个, 只有", account.Stocks, "个币");
Counter.f++;
break;
}
if (ReverseMode == 0) {
}
var pos = Trade(exchange, reverseType, reverseAmount, OpMode, SlidePrice, MaxSpace, Interval);
if (pos) {
Log(reverseType == ORDER_TYPE_BUY ? "多仓" : "空仓", "加倍开仓完成");
}
return pos;
} else {
var orders = _C(exchange.GetOrders);
if (orders.length == 0) {
Counter.s++;
var account = GetAccount(exchange, true);
LogProfit(account.Stocks, account);
break;
}
}
}
return null;
}

function onexit() {
StripOrders(exchange);
Log("Exit");
}

function main() {
if (exchange.GetName().indexOf("Futures") == -1) {
throw "只支持期货, 现货暂不支持";
}
EnableLogLocal(SaveLocal);
if (exchange.GetRate() != 1) {
Log("已禁用汇率转换");
exchange.SetRate(1);
}

StopProfit /= 100;
StopLoss /= 100;

var eName = exchange.GetName();
if (eName == "Futures_CTP") {
throw "暂只支持数字货币期货"
}
exchange.SetContractType(Symbol);
exchange.SetMarginLevel(MarginLevel);
Interval *= 1000;
StripOrders(exchange);
OrgAccount = GetAccount(exchange, true);
LogStatus("启动成功");
var pos = null;
var positions = GetPosition(exchange);
if (positions.length == 1) {
pos = positions[0];
Log("发现一个仓位, 已经自动恢复进度");
} else if (positions.length > 1) {
throw "发现持仓超过1个";
}
while (true) {
pos = loop(pos);
if (!pos) {
_Failed = 0;
} else {
_Failed++;
}
Sleep(Interval);
}
}

```

Related

More

qq813380629 无法回测，咋回事出师不利, 开仓失败

qq813380629 无法回测，咋回事

qq813380629 无法回测，咋回事