• Australian (ASX) Stock Market Forum

Posts:
402
37
Joined:
Nov 1, 2009
2. ### Newt

Posts:
499
734
Joined:
Oct 17, 2012
Hi,

Perhaps try something like below. If you're going to do backtesting, then yes you need a Buy and Sell statement. The buy below says buy if today's close is greater than the 250 closes before (going back from yesterdays - you don't usually want to include today's close in such a statement). The 2nd part of the buy statement is "buy if close is greater than yesterday's Low).

The sell is a simple "sell if below 20 day moving average of closing price).
You don't really need the first 7 lines, but their an example of how to simulate a maximum of 10 open positions, trading the next day (for buy and sells), and some randomising code for your optimising runs.

Go nuts and play.....!

PS = Optimize( "Postion Score", 1, 1, 1000, 1 );
PositionScore = Random() * PS;

Positions = 10;
SetOption( "InitialEquity", 100000 );
SetOption( "MaxOpenPositions", Positions );
SetTradeDelays( 1, 1, 1, 1 );
SetPositionSize( 100/Positions, spsPercentOfEquity ); //Compounding profits and return

Sell = C<MA(C,20);

3. ### darkhorse70

Posts:
896
10
Joined:
Aug 20, 2013
Thanks superglue. The problem is I dont fully understand the simple definitions the afl library goves out hence the dilemma.

Thanks newt. Thats perfect. Did you figure the formulas out through trial and error or is their a specific book which gives you examples of each formula as youve clearly done? Or a website?

4. ### WysiwygEveryone wants money

Posts:
8,424
260
Joined:
Aug 8, 2006
You gotta read and comprehend just like math or english in school. Then write your thoughts on a piece of paper or type in wordpad. Then look at the various functions in the categorised list of functions, User Guide. Spoon fed learns nothin.
http://www.amibroker.com/guide/h_portfolio.html

OR

5. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
What formula are you talking about?

6. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
If you mean the AFL users library (codes made by AB users) then you should read what AmiBroker have written at the top of each formula there.

So you should rather contact or comment to the author of each code.

7. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
You should probably start to read the AB guide first and read things like this one
www.amibroker.com/guide/h_understandafl.html

If you build a house you probably don't start with the roof too but instead you go to the bottom where it gets more dirty first.

8. ### darkhorse70

Posts:
896
10
Joined:
Aug 20, 2013
Thanks trash. All I meant was the few words that explain what the formula does for example the HHV formula isnt clear enough for me.

Thanks for the link by the way.

9. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
HHV( parameter1, parameter2 ) means Highest High Value as the help defines it. So it outputs just what the name says ... it outputs the HHV of an inserted array within a defined period. HHV result is an array too. Now first param can be whatever array of your choice or own creation while prameter2 (the period) may be a number like 20 bars or an array like BarsSince( Month() != Ref(Month(), -1) ) etc.

Simply plot a price graph and a graph plotting HHV in same chart pane then you will understand better. For HHV plot use styleLine.

10. ### darkhorse70

Posts:
896
10
Joined:
Aug 20, 2013
Thanks trash. Will try to plot it tonight. My trading is basically at a halt till I can test my strategy.

11. ### darkhorse70

Posts:
896
10
Joined:
Aug 20, 2013
So while I wait for my book to come on Thursday to clarify a few things I have a few more questions.
Can you customize your back tester settings even further using specific formulas or is it limited to the setting standards?

One last question also. One of my conditions for entering a long trade would be an intermediate to primary upward trend. In the AFL code wizard I don't think that function is available. Is it possible to create your own customized formula? If so, im assuming it would be hard? Would the only way to get your hands on some thing like that through sharing? If so, any one mind giving me the formula for an upward trend?

Ps, the last thing I want is to be spoon fed. Im just a little lost but im sure ill get the jist of things soon enough.

12. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
Most AA (Auto-analysis) settings can be used via code level via SetOption() function.

The only ones not being usable via setoption are

THE FOLLOWING SETTINGS CANNOT BE SET PROGRAMMATICALLY VIA AFL AND SHOULD BE CHECKED IN AA SETTINGS:
// Reference symbol (Pad and align)
// Risk-free rate Sharpe
// Risk-free rate UPI
// Walk forward mode and data parameters
// Optimization target

Some other settings are only accessible via setoption but not in AA settings like

Code:
```SetOption(	"WorstRankHeld",	1 );	// Not in settings
SetOption(	"PriceBoundChecking",	False	);	// Not in settings,
SetOption(	"UsePrevBarEquityForPosSizing",	False	);	// Not in settings,
SetOption(	"UseCustomBacktestProc",	False	);	// Not in settings,
SetOption(	"DisableRuinStop",	False	);	// Not in settings
SetOption(	"EveryBarNullCheck",	False	);	// Not in settings
SetOption(	"HoldMinBars",	0	);	// Not in settings, disables exits for this number of bars
SetOption(	"HoldMinDays",	0	);	// Not in settings, disables exits for this number of days
SetOption(	"EarlyExitBars",	0	);	// Not in settings
SetOption(	"EarlyExitDays",	0	);	// Not in settings
SetOption(	"EarlyExitFee",		0	);	// Not in settings
SetOption(	"SeparateLongShortRank",	False	);	// Not in settings
SetOption(	"MaxOpenLong",	0	);	// Not in settings, number of max open long positions
SetOption(	"MaxOpenShort",		0	);	// Not in settings, number of max open short positions
SetOption(	"RefreshWhenCompleted",	False	);	// Not in settings when set to TRUE, it will perform View->Refresh All
```

13. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
Also backtest mode can only be set via code level

Code:
```//:::::::::::::::::::::::::::::::::::::::::::: Backtest Mode Selection start :::::::::::::::::::::::::::::::::::::::::::::::
// default, as in 4.90, regular, signal-based backtest, redundant signals are removed
//SetBacktestMode( backtestRegular );

// SetBacktestMode( backtestRegularRaw );
// signal-based backtest, redundant (raw) signals are NOT removed, only one position per symbol allowed

// SetBacktestMode( backtestRegularRaw2 )
// for custom backtester users only, the same as backtestRegularRaw, but redundant exit signals are also kept.
// AVOID this mode - it requires lots of memory AND slows everything down.
// The difference between backtestRegularRaw and backtestRegularRaw2
// is JUST this single thing (Raw2 mode keeps ALL Sell signals, while Raw mode keeps only Sell Signal
// for symbols that generated a Buy before).

// SetBacktestMode( backtestRegularRawMulti );
// signal-based backtest, redundant (raw) signals are NOT removed,
// MULTIPLE positions per symbol will be open if BUY/SHORT signal is "true" for more than one bar and there are free funds
// Sell/Cover exit all open positions on given symbol, Scale-In/Out work on all open positions of given symbol at once.

// SetBacktestMode( backtestRegularRaw2Multi )
// for custom backtester users only, same as backtestRegularRawMulti,
// but redundant exit signals are also kept. AVOID this mode - it requires lots of memory AND slows everything down.

// SetBacktestMode( backtestRotational );
//:::::::::::::::::::::::::::::::::::::::::::: Backtest Mode Selection end :::::::::::::::::::::::::::::::::::::::::::::::::```

14. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
Code wizard is for beginners and does just basic stuff.

On the other hand in the formula editor you can code anything you can imagine.

15. ### Newt

Posts:
499
734
Joined:
Oct 17, 2012

Once you get into the language reference, search on HHV, select a topic, then read away.

HHV
- highest high value Lowest/Highest

SYNTAX hhv( ARRAY, periods )
RETURNS ARRAY
FUNCTION Calculates the highest value in the ARRAY over the preceding periods (periods includes the current day). HHV accepts periods parameter that can be constant as well as time-variant (array).
EXAMPLE The formula "hhv( close, 4)" returns the highest closing price over the preceding four periods; "hhv( high, 8)" returns the highest high price over the preceding eight periods.

References:
The HHV function is used in the following formulas in AFL on-line library:

Pivots And Prices And Swing Volume
% B of Bollinger Bands With Adaptive Zones
10-20 Indicator
30 Week Hi Indicator - Calculate
52 Week New High-New Low Index
Adaptave Zones O/B & O/S Oscillator
AFL Example
AFL Example - Enhanced
Against all odds

16. ### darkhorse70

Posts:
896
10
Joined:
Aug 20, 2013
Thanks trash for the clarification.

Newt call me stupid, ive read that definition a few times. The part I get confused about is the -1 after the 250day period . If the afl library had 2 or 3 detailed examples it would make it so much easier.

Howard bandy book will hopefully arrive tomorrow and im hoping it has alot of examples to give me a better picture of the whole thing.

Thanks for the feed back felllas. Goes a long way

17. ### WysiwygEveryone wants money

Posts:
8,424
260
Joined:
Aug 8, 2006
I can break it down for you.

C > Ref(HHV(H, 250), -1)

C = Close Price this bar (most recent)
> = Greater Than
Ref = Reference (previous period defined by -1 in this example)
( = Parenthesis
HHV = Highest High Value in the defined array (H or High in this example) and period (250 in this example)
( = Parenthesis
H = High Price for the Highest High Value
, = comma
250 = Period for HHV
) = Parenthesis
, = comma
-1 = Period of reference to check the array HHV(H, 250) which is the bar before today (one period before most recent)
) = Parenthesis

I am a tradesman and programming language is not one of my interests but I can tell you that the secret isn't in the formula, it is in the knowledge and experience. Both of which I am still processing. Amibroker has helped me understand the market gyrations much better. It is a longer road than I thought it would be. For some reason I can't get synchronised with Mr. Market and his cronies.

Trust this will help.

18. ### darkhorse70

Posts:
896
10
Joined:
Aug 20, 2013
Thanks wysiwig. Now I get what that -1 means haha.
screen shotted that explanation. Even though I shouldnt be trading I still have a position or two live. I just got stopped out on one and the other is close to.

I want to test my strategy asap so I can get some sort of picture of what my systems potential is or if its garbage.

Seems like most arnt in synch with the markets. Haha

Thanks wysi

19. ### trash

Posts:
412
17
Joined:
Mar 31, 2012
So I guess you haven't had the time yet to follow the advice of simply plotting HHV variable or Ref( HHV(...), -x ) variable with price plot and to play around with values so that you would understand by yourself what it is actually doing, right?

Instead every little detail has to be brought to you on a silver plate.

Just FYI but Ref() (just like every other function) also has a function reference to be found in the documentation.

And BTW if you are confused by brackets/braces then in the new editor you can just mark (or leftclick right before or after) each one and it will auto-highlight the corresponding beginning/ending bracket/brace.

Posts:
896