GithubHelp home page GithubHelp logo

Comments (9)

hmpmarketing avatar hmpmarketing commented on August 18, 2024

Here is a recent test I made:

Full Code:
backtest.txt

Periods CSV:
periods.csv

Log:

node backtest.js
13:41:27.190 | INFO | Using Mida 2023.1.2
Start Account Balance: 100000
Price is 15490.551
13:41:27.416 | INFO | Order 19e3c60f-452d-483f-8205-5413d6c930c3 | Status changed from requested to accepted
13:41:27.418 | INFO | Position e4d1e1ac-9cf6-4b9c-8377-1fc6ec27dc9f | Trade 7bd42c85-d03b-4531-9c0b-b7807da41295 executed
13:41:27.419 | INFO | Order 19e3c60f-452d-483f-8205-5413d6c930c3 | Trade 7bd42c85-d03b-4531-9c0b-b7807da41295 executed
13:41:27.419 | INFO | Order 19e3c60f-452d-483f-8205-5413d6c930c3 | Status changed from accepted to executed
Account Balance After Sell: 101548.9551
Price after 5 hour elapsed: 15439.931
End Account Balance: 101548.9551

I am wondering why end balance is not 51 pips * the volume

from mida.

Vasile-Peste avatar Vasile-Peste commented on August 18, 2024

Hello, you should set lotUnits to 1 and not 0. The lot units indicates how many units of the assets you can buy with 1 lot.

For Forex, usually 1 lot EUR equals to 100.000EUR.
For Contracts and Crypto usually 1 lot equals 1 unit, so 1 lot of BTC is 1 BTC.
If you set it to zero it's as you are buying/selling nothing.

Can you try and let me know? Thank you!

from mida.

hmpmarketing avatar hmpmarketing commented on August 18, 2024

@Vasile-Peste

const symbolParameters = {
symbol: "GER40",
baseAsset: "USD",
quoteAsset: "USD",
description: "",
leverage: decimal(),
minLots: decimal(1),
maxLots: decimal(1),
lotUnits: 1,
pipPosition: -1,
digits: 2,
};

Seems to get the same result:

Log:

Start Account Balance: 100000
Price is 15490.551
16:0:5.69 | INFO | Order fee6c417-75b7-493c-ac2b-d56a9083ba90 | Status changed from requested to accepted
16:0:5.71 | INFO | Position 208a9376-255a-4225-afd2-34990baf258b | Trade ec8672ab-4650-4d21-a922-56f223fc9f26 executed
16:0:5.72 | INFO | Order fee6c417-75b7-493c-ac2b-d56a9083ba90 | Trade ec8672ab-4650-4d21-a922-56f223fc9f26 executed
16:0:5.72 | INFO | Order fee6c417-75b7-493c-ac2b-d56a9083ba90 | Status changed from accepted to executed
Account Balance After Sell: 101548.9551
Price after 5 hour elapsed: 15439.931
End Account Balance: 101548.9551

from mida.

hmpmarketing avatar hmpmarketing commented on August 18, 2024

@Vasile-Peste FYI, I am trying to simulate trading with index CFDs. So that when you buy lets say $1 stake size (volume), a move of the price is 50 points on your direction, if you are buying/going long would mean a profit of $50

from mida.

Vasile-Peste avatar Vasile-Peste commented on August 18, 2024

I understand now, in that case, it's very simple. This is a simulator of a trading engine, there is no concept of CFD/short selling. We just buy/sell assets, so when you check your balance at the end, you just have to buy back the GER40 that you sold.

await myAccount.placeOrder({
    symbol: "GER40",
    direction: MidaOrderDirection.BUY,
    volume: 0.1,
});

Why? When you place the sell order, you are just exchanging 0.1 GER40 for USD at market price.
This means that 0.1 GER40 is deducted from your balance and the equivalent of USD value is added.

Now to check the profit after the price has changed you have to buy the 0.1 GER40 back, to get the portfolio to the initial state, so the USD balance can be deducted as the position will close.

In that case the P/L would be: (open price - close price) * volume * lotSize = (15462.951 - 15445.401) * 0.1 * 1 = 1.7549999999999273
With an end balance of 100001.755 USD started with 100000 USD.

Let me know if it's clear, thank you!

from mida.

hmpmarketing avatar hmpmarketing commented on August 18, 2024

Hi @Vasile-Peste

So this is my code now:

const symbolParameters = {
    symbol: "GER40",
    baseAsset: "USD",
    quoteAsset: "USD",
    description: "",
    leverage: decimal(),
    minLots: decimal(1),
    maxLots: decimal(1),
    lotUnits: 0.1,
    pipPosition: -1,
    digits: 2,
};

