stop loss that looks at closes and act at open of the day after

My trading system looks at the closes and trade the next day at open.
I have 2 condition to close the trade one of them is a stop loss calculated on the close price of today. If the loss is more than a certain percent, then tomorrow at the open I'm gonna close the position. I do not look a the minimum of today, only at the close price.

The code I'm actually using to do this is the following:

// Custom Stop Loss
b_price = Buy*O;
running_profit = 0; 

for(i=1; i<BarCount; i++)
	{
	if(Buy[i] == 0 AND Sell[i] == 0) {b_price[i] = b_price[i-1];}
	if(Buy[i] == 0 AND Sell[i] == 1) {b_price[i] = 0;}
	if(b_price[i] != 0) {running_profit[i] = (C[i] - b_price[i])/b_price[i]*100;}
	}

// SL:
SL = ref(running_profit,-1) < -SLpercent;
Sell = Sell OR SL;
Sell = ExRem(Sell,Buy);

for(i=1; i<BarCount; i++)
	{
	if(Buy[i] == 0 AND Sell[i] == 0) {b_price[i] = b_price[i-1];}
	if(Buy[i] == 0 AND Sell[i] == 1) {b_price[i] = 0;}
	if(b_price[i] != 0) {running_profit[i] = (C[i] - b_price[i])/b_price[i]*100;}
	else {running_profit[i] = 0;}
	}

This code is very inefficient and I'm wondering if there is a smarter way to achieve my task using the arrays or some specific AFL's commands.

I've considered to use the ApplyStop command but I can't find a way to fit it correctly:

  • scenario 3 is similar, but looks at the H-L prices instead of the close
  • scenario 4 is also similar, but closes at today's close instead of tomorrow open.

Thank's for helping me

Marco

1 Like

@2DD,

For posting questions on the forum, you are required to get "Verified".

Please search for "Verified" or "Verified Badge" and follow the steps.

@TrendSurfer,
I've just verified my account, thank you for the hint

Marco

1 Like

See Plot Trailing Stop,

You could modify that to suit, also search forum for other examples of 'Trailing Stop'.

Let us know if you need further assistance.

@2DD, if you are using End of Day data, then could you use the Trade Delay setting?

Hello Snoopy.pa30, thank you for your reply.
I actually manage the entry and exit signals looking at yesterday close and buy or selling at today open. I mean something like:

// this is only an example of the logic I've used

buy_indicator = MA(C,20);
sell_indicator = MA(C,200);

buy_signal = Cross(C, buy_indicator);
sell_signal = Cross(sell_indicator, C);

Buy = Ref(buysignal, -1);
Sell = Ref(sellsignal, -1);

// then I also need a stop loss based on the **close**
// pseudo code:
//  if [(Ref(C, -1) - trade_buy_price)*size] >= MaxLoss then Sell=1

Maybe I'm wrong, but I don't think that only changing the trade delays can help me so much, because the standard applystop function only looks at the maximum and minimum price (hence at the intraday drawdown); I need to look at the end of day drawdown instead.

Actually I don't know where the entry prices, the position size and the at_market variables are stored and if they can be retrieved in any way, so my way to work around this was to loop on the price series recreating the signals to build the entry_price and the at_market arrays to compute the end of day drawdown and finally the stop loss signals based on the close price.

Is there a smarter way to achieve this?
Marco

Hi TrendSurfer, many thanks for the hint.
Well, I'm actually doing it in that way really, so my code loops the price series from the beginning to the end calculating all the variables needed, but I've discovered that using the for loop is very time expensive if compared to array accessing. Seeing that the ApplyStop function is very fast, my first attempt was to use it, but I've noticed that the %loss is calculated on the L or H values. My system needs to calculate the loss ONLY at the end end of day, hence ignoring the intraday drawdown.

Do you have any idea on how to calculate the end of day profit without using loop structures?

Marco

Here is an example but in your case this is not applicable because you need 'For Loop' (to control your specific stop loss logic).

// Eample to calulate EOD profit (Daily Bars)
Buy = Sell = Short = Cover = 0;
BuyPrice = SellPrice = ShortPrice = CoverPrice = 0;

period = 20; // number of averaging periods 
m = MA(Close, period); // simple moving average
Buy = Cross(Close, m); // buy when close crosses ABOVE moving average
Sell = Cross(m, Close); // sell when closes crosses BELOW moving average

Buy = Ref(Buy, -1);
BuyPrice = Open;
entryPrice = ValueWhen(Buy, BuyPrice);
inTrade = Flip(Buy, Sell);
entryPrice = IIf(inTrade, entryPrice, Null);
EOD_profit_pct = IIf(inTrade, (Close - entryPrice) / entryPrice * 100, Null);

If none of the 'ApplyStop' options meet your criteria then use one master 'For Loop' as your overall controller.

Thank you TrendSurfer for the useful example.
So it finally seems that my code can't be optimized.
Thank you anyway (y)

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