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

Re: [amibroker] Re: Low Level CBT Executing Sales before Buys



PureBytes Links

Trading Reference Links

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" <vlanschot@xxxxxxxxx>
To: <amibroker@xxxxxxxxxxxxxxx>
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 amibroker@xxxxxxxxxxxxxxx, "tipequity" <l3456@xxx> 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
> 
> 
> 
> 
>


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/