# Paper Trading with Mida

Mida comes with an out of the box engine simulating exchanges and spot trading accounts.

# Engine

A trading engine simulates an exchange, it has its own local date, this means that if we want to backtest a time series from the year 2000 to 2001 we need to set the local date to 2000 and elapse it by one year, this will trigger all the market ticks, derived data and events.

import { date, MidaPlaygroundEngine, } from "@reiryoku/mida";

// Create an exchange engine
const engine = new MidaPlaygroundEngine({
    localDate: date("2020-01-01"),
});

An exchange engine is empty by default, this means that it has no symbols or market data, you need to add the tradable symbols and the respective historical market ticks.

# Accounts

To start paper trading and/or backtesting a trading account is necessary.

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

// Deposit additional assets
await myAccount.deposit("ETH", 32);
await myAccount.deposit("TRX", 200);

createAccount() returns a MidaTradingAccount, this means that it contains the same properties and methods as a real/live account and that you can easily write code to backtest and deploy it to real markets without code changes.

# Symbols and ticks

A symbol can be added into a trading account with addSymbol() which accepts a symbol constructor as parameter.

// Add the ETHUSD symbol
await myAccount.addSymbol({
    symbol: "ETHUSD",
    baseAsset: "ETH",
    quoteAsset: "USD",
    lotUnits: 1,
});

Ticks can be added with addSymbolTicks() which takes the symbol and an array of MidaTick as parameters.

// Add ETHUSD ticks
const ethUsdTicks = [ /* ... */ ];
await engine.addSymbolTicks("ETHUSD", ethUsdTicks);

The account is the same platform-neutral account that works with real trading platforms, this means that you can refer to the API and use it as a ordinary trading account.

# The passage of time

At this point you can start simulating a trading platform and account. The market ticks can be emitted in two ways: with elapseTime() or elapseTicks().

The elapseTime() method elapses a given amount of seconds in the engine local date, this means that all the ticks between the local date and the new local date will be triggered.

The elapseTicks() method elapses a given amount of ticks for all the symbols available, the engine local date will be set to the most recent tick date.

Furthermore, when a tick is emitted, the engine local date will be set to the respective tick date.

# Simulation

If you want to simulate a real-time trading plaform you can do as follows.

// Every second elapse one second in the engine local date
setInterval(async () => {
    await engine.elapseTime(1);
}, 1000);

If you want to quickly backtest your strategy over one week you can elapse one week of ticks.

await engine.elapseTime(604800); // Elapse one week of ticks

Both elapseTime() and elapseTicks() do return a Promise<MidaTick[]> that resolves to the elapsed ticks.

For the complete documentation read the Playground API.

# Considerations

At this point, if you are familiar with the Trading System API, you can directly attach the trading account to your trading system and backtest it.

# Where do I get market historical data?

...I don't know if this has been shared, but it's very useful for downloading tick and candle data with just one command from terminal https://github.com/Leo4815162342/dukascopy-node

# Commission customizer

Commissions can be added through a commission customizer, a custom function that given the order and the details of the trade that is being executed, returns a commission for the respective trade. f

import { decimal, } from "@reiryoku/mida";

// Set a fixed commission of 1 USD to all trades
engine.setCommissionCustomizer((order, executedVolume, executionPrice) => [ "USD", decimal(1), ]);

// Set a commission relative to the traded volume
engine.setCommissionCustomizer((order, executedVolume, executionPrice) => {
    return [
        "USD",
        executedVolume.multiply(1), // Commission = lots * 1 USD
    ];
});