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

[amibroker] Re: Moving average crossover



PureBytes Links

Trading Reference Links

I had found this piece of code on EMA CrossOver from AB website.
Hope this helps.

SetOption("InitialEquity", 1000000);
SetOption("MinShares", 50);
SetOption("NoDefaultColumns", True );
SetOption("CommissionMode", 2); //$$ per trade
SetOption("CommissionAmount", 0);
SetOption("MarginRequirement", 10);
SetOption("UsePrevBarEquityForPosSizing", True);
SetTradeDelays( 1, 1, 1, 1 );
RoundLotSize = 50;

function EMA0(A, p)
{
	r[0] = a[0];
	ep = 2/(p+1);
	for(i = 1; i < BarCount; i++)
	{
		r[i] = r[i-1] + (a[i] - r[i-1]) * ep;
	}
	return r;
}

tr = Max(H-L, Max(abs(H-Ref(C, -1)), abs(Ref(C, -1)-L)));
tr[0] = H[0] - L[0];
movshort = EMA( C, Prefs(4) );

fast = EMA0(C, Optimize("FastEMA", 50, 20, 90, 10));
slow = EMA0(C, Optimize("SlowEMA", 200, 100, 500, 50));
Buy = Cross(movshort,fast);
Sell = Cross( fast,movshort);
Buy[1] = 0; // to avoid false signal at the beginning

ATR_multi = Optimize("ATP Multi", 5, 3, 9, 2);
ATR0 = EMA0(tr, 20);
Risk_Per_Share = Ref(ATR0, -1) * ATR_multi;
Heat = Optimize("Heat", 0.05, 0.01, 0.21, 0.02);
BuyPrice = (H+O)/2;
SellPrice = (L+O)/2;

PosSize = Heat * BuyPrice/ Risk_Per_Share * 100; // percentage of equity
PSAdjust = 0.45; // the additional ~0.4% is used to simulate Ed's
rounding to "nearest 50" instead of rounding down.
SetPositionSize(PosSize + PSAdjust, spsPercentOfEquity);

Filter = 1;

AddColumn( DateTime(), "Date", formatDateTime ); 
AddColumn(O, "Open");
AddColumn(H, "High");
AddColumn(L, "Low");
AddColumn(C, "Close");
AddColumn(fast, "FastEMA", 1.3);
AddColumn(slow, "SlowEMA", 1.3);
AddColumn(ATR0, "ATR", 1.3);
AddColumn(IIf(Buy, 111, IIf(Sell, 222, 0)) , "Buy1Sell2", 1);
AddColumn(PosSize, "PosSize%Eq");
AddColumn(Equity() , "Equity");

Plot(fast, "FastEMA", colorRed);
Plot(slow, "SlowEMA", IIf(movshort>slow,colorSkyblue,colorDarkRed));
Plot(Close,"",IIf(movshort>fast,colorGreen,IIf(movshort<fast,colorRed,colorWhite)),styleCandle,styleThick);
Plot(movshort, "", colorBlue, styleThick+styleNoLabel);

--- In amibroker@xxxxxxxxxxxxxxx, "infynhome" <infynhome@xxx> wrote:
>
> Hi,
> 
> Call me stupid, but I need help on creating backtesting and scan codes
> for Moving average crossovers, since they are quite reliable.
> 
> I trust 14 and 21 days simple ma for short term and 20/50 day ma for
> intermediate term.
> 
> Buy when 14 crosses 21 / 20 crosses 50.
> Vice versa for sell.
> 
> I also need optimization help to find out the perfect match of moving
> average combinations.
> 
> Expecting help.
> Mithil
>