Hi Steve,

I am fairly new here but I have been reading this thread between you and
Mike. I wrote some fairly simple code that might help you with your
original problem. I have used the ApplyStop function in my projects and
I think this code will do what you want to do. Your original questions
were:

"What do I need to do to make sure:

1. All buys and sells are done as CLOSE+1 (I only use EOD data for
mutual
funds).
2. Make sure stops are triggered based on the purchase price, not the
price on
the -1 signal date."

I tested my code below on AAPL. It generates just 1 signal (arbitrarily)
on the night of 5/4/2010 to buy, which then executes the next day's
Close on 5/5/2010.

My code only sells via the ApplyStop function. The key setting you need
is SetOption( "ActivateStopsImmediately", True). This will make it so
that you exit at the Close of the day the stoploss was actually hit,
rather than the next day. This combined with the "2" setting for the
ExitAtStop parameter in the ApplyStop function will make it so that we
get stopped out on 5/6/2010, the day after we entered. (For testing I
forced it to get stopped out immediately with a tight 0.5% stoploss.)

Try applying this to just one symbol and backtesting. Then right click
on the one trade and choose "show arrows for actual trades". On the
chart you will then see the entry and exit as you desired I believe.

So to summarize, with the settings below, the trade enters the Close of
the day after the signal, and the stoploss is triggered right away, but
the trade price of the stop exit is still the Close of that bar (rather
than the stoploss price.) Hope this helps.

-Paul

//these settings here override anything you might set in the Settings
button area
BuyPrice = Close;
SellPrice = Close;
ShortPrice = Close;
CoverPrice = Close;

//all entries are made the NEXT day's close;
//all exits are made TODAY'S Close; (I don't think this is applicable to
the stoplosses though)
//again these settings override "Settings Button" settings of automatic
analysis window
SetTradeDelays(1,0,1,0);

//if set to true, it means that stops are activated right after
execution of entry signals
//once again, this overrides the equilvalent setting in the automatic
analysis Settings Button dialog
SetOption( "ActivateStopsImmediately", True);

//dummy test buy signal generated on night of 5/4/2010; executed on
close of 5/5/2010
Buy = Month()==5 AND Day()==4 AND Year()==2010;

//never sell normally (only sell with stoploss below).
Sell = 0;

//from userguide: "2: check High-Low prices but exit NEXT BAR on regular
trade price."
//however, if you have ActivateStopsImmediately set to true, it will
actually exit THIS BAR
//(the bar stoploss was penetrated)
ExitAtStop = 2;

SLpercent = 0.5;    //0.5% stoploss
ApplyStop(stopTypeLoss, stopModePercent, SLpercent, ExitAtStop, False,
0);



