ApplyStop on foreign instrument

My afl is using two instruments with the traded instrument (like ES) as foreign instrument. Is it possible to use applystop for the price levels on foreign instrument? If you have a alternative solution then please feel free to suggest.

I got it.

SetForeign (ForeignTicker, True, True);


This is WRONG.

You should not do that. Instead read the Knowledge Base
Use Foreign to reference data for calculations but NOT TRADING.
Trading should be on the “current” symbol. So what you are doing is up-side down.
You should do this other way round.

If you have two instruments to trade you should use “APPLY TO: FILTER” and define watch list with these TWO INSTRUMENTS.

All described here:

1 Like

I understand but I changed it upside down to fix the ‘holes’ issue in traded instrument. The holes in the traded instrument was causing missing signals as the foreign instrument which i use as the base instrument triggered a signal but since the primary instrument didn’t have a tick it was missed. I read the kb article My issue is mentioned in that article

If the current ticker has no data for a particular bar, the corresponding bar-interval for the foreign array will not be displayed and will not be available for calculations. This blocks foreign data and could result in missed signals.

This article suggests that i create a composite and use that as the primary ticker. If that is true then are we not using a another instrument (composite) as the primary instrument?

You got it wrong.

Holes are fixed by turning ON "Pad and align to reference symbol" option see Users Manual


That is the ONLY proper way to handle holes and actually SIMPLEST ONE - just turn it ON and type reference ticker. DONE.

BTW: The article you read comes from USERS' kb, which is UNofficial community driven, written by USERS, not us. It contains bits of information coming from year 2007, opinions, etc collected from various places. The quality of articles there is mixed.
It mentions "Pad and align" first but then goes to great lengths of discussion that is unnecessary and (what's worse) confusing as "Pad and align" just solves the issue with ONE CLICK.

Your case suggest that I will have to add big red label that Users kb is NOT authoritative source of information.

Official Knowledge Base written by us is here: and that is authoritative source of information plus Users Guide

Will turning on “pad and align” fix holes in primary instrument in real time trading? I tried that but it didn’t solve the issue. This is only a backtester fix in my opinion.

Could you please respond?

No it is not “only backtester”. Any code (scan, exploration, backtest, optimization) that is run from Analysis windows uses this padding. So if you run the code from Analysis it gets padding. Analysis supports “Run every” feature so you don’t need to click buttons to run it

BTW: I had to sleep sometimes, you know, so I did no answer earlier (it was 1:00 AM midddle of the night when you asked your question)

Sorry i was not aware of your timezone. Is there a way to use this feature in chart? Most people use the chart for visualization during rt trading hence it will be immensley useful to have it.

@Jefforey the "Pad and Align’ function uses a “base” or “primary” instrument as its guide. Only the data feed of the primary is used. If there are holes in the primary, there are holes. Nothing can fix that.

The pad and align “fixes” the foreign instrument to have data everywhere the primary has data.

I have to disagree with you. I think ‘pad & align’ will use the reference instrument (whatever is set on the backtester settings) as the base instrument. It should be able to fix holes in the primary instrument otherwise it wud be useless. If it’s your only concern to fix holes in the foreign instrument then you don’t need pad & align as that is done automatically (setforeign fixup)

I would just like to suggest that you add the ability for the charts to visualise the ‘pad & align’ function. You already have the feature and all that needs to be done is to add the ‘padded’ bars in the chart.

The functionality exists:

  1. Activate chart you want to use (click)

  2. Select "YourReference" ticker in the symbol field


  3. In your formula on the top use SetForeign("OtherTicker")

It is equivalent of running Analysis on "OtherTicker" when "YourReference" is selected as "Pad and align" reference ticker. You will get same padding and alignment that way.

Note that in ORIGINAL question you asked about ApplyStop. ApplyStop is NOT a chart feature. It is BACKTESTER feature. That is why I was talking about backtesting in replies.

That does not solve the problem. My afl uses 2 instruments. One is the index (reference) and other is the future (primary chart instrument). Holes in future blocks the reference instrument data for the corresponding bar/tick. Holes in future are to be fixed, not the otherway round. Holes in reference are fixed automatically (see foreign fixup). Adding setForeign() on top of afl does not solve this problem.

Actually i had posted 2 different questions/threads one on applystop and another on foreign instrument values in aa. You had closed my second thread and clubed it with this and thats why the confusion. Also i don’t thing applystop is purely a backtester feature because a sell/cover caused by applyStop can be visualised in the chart.



See the difference. There is 7 second delay caused by holes in the ES data. But if pad and align is used then this problem can be fixed. But pad and align is purely a backtester/aa feature. The question is : is there a way to visualize 'Pad & Align' on the charts?

It IS purely backtester feature. ApplyStop has NO other effect than telling backtester to apply stops when doing backtest. It was explained many times on the forum.

Also I already answered providing you with the code that does precisely THE SAME THING as “pad and align” in Analysis. What you choose as a reference is your choice and it can be done one way or another. In fact you can even choose third symbol as reference. Or even have synthetically generated hole-free symbol. You can pick whatever reference you want. It “just works” so I really don’t know what your problem is.

Also I don’t know what interval and data source you are using but those timestamps that you have look very suspicious to me. What interval is that ? Is that 1 second data or? And what data source is that?

Yes it is 1 second data from I am very confused right now. I will do some work and post back with examples. I don’t know why you say applyStop is backtester-only. Maybe i am mis-interpreting your words. As far as i can see applyStop produces a sell/cover signal. Anyway i will get back to you on this one.

As I wrote ApplyStop() is backtester only. It does not produce sell/cover signals by itself and it does not return any value. It is mere switch for backtester.

What can produce signals is the (old, single-symbol) backtester-in-a-box function called Equity(). See
If you call Equity(1) you are invoking IN-PLACE single-symbol backtest in that very line. If you pass 1 you are telling single symbol backtester-in-a-box to write back signals generated by stops to sell/cover arrays.

So you are likely to be calling Equity(1) and that is the in-line single-symbol backtester.

Yes i use Equity(1) and use the stops generated by applystop to sell/cover.

I’m having lot of trouble using ApplyStop() in Primary and Foreign instruments. My logic is to stop the trade whenever the stop level is hit in either the primary or the foreign instrument. Whichever comes first. It works fine if there is only one instrument but the moment i use foreign it is giving wrong result. It is stopping out the trade early. I would be grateful if someone can look into this. I am posting here as a last resort after banging my head for a 3 days. :slight_smile:

if (StrToLower(isDifferentBaseInstrument)=="yes"){
SetForeign(BaseInstrumentTicker, True, true);
ApplyStop(stopTypeLoss, stopMode1Num, 0, 1);
ApplyStop(stopTypeLoss, stopMode2Num, stopLossInPointsBI, 1);
ApplyStop(stopTypeLoss, stopMode1Num, 0, 1);
ApplyStop(stopTypeLoss, stopMode2Num, stopLossInPointsTI, 1);