[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Avoid reentering



PureBytes Links

Trading Reference Links

At 2:17 PM +0100 1/7/03, Scorpio  Massimiliano wrote:

>I need a little help in coding.
>
>I have a strategy working this way:
>
>If the short moving average is above the long one I buy the market, the
>opposite for selling.
>If I want to use a trailing stop or profit taking and I exit from the
>market, if the condition is still true, I immediatly re-enter in the same
>position that I just closed.
>
>Is there a way to avoid this problem and if I'm stopped either in profit or
>loss and the condition is still the same I do not enter in the same position
>again ?

If I understand the problem correctly:

   if ShortAve > LongAve then Buy...
   if ShortAve < LongAve then Sell...

   if MyProfit > XXX then ExitLong...
   if MyProfit > XXX then ExitShort...

This code will reenter after the profit target is hit since the conditions for reentry will still be true.

It can be fixed by using "crosses over" and "crosses under":

   if ShortAve crosses over LongAve then Buy...
   if ShortAve crosses under LongAve then Sell...

   if MyProfit > XXX then ExitLong...
   if MyProfit > XXX then ExitShort...

This code enters long when the short average crosses over the long average as before but will not reenter again after the profit target exit.

Bob Fulks