I really though I had the basics of AFL nailed at this point, but obviously not. I have a system of 15 strategies that I trade, but I found some weird behavior in it yesterday. I have cut out the core of the problem in the code below, and I'm pretty sure it´s related to returning an array from the function "evaluate_strat1". When retuning a bool, it works differently.
I have a mix of array processing first, then I loop though, because I need to send alerts on the last bar. So I know the structure of the code looks overly complicated, but this is just a snippet of the whole thing.
Can anyone spot why the WriteIf()-function doesn't trigger as expected? Isn't it possible to use arrays in this way?
function evaluate_strat1() {
ret = DayOfWeek()==1;
return ret; // <---- **This does not trigger the WriteIf function below**
//return True; // <---- **This triggers the WriteIf function below**
}
procedure setup_entry_arrays() {
global buy_strat;
buy_strat = evaluate_strat1();
strat_buy_str = WriteIf(buy_strat, strat_buy_str+":STRAT1",strat_buy_str);
}
procedure generate_signals() {
for (i = 0; i < BarCount; i++) {
if ( (buy_strat[i])) {
Buy[i] = True;
}
}
}
setup_entry_arrays();
generate_signals();
It is not clear to me the purpose of the code, but if you need to display in the exploration results different strings (based on an array value) you should use the AddMultiTextColumn().
The point is that I want to produce a string that I then can send as a telegram notification to myself. I want to know the tickers and the strategies to buy. But if WriteIf only uses the SelectedValue, that will of course not work.
Do you have a suggestion how to produce a string with the names of the strategies that triggers, without looping?
I still don't understand the goal of the WriteIf...
However in the post you say "I need to send alerts on the last bar."
In this case, perhaps, you could modify the evaluate_stratx() code to get a single value to use in the the WriteIf function: if you need to capture multiple recent signals use either the combination LastValue(Sum(signalsArray)) or LastValue(Cum(signalsArray)) in case you need to examine all bars; if the result is greater than zero then there are signals to be processed (and possibly send further alerts for each individual signal within the generate_signals() loop).
strat_buy_str is not defined anywhere. It's hard to help without all the code.
Since you want to use strat_buy_str for an alert, you only need to evaluate the last value of buy_strat, not the array: strat_buy_str = WriteIf( LastValue( buy_strat) , strat_buy_str+":STRAT1",strat_buy_str);
You don't need the loop at all, because if you only care about the last bar for alerting, you can just use LastValue( buy_strat ).
For Telegram alerts, I just want the last bar. But I also do exploration runs for the last week, and then I'd like to see what strategy triggered what day during the week. So I use that string to keep the names of the strategies that I then write in columns in the Analysis Window.
But it sounds to me that strings and arrays don´t really mix well, so maybe it´s better that I write everything in loops, given my limited skills in AFL?
For the exploration, you would need a loop. Inside the loop, if the current bar has a signal, then use AddRows() to output to the exploration window. That's probably the simplest way to go.