bolak / Mida

The framework for trading in global financial markets

Home Page:https://www.mida.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool



Mida Mida


The framework for trading in global financial markets

HomeDocumentationHandbookEcosystem




Introduction

Mida is the framework for trading financial assets such as stocks, crypto, forex or commodities. It is designed from the ground up to provide a solid, versatile and platform-neutral environment for creating trading systems, indicators, market analysis tools or just trading applications depending on use cases. The Mida ecosystem is built from the ground up in TypeScript and C++. The framework can be used with TypeScript/JavaScript on Node.js and is distributed on NPM.

Join the community on Discord and Telegram to get help you with your first steps.

Table of contents

Ecosystem

Project Status Description
Mida Image The Mida core
Mida Binance Image A Mida plugin for using Binance
Mida cTrader Image A Mida plugin for using cTrader
Mida Tulipan Image A Mida plugin providing technical analysis indicators
Apollo Image A library for getting real-time economic data

Supported platforms

Mida is platform-neutral, this means that any trading platform could be easily integrated in the ecosystem. Trading systems/applications built with Mida can be easily executed on different trading platforms without code changes. Here are some of the most popular trading platforms supported by Mida.





Installation

To get started use the command below in your terminal, the installer will pop up and create an empty Mida project.

npm init mida

Usage

Account login

How to login into a Binance Spot account.

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

const myAccount = await login("Binance/Spot", {
    apiKey: "...",
    apiSecret: "...",
});

Read how to use Binance to get the apiKey and apiSecret credentials.

How to login into a cTrader account.

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

const myAccount = await login("cTrader", {
    clientId: "...",
    clientSecret: "...",
    accessToken: "...",
    cTraderBrokerAccountId: "...",
});

Read how to use cTrader to get the clientId, clientSecret, accessToken and cTraderBrokerAccountId credentials.

Balance, equity and margin

How to get the account balance, equity and margin.

console.log(await myAccount.getBalance());
console.log(await myAccount.getEquity());
console.log(await myAccount.getFreeMargin());
console.log(await myAccount.getUsedMargin());

Orders, deals and positions

How top open a long position for Bitcoin against USDT.

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

const myOrder = await myAccount.placeOrder({
    symbol: "BTCUSDT",
    direction: MidaOrderDirection.BUY,
    volume: 1,
});
const myPosition = await order.getPosition();

console.log(myOrder.id);
console.log(myOrder.executionPrice);
console.log(myOrder.positionId);
console.log(myOrder.trades);
console.log(myPosition);

How to open a short position for EUR against USD.

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

const myOrder = await myAccount.placeOrder({
    symbol: "EURUSD",
    direction: MidaOrderDirection.SELL,
    volume: 0.1,
});
const myPosition = await order.getPosition();

console.log(myOrder.id);
console.log(myOrder.executionPrice);
console.log(myOrder.positionId);
console.log(myOrder.trades);
console.log(myPosition);

How to open a long position for Apple stocks with error handler.

import {
    MidaOrderDirection,
    MidaOrderRejection,
} from "@reiryoku/mida";

const myOrder = await myAccount.placeOrder({
    symbol: "#AAPL",
    direction: MidaOrderDirection.BUY,
    volume: 888,
});

if (myOrder.isRejected) {
    switch (myOrder.rejection) {
        case MidaOrderRejection.MARKET_CLOSED: {
            console.log("#AAPL market is closed!");

            break;
        }
        case MidaOrderRejection.NOT_ENOUGH_MONEY: {
            console.log("You don't have enough money in your account!");

            break;
        }
        case MidaOrderRejection.INVALID_SYMBOL: {
            console.log("Your account doesn't support trading Apple stocks!");

            break;
        }
    }
}
More examples

How to open a long position for GBP against USD with stop loss and take profit.

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

const symbol = "GBPUSD";
const lastBid = await myAccount.getSymbolBid(symbol);
const myOrder = await myAccount.placeOrder({
    symbol,
    direction: MidaOrderDirection.BUY,
    volume: 0.1,
    protection: {
        stopLoss: lastBid - 0.0010, // <= SL 10 pips
        takeProfit: lastBid + 0.0030, // <= TP 30 pips
    },
});

How to close an open position.

import {
    MidaOrderDirection,
    MidaPositionDirection,
} from "@reiryoku/mida";

await myPosition.close();
// or
await myPosition.subtractVolume(myPosition.volume);
// or
await myAccount.placeOrder({
    positionId: myPosition.id,
    direction: myPosition.direction === MidaPositionDirection.LONG ? MidaOrderDirection.SELL : MidaOrderDirection.BUY,
    volume: myPosition.volume,
});

How to retrieve all open positions and pending orders.

console.log(await myAccount.getOpenPositions());
console.log(await myAccount.getPendingOrders());

How to set take profit and stop loss for a position.

await myPosition.changeProtection({
    takeProfit: 200,
    stopLoss: 100,
});

Symbols and assets

How to retrieve all symbols available for your trading account.

const symbols = await myAccount.getSymbols();

console.log(symbols);

How to retrieve a symbol.

const symbol = await myAccount.getSymbol("#AAPL");

