How to make my scan/exploration to display only the last signal on a range

Good day,

This is my first time to post, newbie on coding.

After setting a range and exploring/scan. How can I make the output to display the last signal.image
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

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


Filter=Buy OR Sell;
SetOption("NoDefaultColumns", True );
AddColumn( DateTime(), "Date", formatDateTime );
AddColumn( IIf( Buy, 66, 83 ), "Signal", formatChar );
addcolumn( Close, "Close price", 1.4 );

@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

AddColumn( IIf( Buy, 66, 83 ), "Signal", formatChar );

Which I thought would be this instead,

AddColumn( IIf( Buy, 'B', 'S' ), "Signal", formatChar );

No confusion required as it is just from ASCII code table.
https://msdn.microsoft.com/en-us/library/60ecse8t(v=vs.80).aspx
https://msdn.microsoft.com/en-us/library/4z4t9ed1(v=vs.80).aspx

'B' 

is type number also

Check out in Interpretation window

x = 'B';

printf( "'B' is equal to %g and is of type: %s", x, typeof( x ) );

There are three options:

  1. using ASCII codes as from the links above
  2. using single character literals such as
'B'

or

'S'

etc.

  1. or using Asc() function i.e.
AddColumn( IIf( Buy, Asc("B"), Asc("S")), "Signal", formatChar );

As for the first post

For example you may use Filter such as this one

Filter = Status("LastBarInRange") AND (Buy OR Sell);
2 Likes

@fxshrat and @portfoliobuilder thank you for replying on my post.
I would like to explain more on my above post. refer to image below:
image

Tried the

This insance it will not output a value, I noticed that there will be an output if the "First buy signal" occurs on the last bar that is a buy signal.
image

1 Like

“First, last, last bar, …”

What?

Please work on your communication!

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.

2 Likes

@fxshrat my apologies for not explaining properly. I tried the code above and its working. I appreciate the detailed explanation. Thanks.

No problem.

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
// ....
3 Likes

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. :slight_smile:

@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.
image

Hope that helps you get started. Good luck.

5 Likes

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 :slight_smile:

3 Likes

@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
1 Like

@portfoliobuilder Noted and simplified. Thank You, Regards, :slight_smile: