--- In
amibroker@xxxxxxxxxxxxxxx, "Howard Bandy" <howardbandy@xxx> wrote:
>
> Greetings all --
>
> I have received a couple of inquiries about using the equity curve to
> control the logic of a trading system. Pasted into this posting is a
> trading system that includes two examples. There are a lot of other
> ways the equity curve can be used.
>
> Thanks,
> Howard
>
>
> // EquityCurveLogicSwitcher.afl
> //
> // Howard Bandy
> // February 2009
> //
> // In response to an inquiry from the forums --
> //
> // This is an example of using the daily equity
> // to control the logic of a trading system.
> //
> // Code two trading systems -- System 1 and System 2
> // For this example, they are the reverse of each other.
> //
> // Compute a moving average of daily equity of System 1.
> // Refer to either today's value, or yesterday's,
> // depending on when you want to measure and when you
> // want to act and are able to act.
> // When the moving average of the equity curve of System 1 is
> // higher than it was the previous day, trade System 1.
> // When the moving average is lower, trade System 2.
> //
> // An alternative is to use only System 1.
> // Take signals in the original direction
> // when the equity curve is rising,
> // take signals in the opposite
> // direction when the equity curve is falling.
> //
> // ==========================================================
> // Disclaimer
> //
> // This is only an EXAMPLE, it is NOT A FINISHED TRADING SYSTEM.
> // There are a lot of subtleties to consider when coding
> // and potentially trading this.
> //
> // For example:
> // When do you enter and exit trades
> // When do you calcaulate the original equity curve
> // When do you measure the slope of the original equity curve
> // Making certain that a signal to go Short causes a Long position
to exit
> // And so forth
> //
> //
> // I have probably forgotten to do something important,
> // and this code is probably incorrect in exactly the way that
> // will cause you problems.
> // ==========================================================
> //
> // System 1 is a moving average crossover -- replace this
> // with whatever you want to use.
>
> // Code the two trading systems.
>
> MA1Len = Optimize("MA1Len",4,1,25,1);
> MA2Len = Optimize("MA2Len",5,1,25,1);
>
> MA1 = MA(C,MA1Len);
> MA2 = MA(C,MA2Len);
>
> System1Buy = Cross(MA1,MA2);
> System1Sell = Cross(MA2,MA1);
>
> System2Buy = Cross(MA2,MA1);
> System2Sell = Cross(MA1,MA2);
>
> // Compute the equity curve resulting from trading System 1.
>
> Buy = System1Buy;
> Sell = System1Sell;
>
> eSystem1 = Equity();
>
> EquitySmoothLen = 252;
> SmoothedEquity = EMA(eSystem1,EquitySmoothLen);
> SmoothedEquityRising = SmoothedEquity > Ref(SmoothedEquity,-1);
>
> // The two options --
> // TRUE == trade two systems
> // FALSE == trade System 1 in the opposite direction
>
> LogicOption = False;
>
> if (LogicOption)
> {
> // Use the slope of the equity curve of System 1 to
> // decide whether to trade System 1 or System 2.
>
> Buy = IIf(SmoothedEquityRising,System1Buy,System2Buy);
> Sell = IIf(SmoothedEquityRising,System1Sell,System2Sell);
> }
> else
> {
> // Use the slope of the equity curve of System 1 to
> // decide whether or Buy or Short when the System1Buy signal appears
>
> Buy = IIf(SmoothedEquityRising,System1Buy,0);
> Sell = IIf(SmoothedEquityRising,System1Sell,0);
> Short = IIf(NOT(SmoothedEquityRising),System1Buy,0);
> Cover = IIf(NOT(SmoothedEquityRising),System1Sell,0);
> Sell = Sell OR Short;
> Cover = Cover OR Buy;
> }
>
> e = Equity();
>
> // Plot
>
> Plot(C,"C",colorBlack,styleCandle);
> Plot(MA1,"MA1",colorRed,styleLine);
> Plot(MA2,"MA2",colorBlue,styleLine);
>
>
Plot(SmoothedEquity,"SmoothedEquity",colorRed,styleLine|styleLeftAxisScale);
> Plot(eSystem1,"eSystem1",colorBlue,styleLine|styleLeftAxisScale);
> Plot(e,"eUsingSwitch",colorGreen,styleLine|styleLeftAxisScale);
>
> // The End
> // ----------------------------------------------
>