Featured Content Ads
add advertising hereA JavaScript framework to with out complications operate in world monetary markets
Featured Content Ads
add advertising here
Introduction
Designed to:
- Exchange monetary assets equivalent to shares, crypto, forex or commodities;
- Form expert advisors to automate and backtest trading ideas;
- Operate with any broker/commerce the usage of handiest JavaScript/TypeScript;
- Analyze markets and costs with indicators and evaluation interfaces.
Be part of the Discord and Telegram neighborhood.
Installation
To employ cTrader broker accounts, additionally set up the Mida cTrader plugin
Featured Content Ads
add advertising herenpm i @reiryoku/mida-ctrader
Utilization
Dealer chronicle login
How to login into a cTrader broker chronicle.
const myAccount = await MidaBroker.login(“cTrader”, {
clientId: “”,
clientSecret: “”,
accessToken: “”,
cTraderBrokerAccountId: “”,
});”>
const { Mida, MidaBroker, } = require("@reiryoku/mida"); // Expend the Mida cTrader plugin Mida.employ(require("@reiryoku/mida-ctrader")); // Login into any cTrader broker chronicle const myAccount = stop up for MidaBroker.login("cTrader", { clientId: "", clientSecret: "", accessToken: "", cTraderBrokerAccountId: "", });
To procure a clientId
, clientSecret
and accessToken
it’s miles a truly considerable to make an chronicle on
cTrader Open API.
Dealer orders and positions
How top originate a long situation for Bitcoin in opposition to USD.
const { MidaBrokerOrderDirection, } = require("@reiryoku/mida"); const myOrder = stop up for myAccount.openPosition({ symbol: "BTCUSD", route: MidaBrokerOrderDirection.BUY, quantity: 1, }); console.log(myOrder.identification); console.log(myOrder.openPrice);
How to originate a short situation for EUR in opposition to USD.
const { MidaBrokerOrderDirection, } = require("@reiryoku/mida"); const myOrder = stop up for myAccount.placeOrder({ symbol: "EURUSD", route: MidaBrokerOrderDirection.SELL, quantity: 0.1, }); console.log(myOrder.identification); console.log(myOrder.openPrice);
How to originate a short situation for Apple shares with error handler.
const { MidaBrokerOrderDirection, MidaBrokerOrderRejectionType, } = require("@reiryoku/mida"); const myOrder = stop up for myAccount.placeOrder({ symbol: "#AAPL", route: MidaBrokerOrderDirection.SELL, quantity: 1, }); if (myOrder.isRejected) { swap (myOrder.rejectionType) { case MidaBrokerOrderRejectionType.MARKET_CLOSED: { console.log("#AAPL market is closed!"); shatter; } case MidaBrokerOrderRejectionType.NOT_ENOUGH_MONEY: { console.log("You destroy no longer come up with the cash for in your chronicle!"); shatter; } case MidaBrokerOrderRejectionType.INVALID_SYMBOL: { console.log("Your broker chronicle would not strengthen trading Apple shares!"); shatter; } } }
More examples
As effectively as, canPlaceOrder
is used to know if an uncover would perchance additionally be placed with out predicted rejections. Attributable to the excessive volatility of financial markets, these ideas can not squawk that the uncover goes to be placed with none rejection.
if (canPlaceOrder !== steady) {
console.log(“Narrate can’t be placed”)
if (placeOrderObstacles.involves(MidaBrokerOrderRejectionType.MARKET_CLOSED)) {
console.log(“XAUUSD market is closed!”);
}
if (placeOrderObstacles.involves(MidaBrokerOrderRejectionType.NOT_ENOUGH_MONEY)) {
console.log(“You destroy no longer come up with the cash for in your chronicle!”);
}
}”>
const { MidaBrokerOrderDirection, MidaBrokerOrderRejectionType, } = require("@reiryoku/mida"); const orderDirectives = { symbol: "XAUUSD", route: MidaBrokerOrderDirection.SELL, tons: 1, }; const canPlaceOrder = stop up for myAccount.canPlaceOrder(orderDirectives); // => steady | MidaBrokerOrderRejectionType[] if (canPlaceOrder !== steady) { console.log("Narrate can't be placed") if (placeOrderObstacles.involves(MidaBrokerOrderRejectionType.MARKET_CLOSED)) { console.log("XAUUSD market is closed!"); } if (placeOrderObstacles.involves(MidaBrokerOrderRejectionType.NOT_ENOUGH_MONEY)) { console.log("You destroy no longer come up with the cash for in your chronicle!"); } }
How to originate a long situation for GBP in opposition to USD with cease loss and take profit.
const { MidaBrokerOrderDirection, } = require("@reiryoku/mida"); const symbol = "GBPUSD"; const lastBid = stop up for myAccount.getSymbolBid(symbol); const myOrder = stop up for myAccount.placeOrder({ symbol, route: MidaBrokerOrderDirection.BUY, quantity: 0.1, safety: { stopLoss: lastBid - 0.0010, // <= SL 10 pips takeProfit: lastBid + 0.0030, // <= TP 30 pips }, });
Symbols
How to retrieve all symbols available for your broker account.
if (!symbol) {
console.log(“Apple shares are no longer readily available for this chronicle!”);
}
else {
console.log(symbol.digits);
console.log(symbol.leverage);
console.log(stop up for symbol.isMarketOpen());
}”>
const symbol = stop up for myAccount.getSymbol("#AAPL"); // => MidaSymbol | undefined if (!symbol) { console.log("Apple shares are no longer readily available for this chronicle!"); } else { console.log(symbol.digits); console.log(symbol.leverage); console.log(stop up for symbol.isMarketOpen()); }
How to procure the worth of a logo.
const symbol = stop up for myAccount.getSymbol("BTCUSD"); const designate = stop up for symbol.getBid(); console.log(`Bitcoin designate is ${designate} US greenbacks.`); // or console.log(stop up for myAccount.getSymbolBid("BTCUSD"));
How to listen the ticks of a logo.
const { tick, } = event.descriptor;
console.log(`Bitcoin designate is now ${tick.expose} US greenbacks`);
});”>
const { MidaMarketWatcher, } = require("@reiryoku/mida"); const marketWatcher = new MidaMarketWatcher({ brokerAccount: myAccount, }); stop up for marketWatcher.ogle("BTCUSD", { watchTicks: steady, }); marketWatcher.on("tick", (event) => { const { tick, } = event.descriptor; console.log(`Bitcoin designate is now ${tick.expose} US greenbacks`); });
How to listen when candlesticks are closed.
const { duration, } = event.descriptor;
swap (duration.timeframe) {
case MidaTimeframe.M5: {
console.log(`M5 candlestick closed at ${duration.conclude}`);
shatter;
}
case MidaTimeframe.H1: {
console.log(`H1 candlestick closed at ${duration.conclude}`);
shatter;
}
}
});”>
const { MidaMarketWatcher, MidaTimeframe, } = require("@reiryoku/mida"); const marketWatcher = new MidaMarketWatcher({ brokerAccount: myAccount, }); stop up for marketWatcher.ogle("BTCUSD", { watchPeriods: steady, timeframes: [ MidaTimeframe.M5, MidaTimeframe.H1, ], }); marketWatcher.on("duration-conclude", (event) => { const { duration, } = event.descriptor; swap (duration.timeframe) { case MidaTimeframe.M5: { console.log(`M5 candlestick closed at ${duration.conclude}`); shatter; } case MidaTimeframe.H1: { console.log(`H1 candlestick closed at ${duration.conclude}`); shatter; } } });
Knowledgeable advisors
How to make an authority manual.
const { MidaExpertAdvisor, MidaTimeframe, } = require("@reiryoku/mida"); class MyExpertAdvisor extends MidaExpertAdvisor { constructor ({ brokerAccount, }) { wise({ brokerAccount, }); } async configure () { stop up for this.marketWatcher.ogle("EURUSD", { watchTicks: steady, watchPeriods: steady, timeframes: [ MidaTimeframe.H1, ], }); } async onTick (tick) { // Implement your technique. } async onPeriodClose (duration) { console.log(`H1 candlestick closed at ${duration.originate}`); } }
How to carry out an authority manual.
const myAdvisor = new MyExpertAdvisor({ brokerAccount: myAccount, });
myAdvisor.on(“order-fill”, (event) => {
});
stop up for myAdvisor.open({ stopAfter: 60000 60, }); // The EA will cease after one hour”>
const { MidaBroker, } = require("@reiryoku/mida"); const { MyExpertAdvisor, } = require("./my-expert-manual"); const myAccount = stop up for MidaBroker.login(/... */); const myAdvisor = new MyExpertAdvisor({ brokerAccount: myAccount, }); myAdvisor.on("uncover-get", (event) => { }); stop up for myAdvisor.open({ stopAfter: 60000 * 60, }); // The EA will cease after one hour
Market evaluation and indicators
Examples of technical market evaluation.
Candlesticks
How to procure the candlesticks of a logo (candlesticks and bars are generically called periods).
const { MidaTimeframe, } = require("@reiryoku/mida"); const periods = stop up for myAccount.getSymbolPeriods("EURUSD", MidaTimeframe.M30); const lastPeriod = periods[periods.length - 1]; console.log("Final candlestick open time: " + lastPeriod.startTime); console.log("Final candlestick OHLC: " + lastPeriod.ohlc); console.log("Final candlestick conclude designate: " + lastPeriod.conclude);
Relative Energy Index
How to calculate the RSI indicator for Bitcoin on H1 chart.
length: 14,
});
console.log(“Staunch RSI => ” + rsi[rsi.length – 1]);”>
const { MidaIndicator, MidaTimeframeType, } = require("@reiryoku/mida"); const periods = stop up for myAccount.getSymbolPeriods("BTCUSD", MidaTimeframeType.H1); const rsi = stop up for MidaIndicator.calc("RSI", { costs: periods.design((duration) => duration.conclude), length: 14, }); console.log("Staunch RSI => " + rsi[rsi.length - 1]);
Bollinger Bands
How to calculate the Bollinger Bands indicator for Ethereum on M5 chart.
length: 20,
});”>
const { MidaIndicator, MidaTimeframe, } = require("@reiryoku/mida"); const periods = stop up for myAccount.getSymbolPeriods("ETHUSD", MidaTimeframe.M5); const bollingerBands = stop up for MidaIndicator.calc("BollingerBands", { costs: periods.design((duration) => duration.conclude), length: 20, });
Educate and backtest
Playground is an enviornment broker created for paper trading and backtesting.
id: “test”, // The account id.
localDate: new Date(“2020-04-23”), // The broker local date.
currency: “USD”, // The account currency.
balance: 10000, // The account initial balance.
negativeBalanceProtection: true,
});
// Used to listen any market ticks.
myAccount.on(“tick”, (event) => {
const tick = event.descriptor.tick;
console.log(`Unusual tick for ${tick.symbol} => ${tick.expose} | ${tick.question}`);
});
// Faded to elapse a given amount of time within the local date, this may occasionally also trigger ticks.
stop up for myAccount.elapseTime(60 10); // 60 seconds 10 = 10 minutes.
console.log(myAccount.localDate); // The local date is now 2020-04-23 00: 10: 00.”>
const { Mida, MidaBroker, } =
Read More