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

Re: wait function



PureBytes Links

Trading Reference Links

> I am really struggling (again) with code to say wait N days if stopped
> to enter a new position on same side of the market.  Is there a sample
> of this code somewhere that someone knows about? Chris E.

Seems simple enough.  How about something like this (untested):

--------------------------------------------------------------------
{function: OK_to_trade - series function
 Must be called on every bar.
 Returns True if it's been at least N bars since the end of the
 last trade in the Direction specified.}

inputs:
    N(NumericSimple),         {number of days to wait}
    direction(NumericSimple); {direction to test 1=long, -1=short}

vars: tt(0), mp(0), BarsSinceLong(0), BarsSinceShort(0);

tt = TotalTrades;     {number of closed trades}
mp = MarketPosition;  {current market position}

if tt > tt[1] then
    if mp[1] > 0 then BarsSinceLong = 0 else BarsSinceShort = 0;

BarsSinceLong = BarsSinceLong + 1;   {bars since end of last long trade}
BarsSinceShort = BarsSinceShort + 1; {bars since end of last short trade}

if (direction > 0 and BarsSinceLong >= N) or
   (direction < 0 and BarsSinceShort >= N) then
    OK_to_trade = true
else
    OK_to_trade = false;
--------------------------------------------------------------------

Your specification "wait N days if stopped to enter a new position on same
side of the market" is kind of ambiguous.  Stopped out at a loss?  Does it
matter if the exit was due to stopping out or reaching a target profit?

The code above doesn't regard whether the previous trade was profitable or
not; only that the previous trade was long or short and ended at least N
days ago.  If you want to specify that the previous trade to test must
have been a loss, then you need to modify the first 'if' statement to test
for PositionProfit(1), like:

if tt > tt[1] and PositionProfit(1) < 0 then
    if mp[1] > 0 then BarsSinceLong = 0 else BarsSinceShort = 0;

-Alex