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

[amibroker] Rotational Trading with Trailing Stop Exit - Example???



PureBytes Links

Trading Reference Links

TJ or anyone:
 
Is there an example code anywhere which shows using the custom backtester
with rotational trading AND a trailing stop exit??
 
IOW, rotational trading as normal if a signal is on a buy, but if the signal
goes on a sell, no further rotational trading AND use a trailing stop to
EXIT, only to BUY again at the next Buy signal?
 
Where is an example that might help someone code such a situation?
 
Thanks for any advice.
 
Ken

  _____  

From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf
Of Tomasz Janeczko
Sent: Wednesday, November 01, 2006 4:46 AM
To: amibroker@xxxxxxxxxxxxxxx
Subject: Re: [amibroker] Counting ALL entries. Hi Mark H. could u pls read.


Hello,
 
There are two reasons:
1. non-technical: for sake of not producing non realistic results AB must
not allow to specify different prices
for rotation entry and exit because otherwise you would be able to rotate
enter on open without exiting previous
positions first and then exit at the end of the day which would in reality
require having twice as much funds to do
or margin account or reducing trade sizes to half the size of buying power.
So if backtester allowed that you would
get too optimistic results.
 
2. The technical reason is that prices are NOT taken from settings, exit
prices are taken from
SellPrice and CoverPrice arrays (even if you don't define them - there are
preset from what you have choosen in settings) 
This are per-symbol and per-quotation arrays so there are huge amounts of
them
when testing on portfolio. Now AB keeps track of individual perquote / per
symbol prices only when
SIGNALS are present. So in rotational mode there are only ENTRY signals so
buyprice array values are available
to backtester , but there are no sell signals so no sellprice / coverprice
arrays are available.

Best regards,
Tomasz Janeczko
amibroker.com

----- Original Message ----- 
From: Mark  <mailto:amibroker@xxxxxxxxxxxxx> H 
To: amibroker@xxxxxxxxxxxxxxx 
Sent: Tuesday, October 31, 2006 8:03 PM
Subject: Re: [amibroker] Counting ALL entries. Hi Mark H. could u pls read.

Tomasz:
 
I kind of have a different opinion here. No matter what mode you are in,
rotational or regular, you still do buy and sell, short and cover. 
In the case of rotational mode, the *implicit* exit signal is "falls down in
ranking table below WorstHeld rank". It would be nice to make all those on
Setting | Trade page
applicable under this mode, unless there are other technical reasons not to
do so. 
 
Of course you can set the prices to whatever you want under CBT for
EnterTrade and ExitTrade (though somewhat difficult for n-bar stop etc). It
would be nice for those who don't use CBT to set the prices in the Settings.
 
- Mark H.
 

----- Original Message ----- 
From: Tomasz  <mailto:groups@xxxxxxxxxxxxx> Janeczko 
To: amibroker@xxxxxxxxxxxxxxx 
Sent: Tuesday, October 31, 2006 1:14 PM
Subject: Re: [amibroker] Counting ALL entries. Hi Mark H. could u pls read.




Yes this is so and will remain so because there are NO exit signals in
rotational mode.
Exits in rotational mode are made if symbols falls down in ranking table and
therefore
it is no longer in Top-N symbols. Since there are NO exit signals, so there
are no special exit prices.
Plain and simple.
 
You can, however control exit price individually in custom backtest
procedure.
There are lots of examples in Knowledge Base
http://www.amibroke
<http://www.amibroker.com/kb/category/afl/custom-backtest/>
r.com/kb/category/afl/custom-backtest/

Best regards,
Tomasz Janeczko
amibroker.com

----- Original Message ----- 
From: Mark  <mailto:amibroker@xxxxxxxxxxxxx> H 
To: amibroker@xxxxxxxxx <mailto:amibroker@xxxxxxxxxxxxxxx> ps.com 
Sent: Tuesday, October 31, 2006 6:09 PM
Subject: Re: [amibroker] Counting ALL entries. Hi Mark H. could u pls read.

