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 [email protected], "Tomasz Janeczko" <[EMAIL PROTECTED]> 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" <[EMAIL PROTECTED]> > To: <[email protected]> > 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 [email protected], "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: <[email protected]> > >> 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 [email protected], "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: <[email protected]> > >> >> 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 [email protected], "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 > > > > > > > > > > >
