PureBytes Links
Trading Reference Links
|
Hello
I have been looking for a way to determine Closed Equity to use for
positionsize without success. Below is the code developed so far.
Many thanks to those who have started this and provided suggestions.
At the end of my skills. All Please help.
I have used it with the basic MACD system and positionsize of 10% of
Closed Equity. If the composite does not exist (first time run), It
still causes access violation cleared by AA -> settings -> portfolio
-> uncheck -> enable CBT. It is basically an unstable system ie. the
backtest result (equity) does not settle to a value but oscillates;
chaotic!! All caused because a closed equity is not available to AFL
system since calculated using CBT and then changes each time backtest
is run. So numerous backtests are needed. I believe this is a good
reason to have AB provide closed equity for simpler positionsize
calculations. Lets see. Australia to win football. Yeahhhh
Included is code to Plot the play of closed and open Equity.
Many thanks for suggestions and comments
franc
///////////////////////////////////////////////////////
// Closed Portfolio Equity Composite to determine Positionsize
// various contributions from many
//AIM: Want to generate a Closed Equity Chart for a test period
//Method: Loop through all bars in test period
// for each bar iterate through closed trade list and
// at the bar determine the realised profit and
// at the bar add profit to the previous bar Closed Equity
// add the open trade profit (close out) for last test bar
//CHANGE: added positionsize expression
//-----------------------------------------------------------------------
SetCustomBacktestProc("");
if(Status("action") == actionPortfolio)
{
bo = GetBacktesterObject();
bo.BackTest();
//-----------------------------------------------------------------------
//CLOSED EQUITY ~COMPOSITE
//NB:backtest automatically assigns FIR==[i]=0 and LIR==[i]=barcount-1
SumProfits[0] = bo.InitialEquity;
dt = DateTime();
//for BIR the equity is non-zero; more is better
for(i=1; i<BarCount; i++)
{
//search through closed trade list; get the profit when bar==exitdate
SumProfits[i] = SumProfits[i-1];
for(trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade())
{
ExitDate = trade.ExitDateTime;
if(ExitDate==dt[i]) { SumProfits[i] = SumProfits[i] +
trade.GetProfit(); }
}
//search through open trades list; get the profit when i==lastbar
(close out open trades)
if(i==BarCount-1)
{
for(trade = bo.getFirstOpenPos(); trade; trade = bo.getNextOpenPos())
{ SumProfits[i] = SumProfits[i] + trade.GetProfit(); }
}
}
//clear composite before each test, put in group 253, enable atc in
custom portfolio backtest
AddToComposite(SumProfits,"~ClosedEquity","O",
atcFlagEnableInPortfolio|atcFlagEnableInBacktest|atcFlagEnableInExplore|atcFlagDefaults);
AddToComposite(SumProfits,"~ClosedEquity","C",
atcFlagEnableInPortfolio|atcFlagEnableInBacktest|atcFlagEnableInExplore|atcFlagDefaults);
} //-end actionportfolio
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// your trading system here
Capital = GetOption("InitialEquity");
fast = Optimize("fast", 12, 5, 20, 1 );
slow = Optimize("slow", 26, 10, 25, 1 );
Buy=Cross(MACD(fast,slow),Signal(fast,slow)) AND V>0.3*MA(V,51);
Sell=Cross(Signal(fast,slow),MACD(fast,slow));
BuyPrice=Open;
SellPrice=Open;
//-----------------------------------------------------------------------
//Closed and Open Equity Curve
OE = Foreign( "~~~Equity", "C" );
CE = Foreign( "~ClosedEquity", "C" );
PositionSize = 0.10*Ref(CE,-1); //10% of Closed Equity in $$ terms
//-----------------------------------------------------------------------
// Plotting
GraphXSpace=5;
Plot(CE,"ClosedEquity",colorWhite,styleLine, styleCloud);
PlotOHLC( CE,CE,0,CE, "ClosedEquity", IIf( CE > 0, colorGreen,
colorRed ),
styleCloud );
PlotOHLC( OE,OE,0,OE, "OpenEquity", IIf( OE > CE, colorRed,
colorRed),
styleCloud );
//-----------------------------------------------------------------------
// Title
ChgCE = IIf(CE-Ref(CE,-1)<0,colorRed,colorBrightGreen);
ChgOE = IIf(OE-Ref(OE,-1)<0,colorRed,colorBrightGreen);
Daycolor = IIf(DayOfWeek()==5,colorBrightGreen,colorRed);
_N(
EncodeColor(colorWhite)
+Title=
EncodeColor(colorWhite)
+Name()
+EncodeColor(colorGold)+" "
+WriteIf(Interval()==86400,"DAILY",
WriteIf(Interval()==432001,"WEEKLY",
WriteIf(Interval()==2160001,"MONTHLY","")))
+EncodeColor(SelectedValue(Daycolor))
+WriteIf(DayOfWeek()==5," Friday ",
WriteIf(DayOfWeek()==4," Thursday ",
WriteIf(DayOfWeek()==3," Wednesday ",
WriteIf(DayOfWeek()==2," Tuesday ",
WriteIf(DayOfWeek()==1," Monday ","")))))
+EncodeColor(colorWhite)
+Date()
+" Open Equity = $"+WriteVal(OE,1.1)
+EncodeColor(SelectedValue(ChgOE))
+ " ($"+WriteVal(OE-Ref(OE,-1),1.2)+ ")"
+EncodeColor(colorWhite)
+" Closed Equity = $"+WriteVal(CE,1.1)
+EncodeColor(SelectedValue(ChgCE))
+ " ($"+WriteVal(CE-Ref(CE,-1),1.2)+ ")");
|