Tomasz

I made the change that you suggested as follows. Subsequent to 
running the backtest I get an error message on the "if( 
openpos.IsOpen AND openpos.IsLong )". 

/*====================================================================
==========
        Global Settings
======================================================================
========*/
SetFormulaName("Testing a Creation of Trade Ledger"); /* name it for 
backtest report identification */
SetBarsRequired(1000000,0); /* this ensures that the charts include 
all bars AND NOT just those on screen */
SetOption("InitialEquity", 100000); /* starting capital */
SetOption("CommissionAmount",8); /* commissions AND cost */
SetOption("CommissionMode", 2); /* set commissions AND costs as $ per 
trade */
SetTradeDelays( 1, 1, 1, 1);
SetOption("PriceBoundChecking", 1); /* trade only within the chart 
bar's price range */
SetOption("UsePrevBarEquityForPosSizing", 1); /* set the use of last 
bars equity for trade size */
SetOption("AllowPositionShrinking", True);
SetOption("MinShares", 100);
SetOption("AccountMargin", 100); 
RoundLotSize = 1 ;
PositionSize = - 10; /* trade size will be 10% of available equity */

TradeDate = DateTime(); 

SetCustomBacktestProc(""); 

MaxBuys = 3; // Set no more than 4 buys per day

if( Status("action") == actionPortfolio )
{ 
        bo = GetBacktesterObject(); 
        bo.PreProcess(); 
   for( i = 0; i < BarCount; i++ ) 
        { 
                Symbol = " ";
                TransType = "";
                Rank = 0;
                Shares = 0;
                TransAmount = 0;
                Reason = 0;
                Price = 0;
                cntBuys = 0; 
                Balance = bo.Equity;
    // look at new signals and exclude signals if they exceed maxBuys 
                for( sig = bo.GetFirstSignal(i); sig; sig = 
bo.GetNextSignal(i) )
                { 
    // check for entry signal
                        if( sig.IsEntry() ) 
                        { 
                                if( cntBuys > MaxBuys )
                                {   
                                        sig.PosSize = 0; 
                                } 
                                else 
                                { 
                                        cntBuys = cntBuys + 1;
                                        Symbol = sig.Symbol;
                                        TransType = "Buy";
                                        Rank = sig.PosScore;
                                        Shares = round((bo.Equity / -
sig.PosSize)/sig.Price);
                                        Price = sig.Price;
                                        TransAmount = Shares * Price;
                                        Balance = Balance - 
TransAmount;                                    
                                        Reason = sig.Reason;
                                        

                                        bo.RawTextOutput(
                                                                
                                Symbol +
                                                                
                "\t" + TransType +
                                                                
                "\t" + DateTimeToStr(TradeDate[ i ]) +
                                                                
                "\t" + Price +
                                                                
                "\t" + Rank +
                                                                
                "\t" + "Shares " + Shares +
                                                                
                "\t" + "Amount " + TransAmount +
                                                                
                "\t" + "Balance " + Balance +
                                                                
                "\t" + "Reason " + Reason 
                                                                
                );
                                } 
                        } 
                        else if (sig.IsExit() AND sig.Type == 2 )
                        {
                                // scan through open positions
//                              for( openpos = bo.GetFirstOpenPos(); 
openpos; openpos = bo.GetNextOpenPos() ) 
//                              { 
                                openpos = bo.FindOpenPos( 
sig.Symbol );
                                // check for entry signal and long 
signal 
                                        if( openpos.IsOpen AND 
openpos.IsLong )
//                                      if(openpos.IsOpen AND 
openpos.IsLong AND openpos.Symbol == sig.Symbol)
                                        {
                                        Symbol = sig.Symbol;
                                        TransType = "Sell";
                                        Rank = sig.PosScore;
                                        Price = sig.Price;
                                        TransAmount = Shares * Price;
                                        Balance = Balance + 
TransAmount;                                    
                                        Reason = sig.Reason;

                                        bo.RawTextOutput(
                                                                
                                Symbol +
                                                                
                "\t" + TransType +
                                                                
                "\t" + DateTimeToStr(TradeDate[ i ]) +
                                                                
                "\t" + Price +
                                                                
                "\t" + Rank +
                                                                
                "\t" + "Shares " + Shares +
                                                                
                "\t" + "Amount " + TransAmount +
                                                                
                "\t" + "Balance " + Balance +
                                                                
                "\t" + "Reason " + Reason 
                                                                
                );
                                        }
//                              }
                        }
                } 
   bo.ProcessTradeSignals( i ); 
        } 
   bo.PostProcess(); 
}


