A learners guide.- AFL Example.
Hope it is of some help.
Most liked posts in thread: Please help me define Amibroker formulas!
Page 1 of 3
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
Buy = C>Ref(HHV(C,250),-1) AND C>Ref(L,-1);
Sell = C<MA(C,20);
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?
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.
type amibroker momentum into google and we will help you with buy/sell code if you want to skip the know why bit.
You should probably start to read the AB guide first and read things like this one
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.
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.
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?
Thanks in advance
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.
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:
// Pad and align
// Reference symbol (Pad and align)
// Risk-free rate Sharpe
// Risk-free rate UPI
// Add artificial future bar
// Limit trade size %
// Disable trade size limit
// Walk forward mode and data parameters
// Optimization target
Some other settings are only accessible via setoption but not in AA settings like
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
Also backtest mode can only be set via code level
//:::::::::::::::::::::::::::::::::::::::::::: 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 ); // rotational trading mode - equivalent of EnableRotationalTrading() call //:::::::::::::::::::::::::::::::::::::::::::: Backtest Mode Selection end :::::::::::::::::::::::::::::::::::::::::::::::::
Amibroker Help (AFL language reference) and Google are your friend.
Once you get into the language reference, search on HHV, select a topic, then read away.
- highest high value Lowest/Highest
SYNTAX hhv( ARRAY, periods )
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.
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
30 Week Hi Indicator - Calculate
52 Week New High-New Low Index
Adaptave Zones O/B & O/S Oscillator
Adaptive Price Channel
Advanced MA system
AFL Example - Enhanced
Against all odds
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
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.
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
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.
Trash I just go through fases of anxiety and to be honest the last week has been abit of an anxious week so I havent really been sitting behind a computer much. Just been doing physical stuff, trying to keep focused.
Even though my improvement as a trader is my biggest priority some times I have to give it a weeks rest. Your advice is absorbed. I have to sit behind the program for a few hrs to mess about and figure out how to plot it etc so thats why I didnt do it staight away and even half the advice which is given my brain cant interpret it yet.
Page 1 of 3