I currently have a strategy in CoffeeScript which i'm porting over to .AFL, almost there, but still can't 100% replicate the results on Amibroker from the program I'm using to trade, also, the backtest results I've been getting does not necessarily improve the performance.

Anyone could take a look and tell me what I'm missing?

Original code (this is just the indicators used as well as the var and buy/sell conditions.

The strategy is for 15 mins ticks and it's an "either or" position type, Long Margin or Short Margin, just opens and closes the position when it gets the opposite signal

Coffescript:

```
@NATR: (high,low,close,period) ->
if close.length < period
period = close.length
results = 100*(FUNCTIONS.MAX(high,period)-FUNCTIONS.MIN(low,period))/(FUNCTIONS.SMA(close,period))
@LRS: (data, lag, period) ->
if data.length < period
period = data.length
results = talib.LINEARREG_SLOPE
inReal : data
startIdx: 0
endIdx: data.length - lag - 1
optInTimePeriod:period
_.last(results)
@LINREG: (data, lag, period) ->
if data.length < period
period = data.length
results = talib.LINEARREG
inReal : data
startIdx: 0
endIdx: data.length - lag - 1
optInTimePeriod:period
_.last(results)
@SMA: (data, period) ->
if data.length < period
period = data.length
results = talib.SMA
inReal: data
startIdx: 0
endIdx: data.length - 1
optInTimePeriod: period
_.last(results)
@bop: (open, high, low, close) ->
results = talib.BOP
open: open
high: high
low: low
close: close
startIdx: 0
endIdx: high.length - 1
_.last(results)
@rsi: (data, period) ->
if data.length < period
period = data.length
results = talib.RSI
inReal: data
startIdx: 0
endIdx: data.length - 1
optInTimePeriod: period
_.last(results)
natr1 = FUNCTIONS.NATR(insx.high,insx.low,insx.close,NATRP1)
lr1 = FUNCTIONS.LINREG(c,0,P1)+FUNCTIONS.LRS(c,0,P1)*(FL1)
lr2 = FUNCTIONS.LINREG(c,LAG2,P2)+FUNCTIONS.LRS(c,LAG2,P2)*(FL2)
bop = FUNCTIONS.bop(ins.open,ins.high,ins.low,ins.close)
rsi = FUNCTIONS.rsi(c,RSIP)
if natr1 < NATRL
bc1 = bc2 = bc3 = bc4 = sc1 = sc2 = sc3 = sc4 = false //notrade
bc1 = lr1>lr2
bc2 = bop < 0
bc3 = rsi > RSIT2
sc1 = lr1<lr2
sc2 = bop > 0
sc3 = rsi > RSIT1
long position = bc1 and bc2 and bc3
short position = sc1 and sc2 and sc3
//AFL
OptimizerSetEngine("cmae");
_SECTION_BEGIN;
NATRP1 = 700;
NATRL = 11;
P1 = 105;
FL1 = 1.5;
LAG2 = 5;
P2 = 134;
FL2 = -4;
RSIP = 15;
RSIT1 = 49;
RSIT2 = 15;
// Trading
//
natr1 = 100 * (Max(High, NATRP1) - Min(Low, NATRP1)) / MA(Close, NATRP1);
upper = Close + 2 * natr1;
lower = Close - 2 * natr1;
// Linear regression
lr1 = LinearReg(Close, P1) + LinRegSlope(Close, P1) * FL1;
// Laggy linear regression
lr2 = Ref(LinearReg(Close, P2), - LAG2) + Ref(LinRegSlope(Close, P2), - LAG2) * FL2;
// Balance of power
thl = IIf(H != L, H - L, 0.001);
bop = (Close - Open)/thl;
// Linear regression slope normalized by simple moving average
lrs = 1000 * LinRegSlope(Close, LRSP)/MA(Close, LRSP);
// Trading conditions
// If natr1 < NATRL, we should have false no matter what
tc1 = natr1 >= NATRL;
// Buying conditions
bc1 = lr1 > lr2;
bc2 = bop < 0;
bc3 = RSI(RSIP) > RSIT2;
// Sell conditions
sc1 = lr1 < lr2;
sc2 = bop > 0 ;
sc3 = RSI(RSIP) > RSIT1;
longmode= bc1 AND bc2 AND bc3 AND tc1;
shortmode= sc1 AND sc2 AND sc3 AND tc1;
Cover = longmode;
Short = shortmode;
Buy = Cover;
Sell = Short;
// Remove extra signals
Sell = ExRem(Sell, Buy);
Buy = ExRem(Buy, Sell);
Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);
_SECTION_END();
```

Can anybody take a look?