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

[amibroker] Test to see maxlosspercent has been met CBI



PureBytes Links

Trading Reference Links

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));



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/