//fast = Optimize("fast", 12, 5, 20, 1 ); 
//slow = Optimize("slow", 26, 10, 25, 1 ); 
Buy=Cross(MACD(12,26),Signal(12,26)); 
Sell=Cross(Signal(12,26),MACD(12,26));




--- In [email protected], "Tomasz Janeczko" <[EMAIL PROTECTED]> 
wrote:
>
> Hello,
> 
> Or.... use FindOpenPos without loop - which is faster/easier:
> 
> openpos = bo.FindOpenPos( sig.Symbol );
> 
> Best regards,
> Tomasz Janeczko
> amibroker.com
> ----- Original Message ----- 
> From: "gp_sydney" <[EMAIL PROTECTED]>
> To: <[email protected]>
> Sent: Thursday, September 06, 2007 10:39 AM
> Subject: [amibroker] Re: Limiting Sell to Open Position in CBI
> 
> 
> > The way you're using the open position list is not correct. You 
need
> > to match the open position symbol with the signal symbol to ensure
> > you're referencing the same open position stock as the sell signal
> > refers to. The way you have it now, as long as there is a single 
long
> > position in the open position list, all sell signals will be 
printed.
> > 
> > Try changing the open position conditional statement to:
> > 
> > if(openpos.IsOpen AND openpos.IsLong AND openpos.Symbol == 
sig.Symbol)
> > 
> > You could also then put a "break" at the end of the following 
code, as
> > once you have found a matching symbol you don't need to continue 
with
> > the for loop.
> > 
> > And, you shouldn't need to check "IsOpen" when traversing the open
> > position list. All trades in the open position list should be 
open.
> > 
> > Regards,
> > GP
> > 
> > 
> > --- In [email protected], "tipequity" <l3456@> wrote:
> >>
> >> Below is a code I am playing with. The final objective is to 
have 
> >> backtest report that keeps ledger of buys and sells similar to 
> >> quicken. The buy side of the trades seems to work fine. On the 
sell 
> >> side it is selling with no open position. How can I limit the 
sell to 
> >> closing open positins. Many thanks in advance for you help and 
> >> comments.
> >> 
> 
>> /*=================================================================
===
> >> ==========
> >> Global Settings
> >> 
======================================================================
> >> ========*/
> >> SetFormulaName("Testing a Creation of Trade Ledger"); /* name it 
for 
> >> backtest report identification */
> >> SetBarsRequired(1000000,0); /* this ensures that the charts 
include 
> >> all bars AND NOT just those on screen */
> >> SetOption("InitialEquity", 100000); /* starting capital */
> >> SetOption("CommissionAmount",8); /* commissions AND cost */
> >> SetOption("CommissionMode", 2); /* set commissions AND costs as 
$ per 
> >> trade */
> >> SetTradeDelays( 1, 1, 1, 1);
> >> SetOption("PriceBoundChecking", 1); /* trade only within the 
chart 
> >> bar's price range */
> >> SetOption("UsePrevBarEquityForPosSizing", 1); /* set the use of 
last 
> >> bars equity for trade size */
> >> SetOption("AllowPositionShrinking", True);
> >> SetOption("MinShares", 100);
> >> SetOption("AccountMargin", 100); 
> >> RoundLotSize = 1 ;
> >> PositionSize = - 10; /* trade size will be 10% of available 
equity */
> >> 
> >> TradeDate = DateTime(); 
> >> 
> >> SetCustomBacktestProc(""); 
> >> 
> >> MaxBuys = 3; // Set no more than 4 buys per day
> >> 
> >> if( Status("action") == actionPortfolio )
> >> { 
> >> bo = GetBacktesterObject(); 
> >> bo.PreProcess(); 
> >>    for( i = 0; i < BarCount; i++ ) 
> >> { 
> >> Symbol = " ";
> >> TransType = "";
> >> Rank = 0;
> >> Shares = 0;
> >> TransAmount = 0;
> >> Reason = 0;
> >> Price = 0;
> >> cntBuys = 0; 
> >> Balance = bo.Equity;
> >>     // look at new signals and exclude signals if they exceed 
maxBuys 
> >> for( sig = bo.GetFirstSignal(i); sig; sig = 
> >> bo.GetNextSignal(i) )
> >> { 
> >>     // check for entry signal
> >> if( sig.IsEntry() ) 
> >> { 
> >> if( cntBuys > MaxBuys )
> >> {   
> >> sig.PosSize = 0; 
> >> } 
> >> else 
> >> { 
> >> cntBuys = cntBuys + 1;
> >> Symbol = sig.Symbol;
> >> TransType = "Buy";
> >> Rank = sig.PosScore;
> >> Shares = round((bo.Equity / -
> >> sig.PosSize)/sig.Price);
> >> Price = sig.Price;
> >> TransAmount = Shares * Price;
> >> Balance = Balance - 
> >> TransAmount; 
> >> Reason = sig.Reason;
> >> 
> >> 
> >> bo.RawTextOutput(
> >> 
> >> Symbol +
> >> 
> >> "\t" + TransType +
> >> 
> >> "\t" + DateTimeToStr(TradeDate[ i ]) +
> >> 
> >> "\t" + Price +
> >> 
> >> "\t" + Rank +
> >> 
> >> "\t" + "Shares " + Shares +
> >> 
> >> "\t" + "Amount " + TransAmount +
> >> 
> >> "\t" + "Balance " + Balance +
> >> 
> >> "\t" + "Reason " + Reason 
> >> 
> >> );
> >> } 
> >> } 
> >> else if (sig.IsExit() AND sig.Type == 2 )
> >> {
> >> // scan through open positions
> >> for( openpos = bo.GetFirstOpenPos(); 
> >> openpos; openpos = bo.GetNextOpenPos() ) { 
> >> // check for entry signal and long 
> >> signal 
> >> if( openpos.IsOpen AND 
> >> openpos.IsLong )
> >> {
> >> Symbol = sig.Symbol;
> >> TransType = "Sell";
> >> Rank = sig.PosScore;
> >> Price = sig.Price;
> >> TransAmount = Shares * Price;
> >> Balance = Balance + 
> >> TransAmount; 
> >> Reason = sig.Reason;
> >> 
> >> bo.RawTextOutput(
> >> 
> >> Symbol +
> >> 
> >> "\t" + TransType +
> >> 
> >> "\t" + DateTimeToStr(TradeDate[ i ]) +
> >> 
> >> "\t" + Price +
> >> 
> >> "\t" + Rank +
> >> 
> >> "\t" + "Shares " + Shares +
> >> 
> >> "\t" + "Amount " + TransAmount +
> >> 
> >> "\t" + "Balance " + Balance +
> >> 
> >> "\t" + "Reason " + Reason 
> >> 
> >> );
> >> }
> >> }
> >> }
> >> } 
> >>    bo.ProcessTradeSignals( i ); 
> >> } 
> >>    bo.PostProcess(); 
> >> }
> >> 
> >> 
> >> //fast = Optimize("fast", 12, 5, 20, 1 ); 
> >> //slow = Optimize("slow", 26, 10, 25, 1 ); 
> >> Buy=Cross(MACD(12,26),Signal(12,26)); 
> >> Sell=Cross(Signal(12,26),MACD(12,26));
> >>
> > 
> > 
> > 
> > 
> > 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
> > 
> > 
> > 
> > 
> >
>


Reply via email to