const stake_size = 5;


  const rawPeriods = await readPeriodCsv("periods.csv")

  const engine = new MidaPlaygroundEngine({
      localDate: date("2023-03-10T00:00:00.000Z"),
  });

  await engine.addSymbolPeriods("GER40",rawPeriods)

  const myAccount = await engine.createAccount({
      id:"my-test-account",
      balanceSheet: {
          "USD": 0,
      },
  });
  await myAccount.addSymbol(symbolParameters)
  await myAccount.deposit("USD",10000)

  console.log(`Start Account Balance: ${await myAccount.getBalance()}`)

  await engine.elapseTime(300); 

  console.log(`Price is ${await engine.getSymbolBid("GER40")}`)

  await myAccount.placeOrder({
      symbol: "GER40",
      direction: MidaOrderDirection.SELL,
      volume: stake_size,
  });

  console.log(`Account Balance After Sell: ${await myAccount.getBalance()}`)

  await engine.elapseTime(18000); 

  console.log(`Price after 5 hour elapsed: ${await engine.getSymbolBid("GER40")}`)

  await myAccount.placeOrder({
      symbol: "GER40",
      direction: MidaOrderDirection.BUY,
      volume: stake_size,
  });

  console.log(`End Account Balance: ${await myAccount.getBalance()}`)

Log:

16:23:6.898 | INFO | Using Mida 2023.1.2
Start Account Balance: 10000
Price is 15490.551
16:23:7.122 | INFO | Order 57fa4368-5bca-42b2-9ba6-2ff8ff0c0123 | Status changed from requested to accepted
16:23:7.124 | INFO | Position ca9d04ba-6c5a-419f-ba5d-957c2efc8c94 | Trade 97eb76bd-33b9-4039-8ebe-36097972f8d3 executed
16:23:7.125 | INFO | Order 57fa4368-5bca-42b2-9ba6-2ff8ff0c0123 | Trade 97eb76bd-33b9-4039-8ebe-36097972f8d3 executed
16:23:7.125 | INFO | Order 57fa4368-5bca-42b2-9ba6-2ff8ff0c0123 | Status changed from accepted to executed
Account Balance After Sell: 87447.755
Price after 5 hour elapsed: 15439.931
16:23:7.154 | INFO | Order 2220cfa8-aa5c-49a9-aabf-f5d11ccebbe2 | Status changed from requested to accepted
16:23:7.155 | INFO | Position bad703fc-78af-4c55-a503-41d3d7df2a6a | Trade 1eedb442-651b-4899-91c9-a81e955c9124 executed
16:23:7.155 | INFO | Order 2220cfa8-aa5c-49a9-aabf-f5d11ccebbe2 | Trade 1eedb442-651b-4899-91c9-a81e955c9124 executed
16:23:7.155 | INFO | Order 2220cfa8-aa5c-49a9-aabf-f5d11ccebbe2 | Status changed from accepted to executed
End Account Balance: 10253.1

So this is what I am expecting. However when I do the opposite (Buy then Sell) I would expect a loss as the price went down, I would expect to lose 253.1 so the end balance would be 9746.9 however I get "Not Enough Money"

16:28:32.212 | INFO | Using Mida 2023.1.2
Start Account Balance: 10000
Price is 15490.551
16:28:32.429 | INFO | Order 70cab825-5775-4bd3-ba8e-5476228ff9fb | Status changed from requested to accepted
16:28:32.430 | INFO | Order 70cab825-5775-4bd3-ba8e-5476228ff9fb | Status changed from accepted to rejected
16:28:32.431 | WARN | Playground | Order 70cab825-5775-4bd3-ba8e-5476228ff9fb rejected: not-enough-money
Account Balance After Sell: 10000
Price after 5 hour elapsed: 15439.931
16:28:32.455 | INFO | Order b5a9239a-1d78-4f07-92dc-f5492182a1ed | Status changed from requested to accepted
16:28:32.457 | INFO | Position 450ea932-d658-45a0-b01e-9afe8aa52dd8 | Trade 35385703-57d1-4c95-8e9d-56f14487d8b0 executed
16:28:32.457 | INFO | Order b5a9239a-1d78-4f07-92dc-f5492182a1ed | Trade 35385703-57d1-4c95-8e9d-56f14487d8b0 executed
16:28:32.458 | INFO | Order b5a9239a-1d78-4f07-92dc-f5492182a1ed | Status changed from accepted to executed
End Account Balance: 87194.655

Not sure what I am missing.

from mida.

fgnm avatar fgnm commented on August 18, 2024

@hmpmarketing I've the same issue, have you found a solution?

from mida.

hmpmarketing avatar hmpmarketing commented on August 18, 2024

@fgnm nope, waiting if @Vasile-Peste can shed a light

from mida.

fgnm avatar fgnm commented on August 18, 2024

@hmpmarketing got the trick... I'm testing ETHUSD, this should be the correct way to setup backtest account

    const myAccount = await engine.createAccount({
        balanceSheet: {
            "USD": 100_000, // Start with 100.000 USD
        },
    });
    await myAccount.deposit("ETH", 32);

I was missing the await myAccount.deposit("ETH", 32);, so the engine was unable to execute the SELL orders because indeed the ETH wallet was at 0 producing not-enough-money exception.

Cheers 😉

from mida.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.