forex-tester-custom-robot-api
v2.0.3
Published
TypeScript types and runtime API for custom strategies in [Forex Tester Online](https://forextester.com/).
Downloads
361
Readme
forex-tester-custom-robot-api
TypeScript types and runtime API for custom strategies in Forex Tester Online.
Install this package in a custom strategy project, extend Robot, then build and upload the result to FTO.
Install
npm install forex-tester-custom-robot-apiWrite a strategy
Export a default class that extends Robot. Implement OnInit and OnTick. The platform injects this.api before OnInit runs.
import {
Robot,
InputNumber,
MovingAverageType,
OrderSelectMode,
OrderType,
PriceType,
} from 'forex-tester-custom-robot-api'
export default class MaCrossover extends Robot {
private fastPeriod!: InputNumber
private slowPeriod!: InputNumber
private lotSize!: InputNumber
public OnInit(): void {
this.api.SetRobotShortName('MA Crossover')
this.api.SetRobotDescription('Buys when the fast MA crosses above the slow MA.')
this.fastPeriod = this.api.CreateIntegerInput('Fast Period', 10, 1, 500, 1)
this.slowPeriod = this.api.CreateIntegerInput('Slow Period', 20, 1, 500, 1)
this.lotSize = this.api.CreateDoubleInput('Lot Size', 0.1, 0.01, 100, 0.01, 2)
}
public OnTick(): void {
const fast = this.api.GetMA(1, 0, this.fastPeriod.value, MovingAverageType.SMA, PriceType.CLOSE)
const slow = this.api.GetMA(1, 0, this.slowPeriod.value, MovingAverageType.SMA, PriceType.CLOSE)
const prevFast = this.api.GetMA(2, 0, this.fastPeriod.value, MovingAverageType.SMA, PriceType.CLOSE)
const prevSlow = this.api.GetMA(2, 0, this.slowPeriod.value, MovingAverageType.SMA, PriceType.CLOSE)
const crossedUp = prevFast <= prevSlow && fast > slow
const crossedDown = prevFast >= prevSlow && fast < slow
if (crossedUp) {
this.closeOpposite(OrderType.SELL)
this.openIfFlat(OrderType.BUY)
} else if (crossedDown) {
this.closeOpposite(OrderType.BUY)
this.openIfFlat(OrderType.SELL)
}
}
private openIfFlat(orderType: OrderType): void {
if (this.hasOrder(orderType)) {
return
}
this.api.PlaceOrder(
this.api.Symbol(),
orderType,
0,
this.lotSize.value,
0,
0,
'MA Crossover',
0
)
}
private closeOpposite(orderType: OrderType): void {
for (let i = this.api.GetActiveOrderCount() - 1; i >= 0; i--) {
if (!this.api.SelectOrder(i, OrderSelectMode.POSITION)) {
continue
}
if (this.api.GetOrderSymbol() === this.api.Symbol() && this.api.GetOrderType() === orderType) {
this.api.CloseOrder(this.api.GetOrderTicket())
}
}
}
private hasOrder(orderType: OrderType): boolean {
for (let i = 0; i < this.api.GetActiveOrderCount(); i++) {
if (!this.api.SelectOrder(i, OrderSelectMode.POSITION)) {
continue
}
if (this.api.GetOrderSymbol() === this.api.Symbol() && this.api.GetOrderType() === orderType) {
return true
}
}
return false
}
}0 is the current forming bar. Use index 1 when you need a confirmed closed bar.
Lifecycle
| Method | Required | When it runs |
| --- | --- | --- |
| OnInit() | yes | Once, when the strategy is created |
| OnTick() | yes | On each price update |
| OnParamsChange() | no | After the user changes inputs |
| OnStart() | no | When the strategy starts running |
| OnStop() | no | When the strategy is stopped |
What this.api covers
- Orders —
PlaceOrder,ModifyOrder,CloseOrder,ClosePartial,CancelOrder,SelectOrder,GetOrderInfo - Inputs —
CreateIntegerInput,CreateDoubleInput,CreateEnumInput,CreateMATypeInput,CreateApplyToPriceInput, and other typed input helpers - Chart data —
Open,High,Low,Close,Volume,Time,Bars,Symbol,Timeframe - Built-in indicators —
GetMA,iMA,iRSI,iMACD, and otheri*helpers - Custom indicators —
CreateIndicator,GetIndicatorValue - Objects, dates, and account info — chart objects,
FTODate, and account/testing helpers
TypeScript types in this package are the source of truth: use autocomplete on this.api and the exported enums.
Documentation
Full authoring guides, setup, and upload steps: FTO custom strategies docs.
