Thank you Tomasz, Nobody doubts the flexibility of the CBT, and you show another example of its power.
I should have been clearer, so allow me to follow up with a question: can this be done also for ScaleTrades? This relates to specifically my application: I'm mostly using rebalancing trades, i.e. by applying your "bo.ScaleTrade"-code in the Knowledge Centre: http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/ How would this work here, since I'm always in the "Open Positions" loop "only" to correct my holdings/weights, i.e. how can I first execute all the ScaleOuts (generate cash), and then divide the cash over the ScaleIns? In a general sense, are there ways to control the cash flows in a portfolio (preferably at the individual security level) when one applies ScaleTrades to rebalance weights? Sorry for taking the original query into this direction. Thx, PS --- In [email protected], "Tomasz Janeczko" <[EMAIL PROTECTED]> wrote: > > Hello, > > Of course you can. > You can do *everything* you need with CBT. I can not stress it more. > > The order in which trades are executed is the order in which you call EnterTrade/ExitTrade functions. > So if you call ExitTrade for TradeA before EnterTrade for TradeB > it will be excuted EXACTLY in this order (first TradeA will be exited and cash returned > to the account and then TradeB will be entered). > > So it is very easy and straightforward. Just call EnterTrade/ExitTrade as you would normally place orders > and they are executed exactly in that order. > > You are NOT limited to perform operations in signal order (signals are sorted by position score > entry signals first, but you are NOT limited to execute them in that order). > > Actually this is what default built-in backtester routine does. > It performs several loops. > First loop iterates through signals to see if there are any pending EXITS matching open positions > and EXITS them. Then It goes through signal list AGAIN and checks entry signals. > > So to answer your question you need to write 2 loops ! > > for( sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i) ) > { > // THIS LOOP PROCESSES EXIT SIGNALS > OpenPos = bo.FindOpenPos( sig.Symbol ); > if( Sig.IsExit() AND OpenPos ) > { > bo.ExitTrade( i, OpenPos.symbol, sig.Price, 1); > } > } > > for( sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i) ) > { > // THIS LOOP PROCESSES ENTRY SIGNALS > if( sig.IsEntry() ) > { > bo.EnterTrade( i, sig.Symbol, True, sig.Price, sig.PosSize, sig.PosScore, RoundLotSize = 1); > > ...... > } > } > //// > Best regards, > Tomasz Janeczko > amibroker.com > ----- Original Message ----- > From: "vlanschot" <[EMAIL PROTECTED]> > To: <[email protected]> > Sent: Monday, October 08, 2007 9:50 AM > Subject: [amibroker] Re: Low Level CBT Executing Sales before Buys > > > > TJ may possibly correct me on this (if so, than hopefully with > > example code), but as far as I know you cannot (if that is your > > intention) prioritize the sales/shorts to be executed first(in a > > seperate loop as it were, and thereby generate cash) before you > > execute the buys/longs. > > > > PS > > --- In [email protected], "tipequity" <l3456@> wrote: > >> > >> Can someone point out a way that the code below would execute sales > >> before buys. TIA Cam > >> > >> SetBacktestMode( backtestRegularRaw ); > >> SetCustomBacktestProc(""); > >> MaxBuys = 3; > >> if( Status("action") == actionPortfolio ) > >> { > >> bo = GetBacktesterObject(); > >> bo.PreProcess(); > >> MaxHoldingPer = 14; > >> TradeDate = DateTime(); > >> for( i = 0; i < BarCount; i++ ) > >> { > >> cntBuys = 0; > >> for( OP = bo.GetFirstOpenPos(); OP; OP = > >> bo.GetNextOpenPos() ) > >> { > >> if ( OP.BarsInTrade >= MaxHoldingPer) > >> bo.ExitTrade( i, OP.Symbol, > >> OP.GetPrice(i, "C"), 1); > >> } > >> for( sig = bo.GetFirstSignal(i); sig; sig = > >> bo.GetNextSignal(i) ) > >> { > >> OpenPos = bo.FindOpenPos( sig.Symbol ); > >> > >> if( Sig.IsExit() AND OpenPos ) > >> { > >> bo.ExitTrade( i, OpenPos.symbol, > >> sig.Price, 1); > >> bo.RawTextOutput( > >> DateTimeToStr(TradeDate[ i ])+ " Sell "+sig.symbol()+" > >> Cash "+bo.cash); > >> } > >> // look at new signals AND Exclude signals if they > >> exceed maxBuys > >> if( sig.IsEntry() ) > >> { > >> bo.RawTextOutput( > >> DateTimeToStr(TradeDate[ i ])+ " BuySig "+sig.symbol()+" > >> Cash "+bo.cash); > >> if( cntBuys > MaxBuys ) > >> { > >> bo.RawTextOutput( > >> DateTimeToStr(TradeDate[ i ])+ " Rejected BuySig "+sig.symbol > >> ()); > >> > >> } > >> else if( IsNull(OpenPos)) > >> { > >> bo.RawTextOutput( > >> DateTimeToStr(TradeDate[ i ])+ " Buy "+sig.symbol()+" > >> Cash "+bo.cash); > >> cntBuys = cntBuys + 1; > >> bo.EnterTrade( i, sig.Symbol, > >> True, sig.Price, sig.PosSize, sig.PosScore, RoundLotSize = 1); > >> bo.RawTextOutput( > >> DateTimeToStr(TradeDate[ i ])+ " Buy2 "+sig.symbol()+" > >> Cash "+bo.cash); > >> } > >> } > >> } > >> bo.HandleStops(i); > >> bo.UpdateStats(i,1); > >> bo.UpdateStats(i,2); > >> } > >> bo.PostProcess(); > >> } > >> > > > > > > > > > > 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 > > > > > > > > > > >
