Australian (ASX) Stock Market Forum

Amibroker FAQ

GB - use a nested loop. In the first loop which triggers a Buy = true, store the low L[j]. Then in the sub loop keep iterating and checking if the current close C[k] < L[j]. If yes, Sell = true.

See how you go!

Thanks rb. I understand what you're saying, but it's a bit beyond my ability unfortunately.
 
Using the ValueWhen function might be easier.

The key is this line:
SellLowClose = C < ValueWhen( Buy, L );

The ValueWhen function finds the bar of the most recent Buy,
then returns the value of the Low on that bar.

The right hand side of the assignment tests every bar to see if
the Close is less than the Low of the bar of the Buy.
If it is, set SellLowClose to True.


Best, Howard

---------------------

// ExitUsingValueWhen.afl

// Buy when fast moving average
// crosses up through slow moving average
// Sell when the Close is lower
// than the Low on the bar of the Buy

FastMALB = 10;
SlowMALB = 30;
FastMA = MA( C, FastMALB );
SlowMA = MA( C, SlowMALB );
Buy = Cross( FastMA, SlowMA );
SellMA = Cross( SlowMA, FastMA );

SellLowClose = C < ValueWhen( Buy, L );

//Sell = SellMA;
Sell = SellLowClose;

Buy = ExRem( Buy, Sell );
Sell = ExRem( sell, Buy );


Plot( C, "C", colorBlack, styleCandle );
Plot( FastMA, "FastMA", colorGreen, styleLine );
Plot( SlowMA, "SlowMA", colorBlue, styleLine );

shapearrows = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shapearrows, IIf( Buy, colorGreen, colorRed ),
0, IIf( Buy, Low, High ) );

// end //
 
Be careful if you use that to backtest.

Using the code Howard posted, if you get a second occurrence of your buy signal, the valuewhen buy will reference the most recent buy signal. So you will sell at the low of the most recent buy signal bar, not the low of the one you actually bought on.

ValueWhenBuy.PNG
 
Be careful if you use that to backtest.

Using the code Howard posted, if you get a second occurrence of your buy signal, the valuewhen buy will reference the most recent buy signal. So you will sell at the low of the most recent buy signal bar, not the low of the one you actually bought on.

View attachment 71478
Lone Wolf's observation is correct. Always be careful.

The code I posted was meant to illustrate that there is a built-in function that might be a solution to the OP's question as an alternative to using one or more loops.

Design of the trading system is more complex. The designer must determine whether such situations could occur or not. If they could occur, whether to block them or allow them. Followed by decision of which Low should be used as the exit reference.

Additionally, the code posted might be one of several exit techniques. When "ored" together, the first exit technique satisfied will cause an exit to the trade. The code I posted showed a potential exit based on a reversal of the moving average crossover. The Sell statement could be:
Sell = SellLowClose or SellMA;

Best,
Howard
 
Anyone know the rule for accessing/referencing elements within a loop for use in the buy line?

Thanks fellas.
Hi GB --

If the program is using a loop to test each bar for some condition, the loop has an index -- say it is i. Test bar by bar and give a True or False value to Buy. Assuming array formulas were used to compute some value before the loop, the code looks something like this:

specialAverage = MA(L,20);

for ( i = 1; i < BarCount; i++ )
{
Buy = L < specialAverage;
}

///// The code within the loop has "square brackets" to refer to each bar. The ASF post program is not showing that character. I'll try using l and r in place of left square bracket and right square bracket. The line inside the loop would be:
Buy lir = Llir < specialAveragelir;
//////

Buy is False on all bars by default. If there is any doubt about the initial value, set it explicitly bar-by-bar. Set Buy (on bar i) to True when the Low of the bar being tested (bar i) is less than the specialAverage (on bar i).

If any variable within the loop makes reference to a single bar of the array, all variables must use array indexing. Trying to compare the value on a single bar with an array raises an error.

Does this answer the question? Or is it something other?

Best, Howard
 
Hi GB --

If the program is using a loop to test each bar for some condition, the loop has an index -- say it is i. Test bar by bar and give a True or False value to Buy. Assuming array formulas were used to compute some value before the loop, the code looks something like this:

specialAverage = MA(L,20);

for ( i = 1; i < BarCount; i++ )
{
Buy = L < specialAverage;
}

///// The code within the loop has "square brackets" to refer to each bar. The ASF post program is not showing that character. I'll try using l and r in place of left square bracket and right square bracket. The line inside the loop would be:
Buy lir = Llir < specialAveragelir;
//////

Buy is False on all bars by default. If there is any doubt about the initial value, set it explicitly bar-by-bar. Set Buy (on bar i) to True when the Low of the bar being tested (bar i) is less than the specialAverage (on bar i).

If any variable within the loop makes reference to a single bar of the array, all variables must use array indexing. Trying to compare the value on a single bar with an array raises an error.

Does this answer the question? Or is it something other?

Best, Howard

Hi Howard, thanks for the help.

I meant a buy line which is outside the loop and is used for backtesting. I think maybe a different process is required, yes?
 
Hi Howard, thanks for the help.

