[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: [amibroker] Loop within loop



PureBytes Links

Trading Reference Links

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: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] Im Auftrag
von cristianc22
Gesendet: Samstag, 26. Jänner 2008 16:13
An: amibroker@xxxxxxxxxxxxxxx
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;
}

}

}

}
 


Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 

<<attachment: winmail.dat>>