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

RE: Exit and Re-Entry firing on the *same* bar



PureBytes Links

Trading Reference Links

Gary's suggestion should work but I prefer using the built-in 
MarketPosition functions using True/False variables for this purpose 
as illustrated below:

I am assuming in the following that you get some condition to initiate a buy signal and that you want to keep that buy signal active until it is filled. 

-----

Vars: BuyOK(FALSE), BuyCondition(FALSE);

<code to set BuyCondition>

if MarketPosition = 0 then begin
    if BuyCondition then BuyOK = TRUE;
end else
    BuyOK = FALSE;

if BuyOK then Buy next bar at XXX stop;

-----

To understand how this works, you need to understand what happens 
WITHIN a bar and what happens when your code is executed at the 
COMPLETION of each bar. Many people confuse these and assume their 
code is running WITHIN a bar. It is not. Logically, it is executed in 
the "instant" at the COMPLETION of each bar for that bar and before 
the next bar starts.

Initially MarketPosition = 0 so at the COMPLETION of the bar when 
your buy condition "BuyCondition" becomes true, your code will run 
and the variable "BuyOK" would be set to TRUE. On the COMPLETION of 
each subsequent bar, the Buy stop statement would again set the Buy 
stop active for the next bar until it is filled.

>  > Some time ago there was a post concerning how to prevent/preclude a
>  > System exiting and immediately re-entering
>  >  12/21/99   9:55am Buy    2  141^05  IJM -L
>  > *12/21/99  10:15am LExit  2  144^05  Profit Target $ 5.50  $ 38.88
>  > *12/21/99  10:15am Buy    2  144^05  IJM -L
>  >  12/21/99  10:25am LExit  2  143^05  Money Mngmt S $-2.50  $ 36.38

Using your example, the BuyCondition would be true at some point and 
begin executing Buy stop signals for subsequent bars. The buy signal 
issued at the COMPLETION of the 9:50am bar was filled WITHIN the 
9:55am bar, so at the COMPLETION of the 9:55am bar when your code 
executes, MarketPosition would equal 1. Therefore, the variable 
"BuyOK" would be set to FALSE when the code is executed at the 
COMPLETION of the 9:55am bar. This will disable further Buy signals 
beginning with the 10:00am bar.

On the 10:15am bar your profit target is hit and you exit WITHIN that 
bar and MarketPosition will be set to 0 WITHIN that bar. So when your 
code is executed at the COMPLETION of the 10:15am bar, MarketPosition 
will equal 0. At that time you need to make sure that the buy 
condition, "BuyCondition", is no longer true or you will again issue 
a buy signal for the next bar.

To accomplish this, you need write the buy condition, "BuyCondition", 
such that it is active for only one bar. For example the following 
condition can be true for many bars so would not be correct:

   BuyCondition = Value1 > Value2;

whereas the condition below would only be true for one bar:

   BuyCondition = Value1 crosses over Value2;

One common way to assure this is to require some condition on two 
consecutive bars such as:

   BuyCondition = Value1 >= Value2 and Value1[1] < Value2[1];

which is the same as "crosses over".

Notice that the MarketPosition function changes value WITHIN a bar 
and has the same value throughout the period when your code is 
executed at the COMPLETION of a bar. It does not change value 
while your code is being executed.

I think the above is correct and would welcome any corrections.

Bob Fulks







>  > Some time ago there was a post concerning how to prevent/preclude a
>  > System exiting and immediately re-entering
>  > 12/21/99  9:55am  Buy   2  141^05  IJM -L
>  > *12/21/99  10:15am LExit2  144^05  Profit Target $ 5.50	$ 38.88
>  > *12/21/99  10:15am Buy  2  144^05  IJM -L
>  > 12/21/99  10:25am LExit	2  143^05  Money Mngmt S $-2.50	$ 36.38
>
>I'd guess that your code continues to issue the "IJM -L" buy signal,
>even after you've gone long.  As long as you're long, and you don't
>allow multiple entries in the same direction, the extra buy signals
>are ignored.
>
>But as soon as you hit your profit target and exit, *poof* you're no
>longer long, and the "IJM -L" buy order (probably a stop?) can jump
>right in.  Instantly you're long again.
>
>Solution:  stop issuing the buy order when you're already long.  If
>you're using something like
>
>   var Bstop(0);
>   Bstop = mumble;
>   buy at Bstop stop;
>
>change it to something like this:
>
>   var Bstop(0);
>   Bstop = mumble;
>   if High > Bstop then Bstop = 0;  { Clear stop when it's hit }
>   if Bstop > 0 then buy at Bstop stop;
>
>Gary