Hi,
I haven't done much with ApplyStop. But, the scenario that you are
trying to accomplish is provided as an example in the user guide here:
http://www.amibroker.com/guide/afl/afl_view.php?id=20
<http://www.amibroker.com/guide/afl/afl_view.php?id=20>
<http://www.amibroker.com/guide/afl/afl_view.php?id=20> You can add the
following line to your code, just to be sure:SetOption(
"ActivateStopsImmediately", true );
For testing purposes, remove all Sell rules by replacing your Sell logic
with Sell = false;
Also, you should add a Plot for the price and then look at the
individual trades to see why you are getting losses greater than you
expected.
Most likely, the stock has gapped down (i.e. opened below your stop
price) and you are immediately getting stopped out at the lower price.
An example of this can be seen by running your code on the symbol AA in
the default AmiBroker database (Data);
Run a backtast over all quotations for single symbol AA, you will see 4
trades and one remaining open position. 3 of the 4 trades stopped out at
exactly -10%, as per your code. 1 trade stopped out at -15.57% (entry
Jul 21/2005, exit Sept 23/2005). Looking at the chart you will see that
AA opened way below your stop price on Sept 23. AmiBroker applied the
stop at the open, exactly as your broker would have.
The revised code looks like this:
SetOption( "ActivateStopsImmediately", true );
SetTradeDelays( 1, 1, 1, 1 );
ExitPct = 10;
ApplyStop( stopTypeLoss, stopModePercent, ExitPct, 1 );
//CONDITIONS---------------------------------------
Cond1 = Cross( MA( C, 10 ), MA( C, 50 ) );
//LONG------------------------------------
Buy = Cond1;
BuyPrice = Open; //open next bar!
Sell = false; //Sell = Cross( MA( C, 50 ), MA( C, 10 ) );
SellPrice = Open ;
//END OF CODE----------------------
_SECTION_BEGIN( "Price1" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g,
Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C,
1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle |
ParamStyle( "Style" ) | GetPriceStyle() );
_SECTION_END();
If you're still having trouble, post the exact formula being run on
exactly which symbols over exactly which dates stating exactly which
trades are not behaving as you expect.
Mike
--- In [email protected], "Gonzaga" <gonzag...@...> wrote:
>
> Grrrr!
> I am crazy with stops.
> I can not find any sense on them..
> Just this code:
> //--------------------------
> SetTradeDelays(1,1,1,1);
> ExitPct =10;
> ApplyStop(stopTypeLoss, stopModePercent, ExitPct, 1); //
> //CONDITIONS---------------------------------------
> Cond1=Cross(MA(C,10),MA(C,50));
> //LONG------------------------------------
> Buy= Cond1;
> BuyPrice=Open; //open next bar!
> Sell=Cross(MA(C,50),MA(C,10));
> SellPrice=Open ;
> //END OF CODE----------------------
> I cannot imagine something easier! Two conditions to open and close,
and a stoploss.
> Well, the stops work as they want.
> First, which have the preference: "Allow exit in the same bar" or
"activate stops immediately"?
> And both are preferred over the 4th parameter of the applyStop? or
not?
>
> But anyway, if the exitPct is -10%.. Why Amibroker close the position
not in -10%, but, -10.5%, -11% or so?
>
> I begin to be tired.. Couldn't be this easier?
>
> puf... anyone knows TradeStation and its EASYlanguage? could be an
alternative?
> Tired greetings
>
>
>
> --- In [email protected], "Gonzaga" gonzagags@ wrote:
> >
> > Thanks for testing the code.
> > I have tested your tweaked code also, using a portfolio of stocks,
or even just one stock.
> > For example, in EOD bars, this code buys GRA on 6th feb2009, selling
in the same bar in a profit stop on 6th Feb. Ok.
> > But, it sells with 4.65% change (4.5% profit), nearly in the high of
the day
> > So, it is supposed to close the position on 1% profit, isn't it?
> > So?
> > And, the stop loss has similar problems, closing positions on the
same bar lower than -1% loss..
> > We can not trigger stops in the same buying bar ?
> > I think I'm gonna program my own stops..
> > mmm..
> > greetings
> >
> >
> >
> >
> > --- In [email protected], "progster01" <progster@> wrote:
> > >
> > > Your code seems to work for me here.
> > >
> > > Here it is again, just slightly tweaked:
> > >
> > > //SETTINGS--->
> > > PosQty=Param("Posiciones",10,1,20,1);
> > > SetOption("InitialEquity", 1000000 );
> > > SetOption("AllowPositionShrinking", True );
> > > SetOption("MaxOpenPositions", PosQty );
> > > SetOption ("accountmargin",100);
> > > PositionSize = -100/(posqty);
> > >
> > >
> > > Buy = Sell = Short = Cover = 0 ;
> > > //-----------
> > >
> > > ExitPct = Param( "ExitPct", 1, .1, 10, .1 ) ;
> > >
> > > SetTradeDelays(1,1,1,1);//buy after signal
> > > ApplyStop(stopTypeProfit, stopModePercent, ExitPct, 1); //exit
intraday on same bar at 2%
> > > ApplyStop(stopTypeLoss, stopModePercent, ExitPct, 1); //exit
intraday on same bar at 2%
> > >
> > > //CONDITIONS---------------------------------------
> > > Cond1=Cross(MACD(12,26),0);
> > >
> > > //long entry-----------------------------
> > > PositionScore= 100-StochK(15,3);
> > > Buy= Cond1;
> > > // BuyPrice=Open*1.001; //in open next bar!
> > > BuyPrice=Open ; //in open next bar!
> > >
> > > Sell=Cross(Signal(12,26,9),MACD(12,26));//sell in open next bar
> > > // SellPrice=Open*0.999;
> > > SellPrice=Open ;
> > >
> > >
> > > Buy=ExRem(Buy,Sell); Sell=ExRem(Sell,Buy);
> > >
> > > //------------END OF CODE------------------
> > >
> > >
> > > Notes:
> > >
> > > 1. My AB 5.3 install requires Buy, Sell, Stop, and Cover to all
be defined.
> > >
> > > 2. I use the Open for BuyPrice and SellPrice. If you multiply
the Open you could wind up with a price that does not exist during that
bar.
> > >
> > > 3. I parameterized the ExitPct, and added the stopTypeLoss.
> > >
> > > 4. When I run a backtest, results look good, with rows and rows
of profit and loss exits at the ExitPct, differing only in the 2nd
decimal place.
> > >
> > > So, I don't know why it isn't working for you ...
> > >
> > > - Progster
> > >
> > >
> > > --- In [email protected], "Gonzaga" <gonzagags@> wrote:
> > > >
> > > > Thanks for the answer.
> > > > But I don't understand, yet. I'm sorry, but, suppose the next
purchase:
> > > > Using end-of-day bars:
> > > > The system triggers a buy signal.
> > > > Then I buy on next day bar, just in the open.
> > > > Then, the stoploss should 'look the price', selling in the same
bar if loss is reached.
> > > >
> > > > How could the system knows if the stops triggers before or after
the buyprice?
> > > > Well, If I buy the open price, if the low is less than the
stoplevel, the system sells.
> > > > Is that simple, I think I could programm.
> > > > But I thought that was easier with the stops commands..
> > > > Where is the mistake?
> > > > Thanx
> > > >
> > > >
> > > >
> > > >
> > > > --- In [email protected], Progster <progster@> wrote:
> > > > >
> > > > > Gonzaga,
> > > > >
> > > > > If a chart bar is the smallest data unit under test, your test
cannot be
> > > > > accurate if price-based entries and exits take place in the
same bar.
> > > > > This is because the path of price inside the bar is not known.
> > > > >
> > > > > Some platforms have an "inside the bar" smallest data unit
under test
> > > > > (e.g. testing actually done to the tick level, assuming tick
data
> > > > > exists, even though you are observing a Daily chart). This can
be
> > > > > somewhat helpful for visualizing and testing some kinds of
strategies,
> > > > > but tests done this way run at tick-bar speed, not at Daily
bar speed.
> > > > > In any case, AB does not work this way.
> > > > >
> > > > > I recommend testing your system on a low enough timeframe such
that
> > > > > entries and exits never occur on the same bar.
> > > > >
> > > > > I also recommend, if you need Daily TA numbers, then run on,
say, a 5m
> > > > > chart (or even a 30m chart, depending), and write your code to
use AB's
> > > > > time-compression/expansion to do the Daily-level TA.
> > > > >
> > > > > - Progster
> > > > >
> > > > >
> > > > > On 6/16/2010 3:50 PM, Gonzaga wrote:
> > > > > >
> > > > > > Well, I have to admit that I don't understand at all the
stop system
> > > > > > performed by Amibroker.
> > > > > >
> > > > > > I have super-simplified my code, letting only a buy
condition, and a
> > > > > > stop, established in the settings dialog, to avoid any code
mistake.
> > > > > > So, I want to have a stoploss or stop profit, for example
5%.
> > > > > > I use settradedelays (1,1,1,1);
> > > > > > the stops execute in the same bar as stop-loss signal, using
the
> > > > > > parameter in settings "exit intraday at stop(1)",which, as
help files
> > > > > > says, "check High-Low prices and exit intraday on price
equal to stop
> > > > > > level on the same bar when stop was triggered".
> > > > > > I also set "activate stops immediately", which is supposed
that if
> > > > > > it's activated, "stop is activated after current bar
signals"
> > > > > > And I also set to true, (I dont know if it's needed) "allow
same bar exit"
> > > > > >
> > > > > > Well, my system buys the day after buy signal, at open. And
the stop
> > > > > > should sell in the stoploss limit exactly, at least if the
stop
> > > > > > triggers in the same buying bar. If it is in another bar,
there could
> > > > > > be a gap that make bigger the loss, of course.
> > > > > >
> > > > > > Well, there's no way. I obtain any loss, in the same bar, in
different
> > > > > > bars, and never limit to the stop loss number.
> > > > > > I understand nothing.
> > > > > > Does anybody know how to enter at next-bar open, and let the
stoploss
> > > > > > triggers in the same bar of the stop signal?
> > > > > > Or at least, where is a clear paper explaining the exact
usage of the
> > > > > > amibroker's stops??
> > > > > >
> > > > > > Thanks a looot
> > > > > >
> > > > > >
> > > >
> > >
> >
>