Vars: SumVS(0), AvgVS(0), DiffVS(0), StdVS(0), SetArr(0), SumArr(0), DiffArr(0), VSLow(0), VSMid(0), VSHigh(0), RiskReward(0);
Array: VS[20](0);
For SetArr = 0 To 4 Begin
VS[SetArr * 4 + 0] = (Open[SetArr] - AvgPrice[SetArr + 1]) / AvgPrice[SetArr + 1];
VS[SetArr * 4 + 1] = (High[SetArr] - AvgPrice[SetArr + 1]) / AvgPrice[SetArr + 1];
VS[SetArr * 4 + 2] = (Low[SetArr] - AvgPrice[SetArr + 1]) / AvgPrice[SetArr + 1];
VS[SetArr * 4 + 3] = (Close[SetArr] - AvgPrice[SetArr + 1]) / AvgPrice[SetArr + 1];
End;

For SumArr = 0 To 19 Begin
If SumArr = 0 Then
SumVS = 0;
SumVS = SumVS + VS[SumArr];
If SumArr = 19 Then
AvgVS = SumVS / 20;

For DiffArr = 0 To 19 Begin
If DiffArr = 0 Then
DiffVS = 0;
DiffVS = DiffVS + Square(VS[DiffArr] - AvgVS);
If DiffArr = 19 Then
StdVS = SquareRoot(DiffVS / 20);
End;
End;

VSLow = AvgPrice * (1 + (AvgVS - StdVS * 2));
VSMid = AvgPrice * (1 + AvgVS);
VSHigh = AvgPrice * (1 + (AvgVS + StdVS * 2));

If MarketPosition = 0 Then Begin
Buy ("Buy") Tomorrow at VSLow Limit;
RiskReward = VSMid-VSLow;
End;

If MarketPosition = 1 Then
sell ("PT") Tomorrow At VSHigh Limit;
{
If MarketPosition = 1 Then
ExitLong ("TS") Tomorrow At VSLow Stop;
If Open Tomorrow >= VSLow Then
ExitLong ("SLa") From Entry ("Buy") At (VSLow-(VSMid-VSLow)) Stop;
If Open Tomorrow < VSLow Then
ExitLong ("SLb") From Entry ("Buy") At (Open Tomorrow-(VSMid-VSLow)) Stop;}