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

[amibroker] Re: Inline Backtester Metrics



PureBytes Links

Trading Reference Links

Here is an elective period StDevP for a non-continuous dataset

//PART 1B - Elective Period StandardDeviation Of The Trade Series


Trades = IIf(CS,TSFactor,0);//reconfigures trade series so that 
tradesNOT == 0,

TradeFlag = IIf(CS,1,0);//returns 1 for every trade

Period = Param("Periods",20,1,100,1);

ST = Sum(Trades,Period);//Sums Trades
STS = Sum(Trades^2,Period);//Sums the TradesSquared
CT = Sum(TradeFlag,Period);//Counts the Trades

StandardDevP = sqrt(  STS/CT - (ST/CT)^2  );//uses Ken Closes 
restated StDev equation,



--- In amibroker@xxxxxxxxxxxxxxx, "brian_z111" <brian_z111@xxx> wrote:
>
> I added progressive StDevP,of the trade series, to the code.
> 
> //P_InLineMetricsI_v2
> 
> /*
> 
> v2  renames some variables (to clarify nomenclature and prepare for 
> later code),
> 
> confirms Buy/Sell signals to allow trade counting AND accounting 
for 
> BreakEven trades,
> 
> adds cumulative Standard Deviation of the trade series.
> 
> Monday is used as the Buy example because it is unambiguous.
> Typically most markets have Mon OR Fri public holidays so it allows 
> for checking code response to non-paireds buys AND sells (Signal 
> errors).
> BarIndex() == 0 is added as an initial Buy condition to workaround 
> the propogating of {empty} values that can arise in the first few 
> bars.
> A Buy latch is used so that only one trade can be entered at a time 
> AND every Sell must have a corresponding Buy.
> Plots the TradeSeriesFactor, as GrowthFactor, which is equivalent 
to 
> % (tradesNOT are recorded as GF == 1).
> Since commissions aren't considered the Equity outcome for break 
even 
> trades (GF also == 1) is correct.
> The progressive product of the GF is equivalent to the compounded 
> Equity Curve for an initial fixed capital investment
> (this assumes the trades are sequential).
> If the Initial Equity is 1 the EqCurve is the standardised Equity 
> Factor (EF),
> (this assumes that no capital addition or rebalancing occurs after 
> the initial allocation).
> Equity == Initial Equity * EF
> 
> 
> */
>  
> 
> //PART IA TRADE SERIES MODULE (CLOSED TRADE EQUITY - ONLY ONE TRADE 
> PER SYMBOL OPEN AT A TIME)
> 
> Filter = 1;//allows code to run as an exploration for code testing
> 
> M = DayOfWeek() == 1;
> F = DayOfWeek() == 5;
> BI = BarIndex()== 0;
> 
> 
> B = M;//Buy on Monday
> S = F OR BI;//first bar set to sell and then sell on Friday's 
> thereafter
> 
> B = Flip(B,S);//latches the Buy signal to prevent concurrent buys
> 
> BP = SP = C;//BuyPrice, SellPrice
> 
> CS = B == 0 AND Ref(B,-1) == 1;//Confirms the sell (it has exited 
> from a latched buy)
> CB = B == 1 AND Ref(B,-1) == 0;//Confirms buy (it enters a latched 
> buy)
> 
> 
> //Plot(B,"Buy",5,1);
> //Plot(S,"Sell",2,1);
> 
> //Plot(CB,"Buy",5,1);
> //Plot(CS,"Sell",2,1);
> 
> 
> //Plot(BP,"BuyPrice",5,1);
> //Plot(SP,"SellPrice",2,1);
> 
> TSFactor = IIf(CS,SP/ValueWhen(CB, BP,1),1);//TradeSeriesFactor as 
> GrowthFactor
> 
> // next section to be changed to log/antilog to avoid need for 
looping
> 
> EF = 1;//initialises EquityFactor to 1
> 
> for (i = 1; i < BarCount; i++)
> {
> EF[i] = EF[i-1] * TSFactor[i];
> }
> 
> //Plot(EF,"EquityFactor",5,1);
> 
> 
> 
> //PART 1B - Progressive StandardDeviation Of The Trade Series
> 
> 
> Trades = IIf(CS,TSFactor,0);//reconfigures trade series so that 
> tradesNOT == 0,
> 
> TradeFlag = IIf(CS,1,0);//returns 1 for every trade
> ST = Cum(Trades);//Sums Trades
> STS = Cum(Trades^2);//Sums the TradesSquared
> CT = Cum(TradeFlag);//Counts the Trades
> 
> StandardDevP = sqrt(  STS/CT - (ST/CT)^2  );//uses Ken Closes 
> restated StDev equation,
> 
> //Plot(,"",1,1); plot template for copy and paste
> 
> //Plot(Trades,"Trades",1,1);
> //Plot(TradeFlag,"TradeFlag",1,1);
> //Plot(ST,"SumTrades",2,1);
> //Plot(CT,"CountTrades",3,1);
> //Plot(STS,"SumTradesSquared",2,1);
> 
> Plot(StandardDevP,"Cumulative StDevP",6,1);
> 
> 
> //Exploration report columns for code testing via manual export to 
> spreadhsheet,
> 
> // tested OK with a sample of 48 closed trades (3 were breakeven 
> trades) - StDevP agreed with Excel StDevP calculations to 4 or 5 
> decimal places
> 
> 
> AddColumn(Trades,"Trades",1.9);
> AddColumn(ST,"SumTrades",1.9);
> AddColumn(CT,"TradesCount",1.0);
> AddColumn(STS,"SumTradesSquared",1.9);
> 
> AddColumn(StandardDevP,"StandardDevTrades",1.9);
> 
> 
> 
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "brian_z111" <brian_z111@> wrote:
> >
> > Herman and Al,
> > 
> > I took the scissors to my first effort.
> > 
> > It isn't an inline backtester, rather an inline RootCauseMetrics 
> > indicator (possibly it could be used for calculating the common 
> > Performance Metrics 'on the fly', including with lookback 
periods). 
> > 
> > 
> > 
> > This is the basic starting module.
> > 
> > It should be possible to modify it to allow for concurrent 
signals, 
> > open trade interest, although it gets a bit harder from here for 
me 
> > (maybe not for others).
> > 
> > I have checked it over a few charts and so far it stands up O.K
> > I haven't used in the line of fire though.
> > 
> > Any potential in it for you guys?
> > 
> > 
> > 
> > FTR
> > 
> > This is a continuation of discussion at topic:
> > 
> > "How To Save Metrics In Composite For Indidual BT's"
> > 
> > A little background discussion about the idea behind the code is 
at
> > 
> > message # 127383 and # 127420 in the above thread.
> > 
> > //P_InLineMetrics
> > 
> > //Monday is used as the example because it is an unambiguous Buy
> > //Typically most markets have Mon or Fri public holidays so it 
> allows 
> > for checking code response to duplicate buys and sells (signal 
> errors)
> > //BarIndex() is added as a buy condition to workaround the 
> > propogating of {empty} values that can arise in the first few bars
> > //A latch is used so that only one trade can be entered at a time 
> and 
> > every Sell must have a corresponding Buy
> > //Plots the trade series, as GrowthFactor, which is equivalent to 
%
> > //No trades are accounted for and recorded as GF 1
> > //The code doesn't identify break even trades i.e. GF == 1 but it 
> can 
> > be modified to do so
> > //since commissions aren't considered the Eq outcome for break 
even 
> > trades is correct
> > //the progressive product of the GF is equivalent to the Equity 
> Curve 
> > for a fixed capital investment
> > //if the intitial Equity is 1 the EQ is the standardised Equity 
> Factor
> > //Equity == Initial Equity * EF
> > //the product of individual GF's, sumbol by symbol, is the 
> Portfolio 
> > Growth Factor the progessive product of which produces the 
> Portfolio 
> > Equity Factor
> > //same bar entry/exits are not allowed but the code can be 
modified 
> > for that if required
> > 
> > 
> >  
> > 
> > //PART IA TRADE SERIES MODULE (CLOSED TRADES - ONLY ONE TRADE PER 
> > SYMBOL OPEN AT A TIME)
> > 
> > M = DayOfWeek() == 1;
> > F = DayOfWeek() == 5;
> > BI = BarIndex()== 0;
> > 
> > 
> > B = M;//Buy on Monday
> > S = F OR BI;//first bar set to sell and then sell on Friday's 
> > thereafter
> > 
> > B = Flip(B,S);//latches the Buy signal to prevent concurrent buys
> > 
> > BP = SP = C;//BuyPrice, SellPrice
> > 
> > 
> > //Plot(B,"Buy",5,1);//FFP - fault finding plot used when writing 
> the 
> > code
> > //Plot(S,"Sell",2,1);//FFP
> > 
> > //Plot(BP,"BuyPrice",5,1);//FFP
> > //Plot(SP,"SellPrice",2,1);//FFP
> > 
> > TradeSeries = IIf(S ==1 AND Ref(B,-1) == 1,SP/ValueWhen(B == 1 
AND 
> Ref
> > (B,-1) == 0, BP,1),1);//
> > 
> > Plot(TradeSeries,"TradeSeries",1,1);//as GrowthFactor
> > 
> > 
> > EF = 1;//initialises EquityFactor to 1
> > 
> > for (i = 1; i < BarCount; i++)
> > {
> > EF[i] = EF[i-1] * TradeSeries[i];
> > }
> > 
> > Plot(EF,"EquityFactor",5,1);
> > 
> > 
> > 
> > 
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "brian_z111" <brian_z111@> 
wrote:
> > >
> > > Herman,
> > > 
> > > IBM PartI
> > > 
> > > I am not sure if this is along the lines that you are 
> investigating.
> > > It might be a starting point.
> > > Sophisticated functions can be built from the concept.
> > > 
> > > Example of using single symbol equity function to back 
calculate 
> > the 
> > > trade series.
> > > 
> > > I used buy Tues(C) and sell Thurs(C) with one month of data to 
> test 
> > > the code as I went along (this gave me unambiguous signals with 
> > only 
> > > one signal at a time - I believe you can use an eq flag to dump 
> > dual 
> > > signals for real life use).
> > > 
> > > Note: some weeks don't have Mons or Fris so I wanted to avoid 
no 
> > > signals, caused by short weeks, during testing
> > > 
> > > I used barindex() > 4 to cut out the first week in the month so 
> > that 
> > > I started with no signals for a few bars before the first buy.
> > > 
> > > I left the plot code in there but commented out (I plotted each 
> > line 
> > > to test the veracity of the code).
> > > 
> > > Note that when the trade series is used to recreate the eq 
curve 
> > (as 
> > > a cross check) it only matches on the exit bars for each trade.
> > > I tried it on 10 years of Yahoo data and the final eqs matched 
to 
> 2 
> > > decimal places (rounded off).
> > > 
> > > //P_InLineEquity
> > > //code to reverse engineer the trade series from the equity 
curve
> > > //it is reversed at the end to check the accuracy of the method
> > > 
> > > InitialEq = 10000;//input required
> > > 
> > > SetOption("InitialEquity", InitialEq ); 
> > > 
> > > Buy = BarIndex() > 4 AND DayOfWeek() == 2;//use your own
> > > Sell = BarIndex() > 4 AND DayOfWeek() == 4;//use your own
> > > 
> > > BuyPrice = C;//use your own
> > > SellPrice = C;//use your own
> > > 
> > > Plot(Equity(),"Equity",5,1);
> > > 
> > > Entry = IIf(Equity() == Ref(Equity(),-1) AND Equity() != Ref
> (Equity
> > > (),1),1,0);
> > > 
> > > //Plot(Entry,"Entry",1,1);
> > > 
> > > Exit = IIf(Equity() != Ref(Equity(),-1) AND Equity() == Ref
(Equity
> > > (),1),1,0);
> > > 
> > > //Plot(Exit,"Exit",2,1);
> > > 
> > > TradeSeries = IIf(Exit ==1,ValueWhen(Exit == 1, 
> > SellPrice,1)/ValueWhen
> > > (Entry == 1, BuyPrice,1),1);
> > > 
> > > //Plot(TradeSeries,"TradeSeries",1,1);
> > > 
> > > GF = 1;//GrowthFactor
> > > 
> > > for (i = 1; i < BarCount; i++)
> > > {
> > > GF[i] = GF[i-1] * TradeSeries[i];
> > > }
> > > 
> > > //codesters might be able to make the above loop 
better/prettier?
> > > //Plot(GF,"GrowthFactor",5,1);
> > > 
> > > 
> > > Plot(InitialEq * GF,"CalculatedEquity",1,1);
> > >
> >
>



------------------------------------

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/