# iMACD

Calculates the Moving Average Convergence/Divergence (MACD) indicator values.

## Syntax

```typescript
iMACD(
    symbol: string | null,
    timeframe: number | null,
    fastEmaPeriod: number,
    slowEmaPeriod: number,
    signalPeriod: number,
    appliedPrice: TPriceType,
    mode: MacdMode,
    index: number
): number
```

## Parameters

| Parameter     | Type                                                                                       | Description                                         |
| ------------- | ------------------------------------------------------------------------------------------ | --------------------------------------------------- |
| symbol        | string \| null                                                                             | Symbol name. Null for current symbol                |
| timeframe     | number \| null                                                                             | Timeframe in minutes. Null for current timeframe    |
| fastEmaPeriod | number                                                                                     | Period for the fast EMA calculation                 |
| slowEmaPeriod | number                                                                                     | Period for the slow EMA calculation                 |
| signalPeriod  | number                                                                                     | Period for the signal line calculation              |
| appliedPrice  | [TPriceType](https://fto-2.gitbook.io/fto-strategies-docs/strategies/constants/price-type) | Type of price to use (Open, High, Low, Close, etc.) |
| mode          | MacdMode                                                                                   | Which MACD value to return (Main, Signal)           |
| index         | number                                                                                     | Index of the bar to calculate MACD for              |

## Return Value

Returns a `number` representing the requested MACD value.

## Description

The `iMACD` method calculates the Moving Average Convergence/Divergence (MACD) indicator, which shows the relationship between two moving averages of an asset's price.

Available Price Types can be found in the [TPriceType](https://fto-2.gitbook.io/fto-strategies-docs/strategies/constants/price-type) section.

Available MACD Modes (MacdMode):

* MODE\_MAIN - MACD line
* MODE\_SIGNAL - Signal line

## Example

```typescript
// Calculate MACD values for current symbol and timeframe
const macdLine = this.api.iMACD(
  null,
  null,
  12,
  26,
  9,
  TPriceType.CLOSE,
  MacdMode.MODE_MAIN,
  0
);
const signalLine = this.api.iMACD(
  null,
  null,
  12,
  26,
  9,
  TPriceType.CLOSE,
  MacdMode.MODE_SIGNAL,
  0
);

console.log(`MACD Line: ${macdLine}`);
console.log(`Signal Line: ${signalLine}`);

// Calculate MACD for a specific symbol and timeframe
const eurusdMacd = this.api.iMACD(
  "EURUSD",
  60, // 1-hour timeframe
  12,
  26,
  9,
  TPriceType.CLOSE,
  MacdMode.MODE_MAIN,
  0
);

// Check for MACD crossover
const currentMacd = this.api.iMACD(
  null,
  null,
  12,
  26,
  9,
  TPriceType.CLOSE,
  MacdMode.MODE_MAIN,
  0
);
const currentSignal = this.api.iMACD(
  null,
  null,
  12,
  26,
  9,
  TPriceType.CLOSE,
  MacdMode.MODE_SIGNAL,
  0
);
const previousMacd = this.api.iMACD(
  null,
  null,
  12,
  26,
  9,
  TPriceType.CLOSE,
  MacdMode.MODE_MAIN,
  1
);
const previousSignal = this.api.iMACD(
  null,
  null,
  12,
  26,
  9,
  TPriceType.CLOSE,
  MacdMode.MODE_SIGNAL,
  1
);

const bullishCrossover =
  previousMacd < previousSignal && currentMacd > currentSignal;
const bearishCrossover =
  previousMacd > previousSignal && currentMacd < currentSignal;

if (bullishCrossover) {
  console.log("Bullish MACD crossover detected");
} else if (bearishCrossover) {
  console.log("Bearish MACD crossover detected");
}
```
