Probably because OpenPos is returning Null when there's no open
position for that stock. Any time you call a function to get an
object, you need to test that the object returned is not Null.

Technically this should be done with a statement like:

if (!IsNull(OpenPos))

but the following also works due to the nature of Null:

if (OpenPos)

Regards,
GP


--- In [email protected], "tipequity" <[EMAIL PROTECTED]> wrote:
>
> 
> if (OpenPos.GetPercentProfit()>15)
> 
> --- In [email protected], "gp_sydney" <gp.investment@> 
> wrote:
> >
> > On which line? And what is the error message?
> > 
> > Regards,
> > GP
> > 
> > 
> > --- In [email protected], "tipequity" <l3456@> wrote:
> > >
> > > Thanks GP for pointing that out. I been fooling around with the 
> code 
> > > all weekend long and inadvertantly posted the wrong one. The 
> problem 
> > > is that it produces error on that line.
> > > 
> > > 
> > > --- In [email protected], "gp_sydney" <gp.investment@> 
> > > wrote:
> > > >
> > > > I've only had a quick glance through your code, but it looks 
> like
> > > > you're testing for the profit being greater than 15%, not the 
> loss.
> > > > 
> > > > I think the sell signal generated by ApplyStop should include 
> reason
> > > > code 1 for maximum loss. Try checking that.
> > > > 
> > > > Regards,
> > > > GP
> > > > 
> > > > 
> > > > --- In [email protected], "tipequity" <l3456@> wrote:
> > > > >
> > > > > How can test to see of max loss percent has been met (using 
> CBI)? 
> > > > > Below is my feeble attempt. 
> > > > > Thanks in advance
> > > > > 
> > > > 
> > > 
> > /*==================================================================
> > > ==
> > > > > ==========
> > > > >       Global Settings
> > > > > 
> > > 
> ======================================================================
> > > > > ========*/
> > > > > SetBarsRequired(1000000,0); /* this ensures that the charts 
> > > include 
> > > > > all bars AND NOT just those on screen */
> > > > > SetFormulaName("System Four with Stochastics-6"); /* name it 
> for 
> > > > > backtest report identification */
> > > > > 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("MinPosValue", ); */
> > > > > SetOption("AllowPositionShrinking", True);
> > > > > SetOption("MinShares", 100);
> > > > > SetOption("AccountMargin", 100); 
> > > > > SetOption("HoldMinBars",2 ); /* Set Minimun Holding Days to 2 
> > > Days*/
> > > > > RoundLotSize = 1 ;
> > > > > PositionSize = - 10; /* trade size will be 10% of available 
> > > equity */
> > > > > MaxLossPercentStop = 15 ;
> > > > > ApplyStop(0, 1, MaxLossPercentStop, 0, False, 0);
> > > > > 
> > > > > SetBacktestMode( backtestRegularRaw ); 
> > > > > 
> > > > 
> > > 
> > /*==================================================================
> > > ==
> > > > > ==========
> > > > >  
> > > > > 
> > > 
> ======================================================================
> > > > > ========*/
> > > > > 
> > > > > TradeDate = DateTime(); 
> > > > > 
> > > > > SetCustomBacktestProc(""); 
> > > > > 
> > > > > MaxBuys = 3; // Set no more than 4 buys per day
> > > > > 
> > > > > if( Status("action") == actionPortfolio )
> > > > > { 
> > > > >       bo = GetBacktesterObject(); 
> > > > >       bo.PreProcess();
> > > > >  
> > > > >               Symbol = " ";
> > > > >               TransType = "";
> > > > >               CommissionAmount = 8;
> > > > >               SEC_Fee = 0;
> > > > >               Rank = 0;
> > > > >               TradeSize = 0;
> > > > >               Shares = 0;
> > > > >               TransAmount = 0;
> > > > >               Reason = 0;
> > > > >               Price = 0;
> > > > >               CashBal = bo.InitialEquity;
> > > > >               EquityBal = bo.Equity;
> > > > >               ProfitPercent = 0;
> > > > > 
> > > > >    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) )
> > > > >               { 
> > > > >                       EquityBal = bo.Equity;
> > > > >                   // check for Sell signal
> > > > >                       if (sig.IsExit() AND sig.Type == 2 )
> > > > >                       {
> > > > >                               // scan through open positions
> > > > >                               OpenPos = bo.FindOpenPos( 
> > > > > sig.Symbol );
> > > > >                               // check for entry signal and 
> long 
> > > > > signal 
> > > > > 
> > > > >                               if (OpenPos.GetPercentProfit()
> >15)
> > > > >                               {
> > > > >                               Symbol = sig.Symbol;
> > > > >                               TransType = "Sold";
> > > > >                               Shares = OpenPos.Shares;
> > > > >                               Price = sig.Price;
> > > > >                               SEC_Fee = round
> ((OpenPos.Shares * 
> > > > > Price)*(1530/1000000))/100;
> > > > >                               CommissionAmount = 8 + 
> SEC_Fee;
> > > > >                               TransAmount = (OpenPos.Shares 
> * 
> > > > > Price)- CommissionAmount;
> > > > >                               CashBal = CashBal + 
> TransAmount;
> > > > >                                       
> > > > >                               ProfitPercent = 
> > > > > OpenPos.GetPercentProfit();
> > > > >                               Reason = "MaxLossPercentStop";
> > > > >                               Rank = "";
> > > > > 
> > > > >                               bo.RawTextOutput(
> > > > >                                                       
>       
> > > > >                       Symbol +
> > > > >                                                       
>       
> > > > >       "\t" + cntBuys +
> > > > >                                                       
>       
> > > > >       "\t" + TransType +
> > > > >                                                       
>       
> > > > >       "\t" + DateTimeToStr(TradeDate[ i ]) +
> > > > >                                                       
>       
> > > > >       "\t" + Price +
> > > > >                                                       
>       
> > > > >       "\t" + "Shares " + Shares +
> > > > >                                                       
>       
> > > > >       "\t" + "Commission " + CommissionAmount + 
> > > > >                                                       
>       
> > > > >       "\t" + "Amount " + TransAmount +
> > > > >                                                       
>       
> > > > >       "\t" + "PosSize " + TradeSize +
> > > > >                                                       
>       
> > > > >       "\t" + "CashBal " + CashBal +
> > > > >                                                       
>       
> > > > >       "\t" + "Cash " + bo.Cash +
> > > > >                                                       
>       
> > > > >       "\t" + "EquityBal " + EquityBal +
> > > > >                                                       
>       
> > > > >       "\t" + "Reason " + Reason +
> > > > >                                                       
>       
> > > > >       "\t" + "Profit% " + ProfitPercent +
> > > > >                                                       
>       
> > > > >       "\t" + Rank
> > > > >                                                       
>       
> > > > >       );
> > > > >                               }
> > > > >                               else if( openpos AND 
> openpos.IsLong )
> > > > >                               {
> > > > >                               Symbol = sig.Symbol;
> > > > >                               TransType = "Sold";
> > > > >                               Shares = OpenPos.Shares;
> > > > >                               Price = sig.Price;
> > > > >                               SEC_Fee = round
> ((OpenPos.Shares * 
> > > > > Price)*(1530/1000000))/100;
> > > > >                               CommissionAmount = 8 + 
> SEC_Fee;
> > > > >                               TransAmount = (OpenPos.Shares 
> * 
> > > > > Price)- CommissionAmount;
> > > > >                               CashBal = CashBal + 
> TransAmount;
> > > > >                                       
> > > > >                               Reason = sig.Reason;
> > > > >                               Rank = "";
> > > > > 
> > > > >                               bo.RawTextOutput(
> > > > >                                                       
>       
> > > > >                       Symbol +
> > > > >                                                       
>       
> > > > >       "\t" + cntBuys +
> > > > >                                                       
>       
> > > > >       "\t" + TransType +
> > > > >                                                       
>       
> > > > >       "\t" + DateTimeToStr(TradeDate[ i ]) +
> > > > >                                                       
>       
> > > > >       "\t" + Price +
> > > > >                                                       
>       
> > > > >       "\t" + "Shares " + Shares +
> > > > >                                                       
>       
> > > > >       "\t" + "Commission " + CommissionAmount + 
> > > > >                                                       
>       
> > > > >       "\t" + "Amount " + TransAmount +
> > > > >                                                       
>       
> > > > >       "\t" + "PosSize " + TradeSize +
> > > > >                                                       
>       
> > > > >       "\t" + "CashBal " + CashBal +
> > > > >                                                       
>       
> > > > >       "\t" + "Cash " + bo.Cash +
> > > > >                                                       
>       
> > > > >       "\t" + "EquityBal " + EquityBal +
> > > > >                                                       
>       
> > > > >       "\t" + "Reason " + Reason +
> > > > >                                                       
>       
> > > > >       "\t" + "Profit% " + ProfitPercent +
> > > > >                                                       
>       
> > > > >       "\t" + Rank
> > > > >                                                       
>       
> > > > >       );
> > > > >                               }
> > > > >                       }
> > > > >                   // check for entry signal
> > > > >               }
> > > > >               bContinue = True;
> > > > >               for( sig = bo.GetFirstSignal(i); sig; sig = 
> > > > > bo.GetNextSignal(i) )
> > > > >               { 
> > > > >                       if( sig.IsEntry() ) 
> > > > >                       { 
> > > > >                               Symbol = sig.Symbol;
> > > > >                               TradeSize = (bo.Equity / -
> > > > > sig.PosSize);
> > > > >                               Shares = round
> (TradeSize/sig.Price);
> > > > >                               Price = sig.Price;
> > > > >                               if( cntBuys > MaxBuys  )
> > > > >                               {   
> > > > >                                       sig.PosSize = 0; 
> > > > >                                       TransType 
> = "Rejected";
> > > > >                                       Reason = "Exceed Max 
> No. 
> > > > > Trades per Day";
> > > > >                                       bo.RawTextOutput(
> > > > >                                                       
>       
> > > > >                               Symbol +
> > > > >                                                       
>       
> > > > >               "\t" + cntBuys +
> > > > >                                                       
>       
> > > > >               "\t" + TransType +
> > > > >                                                       
>       
> > > > >               "\t" + DateTimeToStr(TradeDate[ i ]) +
> > > > >                                                       
>       
> > > > >               "\t" + Price +
> > > > >                                                       
>       
> > > > >               "\t" + "Shares " + Shares +
> > > > >                                                       
>       
> > > > >               "\t" + "Commission " + CommissionAmount +
> > > > >                                                       
>       
> > > > >               "\t" + "Amount " + TransAmount +
> > > > >                                                       
>       
> > > > >               "\t" + "PosSize " + TradeSize +
> > > > >                                                       
>       
> > > > >               "\t" + "CashBal " + CashBal +
> > > > >                                                       
>       
> > > > >               "\t" + "Cash " + bo.Cash +
> > > > >                                                       
>       
> > > > >               "\t" + "EquityBal " + EquityBal +
> > > > >                                                       
>       
> > > > >               "\t" + "Reason " + Reason  +
> > > > >                                                       
>       
> > > > >               "\t" + "Profit% " +
> > > > >                                                       
>       
> > > > >               "\t" + Rank
> > > > >                                                       
>       
> > > > >               );
> > > > >                               } 
> > > > >                               else 
> > > > >                               { 
> > > > >                                       TransType = "Bought";
> > > > >                                       cntBuys = cntBuys + 1;
> > > > >                                       TransAmount = -
> (Shares * 
> > > > > Price)-CommissionAmount;
> > > > >                                       CashBal = CashBal + 
> > > > > TransAmount;
> > > > >                                       Reason = sig.Reason;
> > > > >                                       Rank = sig.PosScore;
> > > > > 
> > > > >                                       bo.RawTextOutput(
> > > > >                                                       
>       
> > > > >                               Symbol +
> > > > >                                                       
>       
> > > > >               "\t" + cntBuys +
> > > > >                                                       
>       
> > > > >               "\t" + TransType +
> > > > >                                                       
>       
> > > > >               "\t" + DateTimeToStr(TradeDate[ i ]) +
> > > > >                                                       
>       
> > > > >               "\t" + Price +
> > > > >                                                       
>       
> > > > >               "\t" + "Shares " + Shares +
> > > > >                                                       
>       
> > > > >               "\t" + "Commission " + CommissionAmount +
> > > > >                                                       
>       
> > > > >               "\t" + "Amount " + TransAmount +
> > > > >                                                       
>       
> > > > >               "\t" + "PosSize " + TradeSize +
> > > > >                                                       
>       
> > > > >               "\t" + "CashBal " + CashBal +
> > > > >                                                       
>       
> > > > >               "\t" + "Cash " + bo.Cash +
> > > > >                                                       
>       
> > > > >               "\t" + "EquityBal " + EquityBal +
> > > > >                                                       
>       
> > > > >               "\t" + "Reason " + Reason  +
> > > > >                                                       
>       
> > > > >               "\t" + "Profit% " +
> > > > >                                                       
>       
> > > > >               "\t" + Rank
> > > > >                                                       
>       
> > > > >               );
> > > > >                               } 
> > > > >                       }
> > > > >               } 
> > > > >    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));
> > > > >
> > > >
> > >
> >
>


Reply via email to