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

[amibroker] Re: MaxRisk (was Optimizing Max Open Positions)



PureBytes Links

Trading Reference Links

More code:

MS1=100;MS2=300;
//MinShares = Optimize("MinShares",100,MS1,MS2,100);
MinShares = Param("MinShares",100,MS1,MS2,100);
SetOption("MinShares", MinShares);
//MarginDeposit = 100;
eq = Foreign("~~~EQUITY", "C");
cash = Foreign("~~~EQUITY", "L");
//dr = eq - Highest(eq);
e_max = Highest(eq); // peak equity so far
MDD = Highest(e_max - eq); //max drawdown so far
LB1=5;LB2=16;
MaxOpenPos= LB = Optimize("MaxOpenPos",6,LB1,LB2,1);
//MaxOpenPos= LB = Param("MaxOpenPos",6,LB1,LB2,1);
SetOption("MaxOpenPositions", MaxOpenPos );
//X = Optimize("X",100,50,100,10);
X = Param("X",100,50,100,10);
PositionSize = -X/MaxOpenPos;
MaxRisk = PositionSize * (-MDD)/eq;

rgds, Pal
--- In amibroker@xxxxxxxxxxxxxxx, "palsanand" <palsanand@xxxx> wrote:
> Came out with the following solution:
> 
> equity_lookback_bars = 0;
> equity_lookback_smoothing = 30;
> equity_drawdown_discount_pct = 90;  //not sure what this should be
> function PerformanceScore() { // returns score for current trade 
> signals;
> //Highest score is used
> e = Equity(0, 0);
> if(equity_lookback_bars == 0) { // test all performance to date
> e_ref = e[0];
> } else { // test performance back spec'd number of bars
> e_ref = Ref(e, -equity_lookback_bars);
> }
> perf_score = (e - e_ref) / e_ref; // growth over lookback period
> perf_score = MA(perf_score, equity_lookback_smoothing) * 100; // 
> smoothed pct
> perf_score = perf_score * 100 / IIf(equity_lookback_bars == 0, Cum
(1),
> equity_lookback_bars); // per 100 bars
> e_max = Highest(e); // peak equity so far
> mdd = Highest(e_max - e); // max drawdown so far
> mdd_fraction = Highest(mdd / e_max); // fraction max drawdown is of 
> peak equity
> perf_score = perf_score - (perf_score * mdd_fraction *
> (equity_drawdown_discount_pct/100)); // reduce score by mdd 
fraction 
> scaled
> //by drawdown discount
> return perf_score;
> }
> PositionScore = PerformanceScore();
> 
> Any Feedback appreciated.
> 
> rgds, Pal
> --- In amibroker@xxxxxxxxxxxxxxx, "palsanand" <palsanand@xxxx> 
wrote:
> > Previously I forgot that I read the following in the ReadMe file 
of 
> > the latest beta of AB:
> > 
> > in regular backtest mode now it is possible to specify the score 
of 
> > the symbol (on bar-by-bar basis) via PositionScore variable. In 
> this 
> > mode the score is used only at trade ENTRY to decide which 
> securities 
> > should be traded in case when there are more simultaneous entry 
> > signals than max. allowable positions or available funds. 
AmiBroker 
> > will 'prefer' securities with higher absolute value of the score. 
> If 
> > PositionScore is not used then it is assumed to be 1 for all 
> > securities.
> > 
> > You can use new PositionScore variable to decide which trades 
> should 
> > be entered if there are more entry signals on different 
securities 
> > than maximum allowable number of open positions or available 
funds. 
> > In such case AmiBroker will use the absolute value of 
PositionScore 
> > variable to decide which trades are preferred. See the code 
below. 
> It 
> > implements simple MA crossover system, but with additional 
flavour 
> of 
> > preferring entering trades on symbols that have low RSI value. If 
> > more buy signals occur than available cash/max. positions then 
the 
> > stock with lower RSI will be preferred. You can watch selection 
> > process if you backtest with "Detailed log" report mode turned on.
> > 
> > // now additional score 
> > // that is used to rank equities 
> > // when there are more ENTRY signals that available
> > // positions/cash
> > PositionScore = 100-RSI(); // prefer stocks that have low RSI;
> > 
> > This would now solve the problem of issue selection.  Now I have 
7 
> > Max open positions, but naturally some "underlying instrument" 
> issues 
> > are different than before I used PositionScore, depending on the 
> > PositionScore, which I hope AB uses to select low RSI for Long 
and 
> > high RSI for Short positions which is the rule I use for 
> reversals.  
> > If not then I would not use PositionScore and let it to default 
to 
> > the value of 1.  RSI doesn't matter for some trades like 
> continuation 
> > signals.    How would I handle these 2 trading situations?  Could 
> > somebody clarify what AB does here with this PositionScore?
> > 
> > rgds, Pal
> > --- In amibroker@xxxxxxxxxxxxxxx, "palsanand" <palsanand@xxxx> 
> wrote:
> > > I modified the AA code as follows:
> > > 
> > > in_trade_long = Flip( Buy, Sell );
> > > in_trade_short = Flip( Short, Cover);
> > > 
> > > AddToComposite( in_trade_long, "~OpenLongPosCount", "V" );
> > > AddToComposite( in_trade_short, "~OpenShortPosCount", "V" );
> > > 
> > > /* We use "~OpenPosLongCount" and "~OpenPosShortCount" 
artificial 
> > > ticker to store the results. Again we should run just Scan of 
the 
> > > formula AND these tickers would become available. 
> > > 
> > > Use */ 
> > > 
> > > Graph1 = Foreign( "~OpenLongPosCount", "V"); 
> > > Graph2 = Foreign( "~OpenShortPosCount", "V"); 
> > > 
> > > /* in Indicator Builder after running the back-test to see the 
> > chart 
> > > of the number of Open long and short positions of your system. 
*/
> > > 
> > > Modified Indicator code as follows:
> > > 
> > > Graph1 = Foreign( "~OpenLongPosCount", "V"); 
> > > Plot(Graph1,"OpenLongPosCount",1,style=1,0,20);
> > > Graph2 = Foreign( "~OpenShortPosCount", "V"); 
> > > Plot(Graph2,"OpenShortPosCount",2,style=1,0,20);
> > > Plot(7,"My line",colorRed);
> > > 
> > > Now, I show both open long (12) and short positions (5), but 
> since 
> > > the MaxOpenPos has been optimized/set to 7, the trades list has 
> > only 
> > > 7 open positions ( 5 long and 2 short).  I have no idea yet 
what 
> > the 
> > > issue selection criteria is for long and short trades, though.  
> Can 
> > > anybody take a guess?
> > > 
> > > rgds, Pal
> > > --- In amibroker@xxxxxxxxxxxxxxx, "palsanand" <palsanand@xxxx> 
> > wrote:
> > > > I came out with the following results adding more code:
> > > > 
> > > > eq = Foreign("~~~EQUITY", "C");
> > > > cash = Foreign("~~~EQUITY", "L");
> > > > dr = eq - Highest(eq);
> > > > MR1 = 0.1;  
> > > > MR2 = 0.5;
> > > > MaxRisk = Optimize("MaxRisk",0.10,MR1,MR2,0.05);
> > > > Pd1=3;Pd2=12;LB1=1;LB2=17;
> > > > MaxOpenPos= LB = Optimize("MaxOpenPos",7,LB1,LB2,1);
> > > > //MaxOpenPos= LB = Param("MaxOpenPos",7,LB1,LB2,1);
> > > > GPS = (MaxRisk * eq)/dr;
> > > > PositionSize = GPS = -100/MaxOpenPos;
> > > > MaxOpenPos = 
> > > > SetOption("MaxOpenPositions", MaxOpenPos );
> > > > Period = Pd = Param("Period", 3, Pd1, Pd2, 1 );
> > > > //Period = Pd = Optimize("Period", 3, Pd1, Pd2, 1 );
> > > > OptStart = (Pd = 3) AND (LB = 1) AND (MR1 = 0.1);
> > > > 
> > > > It was interesting to find that there were a range of values 
> for 
> > > > MaxRisk and the same MaxOpenPos for the highest CAR/MDD and 
> UPI, 
> > > but 
> > > > all the values in the optimization were the same for this 
> range.  
> > > > Since, I want to minimize the Max% of the closed equity to 
> > > > risk/trade, I guess I need to use the minimum in this range, 
> > which 
> > > is 
> > > > 0.10 (10% Max risked/trade).  
> > > > 
> > > > Also, concidentally or optimally, the MaxOpenPos came out to 
be 
> > 7, 
> > > > which is also the same value I originally chose as part of 
the 
> > > trader-
> > > > defined criteria.  Still the discrepancy with the MaxOpenPos 
> and 
> > > the 
> > > > Composite ~OpenPosCount through which I got 12 positions open 
> > now, 
> > > > (where I plot it in a indicator window), still exists.
> > > > 
> > > > rgds, Pal
> > > > 
> > > > 
> > > > 
> > > > 
> > > > --- In amibroker@xxxxxxxxxxxxxxx, "palsanand" 
<palsanand@xxxx> 
> > > wrote:
> > > > > Thanks.  I optimized the Max Open Positions to 8.
> > > > > 
> > > > > Maxpos=Optimize("maxpos",8,1,20,1);
> > > > > PositionSize = -100/Maxpos;
> > > > > SetOption("MaxOpenPositions", Maxpos );
> > > > > 
> > > > > I am getting 8 positions open in the Results window:
> > > > > 
> > > > > 12/4/03						
	