I meant a buy line which is outside the loop and is used for backtesting. I think maybe a different process is required, yes?

Hi GB --

If Buy is already populated with True for the critical bars, it is an array with the same length as all other arrays -- an array with BarCount elements. In order to refer to any individual element of that array, a valid index value must be specified. If the outside loop is processing bar-by-bar (as contrasted with, for example, trade-by-trade), then the index in the loop that refers to the bar being examined can be used to refer to whether Buy is set to True for that bar or not. If the loop is not processing bar-by-bar, then there must be a translation that produces a correct index into the Buy array.

Perhaps post an example?

Best, Howard
 
Hi GB --

If Buy is already populated with True for the critical bars, it is an array with the same length as all other arrays -- an array with BarCount elements. In order to refer to any individual element of that array, a valid index value must be specified. If the outside loop is processing bar-by-bar (as contrasted with, for example, trade-by-trade), then the index in the loop that refers to the bar being examined can be used to refer to whether Buy is set to True for that bar or not. If the loop is not processing bar-by-bar, then there must be a translation that produces a correct index into the Buy array.

Perhaps post an example?

Best, Howard

Say variable x is within a loop, and outside the loop I want to Buy=x>1

AB gives me an error:

Error 29.
Variable 'x' used without having been initialized.
 
Say variable x is within a loop, and outside the loop I want to Buy=x>1

AB gives me an error:

Error 29.
Variable 'x' used without having been initialized.

AmiBroker executes statements from top to bottom. (Functions are defined and placed at the beginning of the file, but not executed until they are "called" among the statements.)

Variables within loops generally continue to be available after the loop has completed, and they usually have the value that was last assigned to them. In some cases that value may be random or undetermined. You can tell by inserting a statement immediately after the loop that writes or prints the value of the variable.

If x has a value upon exit from the loop, then it is available to be tested in the statement Buy = x>1.
Buy (the lefthand side of the statement) is an array with an element for every bar. The righthand side of the state tests to determine whether x is greater than 1. As with all assignment statements, the result is assigned to the variable on the lefthand side. In order for assignment to an array to work, the righthand side must be compatible in type -- that is, it must be an array. If it is not, AmiBroker will raise an execution error.

It is often difficult to identify the exact cause of an error in a computer program, so an error message might be late (not finally identified until some number of statements after the cause), might be incorrectly identified, or the message printed might be one of several messages that could have been printed.

Best regards,
Howard
 
When I add a MA to sheet 1 it appears automatically on all other sheets . I just want it to appear on sheet 1. Is there a way of doing this?
 
I have Premium Data NDU and has dividend dates. I'm using this code:

#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"

_SECTION_BEGIN("NorgateDataAllDividends");
normaldiv = NorgateAllDividendsTimeSeries(1); // 1 = Normal dividends only
specialdiv = NorgateAllDividendsTimeSeries(2); // 2 = Special dividends only

Plot( specialdiv, "Special Distribution", colorTeal , styleHistogram , Null, Null, 0, 1, -150 );
Plot( normaldiv, "Normal Distribution", colorYellow, styleHistogram , Null, Null, 0, 2,-50 );
_SECTION_END();

Any idea how to count the number of dividends issued between the dates set in an Exploration please?
s!Apcqg0CD5-aSiIxENNUxLhOcc0Bg_A
 
Very closely related to the above two posts, I want to count the number of bars as such:

At the current bar I want the value of that bars date and subtract a year (or any other calendar period down to months, such as 3 months) from it. eg. 2017-04-26 --> 2016-04-26
Then I want the number of bars between the two dates.

The link I posted above uses static dates but I'm too stupid to recode it to output an array. Any help appreciated!

The purpose of this is to sum the dividends over 1 calendar year. Also to find the true 1 year ROC. This must all be done using daily bars. I do not want to change and restore time frames.
 
Hello all

need some help here please! If I buy the Amiquote, is the EOD data for ASX or US based stocks usable and reliable? is it adjusted?
 
I was surprised because there was no feedback in regards to that one but just a move on to next "problem". So it is not about asking for help. But providing help is not a fool's job. So finding or getting some free code is not like finding a candy after every step taken in wonderland. So at least a "Thanks for taking the time. It works (or still not working)" is a minimum (always and no matter who is the "fool"). Perhaps you live in a different world but in my world things work differently.

So watch out or you may end up like James Woods

*end of the rant*





Hi Trash,

Today is my first day as a member and I have been reading a lot of random posts and I notice that you reply to most of them.

Thank you for being there to help naive or early ab users.

I truly appreciate the efforts you take to reply to even basic queries.

Thanks a bunch again.
 
Dear All,

This is Backtest Window. Here Price and Ex.Price are showing Candle Close Price.
I want to show here Correct Buy or Sell or Short or Cover Price Details.
Can any one help how to do this ?
 

Attachments

  • Backtest.png
    Backtest.png
    146.2 KB · Views: 21
The Buy, Sell, Short, Cover prices are set in Backtester Setttings from drop down menu. Then backtest column will show the correct price for whatever entry/exit.

Untitled.png
 
Top