Hello,

i have modified your code and removed the k-loop which makes it easier. I
haven't fully checked everything but it seems to work correct. Slippage
needs to be implemented. 
Let me know if it works as expected. I have added shapes and a stop line.

Buy = 0;
Sell = 0;
Stop = Null;
StopArray = Null;

EntryLongSignal = Cross(MA(C, 5), MA(C, 20));
ExitLongPosition = L == LLV(L, 20);
TakeProfit = 10 * ATR(10);
BreakevenMove = 2 * ATR(10);
StopLossAmount = 2 * ATR(10);

for (i = 1; i < BarCount; i++) 
{
        if (EntryLongSignal[i]) 
        {
                Buy[i] = 1;
                BuyPrice[i] = O[i];
                
                for (j = i; j < BarCount; j++) 
                {
                        if (j > i)
                                Buy[j] = 0;

                        // Calc stop
                        if (j == i)
                                Stop = BuyPrice[i] - StopLossAmount[i];
                        else
                                if (H[j-1] >= BuyPrice[i] +
BreakevenMove[i]) 
                                        Stop = BuyPrice[i];

                        StopArray[j] = Stop;

                        if (ExitLongPosition[j-1] AND j > i) // Sell next
bar at open
                        {
                                Sell[j] = 1;
                                SellPrice[j] = O[j];
                                Stop = Null;
                                i = j;
                                break;
                        }
                        else                    
                                if (L[j] <= Stop) // Check stop
                                {
                                        Sell[j] = 1;
                                        SellPrice[j] = Stop;
                                        Stop = Null;
                                        i = j;
                                        break;
                                } 
                                else
                                        if (H[j] >= BuyPrice[i] +
TakeProfit[i]) // Check take profit
                                        {
                                                Sell[j] = 1;
                                                SellPrice[j] = BuyPrice[i] +
TakeProfit[i];
                                                Stop = Null;
                                                i = j;
                                                break;
                                        }
                                        else
                                                if (j == BarCount - 1) 
                                                {
                                                        Stop = Null;
                                                        i = j;
                                                        break;
                                                }
                }
        }
}

Plot(C, "Close", 1, 128);
Plot(StopArray, "Stop", colorGreen, 1);
PlotShapes(Buy * shapeUpArrow, colorGreen, 0, L, -12);
PlotShapes(Sell * shapeDownArrow, colorRed, 0, H, -12);


Thomas
www.PatternExplorer.com

 
Von: [email protected] [mailto:[EMAIL PROTECTED] Im Auftrag
von cristianc22
Gesendet: Samstag, 26. Jänner 2008 16:13
An: [email protected]
Betreff: [amibroker] Loop within loop

Hello,
I am trying to create a loop for my stops. I found a lot of good 
examples in the previous postings.
The "j" loop works well without adding the "k" loop. 
The idea is to have the stop moved at breakeven when the market moves 
in my favor.
The idea is simple. One fixed stoploss point, one profit target point 
and stoploss point moved after a certain profit.
I would appreciate any suggestions. 
In other words - How do I escape from loop k and go back to loop j?
(I don't know if yahoo will let me keep the formating)

for (i = 1; i < BarCount; i++) 
{
if (EntryLongSignal[ i ]) 
{
Buy[ i ] = 1;
BuyPrice[ i ] = O[ i ];
for (j = i + 1; j < BarCount; j++) 
{
if (L[ j ] <= (BuyPrice[ i ]-StopLoss)) 
{
Sell[ j ] = 1;
SellPrice[ j ] = BuyPrice[ i ]-StopLoss;
i = j;
j = BarCount;
} 
else if (H[ j ] >= (BuyPrice[ i ] + 
BreakevenMove)) 
{
for (k = j + 1; k < BarCount; k++)
{
if (L [ k ] <= BuyPrice [ i ] + 0.0002)
{
Sell[ k ] = 1;
SellPrice[ k ] = BuyPrice[ i ];
j = k;
k = BarCount;
}
else if (H[ k ] >= BuyPrice[ i ] + 
TakeProfit)
{
Sell[ k ] = 1;
SellPrice[ k ] = BuyPrice[ i ] + 
TakeProfit;
j = k;
k = BarCount;
}
}
} 
else if (ExitLongPosition[ j ]) 
{
Sell[ j ] = 1;
SellPrice[ j ] = O[ j ];
i = j;
j = BarCount;
}
else if (j == BarCount - 1) 
{
Buy[ i ] = 0;
i = BarCount;
}

}

}

}
 

<<attachment: winmail.dat>>

Reply via email to