> > > > 	
> > > > > 			
> > > > > 	Entry signals(score):GU-9967=short(-1)	 UC-9967=buy
> (1)	 
> > > > > 							
> 	
> > > > > 	Exit signals:GU-9967=sell	 UC-9967=cover	 
> 	
> > > > > 							
> > > > > 	Enter Short	 GU-9967	 Price: 1.7278	 
> Shares: 250
> > > > > 	 Commission: 0	 Rank: -1	 Equity 3712.71	
> 	
> > > > > 		
> > > > > 	Enter Long	 UC-9967	 Price: 1.3007	 
> Shares: 350
> > > > > 	 Commission: 0	 Rank: 1	 Equity 3712.71	
> 	
> > > > > 		
> > > > > 	8 Open Positions:$C-9967	 $E-9967	 AU-
> 9967
> > > > > 	 AC-9967	 E$-9967	 EA-9967	 GU-
> 9967
> > > > > 	 UC-9967	 Equity: 3718.23	 Cash: 317.445
> 	
> > > > > 
> > > > > But I find a discrepancy with the Open position count using 
> the 
> > > > > following code, where I got 11 positions open, when I plot 
it 
> > in 
> > > a 
> > > > > indicator window using 
> > > > > 
> > > > > Graph1 = Foreign( "~OpenPosCount", "V"); 
> > > > > Plot(Graph1,"OpenPosCount",1,style=1,0,100);
> > > > > 
> > > > > The following is in Automatic Analysis:
> > > > > 
> > > > > /* the following line uses Flip function to get "1" after 
the 
> > buy 
> > > > > signal and reset it back to "0" after sell appears. */
> > > > > 
> > > > > in_trade = Flip( Buy, Sell );
> > > > > 
> > > > > AddToComposite( in_trade, "~OpenPosCount", "V" );
> > > > > 
> > > > > /* We use "~OpenPosCount" artificial ticker to store the 
> > results. 
> > > > > Again we should run just Scan of the formula AND 
> > > > the "~OpenPosCount" 
> > > > > ticker would become available. 
> > > > > 
> > > > > Use */ 
> > > > > 
> > > > > Graph0 = Foreign( "~OpenPosCount", "V"); 
> > > > > 
> > > > > /* in Indicator Builder after running the back-test to see 
> the 
> > > > chart 
> > > > > of the number of Open positions of your system. */
> > > > > 
> > > > > Can somebody tell me why this discrepancy occurs.  TIA.
> > > > > 
> > > > > rgds, Pal
> > > > > 
> > > > > 
> > > > > 
> > > > > --- In amibroker@xxxxxxxxxxxxxxx, "bvandyke" 
<bvandyke@xxxx> 
> > > wrote:
> > > > > > Pal,
> > > > > > 
> > > > > > Try the following example and modify the # of positions 
as 
> > > needed:
> > > > > > 
> > > > > > Maxpos=Optimize("maxpos",2,1,4,1);
> > > > > > PositionSize = -100/Maxpos;
> > > > > > SetOption("MaxOpenPositions", Maxpos );
> > > > > > 
> > > > > > Bill
> > > > > > 
> > > > > > --- In amibroker@xxxxxxxxxxxxxxx, "palsanand" 
> > <palsanand@xxxx> 
> > > > > wrote:
> > > > > > > Hi All,
> > > > > > > 
> > > > > > > Does anybody has the code for Optimizing Max Open 
> > Positions.  
> > > I 
> > > > > > > remember seeing it somewhere in AFL guides.  I can't 
seem 
> > to 
> > > > find 
> > > > > > it 
> > > > > > > now or remember how to do it.
> > > > > > > 
> > > > > > > TIA
> > > > > > > 
> > > > > > > rgds, Pal


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/