Skip to content

Commit

Permalink
fix sim backtest leverage bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rainfu committed May 31, 2023
1 parent ace7e8f commit e8ec698
Show file tree
Hide file tree
Showing 5 changed files with 581 additions and 422 deletions.
6 changes: 3 additions & 3 deletions commands/trade.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ module.exports = function (program, conf) {
s.symbols[b.product_id]["last_buy_type"] =
"prev_buy_" +
(b.positionSide === "SHORT" ? "short" : "long");
let orderTime = new Date().getTime();
s.symbols[b.product_id].my_trades.push({
order_id: crypto.randomBytes(4).toString("hex"),
time: new Date().getTime(),
time: orderTime,
execution_time: 0,
slippage: 0,
type: "buy",
Expand All @@ -227,8 +228,7 @@ module.exports = function (program, conf) {
usdtProfit: 0,
position_side: b.positionSide,
});
s.symbols[b.product_id].last_trade_time =
s.symbols[b.product_id].time;
s.symbols[b.product_id].last_trade_time = orderTime;
s.symbols[b.product_id].sell_stop = n(b.entry_price)
.subtract(
n(b.entry_price).multiply(so.sell_stop_pct / 100)
Expand Down
2 changes: 1 addition & 1 deletion extensions/exchanges/sim/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function sim(conf, so, s) {
initFees: function () {
//User can change the fees from client ,so we sould reset the fees for this
real_exchange.initFees();
if (so.mode !== "sim" && so.future) {
if (so.future) {
this.makerFee = real_exchange.makerFee * (so.leverage || 20);
this.takerFee = real_exchange.takerFee * (so.leverage || 20);
} else {
Expand Down
13 changes: 9 additions & 4 deletions extensions/output/panacea/api/backtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ const addSim = (message, cb) => {
var command_args = [
"sim",
message.data.exchange,
"--debug",
"--conf",
"./data/sim/temp.json",
"--start",
message.data.options.startTime,
"--end",
message.data.options.endTime,
"--debug",
];
console.log("Start backtest from client ".green);
console.log("\n ----- Start backtest from client ".cyan, command_args);
var simProcess = spawn(
path.resolve(__dirname, "../../../../", xcoin_cmd),
command_args
Expand All @@ -137,7 +137,7 @@ const addSim = (message, cb) => {
console.log(`sim: ${data}`);
}); */
simProcess.on("error", (error) => {
console.log("error", error);
console.log("error".cyan, error);
let data = {
action: message.action,
toast: "backtest." + message.action + "Error",
Expand All @@ -147,7 +147,11 @@ const addSim = (message, cb) => {
});
simProcess.on("exit", (code) => {
if (code) {
console.log("simProcess exit with code".green, code);
console.log(
"\n ----- Sim error,exit with code".cyan,
code.yellow,
" ------\n"
);
let data = {
action: message.action,
toast: "backtest." + message.action + "Error",
Expand All @@ -160,6 +164,7 @@ const addSim = (message, cb) => {
toast: "backtest." + message.action + "Ok",
};
if (cb) cb(data);
console.log("\n ----- Sim ok ------\n".cyan);
});
};
module.exports = {
Expand Down
Loading

0 comments on commit e8ec698

Please sign in to comment.