if (!symbol) {
    console.log("Apple stocks are not available for this account!");
}
else {
    console.log(symbol.digits);
    console.log(symbol.leverage);
    console.log(symbol.baseAsset);
    console.log(symbol.quoteAsset);
    console.log(await symbol.isMarketOpen());
}

How to get the price of a symbol.

const symbol = await myAccount.getSymbol("BTCUSDT");
const price = await symbol.getBid();

console.log(`Bitcoin price is ${price} USDT`);

// or

console.log(await myAccount.getSymbolBid("BTCUSDT"));

Ticks and candlesticks

How to listen the ticks of a symbol.

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

const marketWatcher = new MidaMarketWatcher({ tradingAccount: myAccount, });

await marketWatcher.watch("BTCUSDT", { watchTicks: true, });

marketWatcher.on("tick", (event) => {
    const { tick, } = event.descriptor;

    console.log(`Bitcoin price is now ${tick.bid} USDT`);
});

How to get the candlesticks of a symbol (candlesticks and bars are generically called periods).

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

const periods = await myAccount.getSymbolPeriods("EURUSD", MidaTimeframe.M30);
const lastPeriod = periods[periods.length - 1];

console.log("Last candlestick start time: " + lastPeriod.startTime);
console.log("Last candlestick OHLC: " + lastPeriod.ohlc);
console.log("Last candlestick close price: " + lastPeriod.close);

How to listen when candlesticks are closed.

import {
    MidaMarketWatcher,
    MidaTimeframe,
} from "@reiryoku/mida";

const marketWatcher = new MidaMarketWatcher({ tradingAccount: myAccount, });

await marketWatcher.watch("BTCUSDT", {
    watchPeriods: true,
    timeframes: [
        MidaTimeframe.M5,
        MidaTimeframe.H1,
    ],
});

marketWatcher.on("period-close", (event) => {
    const { period, } = event.descriptor;

    switch (period.timeframe) {
        case MidaTimeframe.M5: {
            console.log(`M5 candlestick closed at ${period.close}`);

            break;
        }
        case MidaTimeframe.H1: {
            console.log(`H1 candlestick closed at ${period.close}`);

            break;
        }
    }
});

Trading systems

How to create a trading system (expert advisor or trading bot).

import {
    MidaTradingSystem,
    MidaTimeframe,
} from "@reiryoku/mida";

class MyTradingSystem extends MidaTradingSystem {
    constructor ({ tradingAccount, }) {
        super({
            name: "MyTradingSystem",
            version: "1.0.0",
            tradingAccount,
        });
    }

    async configure () {
        // Every trading system has an integrated market watcher
        await this.watchTicks("BTCUSDT");
        await this.watchPeriods("BTCUSDT", MidaTimeframe.H1);
    }

    async onStart () {
        console.log("The trading bot has started...");
    }

    async onTick (tick) {
        // Implement your strategy
    }

    async onPeriodClose (period) {
        console.log(`H1 candlestick closed at ${period.open}`);
    }

    async onStop () {
        console.log("The trading bot has been interrupted...");
    }
}

How to execute a trading system.

import { login, } from "@reiryoku/mida";
import { MyTradingSystem, } from "./MyTradingSystem";

const myAccount = await login(/* ... */);
const mySystem = new MyTradingSystem({ tradingAccount: myAccount, });

await mySystem.start();

Technical indicators

Install the plugin providing technical analysis indicators.

import { Mida, } from "@reiryoku/mida";
import { TulipanPlugin, } from "@reiryoku/mida-tulipan";

// Use the Mida Tulipan plugin
Mida.use(new TulipanPlugin());

How to calculate SMA (Simple Moving Average).

import { Mida, MidaTimeframe, } from "@reiryoku/mida";

// Get latest candlesticks on H1 timeframe
const candlesticks = await myAccount.getSymbolPeriods("EURUSD", MidaTimeframe.H1);
const closePrices = candlesticks.map((candlestick) => candlestick.close);

// Calculate RSI on close prices, pass values from oldest to newest
const sma = await Mida.createIndicator("SMA").calculate(closePrices);

// Values are from oldest to newest
console.log(sma);

How to calculate RSI (Relative Strength Index).

import { Mida, MidaTimeframe, } from "@reiryoku/mida";

// Get latest candlesticks on H1 timeframe
const candlesticks = await myAccount.getSymbolPeriods("BTCUSDT", MidaTimeframe.H1);
const closePrices = candlesticks.map((candlestick) => candlestick.close);

// Calculate RSI on close prices, pass values from oldest to newest
const rsi = await Mida.createIndicator("RSI").calculate(closePrices);

// Values are from oldest to newest
console.log(rsi);

License and disclaimer

LICENSE

Trading in financial markets is highly speculative and carries a high level of risk. It's possible to lose all your capital. This project may not be suitable for everyone, you should ensure that you understand the risks involved. Mida and its contributors are not responsible for any technical inconvenience that may lead to money loss, for example a stop loss not being set.

Contributors

Name Contribution GitHub Contact
Vasile Pește Author and maintainer Vasile-Peste vasile.peste@reiryoku.com

About

The framework for trading in global financial markets

https://www.mida.org

License:MIT License


Languages

Language:TypeScript 98.9%Language:JavaScript 1.1%