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

Re: [amibroker] Re: I am lost



PureBytes Links

Trading Reference Links








Hi Rick,
The code is below.EMP62 was kind enough to review my initial code and it was extremely helpful.As i mentioned,when reviewing the backtest trade results I noticed that I was scaling out of 80%-97% of my position at the first profit target,when the syttem was coded to scale of 50% of the position...
Tomasz pointed out that I should make sure that I selected zero for trade delay in the setting box,and directly enter the dealy entries in the code.He suggested (using Ref() function).
As I am only at this one week,I took this to mean
Buyprice=ref(open,1),as I wanted the open of the following day after the MA crossover.
Cross checking backtest entries vs the chart,I noticed in specific instances,I was getting long at the HIGH the date of the signal,not the open of the follwing day.I am quite confident I screwed something up,misinterperted Tomaz or who knows what.I decided to use  the SetTadeDelays(1,1,1,1) function and evrything seems to work as I hoped....
Sorry for the long expaination.For all I know you may be a power user trying to assist me.In case you are new like me,I was only trying to help make things a bit simpler for you and clarify sometricky setting issues
Allan


Buy = Cross(C, MA( C,21) ); 
Sell = 0; 
SetTradeDelays(1,1,1,1);// Per Tomasz; Set trade delay=0 in settings,and set trade delays directly in code.setting non zero buy delays
//(because then Buy array is shifted while position size array is NOT).
//if you use advanced position sizing, set trade delays to zero AND delay entries directly in the code
//BuyPrice=Ref(Open,1); ////(using Ref() function)??? suggested bt Tomasz...causes inconsistent entries...check with tomasz


// the system will exit 
// 50% of position if FIRST PROFIT TARGET stop is hit 
// 50% of position is SECOND PROFIT TARGET stop is hit 
// 100% of position if TRAILING STOP is hit 
FirstProfitTarget = 20; // profit 
SecondProfitTarget =30; // in percent 
TrailingStop = 50; // also in percent 
priceatbuy=0; 
highsincebuy = 0; 
exit = 0; 
for( i = 0; i < BarCount; i++ ) 
{ 
if( priceatbuy == 0 AND Buy[ i ] ) 
{ 
priceatbuy = BuyPrice[ i ]; 
} 
if( priceatbuy > 0 ) 
{ 
highsincebuy = Max( High[ i ], highsincebuy ); 
if( exit == 0 AND 
High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * 
priceatbuy ) 
{ 
// first profit target hit - scale-out 
exit = 1; 
Buy[ i ] = sigScaleOut; 
} 
if( exit == 1 AND 
High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * 
priceatbuy ) 
{ 
// second profit target hit - exit 
exit = 2; 
SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 
0.01 ) * priceatbuy ); 
} 
if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy ) 
{ 
// trailing stop hit - exit 
exit = 3; 
SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 
0.01 ) * highsincebuy ); 
} 
if( exit >= 2 ) 
{ 
Buy[ i ] = 0; 
Sell[ i ] = exit + 1; // mark appropriate exit code 
exit = 0; 
priceatbuy = 0; // reset price 
highsincebuy = 0; 
} 
} 
} 
SetPositionSize( 10, spsPercentOfEquity ); 
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position 

SetBarsRequired(10000,10000); 
SetChartOptions(0, chartShowDates); 
Plot(C,"",colorWhite,64); 
PlotShapes(IIf(Buy == 1,shapeUpArrow,0),colorWhite, layer = 0, yposition = BuyPrice, offset = 0 ); 
PlotShapes(IIf(Buy == sigScaleOut,shapeDownTriangle,0),colorAqua, layer = 0, yposition = BuyPrice, offset = 0 ); 
PlotShapes(IIf(Sell,shapeDownArrow,0),colorYellow, layer = 0, yposition = SellPrice, offset = 0 ); 
//PlotShapes(IIf(Short,shapeHollowDownArrow,0),colorLightBlue, layer = 0, yposition = ShortPrice, offset = 0 ); 





