Interesting example Howard - thanks for sharing.

I'll admit I don't understand why this would work as a piece of code -
how can you have Amibroker compute a buy and sell for a system and
then run it again in the same piece of code - my understanding was
that you had to use AddToComposite to achieve this.

Two small follow-up questions:
- Is Equity an array?  I'm assuming it is - so if I wanted to,
instead, compute daily returns I could calculate this via
Ref(eSystem1,-1)?
- Second - and this is really basic but I've never understood how to
do it so pardon the stupid question.  When you've got a plot command
within a backtest, how do you have the plots show up?

Thanks, as always, for your contributions to this forum Howard!

Best,

Damian

--- In [email protected], "Howard Bandy" <howardba...@...> 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
> // ----------------------------------------------
>


Reply via email to