First instance of future condition

How do I find the first instance of hitting a stop loss in the future, e.g. how do I find the first future instance where price will cross a barrier?

Thank you

I just do it manually but there are functions like AFL Function Reference - APPLYSTOP that does it for you. Couldn't work out how that did a straight line stop, loop is straightforward for me to understand.

This is not copy/paste working code, it's just to give you an idea. Sorry.

	// SEARCH FOR STOP
	// from ptrEntry, +1 bar, look for a stop
	foundExit = 0;	
	for (j = ptrEntry + 1; j < BarCount - 1; j++)
	{
		if (Low[j] < stopPrice)
		{
			ptrExit = j;
			/* TRADE: On the bar that a limit order is triggered, if the price is lower than the stop
			, then the order will be closed on the open. */			
			ptrBracket[i][enumBracketExitPrice] = Min(Open[j], stopPrice);
			ptrBracket[i][enumBracketExitTime] = dt[j];

			// util_drawLabeledLine("Stopped", j, stopPrice, -1*(j-ptrEntry), colorDarkRed);
			foundExit = 1;
			break;
		}

		if (High[j] > profitPrice)
		{
			ptrExit = j;
			/* TRADE: On the bar that a limit order is triggered, if the price is higher than the take
			profit, then the order will be closed on the open. */
			ptrBracket[i][enumBracketExitPrice] = Max(Open[j], profitPrice);
			ptrBracket[i][enumBracketExitTime] = dt[j];

			// util_drawLabeledLine("Profit", j, profitPrice, -1*(j-ptrEntry), colorGreen);
			foundExit = 1;
			break;
		}
	}

	return foundExit;
1 Like

This also seems highly relevant, have you tried searching the forum?

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.