After setting a range and exploring/scan. How can I make the output to display the last signal.
On the attached image. There are 5 signals. When I run scan it will output all 5 signals. How do I make the output display only the last signal.. Below is the code
@stanleyayochok There are several useful Filter settings for Explorations. If you are testing on one symbol and just want to see the most recent result, try and see if this is what you are after,
Filter = Status("LastBarInRange");
And I must admit you have me confused with your line
Last bar in AmiBroker talk simply refers to very last bar of (partial/entire) array most of the time (see LastValue(), Status( “lastbarinrange” ), …). And first is not last anyway (except for seldom cases of first one being the only one). Please take your time to formulate a post first not leaving room for confusion and guessing.
If you would have written “filter by most recent occurred Buy or Sell” then it would have been much clearer than all your pictures and posts above. “Most recent occurrence” would refer to some element having occurred either at last bar or at any bar before very last bar.
So now since you seem to be looking for most recent one you can do like so for example:
ATTENTION: this is a code snippet not a complete code! You have to add Buy/Sell rules (and optionally some additional exploration output).
/// @link http://forum.amibroker.com/t/how-to-make-my-scan-exploration-to-display-only-the-last-signal-on-a-range/2243/6
/// code SNIPPET to look up (filter) some most recent occurrences
// ....
// your Buy/Sell rules above
// comment or uncomment below two lines
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
dt = DateTime();
nth_recent = 1;// 1 == most recent, 2 == second to most recent, etc...
// lookup recent historical buy/sell date
Buy_dt = Lookup(dt, LastValue(ValueWhen(Buy, dt, nth_recent)));
Sell_dt = Lookup(dt, LastValue(ValueWhen(Sell, dt, nth_recent)));
Filter = dt == Buy_dt OR dt == Sell_dt;
// further code for Exploration output below
// ....
And as always this code is not for selling. Use your own brain.
It will output if there is either a Buy or Sell Signal at last bar of set date range! That’s what the line says.
Note though that Lookup() function in upper code snippet is not required. It got pasted by accident, recognized it too late to edit as time to edit was gone already and didn’t care to write a 2nd post as it is not wrong but just overkill.
But anyway… here is what I mean
(same output as with upper snippet)
/// @link http://forum.amibroker.com/t/how-to-make-my-scan-exploration-to-display-only-the-last-signal-on-a-range/2243/8
/// code SNIPPET to look up (filter) some most recent occurrences
// ....
// your Buy/Sell rules above
// comment or uncomment below two lines
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
dt = DateTime();
nth_recent = 1;// 1 == most recent, 2 == second to most recent, etc...
// lookup recent historical buy/sell date
Buy_dt_check = dt == LastValue(ValueWhen(Buy, dt, nth_recent));
Sell_dt_check = dt == LastValue(ValueWhen(Sell, dt, nth_recent));
Filter = Buy_dt_check OR Sell_dt_check;
// further code for Exploration output below
// ....
Hello,
So working on a similar model, i can up to this post in my search.
For my model, i want to flag the signals which are in a particular sequence.
If the current signal is a Buy, it is valid / flagged only if the last signal OR the second last signal is also a Buy. Similar for Sell.
Also in my code, i am not using the ExRem function, thus there are consecutive Buy and Sell signals.
I tried different code snips to get this output, but i fail to get the right one. Kindly someone suggest the right way.
@Maxis I think that the reason that you may not have received any answers is that without your Buy and Sell signal codes, any forum user is going to have difficulty solving the problem. If I understand your requirements, you will take signals if 2 out of the most recent 3 signals were in the same direction (Buys vs Sells).
I have a solution that works for some dummy code I put together. If your signals do not generate Buys and Sells in a similar manner then this code may not help you (but it would also imply that your coding is wrong).
BuySignal = // your secret code;
SellSignal = // your secret code;
SignalFlag = BuySignal OR SellSignal;
// this example will deal with the Buy only, you can use the same logic for Sells
TwoInRow = IIf( ValueWhen( SignalFlag, BuySignal, 2 ) == 1 AND BuySignal, 1, 0 ); // two in a row
TwoOutof3 = IIf( ValueWhen( SignalFlag, BuySignal, 3 ) == 1 AND BuySignal, 1, 0 ); // 2 of past 3
Buy = TwoInRow or TwoOutof3;
As with most solutions in AmiBroker you can achieve the same result using different approaches. I think it can also be solved with looping code but I am not sure it is any more efficient.
That is what is so efficient about Amibroker. For all my learning curve for coding (and my limited knowledge thereof), i was under the impression that it would take some fuzzy kind of coding logic to get to my desired output.
Thank You PB for the snippet, works perfect as desired.
As for Buy & Sell logic itself, i was just following the thread's theme. But a simple "5 bar break out" logic would suffice here.
Grateful to the Fourm for all the support i get. Regards
@Maxis you are welcome. It was late last night when I posted but I soon realized that we could simplify the code a bit (but I was too lazy to make the change). I think that these two lines
TwoInRow = IIf( ValueWhen( SignalFlag, BuySignal, 2 ) == 1 AND BuySignal, 1, 0 ); // two in a row
TwoOutof3 = IIf( ValueWhen( SignalFlag, BuySignal, 3 ) == 1 AND BuySignal, 1, 0 ); // 2 of past 3
are the equivalent of these (don't require the IIF()) as these line should generate a True (1) or a False (0) just like the above Immediate If codes
TwoInRow = ValueWhen( SignalFlag, BuySignal, 2 ) == 1 AND BuySignal; // two in a row
TwoOutof3 = ValueWhen( SignalFlag, BuySignal, 3 ) == 1 AND BuySignal; // 2 of past 3