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

Re: [amibroker] Re: How to reference LastBuyPrice - arg!



PureBytes Links

Trading Reference Links

Hello,

If you have problem like this you should iterate through bars 
exacly as shown in the User's Guide profit target stop example:
(at the very end of "what's new" part: http://www.amibroker.com/guide/whatsnew.html )



/* a sample low-level implementation of Profit-target stop in AFL: */

Buy = Cross( MACD(), Signal() );

priceatbuy=0;

for( i = 0; i < BarCount; i++ )
{
     if( priceatbuy == 0 && Buy[ i ] ) 
     priceatbuy = BuyPrice[ i ];

     if( priceatbuy > 0 && SellPrice[ i ] > 1.1 * priceatbuy )
     {
       Sell[ i ] = 1;
       SellPrice[ i ] = 1.1 * priceatbuy;
       priceatbuy = 0;
     }
     else
       Sell[ i ] = 0;
}

Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message ----- 
From: "kmckiou" <kmckiou@xxxxxxxxx>
To: <amibroker@xxxxxxxxxxxxxxx>
Sent: Monday, December 29, 2003 6:27 PM
Subject: [amibroker] Re: How to reference LastBuyPrice - arg!


> Graham,
> 
> It's not quite that simple.  Because I am using the last buy price 
> in the calculation of the sell signals, I must calculate the last 
> buy price right when I calculate the buy signals.  The problem is I 
> cannot reference the SELL variable, because it is undefined until I 
> calculate the sell signals.  So...I cannot use exrem(buy,sell) to 
> remove the redundant buy signals prior to calculating the last buy 
> price (because SELL is not defined at that point).
> 
> I've got a circular problem.  If I was using a normal programming 
> language, it would be no problem.  I would just define the arrays at 
> the top of the program and remove any redundant signals after any 
> Buy/Sell calculation.  This AFL is tricker because it forces you to 
> do everything like a spreadsheet - in a single pass.  I love the 
> speed, but this is tricky.
> 
> Still stuck....
> 
>  - Kevin
> 
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "Graham" <gkavanagh@xxxx> wrote:
> > Kevin 
> > You can use exrem(buy,sell) to remove the excess buy signals. 
> Check the help
> > files for details.
> > 
> > Cheers,
> > Graham
> > http://groups.msn.com/asxsharetrading
> > http://groups.msn.com/fmsaustralia 
> > 
> > -----Original Message-----
> > From: kmckiou [mailto:kmckiou@x...] 
> > Sent: Monday, 29 December 2003 1:13 PM
> > To: amibroker@xxxxxxxxxxxxxxx
> > Subject: [amibroker] How to reference LastBuyPrice - arg!
> > 
> > 
> > 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@xxxx
> > Send SUGGESTIONS to suggest@xxxx
> > -----------------------------------------
> > 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/
> 
> 
> 
> 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/ 
> 
> 
> 

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/