Contract Order
Open Position Close Position
JavaScriptOther
$.openPos(buy|sell, amount);
Open Position
$.closePos(buy|sell, amount);
Close Position
Parameters
| Name | Default | What it controls |
|---|---|---|
| SLIP_POINT | 0.01 | slippage |
| TRADE_TIMEOUT | 10 | Transaction timeout |
| FACE_VALUE | 10 | Face Value |
| PRICE_PRECISION | 4 | Price Precision |
| AMOUNT_PRECISION | 8 | Trading volume precision |
Source JavaScript
/*backtest
start: 2020-01-08 00:00:00
end: 2020-04-06 00:00:00
period: 1d
exchanges: [{"eid":"Futures_OKCoin","currency":"ETH_USD","stocks":1400}]
args: [["PRICE_PRECISION",2],["AMOUNT_PRECISION",4]]
*/
var tempOrders = [];
var COLOR = {
GREEN: '#00a500',
RED: '#ff0000',
GRAY: '#b2b2b2',
ORANGE: '#ff9e38'
}
function cancelOrder(ex,order_id){
if (!order_id) return Log('Incorrect orderid', order_id);
Log("Cancel order" + order_id);
ex.CancelOrder(order_id);
Sleep(500);
var orders=_C(ex.GetOrders);
var find=false;
for (var i=0;i<orders.length;++i){
if (orders[i].Id===order_id){
find=true;
break;
}
}
if (find){
cancelOrder(ex.order_id);
}
}
var trade = function(direction, amount) {
var ticker = _C(exchange.GetTicker);
exchange.SetDirection(direction);
var price = 0
if (_.contains(['buy', 'closesell'], direction)) {
price = _N(new Decimal(ticker.Last).plus(SLIP_POINT).toNumber(), PRICE_PRECISION);
} else {
price = _N(new Decimal(ticker.Last).minus(SLIP_POINT).toNumber(), PRICE_PRECISION);
}
var orderInfo = null;
var avgPrice = null;
var oid = null;
var beginTime = null;
var dealAmount = _.reduce(tempOrders, function(p, n) {
return new Decimal(p).plus(n.DealAmount).toNumber();
}, 0);
var remainAmount = Math.floor(new Decimal(amount).minus(dealAmount).toNumber());
if (remainAmount < 1) {
avgPrice = _N(new Decimal(_.reduce(tempOrders, function(p, n) {
return new Decimal(p).plus(new Decimal(n.AvgPrice).mul(n.DealAmount).mul(FACE_VALUE)).toNumber()
}, 0)).div(new Decimal(dealAmount).mul(FACE_VALUE)).toNumber(), PRICE_PRECISION);
orderInfo = {
Id: _.last(tempOrders).Id,
Type: _.contains(['buy', 'closebuy'], direction) ? PD_LONG : PD_SHORT,
Price: price,
AvgPrice: avgPrice,
Amount: amount,
DealAmount: dealAmount,
Orders: _.pluck(tempOrders, 'Id')
}
tempOrders = [];
return orderInfo;
} else {
oid = _.contains(['buy', 'closesell'], direction) ? exchange.Buy(price, remainAmount) : exchange.Sell(price, remainAmount);
if (oid) {
beginTime = Unix();
while(1) {
orderInfo = _C(exchange.GetOrder, oid);
if (orderInfo.Status !== ORDER_STATE_PENDING) {
tempOrders.push(orderInfo);
return trade(direction, amount);
}
if (Unix() - beginTime >= TRADE_TIMEOUT) {
Log('Transaction timeout, Cancel order' + oid + COLOR.RED);
cancelOrder(exchange, oid);
continue;
}
Sleep(250);
}
} else {
return null;
}
}
}
$.OpenPos = function(direction, amount) {
return trade(direction, amount);
};
$.ClosePos = function(direction, amount) {
return trade('close' + direction, amount);
}
function main() {
exchange.SetContractType('this_week');
exchange.SetPrecision(PRICE_PRECISION, 0);
Log($.OpenPos('buy', 2));
$.OpenPos('sell', 1);
$.ClosePos('buy', 1);
$.ClosePos('buy', 1);
$.ClosePos('sell', 1);
Log(exchange.GetPosition());
}Published by 一拳男孩 on the FMZ strategy square · last modified 2020-09-19. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Contract-Order.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.