Valuewhen exit problem

Hi, thanks in advance for any help.

I am trying to have my program sell when a trade has its first profitable close. I thought it was simple to code but I'm getting erroneous exits. I have used exploration to diagnose and not sure how I can simply my code further.

I have included the code below and a snapshot from my exploration. As you can see, on 8/15/2019 the close is below the buyprice, but some reason "firstprofit" is triggered and the program sells.

SetTradeDelays(0,0,0,0);
BuyPrice= C;
SellPrice = C; 

Buy = RSI(14) < 30; 
Firstprofit = C > ValueWhen(Buy, C, 1); 
Sell = firstprofit; 
Buy= ExRem(Buy, Sell);
Sell= ExRem(Sell, Buy);



Filter = Buy OR Sell; 
AddColumn(Buy, "Buy");
AddColumn(Sell, "Sell");
AddColumn(C, "C", 1.2);
AddColumn(ValueWhen(Buy, BuyPrice, 1), "BuyPX", 1.2); 
AddColumn(Firstprofit, "Firstprofit"); 
AddColumn(RSI(14), "RSI", 1.1); 

Untitled

The problem is most likely that you are getting several buy signals in a row before the exit triggers, so ValueWhen isn't returning the value you expect. You can easily check this by changing:

Filter = Buy OR Sell;

to

Filter = True;

Have you considered using ApplyStop() to exit on a 0% profit target? If that's not an option, you may need to write a loop to find the "real" (i.e. first) Buy signals and prices so that you trigger an exit at the correct time.

@mradtke Thank you - that does seem to be the problem. It's a bit surprising given I have exrem(buy, sell) in the code. I guess exrem only disregards the buy signals and doesn't remove them?

The applystop w/ 0% profit target is a good idea. Thanks again.

The ExRem doesn't remove the extra Buy signals until after you've generated your Sell signals. But the Sell signals are incorrect because you're not correctly identifying the Buy signals.

You could reduce the number of extra Buy signals by using Cross() instead of the less than operator, for example:

Buy = Cross (30, RSI(14));

However, that still doesn't guarantee that you won't get a second Buy signal before you encounter a profitable close.

As an observation, I find that strategies that only have exit rules that result in profit seldom work. You need to have some kind of contingency exit, like a time stop, for when trades don't work out. Otherwise, the backtest will just hold those positions forever, waiting for a profitable exit that never occurs, or takes an extremely long time to occur.

1 Like

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