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

[amibroker] Exit Coding



PureBytes Links

Trading Reference Links

I am trying to help an associate who is migrating from MetaStock to 
AB.

The system is for shorting, does not have pyramiding or scaling and 
his exit rules are
1 - Disaster stop exit - EntryPrice + 4 * ATR(20)  - intra day exit
 
2 - Trailing/Profit Stop - Cover at MA(3) - 0.5 * ATR(20)  - intra 
day exit
 
3 - Time Exit - after being in trade for 2 days exit NEXT DAY ON OPEN
 
4 - Reversion test - If the minimum value of today's Open/Close - Low 
> 1 * ATR(15) then exit next day ON OPEN - this should not trigger on 
entry day of trade.

The only way I could see of achieving these is to use looping.  Is 
there a simpler way of combining these exits?  My code for the exits 
follows
SetTradeDelays(0,0,0,0); 					
			// Intraday Entry & Exit signals, no forward 
references
/*
Short = Ref(Entry Signal,-1) 
		AND Low <= Entry Price;
ShortPrice = Min(Open, Entry Price);
*/
Cover = 0;

xDisasterATRMult = 4;
//xDisasterATRMult = Optimize("Disaster ATR Multiplier", 4, 2, 6, 
0.5);
xDisasterATRPer = 20;
//xDisasterATRPer = Optimize("Disaster ATR Period", 20, 10, 30, 5);
xaDisasterStopOffset = xDisasterATRMult * ATR(xDisasterATRPer);
xDisasterStop = Null;

xProfitATRMult = 0.5;
//xProfitATRMult = Optimize("Profit ATR Multiplier", 0.5, 0.2, 1.1, 
0.3);
xProfitATRPer = 20;
//xProfitATRPer = Optimize("Profit ATR Period", 20, 10, 30, 5);
xProfitMAPer = 3;
//xProfitMAPer = Optimize("Profit MA Period", 3, 2, 5, 1);
xaProfitStop = MA(Close, xProfitMAPer) - (xProfitATRMult * ATR
(xProfitATRPer));

xReversionATRMult = 1;
//xReversionATRMult = Optimize("Reversion ATR Multiplier", 1, 0.5, 2, 
0.5);
xReversionATRPer = 15;
//xReversionATRPer = Optimize("Reversion ATR Period", 15, 10, 30, 5);
xaReversionValue = xReversionATRMult * ATR(xReversionATRPer);

xBarsInTrade = 0;
xEntryBar = 0;
xExitType = Null;
xTimeStopPer = 2;
//xTimeStopPer = Optimize("Time Stop Period", 2, 1, 5, 1);

for(xBar = 1; xBar < BarCount; xBar++)
{
	if(xDisasterStop == Null
		AND Short[xBar] == 1)	// Entry Bar & 0 positions 
open
	{
		// Position Opened
		
		xDisasterStop = ShortPrice[VBar] + 
xaDisasterStopOffset[xBar - 1];	// use previous bar ATR
		xEntryBar = xBar;
	}
	else
	{
		Short[xBar] = 0;	// Remove excess short signals
	}	// Not Entry Bar

	if(xDisasterStop != Null)	// trade is open
	{
		xBarsInTrade = xBar - xEntryBar;	// determine 
bars since trade opened

		if (xDisasterStop != Null
			AND High > xDisasterStop)	// RULE 1
		{	
			Cover[xBar] = 1;
			if (xBarsInTrade == 0)
			{
				CoverPrice[xBar] = xDisasterStop;
			}
			else
			{
				CoverPrice[xBar] = Max(Open, 
xDisasterStop);
			}
			xExitType[xBar] = "Rule 1";
			xDisasterStop = Null;	// resets flag to 
indicate "no open trades"
		}
		
		if (xDisasterStop != Null
			AND Low < xaProfitStop[xBar - 1])	// 
RULE 2		 - use previous bar stop value
		{	
			Cover[xBar] = 1;
			CoverPrice[xBar] = Min(Open, xaProfitStop
[xBar - 1]);
			xExitType[xBar] = "Rule 2";
			xDisasterStop = Null;	// resets flag to 
indicate "no open trades"
		}
		
		if (xDisasterStop != Null
			AND xBarsInTrade > xTimeStopPer)	// 
RULE 3
		{	
			Cover[xBar] = 1;
			CoverPrice[xBar] = Open;
			xExitType[xBar] = "Rule 3";
			xDisasterStop = Null;	// resets flag to 
indicate "no open trades"
		}

		if (xDisasterStop != Null
			AND xBarsInTrade > 0	// RULE 4
			AND Min(Open[xBar - 1], Close[xBar - 1]) - Low
[xBar - 1] > xaReversionValue[xBar - 1])	// use previous bar 
values
		{	
			Cover[xBar] = 1;
			CoverPrice[xBar] = Open;
			xExitType[xBar] = "Rule 4";
			xDisasterStop = Null;	// resets flag to 
indicate "no open trades"
		}
	}
}



Thanks for any help

Graham



------------------------------------

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/