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

[amibroker] Re: Setup for Forex backtesting



PureBytes Links

Trading Reference Links

/* in Indicator Builder after running the back-test to see the chart 
 of the number of Open long and short positions of your system. Use 
the following code: */

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(5,"MaxOpenPos",colorYellow);

rgds Pal

 
--- In amibroker@xxxxxxxxxxxxxxx, "Pal Anand" <palsanand@xxxx> wrote:
> You can run the backtest to find the number of open positions.  I 
am 
> not aware of a direct function but in AFL, I have some code you 
might 
> consider using:
> 
> /* the following line uses Flip function to get "1" after the buy 
> signal and reset it back to "0" after sell appears. */
> 
> 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. */
> 
> e = Equity(1);
> 
> tradeend1 = Sell;
> tradeend2 = Cover;
> 
> profit1 = e - ValueWhen( Buy, e );
> profit2 = e - ValueWhen( Short, e);
> 
> endprofit1 = IIf( tradeend1 , profit1, 0 );
> endprofit2 = IIf( tradeend2 , profit2, 0 );
> 
> LosingLongTrades = LastValue( Cum( endprofit1 < 0 ) );
> LosingShortTrades = LastValue( Cum( endprofit2 < 0 ) );
> WiningLongTrades = LastValue( Cum( endprofit1 > 0 ) );
> WiningShortTrades = LastValue( Cum( endprofit2 > 0 ) );
> TotalLongTrades = LastValue( Cum( tradeend1 ) );
> TotalShortTrades = LastValue( Cum( tradeend2 ) );
> 
> 
> AddColumn( LosingLongTrades, "Losing Long trades", 1 );
> AddColumn( WiningLongTrades, "Wining Long trades", 1 );
> AddColumn( TotalLongTrades, "Total Long trades", 1 );
> AddColumn( LosingShortTrades, "Losing Short trades", 1 );
> AddColumn( WiningShortTrades, "Wining Short trades", 1 );
> AddColumn( TotalShortTrades, "Total Short trades", 1 );
> 
> 
> Filter = Status("lastbarinrange");
> 
> // now we will count the number of 
> // count trades winning in given $ ranges
> NumRanges = 25; // number of ranges to generate
> RangeSize = 100; // controls $ size of the range
> NumColumns = 6;
> 
> for( i = 0; i < NumRanges; i++ )
> {
>   rangestart = i * RangeSize;
>   rangeend = (i+1)*RangeSize;
>   if( i == NumRanges - 1 ) rangeend = 999999;
>   AddColumn( 
>   LastValue( 100* Cum( endprofit1 > rangestart AND endprofit1 < 
> rangeend ) /TotalLongTrades ),
>   "% with profit " + rangestart + ".." + rangeend );
> AddColumn( 
>   LastValue( 100* Cum( endprofit2 > rangestart AND endprofit2 < 
> rangeend ) /TotalShortTrades ),
>   "% with profit " + rangestart + ".." + rangeend );
> 
> }
> 
> for( i = 0; i < NumRanges; i++ )
> {
>   rangestart = (-i - 1 ) * RangeSize;
>   rangeend = -i*RangeSize;
>   if( i == NumRanges - 1 ) rangestart = -999999;
>   AddColumn( 
>   LastValue( 100* Cum( endprofit1 > rangestart AND endprofit1 < 
> rangeend ) /TotalLongTrades ), 
>   " % with loss " + rangeend + ".." + rangestart );
> AddColumn( 
>   LastValue( 100* Cum( endprofit2 > rangestart AND endprofit2 < 
> rangeend ) /TotalShortTrades ), 
>   " % with loss " + rangeend + ".." + rangestart );
> 
> }
> 
> // Expectation analysis
> dBuy = Ref( Buy,-1);
> dSell = Ref( Sell,-1);
> dShort = Ref(Short,-1);
> dCover = Ref(Cover,-1);
> LongProfit = IIf(dSell,E - ValueWhen(dBuy,E),0);
> ShortProfit = IIf(dCover,E-ValueWhen(dShort,E),0);
> 
> Winnings = Cum(IIf(LongProfit>0,LongProfit,0) +
> IIf(ShortProfit>0,ShortProfit,0));
> Losses = Cum(IIf(LongProfit<0,LongProfit,0) +
> IIf(ShortProfit<0,ShortProfit,0));
> NumWinTrades = Cum(((LongProfit>0) OR (ShortProfit>0)) AND
> Status("BarInRange"));
> NumLosTrades = Cum(((LongProfit<0) OR (ShortProfit<0)) AND
> Status("BarInRange"));
> TotalTrades = Cum(dSell OR dCover);
> WinningTrades = Cum((LongProfit > 0) OR (ShortProfit >0));
> LosingTrades = Cum((shortProfit < 0) OR (LongProfit < 0));
> AveWinTrade = Winnings / WinningTrades;
> AveLosTrade = Losses / LosingTrades;
> PercentWinners = WinningTrades / TotalTrades;
> Expectation = ( 1 + AveWinTrade/abs(AveLosTrade)) * PercentWinners -
 
