PureBytes Links
Trading Reference Links
|
This seems like such a reasonable thing to do, but I have not been
able to figure how to do it....
How do you reference the last BuyPrice in AFL?
I used
Buy = myBuyCondition*myBuyFilter;
lastbuyprice=ValueWhen(Ref(Buy, -1)==True, BuyPrice, 1);
but the value is updated everytime there is a redundant Buy signal.
The back tester filters the extra Buy signals just fine.
Also, when I put Equity(1) in from of the plot commands, the extra
buy signals are filtered.
I tried putting Equity(1) just after the Buy assignment like this
Buy = myBuyCondition*myBuyFilter;
Equity(1);
lastbuyprice=ValueWhen(Ref(Buy, -1)==True, BuyPrice, 1);
but it seemed to really mess up the signals.
I'm stuck. How do I reference the last BuyPrice where a non-
redundant Buy signal occured.
A complete code listing is attached if you want context.
Thanks,
- Kevin
---------------------Pull back Buyer-----------------
//------------------Initialization of variables----------------------
--
SetOption("InitialEquity", 100000 );
SetTradeDelays(1,1,1,1);
RoundLotSize = 1;
posqty = 5; //Optimize("PosQty", 5, 1, 20, 1 );
SetOption("MaxOpenPositions", posqty);
// desired position size is 100% portfolio equity
// divided by PosQty positions
PositionSize = -100/posqty;
//----------------------End Initialization--------------------------
//----------------------Start Parameters----------------------------
F1=30; //Optimize("F1", 20, 20, 30, 1); //22, 30
F2=50; //Optimize("F2", 50, 50, 80, 2); //62, 50
LLParam=6; //Optimize("LLParam", 10, 5, 20, 1); //7, 10, 7, 6
SMA=60; //Optimize("Sell MA", 60, 30, 80, 5);//50, 35, 60
BMA=30; //Optimize("Buy MA", 30, 20, 50, 2);//35, 30
//maximum risk percentage of buyprice
maxRisk = 30; // Optimize("Risk %", 30, 25, 50, 1); //25, 30
//Keep at least Minprofit fraction of profits after you reach
profitTarget
tp = 21; //Optimize("tp", 15, 21, 30, 1); //15, 15, 10, 22
targetProfit = tp/100;
kp = 70; //Optimize("kp", 70, 65, 80, 1); //50 ,70, 70, 70
keepProfit = kp/100;
BuyMA=MA(Close, BMA);
SellMA=MA(Close, SMA);
//----------------------End Parameters------------------------------
//----------------------Begin Filters--------------------------------
//Must be in an uptrend and below the Buy MA
BF1=MA(Close,F1)>MA(Close,F2) AND Close < BuyMA;
//BF2=MA(Close,F1)>MA(Close,F2)>MA(Close, F3);
//----------------------End Filters-----------------------------
//-------------------Begin Buy Rules----------------------------
//Buy if equal to or lower than the most recent LLParam day low
BC1 = Close<=LLV(Close, LLParam);
//BC2 = Cross(Close, BuyMA); //only executed if a quick excursion
below BuyMA
Buy = (BF1 * BC1); // OR (BF2 * BC2);
//SellPrice is updated on every bar. It is not the value when you
last sold.
Highval=HighestSince(Buy==True, SellPrice, 1);
//BuyPrice is updated on every bar. It is not the value when you
last bought.
mybuyprice=ValueWhen(Ref(Buy, -1)==True, BuyPrice, 1);
//---------------------------End Buy Rules---------------------
//-------------------------Begin Sell Rules------------------
//Sell if price crosses above Sell MA
SR1 = Cross(Close, SellMA);
//Sell if price crosses below the maxrisk
SR2 = Cross(myBuyPrice*(1-maxrisk/100), Close);
//Don't let a profit evaporate. Set Sell Triggers (ST)
ST1 = IIf((Close/myBuyPrice) >=(1+targetProfit), True, False);
ST2 = IIf(Highval>Close>myBuyPrice,((Close-myBuyPrice)/(Highval-
myBuyPrice)) <= keepProfit, False);
SR3 = ST1 * ST2;
Sell = SR1 OR SR2 OR SR3;
Short=Cover=False;
//-------------------------End Sell Rules--------------------
//-----------------Start Alerts-----------------------
/*
AlertIf( Buy, "", "Buy Alert "+FullName(), 1 );
AlertIf( Sell, "", "Sell Alert "+FullName(), 2 );
*/
//------------------End Alerts-----------------------
//-----------------Start Plots----------------------
Equity(1); //eliminate any redundant buy or sell signals
BuyRef=Ref(Buy, -1);
SellRef=Ref(Sell, -1);
Plot(Highval, "Highval", colorBlue, styleLine);
Plot(myBuyPrice, "myBuyprice", colorRed, styleLine);
Plot(BuyPrice, "buyprice", colorOrange, styleLine);
Plot(Close, "close", colorBlack, styleCandle);
Plot(BuyMA, "BuyMA", colorBlue, styleLine);
Plot(SellMA, "SellMA", colorOrange, styleLine);
shape0 = BuyRef * shapeUpArrow + SellRef * shapeDownArrow;
PlotShapes( shape0, IIf( BuyRef, colorGreen, colorRed ), 0, IIf(
BuyRef, Low, High ) );
shape1 = SellRef * Ref(SR1, -1) * shapeDigit1;
PlotShapes( shape1, colorBlack, 0, Low, -24 );
shape2 = SellRef* Ref(SR2, -1) * shapeDigit2;
PlotShapes( shape2, colorBlack, 0, Low, -36 );
shape3 = SellRef* Ref(SR3, -1) * shapeDigit3;
PlotShapes( shape3, colorBlack, 0, Low, -48 );
GraphXSpace = 5;
//-----------------End Plots-------------
Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Links
To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
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/
|