ApplyStop on foreign instrument


The code you wrote is wrong. It is wrong in the basic principle/core.
This does NOT work that way. As explained many times ApplyStop does not implement stops inside the function call. It is just a SWITCH that tells BACKTESTER to apply stop at the BACKTEST time, i.e. NOT when your formula executes but AFTER when actual backtesting procedure happens.
Therefore ApplyStop applies ALWAYS to currently backtested instrument and NOT to any other instrument that you might called in your SetForeign.

Multiple calls to ApplyStop with same type have NO effect, other than the LAST ONE applies, because as I wrote, it is just a SWITCH for the backtest (as in the settings). The fact that you turn the switch on and off ten times does not have any effect other than the FINAL setting.

To apply stop to OTHER symbol, you have to run MULTIPLE SYMBOL BACKTEST and use the code like this:

if( Name() == "SYMBOL1" )
 // rules and stop FOR SYMBOL 1
  Buy =
  Sell = 

if( Name() == "SYMBOL2" )
 // rules and stop FOR SYMBOL 2
  Buy =
  Sell = 

See also this:

You really need to carefully read this. You should mimick exactly what is written in the KB.


Thanks Tomasz. I was really confused with applyStop there. I thought if i use Foreign(), applyStop followed by Equity () then i could get the stops in the foreign instrument as well. But now that is all cleared up.

I want to ask you the real purpose of appyStop(). I could not find any document that describes the true purpose and implementation of applyStop(). I will tell you my understanding. Please correct me if I am wrong. I think applyStop is the only way to stop-out trades that have buyprice/shortprice based stoploss like buyprice minus 2 points. If i use the regular sell like

//sell when price reaches 2 points below BuyPrice of the last buy signal sell=cross(valueWhen(buy, buyPrice) - 2, L);

then this may not work because there may be many buy signals (which will be exremed later) after the first buy signal and each one of them my have a different buyprice which means different stoplevel. Whichever stop gets hit first shall remain and the rest will be exremed. But the stop that got hit may not be the one based on the first buyprice. applyStop is the only workaround this. well applystop or loops. Is my understanding correct. I must admit that still I don’t have a good understanding of applystop() and how it is actually implemented.


care to respond to y question on ApplyStop() and its implementation?


I already explained absolutely everything in my previous replies. Also there is plenty of documentation in the documentation and in the Knowledge Base (search for ApplyStop).


Ok, I re-read all your replies and also read all KB articles about applyStop but I am still confused. I understand that applyStop is a backtester feature and it could be used along with equity() to write the stop signal into sell/cover array.
But I am still confused as to how to use a stop-level that is dependent on BuyPrice i.e (BuyPrice - 2 points). How should i write the sell statement for this stop. Should i write

sell=cross(ValueWhen(Buy, buyprice)-2, L);

This will be incorrect because there could be many buy signals after the first buy signal and hence there could be many buyprice(s). The sell condition will be true when highest (buyprice-2) level gets hit and also at other (buyprice-2) levels. Later when we exrem except the first Buy and the first Sell everything else will be removed. And when that happens our trade would have exited prematurely.


Having watched this conversation for awhile. I get the sense you might be overthinking Applystop. As @Tomasz stated in the documentation, it is a switch for backtesting that returns nothing. And is useful for most kinds of trading exit situations in a backtest. To factor in a foreign symbol the link in Tomasz’s answer above has all that is needed. Also, if you haven’t already, study this Including the Limitations, Examples and Comments at the bottom.

The food has been provided, but it cannot be digested for you.


Ok, point taken. I will keep exploring.