That's the nature of rotational mode, the following is from the on-line doc
for EnableRotationalTrading():
Important:
The rotational trading mode uses "buy price" and "buy delay" from the
Settings | Trade page as trade price and delay for both entries and exits
(long and short)
 
I don't why it was designed like that, but it is what it is now. To
workaround that, you can set the buy price as close so that the stop uses
the close, but in the bo.EnterTrade(..) function, use the actual open price
of that symbol instead of sig.Price.
 

----- Original Message ----- 
From: jlami11 <mailto:jlami11@xxxxxxxxx>  
To: amibroker@xxxxxxxxx <mailto:amibroker@xxxxxxxxxxxxxxx> ps.com 
Sent: Tuesday, October 31, 2006 3:21 AM
Subject: [amibroker] Counting ALL entries. Hi Mark H. could u pls read.


Hello, Mark H.,

below is the code you referred me to for Counting all Entry signals on
backtester, even on existing open positions.
I got it to work for my purposes, but I'm stuck.

I'm trying to Enter on the open (1 day delay), while exiting on the
close after X days.

I just can't get it to enter on open while exiting on close. I don't
know what code to write for these.

I tried setting it in trade settings page, but when I set Entry to
Open price, the Exit exit's on Open price too, even though it's set on
Close price.

I've spent ages trying to work this out, if anyone could give some
help i'd be very grateful.

---Below is coding u reffered me to... i used almost same code--

EnableRotationalTrading();
SetOption("WorstRankHeld",160); // this number needs to be big enough.
Only 2*WorstRankHeld signals will be held by CBT each bar.
SetOption("MaxOpenPositions", 100);
SetOption("InitialEquity", 30000);
SetOption("CommissionMode", 1); //% per trade
SetOption("CommissionAmount", 0.5);
SetOption("MarginRequirement", 50);
SetOption("UsePrevBarEquityForPosSizing", True);
SetOption("MinShares", 100);

SetTradeDelays( 1, 1, 1, 1 );
RoundLotSize = 5;

......

ApplyStop( stopTypeNBar, stopModeBars, 10);

Sell0 = ...; // Rename sell/buy to sell0/buy0 since you can have
sell/buy in rotational mode.
Buy0 = ...;
Sell0[0] = 1; // trick to remove leading sell signals
Sell0 = ExRem( Sell0, Buy0 );
Sell0[0] = 0;

RawScore = 100 + ......; // make sure it is greater than 2
PositionScore = 1;
for(i = 0; i < BarCount; i++)
{
if(Buy0[i]) PositionScore[i] = RawScore[i];
else if(Sell0[i]) PositionScore[i] = 2;
else PositionScore[i] = C[i]/H[i]; //semi-random
}

SetOption("UseCustomBacktestProc", True );

if( Status("action")== actionPortfolio )
{
bo = GetBacktesterObject();

bo.PreProcess(); // Initialize backtester

for(bar=0; bar < BarCount; bar++)
{
bo.HandleStops(bar-1); // if use bar not bar-1, the
n-bar exits have one extra bar delay. don't know why.
for ( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar) )
{ 
// first handle exit signals (PosScore = 2)
if ((sig.PosScore == 2 OR sig.isExit()) AND sig.Price
!= -1 )
{
bo.ExitTrade(bar,sig.symbol,sig.Price);
}
}

// update stats after closing trades
bo.UpdateStats(bar, 1 );

for ( sig=bo.GetFirstSignal(bar); sig;
sig=bo.GetNextSignal(bar))
{
// Entry Signals (PosScore > 2)
// Only one position per symbol
if (sig.PosScore > 2 AND sig.Price != -1 AND IsNull(
bo.FindOpenPos( sig.Symbol )))
{ 
// long only
bo.EnterTrade(bar, sig.symbol, True, sig.Price,
sig.PosSize, sig.PosScore,sig.RoundLotSize);
} 
}

bo.UpdateStats(bar,1); // MAE/MFE is updated when
timeinbar is set to 1.
bo.UpdateStats(bar,2); 
}
bo.PostProcess(); // Finalize backtester
}









 

Content-Description: "AVG certification"
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.409 / Virus Database: 268.13.27/517 - Release Date: 11/3/2006