Amibroker: Help with loop + backtest

I've been working on a simple system

1 - buy when the prices are above the MA and it touchs the MA (At the price of MA. I call the point of the touch "ProjectedMA"

2 - Sell when the prices are above the MA and it touchs the MA.

So, I wanted to add a filter, to show amibroker that when princes are moving up or down. So I though of using Linear regression slope. When its above 0, it is moving upwards, and vice-versa. So, I add this filter:

Only buy when the Linear Regression slope is above 0;
Only sell when the Linear Regression slope is below 0.

In the optimization I made, the best results gave me the Linear Regression Slope Period of 2. In the backtest, as it consider only the close price when checking the Slope, there are many trades that would not happen in Real Time, since at the moment of the touch, the slope would be different.

So, can I determine how the slope will be the moment price touch the MA?

A picture to show better my doubt:

6g4x.png

Then, I went to study session... that lasted two full days.

First, I found how to calculate the slope using only the data on the chart. I found it here:

http://www.statisticshowto.com/how-t...ression-slope/

As you can see, we can find the slope putting the data in a table. So, in translation, it become like this:

X axis = we numerate the bars, in crescent order, from 1 to last (here we use 3 bars);
Y axis = prices. we use the close prices in bars 1 and 2, but uses the MA price in bar 3;
The rest of the table doesnt really need explaining, does it?

9yko.png

So, I began making a "for loop" that would numerate (put an index) the bars accordingly with the regression period I wanted to use. I did it, and it became like this:

ssjj.png

As you can see, the loop is working. If I put period 10, it will numarate bar [barcount -10] as 1 and bar [barcount -1] as 10. Then I just mounted the equation, as the code below:

Code:
```//ProjectedMA Construction ===========================================================

SetPositionSize( 1, spsShares );

ArrayMA = ParamField("Price field", 3 );
Periods = Optimize("MAPeriods", 4, 4, 58, 2 );
ProjectedMA = Ref( WMA( arrayma, periods -1)  , -1);
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style", styleLine | styleNoLabel);

CenterLine = WMA( ArrayMA, Periods );

//Antecipated Slope

//Here i start the loop.

per=3; //period of the regression.
n=BarCount-per;
indiceX = Null;

function indeX(per)
{

for( i=n , j=1; i<BarCount && j<=per; i++, j++)
indiceX[i] = i - (i-j);

return indiceX;

}

Plot(index(per),"",colorRed);

SumX = Sum(indeX(per), per); //the sum of all the indexes

indiceX2 = indeX(per)^2; //Here I define the X^2 that im gonna need for the equation.
SumX2 = Sum( indiceX2 , per);  //the sum off the indexes^2

XY = IndeX(per)*Close; //index * closes

//slope da banda
//Slope when the prices touch the MA

SumXY = (Sum( Ref(XY , -1) , per-1)) + (ProjectedMA*per); //the sum of all X*Y. The lastvalue used is from the projectedMA
SumY = (Sum(Ref(Close, -1) , per-1)) + (ProjectedMA); // again, last value is from the MA

LinNumerator = (per*SumXY) - (SumX*SumY); //here is the up part of the final equation
LinDenominator = (SumX2) - (sumX^2); //here is the down part of the final equation
Slope = LinNumerator / LinDenominator;  // here is the result, the slope.

//Plot(slope, "",colorRed, styleLine, per);

//Fim/end of slope calculus======================================================================

//Now for the signals==========================================================================

BuyCondSlope = Ref(LinRegSlope(C, per) , -1) >0 AND slope> 0;  //slope is above 0

BuyCondBandTouch = (Ref(Low , -1) > Ref( ProjectedMA , -1)) AND (Low <= ProjectedMA);  //prices touch the MA from above

//ShortCondTL = TL2 > TL1;

ShortCondSlope = Ref(LinRegSlope(C , per) , -1) <0 AND slope < 0;  //slope is below 0

ShortCondBandTouch = (Ref(High , -1) < Ref( ProjectedMA , -1)) AND (High >= ProjectedMA);  //prices touch the MA from below

TickSize = 5;

ShortPrice = ProjectedMA;
Short = ShortCondSlope AND ShortCondBandTouch AND ShortCondTimeLimit;

StopLoss_Mult = Optimize("StopLossMult" , 25 , 10 , 60 , 5);

StopLoss = StopLoss_Mult*TickSize;

ApplyStop(stopTypeLoss , stopModePoint , StopLoss , 1 , False , 0 );
ApplyStop(stopTypeProfit , stopModePoint , StopLoss , 1 , False , 0 );

//END OF ALL CODE======================================================================================```
Well, the slope of each bar is calculated correctly. But I cant plot it. When I try, it appears like this:

8wdo.png

And if I try to run a backtest, it shows no result.
If I try optimization, it shows all the trades as 0,00.

So, what did I do wrong? Can anyone help me? Mr Trash?

Thanks for the atention, guys!

Best regards!