Skip to content

Commit

Permalink
Added debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tomav committed Jan 7, 2024
1 parent 4454a32 commit 3f7831a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
92 changes: 49 additions & 43 deletions class_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Order {
break;
case 'limit_buy':
case 'limit_sell':
case 'debug':
case 'stop_market_buy':
case 'stop_market_sell':
required('a', this.order.a, this.order.t)
Expand Down Expand Up @@ -90,49 +91,54 @@ class Order {
amount = this.order.a.toString().includes("%") ? await this.getPositionSize(30, this.order.a) : this.order.a
}
console.log("-> Processing", this.order)
switch (this.order.t) {
case 'limit_buy':
eval(this.account).createOrder(this.instrument, "limit", "buy", amount, this.order.p)
return "executed limit_buy"
case 'limit_sell':
eval(this.account).createOrder(this.instrument, "limit", "sell", amount, this.order.p)
return "executed limit_sell"
case 'scaled_buy':
var prices = this.getScaledOrderPrices();
amount = amount/this.order.n
prices.forEach(price => eval(this.account).createOrder(this.instrument, "limit", "buy", amount, price));
return "executed scaled_buy"
case 'scaled_sell':
var prices = this.getScaledOrderPrices();
amount = amount/this.order.num
prices.forEach(price => eval(this.account).createOrder(this.instrument, "limit", "sell", amount, price));
return "executed scaled_sell"
case 'market_buy':
eval(this.account).createOrder(this.instrument, "market", "buy", amount)
return "executed market_buy"
case 'market_sell':
eval(this.account).createOrder(this.instrument, "market", "sell", amount)
return "executed market_sell"
case 'stop_market_buy':
eval(this.account).createOrder(this.instrument, "stop_market", "buy", amount, null, { "trigger_price": this.order.p, "trigger": "mark_price", "reduce_only": true })
return "executed stop_market_buy"
case 'stop_market_sell':
eval(this.account).createOrder(this.instrument, "stop_market", "sell", amount, null, { "trigger_price": this.order.p, "trigger": "mark_price", "reduce_only": true })
return "executed stop_market_sell"
case 'close_position':
let position = await this.getCurrentPosition(this.account, this.instrument);
if ( ["buy", "sell"].includes(position[0]) ) {
let closing_order = this.getClosingOrder(position)
eval(this.account).createOrder(this.instrument, "market", closing_order[0], closing_order[1])
eval(this.account).cancelAllOrders(this.instrument)
console.debug("-> Set closing_order", closing_order, "and canceled pending orders.")
return "executed close_position"
} else {
return "No position, nothing to close."
}
default:
console.error("xx Unknown order type, please refer to the documentation.", this.order.t);
return "Error: Unknown type " + this.order.t
if (this.order.t == 'debug') {
console.log("<- Debug mode, no order placed.")
return "executed debug order"
} else {
switch (this.order.t) {
case 'limit_buy':
eval(this.account).createOrder(this.instrument, "limit", "buy", amount, this.order.p)
return "executed limit_buy"
case 'limit_sell':
eval(this.account).createOrder(this.instrument, "limit", "sell", amount, this.order.p)
return "executed limit_sell"
case 'scaled_buy':
var prices = this.getScaledOrderPrices();
amount = amount/this.order.n
prices.forEach(price => eval(this.account).createOrder(this.instrument, "limit", "buy", amount, price));
return "executed scaled_buy"
case 'scaled_sell':
var prices = this.getScaledOrderPrices();
amount = amount/this.order.num
prices.forEach(price => eval(this.account).createOrder(this.instrument, "limit", "sell", amount, price));
return "executed scaled_sell"
case 'market_buy':
eval(this.account).createOrder(this.instrument, "market", "buy", amount)
return "executed market_buy"
case 'market_sell':
eval(this.account).createOrder(this.instrument, "market", "sell", amount)
return "executed market_sell"
case 'stop_market_buy':
eval(this.account).createOrder(this.instrument, "stop_market", "buy", amount, null, { "trigger_price": this.order.p, "trigger": "mark_price", "reduce_only": true })
return "executed stop_market_buy"
case 'stop_market_sell':
eval(this.account).createOrder(this.instrument, "stop_market", "sell", amount, null, { "trigger_price": this.order.p, "trigger": "mark_price", "reduce_only": true })
return "executed stop_market_sell"
case 'close_position':
let position = await this.getCurrentPosition(this.account, this.instrument);
if ( ["buy", "sell"].includes(position[0]) ) {
let closing_order = this.getClosingOrder(position)
eval(this.account).createOrder(this.instrument, "market", closing_order[0], closing_order[1])
eval(this.account).cancelAllOrders(this.instrument)
console.debug("-> Set closing_order", closing_order, "and canceled pending orders.")
return "executed close_position"
} else {
return "No position, nothing to close."
}
default:
console.error("xx Unknown order type, please refer to the documentation.", this.order.t);
return "Error: Unknown type " + this.order.t
}
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions tests/order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('required params', () => {
});
});


["market_buy", "market_sell"].forEach(function(type){
test(type + ' returns error if "amount" is missing', () => {
let order_json = { "t": type, "p": 10 }
Expand All @@ -44,7 +43,6 @@ describe('required params', () => {
});
});


});

describe('test orders', () => {
Expand Down Expand Up @@ -92,7 +90,6 @@ describe('test orders', () => {
});
});


test('"scaled_buy" type is handled properly', async () => {
let order_json = { "t": "scaled_buy", "a": 12, "u": 100, "l": 90, "n": 10 }
let order = new Order("first_account", "BTC/USDC", order_json)
Expand Down Expand Up @@ -133,5 +130,14 @@ describe('test orders', () => {
});
});

test('"debug" type is handled properly', async () => {
let order_json = { "t": "debug", "a": 10, "p": 10, "u": 100, "l": 90, "n": 10 }
let order = new Order("first_account", "ETH/USDC", order_json)
let markets = await eval(first_account).loadMarkets();

return order.process().then(data => {
expect(data).toBe("executed debug order");
});
});

});

0 comments on commit 3f7831a

Please sign in to comment.