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

[amibroker] Re: Eliminating Phantom Positions in CBT



PureBytes Links

Trading Reference Links

I sent an email to support (ticket# 44280) on 11/7/07, explaining the 
problem that I am experiencing with the code that you kindly provided 
to solve the following problem:
1.	limit the number buys to 4(x) per day (bar)
2.	 Positions are sold if we have held them for at least 2(y) 
bars  from  the date of entry (not from the date of last buy signal), 
if we have sell signal.
3.	 Force sell positions after 15(z) days from the date of entry 
(not from the date of last buy signal).
The problem has to do with the sell signal. When minimum holding 
period is not met the sig.price=-1 remove all the sell signals, until 
a new buy signal is generated. Therefore the position is not sold 
correctly. Somehow we need EXREMSPAN type function from within CBT. 
Any suggestion or help you could provide.

Thanks

--- In amibroker@xxxxxxxxxxxxxxx, "tipequity" <l3456@xxx> wrote:
>
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <groups@> 
> wrote:
> >
> > Hello,
> > 
> > No, I have typed it directly into e-mail. Now I have run this for 
> you
> > (fixed some typos and added some _TRACE statements for Debug 
View):
> > 
> > ApplyStop( stopTypeNBar, stopModeBars, 15 ); // exit after 15 
days 
> > 
> > SetBacktestMode( backtestRegularRaw ); 
> > SetCustomBacktestProc(""); 
> > Buy = Close>0; 
> > Sell =  Close > Ref(Close, -1); 
> > 
> > SetPositionSize( 10, spsPercentOfEquity ); 
> > SetOption("MaxOpenPositions", 10 ); 
> > 
> > 
> > MaxBuys = 3; 
> > if( Status("action") == actionPortfolio ) 
> > { 
> >    bo = GetBacktesterObject(); 
> > 
> >    bo.PreProcess(); 
> >    for( i = 0; i < BarCount; i++ ) 
> >    { 
> >      cntBuys = 0; 
> > 
> >      // look at new signals AND Exclude signals if they exceed  
> maxBuys 
> >      for( sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal
> (i) ) 
> >      { 
> >        // this handles limiting of number of order per day 
> >        CanEnter = False; 
> >        if( sig.IsEntry() AND CntBuys <= MaxBuys ) 
> >        { 
> >             pos = bo.FindOpenPos( sig.Symbol ); 
> >             if( pos ) 
> >             { 
> >                 _TRACE("Pos already exists " + sig.Symbol ); 
> >             } 
> >             else 
> >             {   
> >                CanEnter = True; 
> >                CntBuys++; 
> >                _TRACE("Can Enter bar = " + i + " Symbol = " + 
> sig.Symbol ); 
> >             } 
> >        } 
> > 
> >        // this handles allowing exits only if position is at 
least 
> 2 bars old 
> >        CanExit = False; 
> >        if( sig.IsExit() ) 
> >        { 
> >            if( pos = bo.FindOpenPos( sig.Symbol ) ) 
> >            { 
> >                CanExit = pos.BarsInTrade > 2; 
> >               _TRACE("Can Exit bar = " + i + " Symbol = " + 
> sig.Symbol ); 
> >            } 
> >        } 
> > 
> >        // if can not enter/exit - mark price = -1 - means ignore 
> signal 
> >       if( ! CanEnter AND ! CanExit ) sig.Price = -1; 
> >     } 
> >     bo.ProcessTradeSignals( i ); 
> >   } 
> >   bo.PostProcess(); 
> > } 
> > 
> > Best regards,
> > Tomasz Janeczko
> > amibroker.com
> > ----- Original Message ----- 
> > From: "tipequity" <l3456@>
> > To: <amibroker@xxxxxxxxxxxxxxx>
> > Sent: Friday, October 19, 2007 4:54 AM
> > Subject: [amibroker] Re: Eliminating Phantom Positions in CBT
> > 
> > 
> > > Tomasz, did you try the code below. Beside couple of spelling 
> error 
> > > which I fixed, I get other error messages. When I fix them I 
> don't 
> > > get the intended results. The buy can sell conditions can be 
any 
> > > generic conditions such as 
> > > 
> > > Buy = Close>0;
> > > Sell =  Close > Ref(Close, -1);
> > > 
> > > Regards
> > > 
> > > Kam
> > > --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <groups@> 
> > > wrote:
> > >>
> > >> Seems like one closing brace was missing
> > >> 
> > >> ApplyStop( stopTypeNBar, stopModeBars, 15 ); // exit after 15 
> days 
> > >> 
> > >> SetBacktestMode( backtestRegularRaw ); 
> > >> SetCustomBacktestProc(""); 
> > >> 
> > >> MaxBuys = 3; 
> > >> if( Status("action") == actionPortfolio ) 
> > >> { 
> > >>    bo = GetBacktesterObject(); 
> > >> 
> > >>    bo.PreProcess(); 
> > >>    for( i = 0; i < BarCount; i++ ) 
> > >>    { 
> > >>      cntBuys = 0; 
> > >> 
> > >>      // look at new signals AND Exclude signals if they 
exceed  
> > > maxBuys 
> > >>        for( sig = bo.GetFirstSignal(i); sig; sig = 
> bo.GetNextSignal
> > > (i) ) 
> > >>      { 
> > >>        // this handles limiting of number of order per day 
> > >>        CanEnter = False; 
> > >>        if( sig.IsEntry() AND CntBus <= MaxBuys ) 
> > >>        { 
> > >>             if( ! bo.FindOpenPos( sig.Symbol ) ) 
> > >>             {   
> > >>                CanEnter = True; 
> > >>                CntBuys++; 
> > >>             } 
> > >>        } 
> > >> 
> > >>        // this handles allowing exits only if position is at 
> least 
> > > 2 bars old 
> > >>        CanExit = False; 
> > >>        if( sig.IsExit() ) 
> > >>        { 
> > >>            if( pos = FindOpenPos( sig.Symbol ) ) 
> > >>            { 
> > >>                CanExit = pos.BarsInTrade > 2; 
> > >>            } 
> > >>        } 
> > >> 
> > >>        // if can not enter/exit - mark price = -1 - means 
ignore 
> > > signal 
> > >>       if( ! CanEnter AND ! CanExit ) sig.Price = -1; 
> > >>     } 
> > >>     bo.ProcessTradeSignals( i ); 
> > >>   } 
> > >>   bo.PostProcess(); 
> > >> }
> > >> 
> > >> Best regards,
> > >> Tomasz Janeczko
> > >> amibroker.com
> > >> ----- Original Message ----- 
> > >> From: "tipequity" <l3456@>
> > >> To: <amibroker@xxxxxxxxxxxxxxx>
> > >> Sent: Friday, October 19, 2007 1:55 AM
> > >> Subject: [amibroker] Re: Eliminating Phantom Positions in CBT
> > >> 
> > >> 
> > >> > Thanks Tomasz for helping, I will give it try. More than 
that 
> > > thanks 
> > >> > for explaining the code with comments. Learning AB is 
> > > significantly 
> > >> > more import to me than solving the problem at hand.
> > >> > 
> > >> > --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" 
<groups@> 
> > >> > wrote:
> > >> >>
> > >> >> Hello,
> > >> >> 
> > >> >> The first step to success is to describe the goal, not the 
> step 
> > > you 
> > >> > are locked on.
> > >> >> This is I belive the very first time when you actually 
> > > described  
> > >> > the goal.
> > >> >> 
> > >> >> Once this is establish the rest is easy.
> > >> >> 
> > >> >> 
> > >> >> ApplyStop( stopTypeNBar, stopModeBars, 15 ); // exit after 
15 
> > > days 
> > >> >> 
> > >> >> SetBacktestMode( backtestRegularRaw ); 
> > >> >> SetCustomBacktestProc(""); 
> > >> >> 
> > >> >> Buy = ...
> > >> >> Sell = ...
> > >> >> 
> > >> >> MaxBuys = 3; 
> > >> >> if( Status("action") == actionPortfolio ) 
> > >> >> { 
> > >> >>    bo = GetBacktesterObject(); 
> > >> >> 
> > >> >>    bo.PreProcess(); 
> > >> >>    for( i = 0; i < BarCount; i++ ) 
> > >> >>    { 
> > >> >>      cntBuys = 0; 
> > >> >> 
> > >> >>   // look at new signals AND Exclude signals if they 
exceed  
> > >> > maxBuys 
> > >> >>   for( sig = bo.GetFirstSignal(i); sig; sig = 
bo.GetNextSignal
> > > (i) ) 
> > >> >>   { 
> > >> >>       // this handles limiting of number of order per day 
> > >> >>       CanEnter = False; 
> > >> >>       if( sig.IsEntry() AND CntBus <= MaxBuys ) 
> > >> >>       { 
> > >> >>             if( ! bo.FindOpenPos( sig.Symbol ) ) 
> > >> >>             {   
> > >> >>                CanEnter = True; 
> > >> >>                CntBuys++; 
> > >> >>             } 
> > >> >>       } 
> > >> >> 
> > >> >>       // this handles allowing exits only if position is at 
> > > least 2 
> > >> > bars old 
> > >> >>       CanExit = False; 
> > >> >>       if( sig.IsExit() ) 
> > >> >>       { 
> > >> >>            if( pos = FindOpenPos( sig.Symbol ) ) 
> > >> >>            { 
> > >> >>                CanExit = pos.BarsInTrade > 2; 
> > >> >>            } 
> > >> >>       } 
> > >> >> 
> > >> >>       // if can not enter/exit - mark price = -1 - means 
> ignore 
> > >> > signal 
> > >> >>       if( ! CanEnter AND ! CanExit ) sig.Price = -1; 
> > >> >>        
> > >> >>       bo.ProcessTradeSignals( i ); 
> > >> >>   } 
> > >> >>    
> > >> >>   bo.PostProcess(); 
> > >> >> }
> > >> >> 
> > >> >> Best regards,
> > >> >> Tomasz Janeczko
> > >> >> amibroker.com
> > >> >> ----- Original Message ----- 
> > >> >> From: "tipequity" <l3456@>
> > >> >> To: <amibroker@xxxxxxxxxxxxxxx>
> > >> >> Sent: Friday, October 19, 2007 12:26 AM
> > >> >> Subject: [amibroker] Re: Eliminating Phantom Positions in 
CBT
> > >> >> 
> > >> >> 
> > >> >> > Tomasz, GP
> > >> >> > 
> > >> >> > I have no desire to use low level. I am perfectly happy 
to 
> use 
> > >> > the 
> > >> >> > regular backtester. I am trying to port my trading system 
> to 
> > > AB. 
> > >> > What 
> > >> >> > I need to achieve is as follows which I could not do in 
> > > regular 
> > >> >> > backtester (assuming a long only EOD system). 
> > >> >> > 
> > >> >> > 1. limit the number buys to 4 per day(bar)
> > >> >> > 2. Positions are sold if we have held them for at least 2 
> bars 
> > >> > from 
> > >> >> > the date of entry (not from the date of last buy signal), 
> if 
> > > we 
> > >> > have 
> > >> >> > sell signal.
> > >> >> > 3. Force sell positions after 15 days from the date of 
> entry 
> > > (not 
> > >> >> > from the date of last buy signal).
> > >> >> > 
> > >> >> > Now I have done this very easily in AIQ & WealthLab. I 
can 
> > > drop 
> > >> > AB 
> > >> >> > and go back to those other software packages. AIQ does 
not 
> > > have 
> > >> > the 
> > >> >> > openness of AB and WL is too slow. I have been struggling 
> with 
> > >> > the 
> > >> >> > above for the last three month. I've asked the tech 
support 
> > > and 
> > >> >> > people on this board to no avail.
> > >> >> > 
> > >> >> > Below is code kindly provided by Edward Pottasch and 
> modified 
> > > by 
> > >> > me 
> > >> >> > to limit the number position to 4. However, then I face 
the 
> > >> > problem 
> > >> >> > that I have documented previously. I think I see why it 
> > > produces 
> > >> > the 
> > >> >> > problem. Because when the max buys per day limit is met 
it 
> > > sets 
> > >> > the 
> > >> >> > position size to zero and it does not nullify the buy 
> signal. 
> > > So 
> > >> > the 
> > >> >> > system keeps buy signal as a buy with zero value.
> > >> >> > 
> > >> >> > I sometimes I think Tomasz enjoys to see new users suffer
> (LOL, 
> > >> > TIC).
> > >> >> > 
> > >> >> > SetBacktestMode( backtestRegularRaw ); 
> > >> >> > SetCustomBacktestProc(""); 
> > >> >> > MaxBuys = 3;
> > >> >> > if( Status("action") == actionPortfolio )
> > >> >> > { 
> > >> >> >   bo = GetBacktesterObject(); 
> > >> >> >   bo.PreProcess(); 
> > >> >> >   for( i = 0; i < BarCount; i++ ) 
> > >> >> >  { 
> > >> >> > cntBuys = 0; 
> > >> >> > 
> > >> >> > // look at new signals AND Exclude signals if they exceed 
> > >> >> > maxBuys 
> > >> >> > for( sig = bo.GetFirstSignal(i); sig; sig = 
> > >> >> > bo.GetNextSignal(i) )
> > >> >> > { 
> > >> >> > OpenPos = bo.FindOpenPos( sig.Symbol );
> > >> >> > // check for entry signal and long signal 
> > >> >> >         if( sig.IsEntry() ) 
> > >> >> > { 
> > >> >> > if( cntBuys > MaxBuys )
> > >> >> > {   
> > >> >> >               sig.PosSize = 0; 
> > >> >> >           } 
> > >> >> >            else if( IsNull(OpenPos))
> > >> >> > { 
> > >> >> >                cntBuys = cntBuys + 1; 
> > >> >> > } 
> > >> >> >      } 
> > >> >> > 
> > >> >> >    } 
> > >> >> >    bo.ProcessTradeSignals( i ); 
> > >> >> >   } 
> > >> >> >   bo.PostProcess(); 
> > >> >> > }
> > >> >> > 
> > >> >> > --- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" 
> <groups@> 
> > >> >> > wrote:
> > >> >> >>
> > >> >> >> Exactly. In low-level mode AmiBroker does NOT enter/exit 
> ANY 
> > >> > trades
> > >> >> >> unless you tell it to do so by calling 
> EnterTrade/ExitTrade.
> > >> >> >> Entire signal processing is upto the user. 
> > >> >> >> 
> > >> >> >> Beginners should rather avoid using custom backtester 
esp. 
> > > low-
> > >> >> > level mode unless
> > >> >> >> they are programmers and know exactly what they are 
doing.
> > >> >> >> Even then it is advised to use _TRACE command to log all 
> your
> > >> >> >> conditions and function calls into DebugView window.
> > >> >> >> 
> > >> >> >> Custom backtester low-level is for *advanced* 
programmers 
> only
> > >> >> >> who have experience in debugging their own formulas.
> > >> >> >> 
> > >> >> >> Best regards,
> > >> >> >> Tomasz Janeczko
> > >> >> >> amibroker.com
> > >> >> >> ----- Original Message ----- 
> > >> >> >> From: "gp_sydney" <gp.investment@>
> > >> >> >> To: <amibroker@xxxxxxxxxxxxxxx>
> > >> >> >> Sent: Thursday, October 18, 2007 11:22 PM
> > >> >> >> Subject: [amibroker] Re: Eliminating Phantom Positions 
in 
> CBT
> > >> >> >> 
> > >> >> >> 
> > >> >> >> > If you're using the low-level CBT, then you're 
entering 
> and 
> > >> >> > exiting
> > >> >> >> > trades yourself, so I don't see how there can 
> be "phantom" 
> > >> > trades 
> > >> >> > that
> > >> >> >> > you don't know about.
> > >> >> >> > 
> > >> >> >> > Maybe the issue is with your Buy and Sell arrays. If 
you 
> > > get a 
> > >> > buy
> > >> >> >> > signal on one bar but don't take it, it won't still 
show 
> up 
> > > as 
> > >> > a
> > >> >> >> > signal on the next bar unless you specifically do 
> something 
> > > to 
> > >> >> > make it
> > >> >> >> > so. So if you're only using the Signal object to 
choose 
> > > buys, 
> > >> > any 
> > >> >> > you
> > >> >> >> > ignore at one bar won't still be there at the next bar 
> > > unless 
> > >> > the 
> > >> >> > Buy
> > >> >> >> > array happens to have True values at both bars.
> > >> >> >> > 
> > >> >> >> > Or are you perhaps removing redundant signals with 
ExRem?
> > >> >> >> > 
> > >> >> >> > Regards,
> > >> >> >> > GP
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> > --- In amibroker@xxxxxxxxxxxxxxx, "rdavenportca" 
> > >> > <davenport.r@> 
> > >> >> > wrote:
> > >> >> >> >>
> > >> >> >> >> I am doing portfolio backtesting on a system that has 
a 
> > >> > maximum 
> > >> >> > number 
> > >> >> >> >> of open positions set to 30.  I use the Custom 
> Backtester 
> > >> > Signal 
> > >> >> > Object 
> > >> >> >> >> (low level) to only take certain trades on a given 
day 
> > > (Day 
> > >> > 1).  
> > >> >> > On 
> > >> >> >> >> that day there may have been other valid trades that 
I 
> did 
> > >> > not 
> > >> >> > take or 
> > >> >> >> >> were not taken because I already had my 30 positions 
> full.
> > >> >> >> >> 
> > >> >> >> >> The problem I have is that a symbol that was a 
> potential 
> > >> > trade 
> > >> >> > on Day 1 
> > >> >> >> >> may setup to be a valid trade on Day 2, but I cannot 
> enter 
> > > it 
> > >> > on 
> > >> >> > Day 2 
> > >> >> >> >> because Amibroker "thinks" I'm in the trade.  On the 
> > > symbols 
> > >> > for 
> > >> >> > trades 
> > >> >> >> >> I did not take, I am blocked from taking any future 
> trade 
> > > in 
> > >> >> > that 
> > >> >> >> >> symbol until the exit has triggered.  Remember that 
I'm 
> > > not 
> > >> >> > actual in a 
> > >> >> >> >> position on this symbol, thus it is a "phantom" 
> position.
> > >> >> >> >> 
> > >> >> >> >> My guess is that there is a hanging exit order still 
in 
> > > the 
> > >> >> > system for 
> > >> >> >> >> these symbols causing the program to ignore new 
buys.  
> > > I've 
> > >> >> > tried 
> > >> >> >> >> setting the sig.Price = -1 and everything else I can 
> think 
> > > of 
> > >> > to 
> > >> >> > no 
> > >> >> >> >> avail.  Any ideas?
> > >> >> >> >>
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> > Please note that this group is for discussion between 
> users 
> > >> > only.
> > >> >> >> > 
> > >> >> >> > To get support from AmiBroker please send an e-mail 
> > > directly 
> > >> > to 
> > >> >> >> > SUPPORT {at} amibroker.com
> > >> >> >> > 
> > >> >> >> > For NEW RELEASE ANNOUNCEMENTS and other news always 
> check 
> > >> > DEVLOG:
> > >> >> >> > http://www.amibroker.com/devlog/
> > >> >> >> > 
> > >> >> >> > For other support material please check also:
> > >> >> >> > http://www.amibroker.com/support.html
> > >> >> >> > 
> > >> >> >> > Yahoo! Groups Links
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> > 
> > >> >> >> >
> > >> >> >>
> > >> >> > 
> > >> >> > 
> > >> >> > 
> > >> >> > 
> > >> >> > Please note that this group is for discussion between 
users 
> > > only.
> > >> >> > 
> > >> >> > To get support from AmiBroker please send an e-mail 
> directly 
> > > to 
> > >> >> > SUPPORT {at} amibroker.com
> > >> >> > 
> > >> >> > For NEW RELEASE ANNOUNCEMENTS and other news always check 
> > > DEVLOG:
> > >> >> > http://www.amibroker.com/devlog/
> > >> >> > 
> > >> >> > For other support material please check also:
> > >> >> > http://www.amibroker.com/support.html
> > >> >> > 
> > >> >> > Yahoo! Groups Links
> > >> >> > 
> > >> >> > 
> > >> >> > 
> > >> >> > 
> > >> >> >
> > >> >>
> > >> > 
> > >> > 
> > >> > 
> > >> > 
> > >> > Please note that this group is for discussion between users 
> only.
> > >> > 
> > >> > To get support from AmiBroker please send an e-mail directly 
> to 
> > >> > SUPPORT {at} amibroker.com
> > >> > 
> > >> > For NEW RELEASE ANNOUNCEMENTS and other news always check 
> DEVLOG:
> > >> > http://www.amibroker.com/devlog/
> > >> > 
> > >> > For other support material please check also:
> > >> > http://www.amibroker.com/support.html
> > >> > 
> > >> > Yahoo! Groups Links
> > >> > 
> > >> > 
> > >> > 
> > >> > 
> > >> >
> > >>
> > > 
> > > 
> > > 
> > > 
> > > Please note that this group is for discussion between users 
only.
> > > 
> > > To get support from AmiBroker please send an e-mail directly to 
> > > SUPPORT {at} amibroker.com
> > > 
> > > For NEW RELEASE ANNOUNCEMENTS and other news always check 
DEVLOG:
> > > http://www.amibroker.com/devlog/
> > > 
> > > For other support material please check also:
> > > http://www.amibroker.com/support.html
> > > 
> > > Yahoo! Groups Links
> > > 
> > > 
> > > 
> > > 
> > >
> >
>




Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

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

For other support material please check also:
http://www.amibroker.com/support.html
 
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/