To convert the given TC2000 code into AFL (AmiBroker Formula Language), we need to map the logic of comparing Exponential Moving Averages (EMAs) and Close prices across multiple time periods.
🔍 Understanding the TC2000 Logic
The TC2000 condition checks for a specific EMA crossover pattern on the current bar, one bar ago, and two bars ago. Specifically, it looks for:
XAVGC8 < XAVGC3 → EMA8 is below EMA3
XAVGC34 < XAVGC8 → EMA34 is below EMA8
C < XAVGC3 → Close is below EMA3
C > XAVGC8 → Close is above EMA8
These conditions are repeated for the current bar, 1 bar ago, and 2 bars ago using .1, .2, etc. in TC2000.
🔄 Mapping to AFL
In AFL, the equivalent of XAVGC8 is EMA(C, 8), and referencing past values is done with Ref(...). However, in the provided AFL code, these values are already precomputed:
C_1 = Ref(C, -1);
C_2 = Ref(C, -2);
C_3 = Ref(C, -3);
ema3 = EMA(C, 3);
ema8 = EMA(C, 8);
ema34 = EMA(C, 34);
ema3_1 = Ref(ema3, -1);
ema3_2 = Ref(ema3, -2);
ema3_3 = Ref(ema3, -3);
ema8_1 = Ref(ema8, -1);
ema8_2 = Ref(ema8, -2);
ema8_3 = Ref(ema8, -3);
ema34_1 = Ref(ema34, -1);
ema34_2 = Ref(ema34, -2);
ema34_3 = Ref(ema34, -3);
✅ Final AFL Conversion
Using the precomputed variables, the TC2000 condition is translated into the following AFL expression:
(
(ema8 < ema3 AND ema34 < ema8 AND C < ema3 AND C > ema8)
) OR (
(ema8_1 < ema3_1 AND ema34_1 < ema8_1 AND C_1 < ema3_1 AND C_1 > ema8_1)
) OR (
(ema8_2 < ema3_2 AND ema34_2 < ema8_2 AND C_2 < ema3_2 AND C_2 > ema8_2)
)
📌 Notes
- Logical operators in AFL are uppercase:
AND, OR
- The parentheses ensure clarity and proper evaluation order
- This expression evaluates to
True (1) when any of the three conditions are met (current bar, one bar ago, or two bars ago)
- You can assign this expression to a variable for further use, e.g.:
BuySignal = (
(ema8 < ema3 AND ema34 < ema8 AND C < ema3 AND C > ema8)
) OR (
(ema8_1 < ema3_1 AND ema34_1 < ema8_1 AND C_1 < ema3_1 AND C_1 > ema8_1)
) OR (
(ema8_2 < ema3_2 AND ema34_2 < ema8_2 AND C_2 < ema3_2 AND C_2 > ema8_2)
);
✅ Summary
This AFL expression accurately replicates the TC2000 condition, checking for a specific EMA and Close price relationship on the current bar, and two previous bars, using precomputed reference values.