Code:
///////////////////////////////////////////
// EquityCurveFeedback.afl
//
// There are many ways to use the equity
// in the trading account, and its variation,
// to act as a filter to allow or block trades.
//
// This code shows one example of applying
// trading system logic to the equity curve
// created by another trading system.
//
// The first system is a simple moving
// average crossover -- it could be any system.
//
MALength1 = 14; //Optimize("MALength1",14,2,50,2);
MALength2 = 13; //Optimize("MALength2",13,1,51,2);
Buy = Cross(MA(C,MALength1),MA(C,MALength2));
Sell = BarsSince(Buy)>=5;
Buy = ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
e = Equity();
Plot(C,"C",colorBlack,styleCandle);
PlotShapes(Buy*shapeUpArrow+Sell*shapeDownArrow,
IIf(Buy,colorGreen,colorRed));
Plot(e,"equity",colorGreen,styleLine|styleLeftAxisScale,15000,18000);
// The equity curve is choppy.
// If there are positive serial correlations
// between daily equity values, then we expect
// up days to be followed generally by more up days,
// and down days by more down days.
// So we might try applying a trend following system
// to the equity curve.
// When the equity curve is above its moving average,
// the system is working, and we should take trades.
// When the equity curve is below its moving average,
// the system is out-of-sync, and we should block trades.
// EquityMALength is the lookback period for the equity
// curve moving average.
// We will set Pass to either 0 or 1.
// Pass==0 blocks trades, Pass==1 allows trades.
EquityMALength = Optimize("EquityMALength",12,1,20,1);
EquityMA = DEMA(e,EquityMALength);
Plot(EquityMA,"equityMA",colorBrightGreen,styleLine|styleLeftAxisScale,15000,18000);
Pass = IIf(e>=EquityMA,1,0);
Buy = Buy AND Pass;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
e1 = Equity();
Plot(e1,"FilteredEquity",colorBlue,styleLine|styleLeftAxisScale,15000,18000);
/////////////////////////
Bookmarks