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

Re: [amibroker] Re: Using the Equity Curve to control trading logic



PureBytes Links

Trading Reference Links

Hi Damian --

Yes, the Equity() function returns an array.  When that result is assigned to a variable (which must also be an array, and which it will be unless you go to lengths to define it as something other than an array), whatever values were assigned to the variable remain in that variable as long as they are not overwritten.  So you can, both in concept and in practice, run a trading system and capture its equity curve by assigning it to a variable, then use the values stored in that variable to control the program flow of subsequent AFL code.  AFL code is processed sequentially.  As each line is processed, all elements of any array being assigned a new value are assigned their new values as that one statement is processed.  The Buy and Sell variables that were used (at the top of the code) to accept signals and compute the first equity curve are arrays that have special meaning to AmiBroker.  But they are variables, and can be overwritten (assigned new values) as often as you wish during the execution of an AFL program further down in the code.  Every time the Equity() function is called, it uses the values that are in the Buy and Sell variables at that point in the program execution to compute the equity.  Most programs assign values to Buy and Sell only once, but nothing prevents assigning them new values several times.  As they are overwritten with new values, whatever values are stored in them at the end of the program execution are the ones that you will see as The Buy and Sell signals that create The backtest results. 

ATC is very powerful, and I use it a lot, but it is not necessary for the example posted.

To plot a graph, there are several methods.  I use this one when developing systems --
Open the file (using the Formula Editor) containing the code with the Plot statement.
Click the Apply Indicator icon in the Formula Editor's toolbar (the little graph near the top of the window).

Depending on what you are plotting, and what you are backtesting, the plot may update automatically when backtests are run.  For example, once you have plotted the equity, when you select a new symbol to be analyzed, even without clicking the Backtest button, the equity for that new symbol will be plotted in its window pane as the price series is plotted in its window pane. 

Try it.
Copy the code in the posting.
Open Formula Editor.
Paste the code into Formula Editor.
Save it.
Within Formula Editor, Click Apply Indicator.  A new window pane will open and the plots will appear.
Expand the Symbols menu and select an issue to be the active issue.
Within Formula Editor, Click Analysis > Backtest (click the "!" Icon, then pull down its menu).
Look at the pane that has the plot from this code in it.
Click the Down Arrow key.  If you have not done anything else in between, this will select the next issue in the list.  If necessary, click on the symbol in the Symbols menu again, then click the Down Arrow key.  Note that the equity lines will change.  (The equity line plotted from this AFL, not the equity plotted from the Automatic Analysis Equity button)  AmiBroker has recalculated the equity that would have been achieved by trading that same system on the new symbol and plotted it automatically.

You can use this procedure to test one issue in a Watchlist, then step down through all the members of the watchlist with the Down Arrow key, noting the shape of the equity curve and the value of the final equity.

Thanks,
Howard

On Sun, Feb 1, 2009 at 12:03 PM, droskill <droskill@xxxxxxxxx> wrote:

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 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
> // ----------------------------------------------
>


__._,_.___

**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL 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

*********************************




Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___