----- Original Message -----
From: kb1flr <myersrk@xxxxxxxxxxx>
Date: Sunday, July 16, 2006 6:39 am
Subject: [amibroker] Re: I am lost
> Allen, 
> 
> Would you mind posting the final working code? 
> 
> Rick 
> 
> --- In amibroker@xxxxxxxxxxxxxxx, allansn@xxx wrote: 
> > 
> > Too late Tomasz,you have already solved the mystery... 
> > you were right on in your assessment 
> > 
> > "I forgot to add that your problems may be also due to setting 
> non 
> > zero buy delays 
> > (because then buy array is shifted while position size array is 
> not). 
> > If you use advanced position sizing, set trade delays to zero 
> and 
> >  delay entries directly in the code 
> >  (using Ref() function)." 
> > When I changed the settings from 1 day delay/ open,to 
> Zero/close,I 
> get the 50% scale out that was coded.It makes a HUGE difference as 
> I 
> was scaling out as much as 97% and was clueless as to why.. 
> > Thank you and everyine else who are leading me from the path of 
> programming darkness 
> > 
> > Allan 
> > 
> > 
> > 
> > ----- Original Message ----- 
> > From: Tomasz Janeczko <groups@xxx> 
> > Date: Saturday, July 15, 2006 8:16 pm 
> > Subject: Re: [amibroker] Re: I am lost 
> > > Hello, 
> > > 
> > > As I wrote you through support channel: you need to provide 
> > > settings file (.abs) and all other details needed to reproduce 
> > > your setup. 
> > > 
> > > Without that it's guessing game: it may be because of delays 
> set 
> > > in the settings, 
> > > or round lot sizing effect or other things. 
> > > Anyway without looking on your settings file, ticker list and 
> > > trade list one can not tell. 
> > > 
> > > Best regards, 
> > > Tomasz Janeczko 
> > > amibroker.com 
> > >  ----- Original Message ----- 
> > >  From: allansn@xxx 
> > >  To: amibroker@xxxxxxxxxxxxxxx 
> > >  Sent: Saturday, July 15, 2006 10:21 PM 
> > >  Subject: Re: [amibroker] Re: I am lost 
> > > 
> > > 
> > >  Thank you all for your help.EMP,i copied your code and ran it 
> as 
> > > i still am getting very odd # of shares on the initial scale 
> > > out,and its certainly not a consistent ratio.I will read what 
> i 
> > > can,but this is puzzling.. 
> > > 
> > >   
> > > 
> > > 
> > > --------------------------------------------------------------- 
> -- 
> -- 
> > > ----------- 
> > > 
> > > 
> > > 
> > >  hi, 
> > > 
> > >  i tested your code on HANS. I added some code to display what 
> is 
> > > going on. I always like to visualize the system and this is 
> easy 
> > > to do with Amibroker. 
> > > 
> > >  What you see in the image I added is that the initial Buy 
> signal 
> > > is already in July. In november it scales out followed by a 
> sell 
> > > signal. If I run the backtest I find that scaling out sells 
> half 
> > > of the initial amount. 
> > > 
> > >  Between the initial buy and the scale out there are multiple 
> buy 
> > > signals. However these are ignored by the backtester since the 
> > > initial position is eneterd in July. So maybe your problem has 
> > > something to do with the range you use in your backtest. Try 
> to 
> > > backtest all quotations. 
> > > 
> > > 
> > >  Buy = Cross(C, MA( C,21) ); 
> > >  Sell = 0; 
> > > 
> > >  // the system will exit 
> > >  // 50% of position if FIRST PROFIT TARGET stop is hit 
> > >  // 50% of position is SECOND PROFIT TARGET stop is hit 
> > >  // 100% of position if TRAILING STOP is hit 
> > > 
> > >  FirstProfitTarget = 20; // profit 
> > >  SecondProfitTarget =30; // in percent 
> > >  TrailingStop = 50; // also in percent 
> > > 
> > >  priceatbuy=0; 
> > >  highsincebuy = 0; 
> > > 
> > >  exit = 0; 
> > > 
> > >  for( i = 0; i < BarCount; i++ ) 
> > >  { 
> > >  if( priceatbuy == 0 AND Buy[ i ] ) 
> > >  { 
> > >  priceatbuy = BuyPrice[ i ]; 
> > >  } 
> > > 
> > >  if( priceatbuy > 0 ) 
> > >  { 
> > >  highsincebuy = Max( High[ i ], highsincebuy ); 
> > > 
> > >  if( exit == 0 AND 
> > >  High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * 
> > >  priceatbuy ) 
> > >  { 
> > >  // first profit target hit - scale-out 
> > >  exit = 1; 
> > >  Buy[ i ] = sigScaleOut; 
> > >  } 
> > > 
> > >  if( exit == 1 AND 
> > >  High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * 
> > >  priceatbuy ) 
> > >  { 
> > >  // second profit target hit - exit 
> > >  exit = 2; 
> > >  SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 
> > >  0.01 ) * priceatbuy ); 
> > >  } 
> > > 
> > >  if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy ) 
> > >  { 
> > >  // trailing stop hit - exit 
> > >  exit = 3; 
> > >  SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 
> > >  0.01 ) * highsincebuy ); 
> > >  } 
> > > 
> > >  if( exit >= 2 ) 
> > >  { 
> > >  Buy[ i ] = 0; 
> > >  Sell[ i ] = exit + 1; // mark appropriate exit code 
> > >  exit = 0; 
> > >  priceatbuy = 0; // reset price 
> > >  highsincebuy = 0; 
> > >  } 
> > >  } 
> > >  } 
> > > 
> > >  SetPositionSize( 10, spsPercentOfEquity ); 
> > >  SetPositionSize( 50, spsPercentOfPosition * ( Buy == 
> sigScaleOut 
> > > ) ); // scale out 50% of position 
> > > 
> > > 
> > >  SetBarsRequired(10000,10000); 
> > > 
> > >  SetChartOptions(0, chartShowDates); 
> > >  Plot(C,"",colorWhite,64); 
> > > 
> > >  PlotShapes(IIf(Buy == 1,shapeUpArrow,0),colorWhite, layer = 
> 0, 
> > > yposition = BuyPrice, offset = 0 ); 
> > >  PlotShapes(IIf(Buy == 
> > > sigScaleOut,shapeDownTriangle,0),colorAqua, layer = 0, 
> yposition 
> = 
> > > BuyPrice, offset = 0 ); 
> > > 
> > >  PlotShapes(IIf(Sell,shapeDownArrow,0),colorYellow, layer = 0, 
> > > yposition = SellPrice, offset = 0 ); 
> > >  
> //PlotShapes(IIf(Short,shapeHollowDownArrow,0),colorLightBlue, 
> > > layer = 0, yposition = ShortPrice, offset = 0 ); 
> > > 
> > > 
> > > 
> > >    ----- Original Message ----- 
> > >    From: allansn@xxx 
> > >    To: amibroker@xxxxxxxxxxxxxxx 
> > >    Sent: Saturday, July 15, 2006 4:23 PM 
> > >    Subject: Re: [amibroker] Re: I am lost 
> > > 
> > > 
> > > 
> > > 
> > >    Tomasz, 
> > > 
> > >    that is certainly not the answer I was expecting.I am 
> making 
> a 
> > > diligent effort to learn the coding and I have a specific 
> > > NEED....Perhaps you didnt bother to read my prior message to 
> the 
> board 
> > > 
> > >    I posted the code that I was working with and can NOT get 
> the 
> > > results expected from the code descriptions.It is not like I 
> asked 
> > > the board to code me a system.I supplied the system which I 
> copied 
> > > from either the manual or a user file.I had no problems 
> > > whatsoever,but when I went into the detailed trade report and 
> went 
> > > over the output,I discovered  that the supposed scale out of 
> 50% 
> > > was not occuring.but rather 86%.After spending several hours 
> on 
> > > it,I thought that I may find some help 
> > > 
> > >    "I don't have any experience as a pilot but please give me 
> > > simple 
> > >    one-page instruction how to fly jumbo-jet from New York to 
> > > London" 
> > >    Sorry but this is NOT doable" 
> > > 
> > >    Tomasz,your analogy is way off base and I really dont 
> > > appreciate it. 
> > > 
> > > 
> > > 
> > >    Allan 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > > 
> > >    ----- Original Message ----- 
> > > 
> > > 
> > >    From: Tomasz Janeczko <groups@xxx> 
> > > 
> > >    Date: Saturday, July 15, 2006 8:49 am 
> > > 
> > >    Subject: Re: [amibroker] Re: I am lost 
> > > 
> > > 
> > >    > Hello, 
> > >    > 
> > >    > What you are asking for can be compared to: 
> > >    > . 
> > >    > It takes time and effort to learn how to fly as it takes 
> > > time and 
> > >    > effort to learn how to use any tool more sophisticated 
> than 
> > > a hammer. 
> > >    > 
> > >    > Sorry guys but of you have no experience you should start 
> > > with 
> > >    > something simple and make your system 
> > >    > simple not using pyramiding and such stuff.  Then go 
> slowly 
> > >    > learning AFL. THere is a plenty of material posted to 
> this 
> > > list, 
> > >    > in the knowledge base and in the manual. 
> > >    > 
> > >    > Best regards, 
> > >    > Tomasz Janeczko 
> > >    > amibroker.com 
> > >    >  ----- Original Message ----- 
> > >    >  From: allansn@xxx 
> > >    >  To: amibroker@xxxxxxxxxxxxxxx 
> > >    >  Sent: Saturday, July 15, 2006 2:04 PM 
> > >    >  Subject: Re: [amibroker] Re: I am lost 
> > >    > 
> > >    > 
> > >    > 
> > >    >  Thanks OT, 
> > >    > 
> > >    >  I will take you up on your offer.Ami is  great 
> program,but 
> > > is in 
> > >    > desparate need of a simplied" Dummy" manual for those 
> that 
> > > have 
> > >    > very little programming experience. 
> > >    > 
> > >    >  As I did not write this code,and was using it to learn  
> > > "AFL",I 
> > >    > am having a difficult time pinpointing what is causing an 
> > > 86% 
> > >    > scale out,when it should be 50%.. 
> > >    > 
> > >    >  If there are any power users who have a simple code that 
> > >    > involves "scaling in/out of positions"and replacing 
> stops,or 
> > > a 
> > >    > pyramyiding example would really appreciate seeing it. 
> > >    > 
> > >    >  Allan 
> > >    > 
> > >    > 
> > >    > 
> > >    > 
> > >    > 
> > >    >  ----- Original Message ----- 
> > >    > 
> > >    > 
> > >    >  From: orionsturtle <orionsturtle@xxx> 
> > >    > 
> > >    >  Date: Friday, July 14, 2006 10:20 pm 
> > >    > 
> > >    >  Subject: [amibroker] Re: I am lost 
> > >    > 
> > >    > 
> > >    >  > I don't have the expertise in programming but I have 
> been 
> > >    >  > struggling 
> > >    >  > with this same bit of code for the past 2 weeks. I 
> have 
> > > made 
> > >    > it 
> > >    >  > work 
> > >    >  > on the long only side and would be glad to send you 
> the 
> > > EOD 
> > >    > sys 
> > >    >  > file 
> > >    >  > I have that works. Between the detail log and the 
> trade 
> > > report 
> > >    > you 
> > >    >  > should be able to see what is going on and make 
> > > adjustments to 
> > >    >  > suite 
> > >    >  > your needs. The switches in the backtest settings will 
> > > screw 
> > >    > you 
> > >    >  > up 
> > >    >  > if not set properly and may account for your weird 
> > > results. 
> > >    > make 
> > >    >  > sure in the backtest settings>portfolio tab, that the 
> > > limit 
> > >    > trade 
> > >    >  > size as % of entry bar is set to zero and just 
> the "Trade 
> > > size 
> > >    >  > limit 
> > >    >  > when..." is the only thing checked. i have my max 
> > > positions 
> > >    > set to 
> > >    >  > one for now until I get the code fully to my liking. 
> If 
> > > you 
> > >    > want 
> > >    >  > the 
> > >    >  > SYS file reach me at orionsturtle@xxx 
> > >    >  > 
> > >    >  > peace 
> > >    >  > 
> > >    >  > OT 
> > >    >  > 
> > >    >  > --- In amibroker@xxxxxxxxxxxxxxx, "matrix10014" 
> > > <allansn@> 
> > >    > wrote: 
> > >    >  > > 
> > >    >  > > Hi, 
> > >    >  > > Been playing with a code that I copied to 
> familairize 
> > > myself 
> > >    >  > with 
> > >    >  > > some of the functionality of Amibroker...As you can 
> > > see,the 
> > >    >  > entry 
> > >    >  > is 
> > >    >  > > a simple moving average crossover,that exits 50% of 
> the 
> > >    > position 
> > >    >  > up 
> > >    >  > > 20% and the remaining 50% up 30%.There is also a 
> > > trailing 
> > >    > stop.. 
> > >    >  > > 
> > >    >  > > When i check a detailed log of the trade,I get very 
> > > bizzare 
> > >    >  > results. 
> > >    >  > > 
> > >    >  > > An example is HANS..On 10/31/2005 I go long 205 
> shares 
> > > of 
> > >    > HANS@ 
> > >    >  > 48.59 
> > >    >  > > 
> > >    >  > > On 11/3,I scale out of 176 shares@ is 86% of my 
> > >    >  > > position,when i am supposed to scale out of half.As 
> its 
> > > not 
> > >    > my 
> > >    >  > > code,and i am trying to learn Ami code,i really dont 
> > > know 
> > >    > what 
> > >    >  > went 
> > >    >  > > wrong.. 
> > >    >  > > 
> > >    >  > > 
> > >    >  > > 
> > >    >  > > 
> > >    >  > > 10/31/2005 
> > >    >  > >         Enter Long, HANS, Price: 48.59, Shares: 205, 
> > >    >  > > Commission: 0, Rank: 1, Equity 100000, Margin Loan: 
> 0, 
> > > Fx 
> > >    > rate: 1 
> > >    >  > > 
> > >    >  > > 
> > >    >  > >                 11/3/2005 
> > >    >  > >         Exit signals:HANS=Scale-Out, 
> > >    >  > > Scale-Out Long HANS, Price 59.74, Shares 176, Fx 
> Rate 
> > > 1, 
> > >    >  > > Number of shares - Current: 29, Exited: 176, Max: 
> 205, 
> > > Avg. 
> > >    >  > Entry 
> > >    >  > > Price 48.59, Avg. Exit Price 59.74, Avg Fx. Rate 
> Entry 
> > > 1, 
> > >    > Exit 
> > >    >  > 1, 
> > >    >  > > 
> > >    >  > > 
> > >    >  > > 
> > >    >  > > The code is as follows 
> > >    >  > >                         
> > >    >  > > Buy = Cross(C, MA( C,21) ); 
> > >    >  > > Sell = 0; 
> > >    >  > > 
> > >    >  > > // the system will exit 
> > >    >  > > // 50% of position if FIRST PROFIT TARGET stop is 
> hit 
> > >    >  > > // 50% of position is SECOND PROFIT TARGET stop is 
> hit 
> > >    >  > > // 100% of position if TRAILING STOP is hit 
> > >    >  > > 
> > >    >  > > FirstProfitTarget = 20; // profit 
> > >    >  > > SecondProfitTarget =30; // in percent 
> > >    >  > > TrailingStop = 50; // also in percent 
> > >    >  > > 
> > >    >  > > 
> > >    >  > > priceatbuy=0; 
> > >    >  > > highsincebuy = 0; 
> > >    >  > > 
> > >    >  > > exit = 0; 
> > >    >  > > 
> > >    >  > > for( i = 0; i < BarCount; i++ ) 
> > >    >  > > { 
> > >    >  > >    if( priceatbuy == 0 AND Buy[ i ] ) 
> > >    >  > >     { 
> > >    >  > >        priceatbuy = BuyPrice[ i ]; 
> > >    >  > >     } 
> > >    >  > > 
> > >    >  > >    if( priceatbuy > 0 ) 
> > >    >  > >     { 
> > >    >  > >        highsincebuy = Max( High[ i ], highsincebuy 
> ); 
> > >    >  > > 
> > >    >  > >       if( exit == 0 AND 
> > >    >  > >           High[ i ] >= ( 1 + FirstProfitTarget * 
> 0.01 ) 
> > > * 
> > >    >  > > priceatbuy ) 
> > >    >  > >        { 
> > >    >  > >          // first profit target hit - scale-out 
> > >    >  > >          exit = 1; 
> > >    >  > >          Buy[ i ] = sigScaleOut; 
> > >    >  > >        } 
> > >    >  > > 
> > >    >  > >       if( exit == 1 AND 
> > >    >  > >           High[ i ] >= ( 1 + SecondProfitTarget * 
> 0.01 
> > > ) * 
> > >    >  > > priceatbuy ) 
> > >    >  > >        { 
> > >    >  > >          // second profit target hit - exit 
> > >    >  > >          exit = 2; 
> > >    >  > >          SellPrice[ i ] = Max( Open[ i ], ( 1 + 
> > >    >  > SecondProfitTarget 
> > >    >  > * 
> > >    >  > > 0.01 ) * priceatbuy ); 
> > >    >  > >        } 
> > >    >  > > 
> > >    >  > >       if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * 
> > >    > highsincebuy 
> > >    >  > ) 
> > >    >  > >        { 
> > >    >  > >          // trailing stop hit - exit 
> > >    >  > >          exit = 3;    
> > >    >  > >          SellPrice[ i ] = Min( Open[ i ], ( 1 - 
> > > TrailingStop 
> > >    > * 
> > >    >  > > 0.01 ) * highsincebuy ); 
> > >    >  > >        } 
> > >    >  > > 
> > >    >  > >       if( exit >= 2 ) 
> > >    >  > >        { 
> > >    >  > >          Buy[ i ] = 0; 
> > >    >  > >          Sell[ i ] = exit + 1; // mark appropriate 
> exit 
> > > code 
> > >    >  > >          exit = 0; 
> > >    >  > >          priceatbuy = 0; // reset price 
> > >    >  > >          highsincebuy = 0; 
> > >    >  > >        } 
> > >    >  > >     } 
> > >    >  > > } 
> > >    >  > > 
> > >    >  > > SetPositionSize( 10, spsPercentOfEquity ); 
> > >    >  > > SetPositionSize( 50, spsPercentOfPosition * ( Buy == 
> > >    >  > > sigScaleOut ) ); // scale out 50% of position 
> > >    >  > > 
> > >    >  > 
> > >    >  > 
> > >    >  > 
> > >    >  > 
> > >    >  > 
> > >    >  >  
> > >    > 
> > > 
> > > 
> > 
> 
> 
> 
> 
> 
>