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

[amibroker] How do I buy on the actual MA cross price and not on close?



PureBytes Links

Trading Reference Links

Hi,
I'm just trying to work out how to backtest a simple MA cross but using the actual price on the cross, as the entry/exit. It's just a very basic experiment and I'm using 5min bars and alot of the time the MA cross comes during the first or second minute of the bar. I found a formula to calculate the value of the next bar that would make the MA cross, based on the current bar closing values, but I can't figure out how to implement this in a system which would backtest? Perhaps it could run this formula over each bar, and where there is a cross, use that as the entry price , and not the close price? Even if someone could tell me how to choose any bar from a price array, and run this formula, as it only seems to work in an exploration using the last bar of the array. Many thanks for any help.
Code is below...

//PricePredictSegment.afl
//
//This runs in Explore only...
//Set nLastDays n=1

Length1 = Param("Length1",6,1,81,2);
Length2 = Param("Length2",35,2,200,2);

MA1 = MA(C,Length1);
MA2 = MA(C,Length2);

Buy = Cross(MA1,MA2);
Sell = Cross(MA2,MA1);

e = Equity();
Maxe = LastValue(Highest(e));
Plot(Close,"Price",colorBlack,styleCandle);

Plot(MA1,"MA1",colorGreen,styleLine);
Plot(MA2,"MA2",colorBlue,styleLine);

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

PlotShapes(shape, IIf(Buy,colorGreen,colorRed),0, IIf(Buy,Low,High));

Plot(e,"Equity",colorBlue,styleLine|styleOwnScale,0,Maxe);
Plot(10000,"",colorBlue,styleLine|styleOwnScale,0,Maxe);
GraphXSpace = 5;

///////////////////////////////////
//
//Given the lengths of the two moving averages, each of which uses the value of the close
//to compute it's average, compute the closing price that will cause the two averages
//to intersect
//
//
//

//Retrieve the curent values of the moving average lengths

st = _PARAM_VALUES();
Cp = StrFind(st,",");
P1 = StrMid(st,1,Cp-2);
Ccp = StrFind(st,")");
P2 = StrMid(st,Cp,Ccp-Cp-1);

//Moving average Lengths
//FastLength = Length1;
//SlowLength = Length2;
Fastlength = StrToNum(p1);
SlowLength = StrToNum(p2);

Price = C;

MAfast = MA(Price,FastLength);
MASlow = MA(Price,SlowLength);
MAFM1 = IIf(FastLength>1,MA(Price,FastLength-1),0);
MASM1 = IIf(SlowLength>1,MA(Price,SlowLength-1),0);

Filter = BarIndex() == LastValue(BarIndex());

PartA = FastLength*(SlowLength-1)*MASM1;
PartB = SlowLength*(Fastlength-1)*MAFM1;
LenDiff = SlowLength-FastLength;
MACrossClose = (PartA - PartB)/(LenDiff);

AddColumn(FastLength,"FL",1.0);
AddColumn(SlowLength,"SL",1.0);
AddColumn(MAFast,"MAF",1.9);
AddColumn(MASlow,"MAS",1.9);
AddColumn(MACrossClose,"Crosses",1.9);

//AddTextColumn(st,"Params",1.0);
//AddColumn(cp,"cp",1.0);
//AddTextColumn(P1,"P1",1.0);
//AddColumn(ccp,"ccp",1.0);
//AddTextColumn(P2,"P2",1.0);
//
//




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

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to 
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

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

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/