- Joined
- 30 June 2007
- Posts
- 7,200
- Reactions
- 1,226
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 very much Howard.Using the ValueWhen function might be easier.
The key is this line:
SellLowClose = C < ValueWhen( Buy, L );
Lone Wolf's observation is correct. Always be careful.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
Hi GB --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 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
x[i] = <arguments>;
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.
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*
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?