--- In [email protected], "graphman27" <st...@...> wrote:
>
> I use Add artificial bar so that I can see Tuesday's signals. 
Besides, I don't actually run the backtester for signals at night...I do
it the next day.  It seems to work fine, I'm just frustrated that the
stops don't calculate properly within the backtester.  I'll try some of
these solutions and/or hire someone to program that STOP section for me.
Otherwise, I'll spend too much time on it myself.
>
> Thanks!
>
> Steve.
>
> --- In [email protected], "Mike" sfclimbers@ wrote:
> >
> > I believe that it will solve your signal price problem. I don't
actually use ApplyStop, so my comments are based on what I've read
rather than what I've used.
> >
> > If you rely on the backtester to generate trades, the code provided
may not work, since you will need to already have a bar for Tuesday in
order to see the trade based on Monday's signal. Perhaps a snapshot
prior to Tuesday's close.
> >
> > An exploration would work, since you would have the Monday data and
could search on BuyTrigger instead of Buy, as indicated in my last
reply. You could probably write a master exploration that #include each
of the multiple rule sets.
> >
> > Worse case scenario; remove the ApplyStop functionality from your
existing script and keep running it in the backtester for signal
generation. Just be sure to use the revised one for actual backtesting 
when doing system analysis.
> >
> > Mike
> >
> > --- In [email protected], "graphman27" <steve@> wrote:
> > >
> > > You are the man again Mike.  I will make these changes and give it
a test.
> > >
> > > When you say trade intraday, I just wanted to remind you that I
only trade mutual funds.
> > >
> > > Will this acutally fix the problem of the stop levels using the
signal date price and not the purchase date pirce, which is CLOSE+1?
> > >
> > > Lastly, I think we've gone through this before, but I use the
backtester to get live signals.  I only trade around 12 symbols and they
are typically within the portfolio backtester.  An example would be a
portfolio with 6 different signals for 6 different symbols.  It seems to
be the easiest and fastest way to get the signals each day.  Otherwise,
I would have to run 12-15 different individual symbols, instead of 3
portfolios.
> > >
> > > --- In [email protected], "Mike" <sfclimbers@> wrote:
> > > >
> > > > Steve,
> > > > In response to both this post and your prior one:
> > > > 1.  Ref(Array, -1)  means "what was the value of Array on the
bar
> > > > previous to this one?".2.  BuyPrice = Close means "set the
purchase
> > > > price to this bar's close".
> > > > On Monday you are getting a signal. On Tuesday you want to trade
that
> > > > signal using Tuesday's Close. That is easily modeled using the
two
> > > > points above:
> > > > BuyTrigger = ...;Buy = Ref(BuyTrigger, -1);  // 1. If Monday had
a
> > > > signal, then buy it today (Tuesday).BuyPrice = Close; // Use
today's
> > > > (Tuesday) Close as the purchase price.
> > > > This sets you up perfectly for Scenario 2 of ApplyStop which
says that
> > > > you trade on today's Close (Tuesday) and want to exit intraday
from that
> > > > point on (i.e. you have a stop loss order with your broker that
will
> > > > stop you out anytime the price falls below your stated max
loss). The
> > > > ApplyStop is independent of your Sell logic. Your Sell will
still exit
> > > > at the Close, but your stop may exit intraday.
> > > > Completing your code sample gives the following:
> > > > /*
> > > > Scenario 2:
> > > > You trade on today's close and want to exit intraday on stop
price
> > > >
> > > > Correct settings:
> > > > A. ActivateStopsImmediately turned OFF
> > > > B. ExitAtStop = 1
> > > > C. Trade delays set to zero
> > > > D. Trade price set to close
> > > > */
> > > >
> > > > SetOption("ActivateStopsImmediately", false);  // A.
> > > > ActivateStopsImmediately turned OFF
> > > > SetTradeDelays(0, 0, 0, 0);  // C. Trade delays set to zero
> > > >
> > > > BuyTrigger =  Cross(StochFinal, Trigger) AND (EMA(Close,
EMAShort) >
> > > > EMA(Close, EMALong));
> > > > Buy = Ref(BuyTrigger, -1);
> > > > BuyPrice = Close;  // D. Trade price set to close
> > > >
> > > > SellTrigger =  Cross(Trigger, StochFinal) AND (EMA(Close,
EMAShort) <
> > > > EMA(Close, EMALong));
> > > > Sell = Ref(SellTrigger, -1);
> > > > SellPrice = Close;  // D. Trade price set to close
> > > >
> > > > ApplyStop(   stopTypeLoss,
> > > >           stopModePercent,
> > > >           Optimize("max. loss stop level", 2, 2, 10, 2),
> > > >           1,  // B. ExitAtStop = 1
> > > >           False);
> > > >
> > > >
> > > > I can't fully test your code since your assignments for
StochFinal and
> > > > Trigger were not provided. But, I believe that the above will
solve your
> > > > problem.
> > > > If you want to run an Exploration each night to see what you
should be
> > > > trading the next day, just use AddColumn to output the values of
> > > > BuyTrigger and SellTrigger instead of Buy and Sell respectively.
> > > > Mike
> > > >
> > > > --- In [email protected], "graphman27" <steve@> wrote:
> > > > >
> > > > > I want to simplify my life, believe me.  I'm having a bit of
> > > > difficulty following what you're saying to do.  Here is some
sample
> > > > code, just in case I'm not being clear:
> > > > >
> > > > > Buy = Cross(StochFinal,Trigger) AND (EMA( Close,EMAShort ) >
EMA(
> > > > Close,EMALong ));
> > > > >
> > > > > Sell = Cross(Trigger,StochFinal) AND (EMA( Close,EMAShort ) <
EMA(
> > > > Close,EMALong ));
> > > > >
> > > > > Plot( EMA ( Close,2 ),"Short EMA", colorGreen, styleThick );
> > > > > Plot( EMA ( Close,10 ),"Long EMA", colorRed, styleThick );
> > > > > Plot( StochFinal ,"Period", colorGreen, styleThick );
> > > > >
> > > > > /* max loss stop optimization */
> > > > >
> > > > > ApplyStop(stopTypeLoss,
> > > > >          stopModePercent,
> > > > >          Optimize( "max. loss stop level", 2, 2, 10, 2 ),
> > > > >    2,
> > > > >          False );
> > > > >
> > > > > What do I need to do to make sure:
> > > > >
> > > > > 1.  All buys and sells are done as CLOSE+1 (I only use EOD
data for
> > > > mutual funds).
> > > > > 2.  Make sure stops are triggered based on the purchase price,
not the
> > > > price on the -1 signal date.
> > > > >
> > > > > Right now, under settings, I have Buys and Sells set to CLOSE
+1.
> > > > >
> > > > > Thanks!
> > > > >
> > > > > --- In [email protected], "Mike" sfclimbers@ wrote:
> > > > > >
> > > > > > Hi,
> > > > > >
> > > > > > Since you are trading the Close, you could simplify your
life by
> > > > following one of the well defined scenarios from the ApplyStop
> > > > documentation:
> > > > > >
> > > > > > http://www.amibroker.com/guide/afl/afl_view.php?id=20
> > > > > >
> > > > > > In your case, follow scenario 2 and take your trades based
on
> > > > yesterday's signal.
> > > > > >
> > > > > > e.g.
> > > > > >
> > > > > > SetTradeDelays(0, 0, 0, 0);
> > > > > >
> > > > > > Trigger = ...;
> > > > > > Buy = Ref(Trigger, -1);
> > > > > > BuyPrice = Close;
> > > > > >
> > > > > > Your signal still shows up Monday night. But, you don't act
on it
> > > > until Tuesday Close. That's what you're doing in real life
anyway, so
> > > > just make your code show the same.
> > > > > >
> > > > > > Mike
> > > > > >
> > > > > > --- In [email protected], "graphman27" <steve@>
wrote:
> > > > > > >
> > > > > > > Question:  When stops are coded afl instead of setup under
> > > > settings, do they disregard the system settings for Buy Price
and Buy
> > > > Delay?   Currently, I have the latter set to Buy Price = Close+1
Day Buy
> > > > Delay.  For a new strategy I am working on, I notice that during
live
> > > > testing something doesn't jive.  Here is an example...
> > > > > > >
> > > > > > > Note:  I always use EOD data and CLOSE +1, to get
"tradable"
> > > > signals for mutual funds and indices:
> > > > > > >
> > > > > > > Monday Night:  Formula gave buy signal for Emerging
Markets after
> > > > the close.  With CLOSE+1 Day delay, that would mean buy at
Tuesday's
> > > > Close.
> > > > > > >
> > > > > > > Tuesday Night:  Max Loss Stop is triggered because of a
-3.5% drop
> > > > in the Emerging Markets on Tuesday.
> > > > > > >
> > > > > > > Wednesday at the close, mutual fund is sold.
> > > > > > >
> > > > > > > Question:  How could a Max Loss Stop be triggered if that
fund
> > > > wasn't purchased until AFTER Tuesday's big drop?  Since it was
purchased
> > > > AFTER the drop, there was no loss of -3.5% going into Wednesday.
> > > > > > >
> > > > > > > Here is an example of a typical mutual fund compatible
coded stop:
> > > > > > >
> > > > > > > ApplyStop(stopTypeLoss,
> > > > > > >          stopModePercent,
> > > > > > >          Optimize( "max. loss stop level", 2, 2, 10, 2 ),
> > > > > > >          False );
> > > > > > >
> > > > > > > Do I have to code something else to make sure the STOP is
> > > > triggered based on the CLOSE+1 purchase price and NOT the BUY
SIGNAL
> > > > Price?  Or, do I need to code the Buys and Sells inside the
formula as
> > > > CLOSE+1 instead of relying on system settings?  OR, none of the
above?
> > > > > > >
> > > > > > > Thanks in advance for your help, as usual!
> > > > > > >
> > > > > > > Steve.
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Reply via email to