> 1;
> Ex = LastValue(expectation);
> IC = Pd-Pd1+LB-LB1+(Pd2-Pd1)*(LB-LB1);
> IC = LastValue(Cum(1)) - IC;
> AddToComposite(IIf(Cum(1)== 
IC,Ex,0),"~Expectation","C",10+OptStart);
> AddToComposite(IIf(Cum(1)== 
IC,Pd,0),"~Expectation","V",10+OptStart);
> AddToComposite(IIf(Cum(1)== 
IC,LB,0),"~Expectation","I",10+OptStart);
> AddToComposite(IIf(Cum(1)== IC,LastValue(E),0),"~Exp-
> Equity","X",10+OptStart);
> AddColumn(AveWinTrade,"AveWTr",1.2);
> AddColumn(AveLosTrade,"AveLTr",1.2);
> AddColumn(PercentWinners*100,"%Winners",1.4);
> AddColumn(Expectation, "Expectation",1.3);
> GraphXSpace = 5;
> Filter = Status("LastBarInRange");
> Plot(E,"Equity",1,1);
> /*
> I use two indicators to display the results:
> Displaying the Expectancy and Equity curves: */
> Plot(Foreign("~Expectation","C"),"Expectation",1,1+4);
> Plot(Foreign("~Exp-Equity","X"),"Exp-Equity",4,16384 +65536 );
> GraphXSpace = 10;
> 
> /*Displaying the parameters being optimized:
> 
> Introduction to the AmiBroker AddToComposite()
> By Herman van den Bergen [ psytek@xxxx ]
> Last Updated: 2002-11-12
> Page 32 of 32 */
> Plot(Foreign("~Expectation","V"),"Expectation-Pd",4,1+4);
> Plot(Foreign("~Expectation","I"),"Expectation-LB",6,1+4);
> GraphXSpace = 10;
> 
> rgds, Pal
> 
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "pip_hunter_2003" 
> <pip_hunter_2003@xxxx> wrote:
> > Thx for the information.
> > 
> > I am testing some of my trading systems with Amibroker. If it 
will 
> > work ok I think I will purchase the system.
> > 
> > Btw. Could you tell me is there anything in AFL to check number 
of 
> > open positions? And the type (Long or short) of the open 
positions?
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "Pal Anand" <palsanand@xxxx> 
> wrote:
> > > I have it for Mini-Forex as:
> > > 
> > > Initial equity = 1000
> > > Positions = Long and Short
> > > Periodicity = Daily
> > > Allow position size shrinking
> > > Reverse entry signal forces exit
> > > Allow single bar trade (same bar trade)
> > > MinShares = 130
> > > Round Lot Size = 10 (Assuming 1 contract = 10 Lots)
> > > Tick Size = 1
> > > Commissions & rates = 0  //Assuming no slippages, would set it 
to 
> > 0.5 
> > > for trades > $1,000,000.00
> > > Annual interest rate = 0
> > > Margin requirement = 2
> > > 
> > > LB1=5;LB2=16;
> > > //MaxOpenPos= LB = Optimize
> ("MaxOpenPos",5,LB1,LB2,1); //Optimized 
> > > using UPI
> > > MaxOpenPos= LB = Param("MaxOpenPos",5,LB1,LB2,1);
> > > SetOption("MaxOpenPositions", MaxOpenPos );
> > > 
> > > MS1=130;MS2=390;
> > > //MinShares = Optimize("MinShares",130,MS1,MS2,100);
> > > MinShares = Param("MinShares",130,MS1,MS2,100);
> > > SetOption("MinShares", MinShares);
> > > 
> > > PositionSize=-100/MaxOpenPos=-100/5=-20 Lots(-2 Contracts)
> > > =MaxRisk*Equity/(-MDD/2);
> > > 
> > > /* Assuming MDD of 30% and verified by MCS.  In reality, I 
limit 
> my 
> > > MaxRisk to 10% per trade, eventhough Optimalf% based on 
Win/Loss 
> > > ratio of 96/4 comes out with a MaxRisk (Optimalf%) of 92%.  
> > > Historical Kelly Value for MaxRisk = 0.45 */
> > > 
> > > MaxRisk/trade=-2*(-300/2)/1000=0.3 = 30%
> > > 
> > > //TR1 = Optimize("TotalReturn1",0.25,0.10,0.50,0.05);
> > > TR1 = Param("TotalReturn1",0.25,0.10,0.50,0.05);
> > > //TR2 = Optimize("TotalReturn2",0.45,0.10,0.50,0.05);
> > > TR2 = Param("TotalReturn2",0.45,0.10,0.50,0.05);
> > > //TR3 = Optimize("TotalReturn3",0.45,0.10,0.50,0.05);
> > > TR3 = Param("TotalReturn3",0.45,0.10,0.50,0.05);
> > > //TR4 = Optimize("TotalReturn4",0.45,0.10,0.50,0.05);
> > > TR4 = Param("TotalReturn4",0.45,0.10,0.50,0.05);
> > > 
> > > tr13 = TR1 * (C - Ref(C, -63)) / Ref(C, -63) * 100;
> > > tr26 = TR2 * (C - Ref(C, -126)) / Ref(C, -126) * 100;
> > > tr37 = TR3 * (C - Ref(C, -189)) / Ref(C, -189) * 100;
> > > tr52 = TR4 * (C - Ref(C, -252)) / Ref(C, -252) * 100;
> > > 
> > > RSW = tr13 + tr26 + tr37 + tr52;  //Plan to experiment with 
Most 
> > > Anchored Momentum (MAM) later
> > > 
> > > PositionScore = RSW;
> > > 
> > > Would be curious to know what you come up with.  Feedback 
> > appreciated 
> > > from all.
> > > 
> > > rgds, Pal
> > > 
> > > 
> > > --- In amibroker@xxxxxxxxxxxxxxx, "pip_hunter_2003" 
> > > <pip_hunter_2003@xxxx> wrote:
> > > > How to set up the AmiBroker 4.50 std for EURUSD Forex 
> backtesting?
> > > > 
> > > > I want to buy and sell only 1 slot / trade.


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 

Yahoo! Groups Links

To visit your group on the web, go to:
 http://groups.yahoo.com/group/amibroker/

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/