Sell today at close or tomorrow at open depending on exit trigger

I have a strategy with 2 possible exit conditions.

  1. Sell on close today if security closes at a 5-day high.
  2. Sell tomorrow's open if a "foreign" security (which is really an outside signal) closes at a certain level

I cannot figure out how to tell Amibroker there are two possible exit times.

The security price-based exit can be achieved with a limit-on-close order, which is desirable in this case.

The "foreign" reading won't be known until a few hours after the market close. So selling "on close" today is not an option. Therefore, the foreign-signal sell must be at the open tomorrow. I am not far with the code, but here is what I am looking at...

OutsideSignal = Foreign("OS1","C", 1);  

Sell = C == HHV(C, 5) ;

///stumped on how to write "If outsidesignal < 1 then sell at market on open tomorrow" without messing up the
////"on close" for today...

Thoughts / suggestions appreciated.


How about something like this:

OutsideSignal = Foreign("OS1","C", 1); 
isOutsideSell = Ref(OutsideSignal < 1, -1);
Sell = C == HHV(C, 5) OR isOutsideSell;
SellPrice = IIf(isOutsideSell, O, C);
1 Like

Thanks! That appears to solve it nicely.

Hi mradtke,

would the code below not sell onOpen or onClose in the same period?

SellPrice = IIf(isOutsideSell, O, C);

I have the same issue and tried to reference the next bar open (for backtesting purposes only).

SellPrice = IIf(isOutsideSell, Ref(O,1), C);

But I doesn't seem to work. In case Ref(O,1) < L, Amibroker uses the low of the current period.
Do you have any idea how to solve this issue?


You need to consider three things:

  1. When is the Sell signal generated? (this is controlled by the Sell array)
  2. When is the Sell signal executed? (this is controlled by your trade delays)
  3. At what price does the exit take place? (this is controlled by the SellPrice array)

It would be much easier to offer advice on your problem if you gave a specific example of what you're trying to achieve and provided your code.