OnTick
What is it??
Working with bars
Example Explained
public OnTick() {
if (this.MAFastPeriod.value >= this.MASlowPeriod.value) {
console.error("Fast MA Period must be less than Slow MA Period");
return;
}
// process logic only once per bar opening
const currentTime = this.api.Time(0);
if (currentTime.valueOf() === this.lastBarTime.valueOf()) {
return;
}
this.lastBarTime = currentTime;
// get current and previous MA values
const currSlowMA = this.getMAValue(this.MASlowPeriod.value, 1);
const prevSlowMA = this.getMAValue(this.MASlowPeriod.value, 2);
const currFastMA = this.getMAValue(this.MAFastPeriod.value, 1);
const prevFastMA = this.getMAValue(this.MAFastPeriod.value, 2);
// check for buy signal (bullish crossover)
if (currFastMA > currSlowMA && prevFastMA <= prevSlowMA) {
this.closePosition(TTradePositionType.SELL, this.MagicNumber.value);
this.placePosition(TTradePositionType.BUY, this.MagicNumber.value);
}
// check for bearish crossover
if (currFastMA < currSlowMA && prevFastMA >= prevSlowMA) {
this.closePosition(TTradePositionType.BUY, this.MagicNumber.value);
this.placePosition(TTradePositionType.SELL, this.MagicNumber.value);
}
}
private closePosition(
positionType: TTradePositionType,
magicNumber: number = 0
) {
for (let i = this.api.getActiveOrderCount() - 1; i >= 0; i--) {
if (
this.api.selectOrder(i, 0, 0) &&
this.api.getOrderSymbol() === this.api.Symbol() &&
this.api.getOrderType() === positionType &&
(magicNumber === 0 || this.api.getOrderMagicNumber() === magicNumber)
) {
const ticket = this.api.getOrderTicket();
this.api.closeOrder(ticket);
}
}
}
private placePosition(
positionType: TTradePositionType,
magicNumber: number = 0
) {
const ticket = this.api.placeOrder(
this.api.Symbol(),
positionType,
0,
this.LotSize.value,
0,
0,
"MACrossOverStrategy",
magicNumber
);
if (ticket === null) {
console.error(
`Position ${
TTradePositionType[positionType]
} failed with error #${this.api.GetLastError()}`
);
}
}
private getMAValue(period: number, index: number) {
return this.api.iMA(
this.api.Symbol(),
this.api.Timeframe(),
period,
0,
this.MAMethod.value,
TPriceType.CLOSE,
index
);
}
In Short
Last updated