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

[amibroker] Re: code for trailing stop, exrem seems got problem



PureBytes Links

Trading Reference Links

To answer your previous post, AB works primarily with arrays. When you
code "MyVar = EMA(C,20);", AB creates an array for MyVar, each element
of which contains the 20dema of the previous 20 days.

As for this post, even though you specified a date range of 1/1/2003
to Present, AB actually 'executes' your AFL against ALL of your data. 

In the case of your sample AFL, a LONG trade was generated for CHKP on
11/8/2002 and did not exit until 7/21/2003... and even though you
specified the 1/1/2003 to Present range, AB knew about the 11/8/2002
open trade and the ExRem code prevented AB from generating a Buy
signal on 1/6/2003.

Read the 'Understanding AFL' tutorial carefully.

Phsst


--- In amibroker@xxxxxxxxxxxxxxx, "gary_tiger2001"
<gary_tiger2001@xxxx> wrote:
> exrem function seems got problem
> I test the following code for daily chkp data from 1/1/2003 to 
> today. (long only)
> before apply exrem(buy, sell), there is a buy signal on 1/6/2003
> after apply exrem(buy, sell), the buy signal on 1/6/2003 is gone.
> But 1/6/2003 should be the first buy signal in year 2003. It is very 
> strange.
> 
> //my system
>  
> //1.identify trend
> //close greater than ema20, ema20 is rising
> trendup = Ref(C,-1)>Ref(EMA(C,20),-1) AND Ref(EMA(C,20), -1) > Ref
> (EMA(C,20),-2)AND Ref(EMA(C,20),-2) >Ref(EMA(C,20),-3);
> trenddown = Ref(C,-1)<Ref(EMA(C,20),-1) AND Ref(EMA(C,20), -1) < Ref
> (EMA(C,20),-2)AND Ref(EMA(C,20),-2) <Ref(EMA(C,20),-3);
> 
> //2.setup
> //there is a dip, previous day's close is 3 days low
> dip = (Ref(C,-1) == Ref(LLV(C,3), -1)); 
> //AND (Ref(L,-1) == Ref(LLV(L,3), -1));
>  
> //3.trigger/entry
> //if today highter than yesterdays's close + 0.2*ATR(10);
> alert = Hold(dip, 3);
> Buystop = Ref(C, -1) + 0.2 * Ref(ATR(20), -1);
> 
> Buy = trendup AND alert AND H > Buystop;
> BuyPrice = Max(Buystop, L);
> 
>  
> //standard backtester settings
> RoundLotSize = 100;
> TickSize = 0.01;
> SetTradeDelays( 0, 0, 0, 0 );
>  
> //4.exit
> //Tharp's ATR-based position sizing technique 
> //Sell = 0; // selling only by stop
> priceatbuy = 0;
> //priceatbuy = IIf(Buy, BuyPrice, Ref(priceatbuy, -1));
> Lossstop = priceatbuy - 2 * Ref(ATR(20), -1);
> TrailStop = Ref(L, -1) - 2 * Ref(ATR(20), -1);
> trailstop = Max(trailstop, Ref(trailstop, -1));
> Sellstop = Max(Lossstop, trailstop);
> Sell = L < Sellstop;
> SellPrice = Max(Sellstop, H);
> //Buy = ExRem(Buy, Sell);
> Sell = ExRem(Sell, Buy);
> //Capital = 10000; 
> /* IMPORTANT: Set it also in the Settings: Initial Equity */
> //Risk = 0.01*Capital;
> //PositionSize = (Risk/TrailStopAmount)*BuyPrice;
> //ApplyStop( 2, 2, TrailStopAmount, 1 );
> Plot(C,"Price",colorWhite,styleCandle);
> Plot(L, "", colorBlack);
> Plot(H, "", colorBlack);
> Plot(Buystop, "buystop", colorBrightGreen);
> Plot(Sellstop, "sellstop", colorRed);
> PlotShapes( shapeUpArrow * Buy, colorBrightGreen, 0, L, -12);
> PlotShapes( shapeDownArrow * Sell, colorRed, 0, H, -12);
> PlotShapes( shapeDownArrow * dip, colorWhite, 0, H, -30 );
> Title = Name() + Date(); 
> /*+ WriteVal(Ref(MACDRoc,-1)) + "   Today =" + WriteVal(MACDRoc)
> + "       Weekly -" +
> WriteIf(Cond1==1," NEW UP TREND  ", WriteIf(Cond2==1," NEW DOWN 
> TREND   ", WriteIf(Cond3==1,"   Trend is Up    ",WriteIf(Cond4==1," 
> Trend is Down    ",WriteIf(Cond5==1," Trend is Flat    ","")))));*/
> 
> 
> Filter = 1 > -1;
> AddColumn(C,"close");
> AddColumn(Ref(C,-1),"ref(c,-1)");
> AddColumn(EMA(C,20),"ema20");
> AddColumn(trendup, "trendup");
> AddColumn(dip,"dip");
> AddColumn(alert, "alert");
> AddColumn(ATR(20), "atr20");
> AddColumn(Buystop, "buystop");
> AddColumn(H,"high");
> AddColumn(BuyPrice, "buyprice");
> AddColumn(BarsSince(Buy), "barbuy");
> AddColumn(BarsSince(Sell), "barssell");
> //AddColumn(extra, "extra");
> AddColumn(Buy, "buy");
> AddColumn(Ref(priceatbuy,-1),"pricebuy-1");
> AddColumn(priceatbuy, "priceatbuy");
> AddColumn(Lossstop, "lossstop");
> AddColumn(trailstop, "trailstop");
> AddColumn(Sellstop, "sellstop");
> AddColumn(L,"low");
> AddColumn(Sell, "sell");
> 
> 
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "Phsst" <phsst@xxxx> wrote:
> > After your Buy and Sell assignments... code:
> > 
> > Buy = ExRem(Buy,Sell); Sell = ExRem(Sell,Buy);
> > 
> > this will prevent subsequent Buy signals until a Sell occurs.
> > 
> > Phsst
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "gary_tiger2001"
> > <gary_tiger2001@xxxx> wrote:
> > > How to remove excessive buy signals?
> > > After buy, it is possible the price never reach 1.1 * 
> priceatbuy. 
> > > Then another buy signal comes up. How to handle this senario?
> > > 
> > > --- In amibroker@xxxxxxxxxxxxxxx, "Phsst" <phsst@xxxx> wrote:
> > > > I had same general issue a few weeks ago.
> > > > 
> > > > Tomasz responded with code snippet sample from "What's new in 
> > > Amibroker":
> > > > 
> > > > 
> > > > /* 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;
> > > > }
> > > > 
> > > > Change logic from 'Profit Target' to trailing stop of your 
> > > preference. 
> > > > 
> > > > Phsst
> > > > 
> > > > --- In amibroker@xxxxxxxxxxxxxxx, "Adrian Zaremba" 
> > > <headcutter@xxxx>
> > > > wrote:
> > > > > Can I find somewhere a code for a trailing stop? I am not 
> > > talking 
> > > > > about applystop function but a manually written code, like 
> in 
> > > > > Tradestation.


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Special Sale: 50% off ReplayTV
Easily record your favorite shows!
CNet Ranked #1 over Tivo!
http://us.click.yahoo.com/WUMW7B/85qGAA/ySSFAA/GHeqlB/TM
---------------------------------------------------------------------~->

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 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/