PureBytes Links
Trading Reference Links
|
Yep. From what you described, you may need rotational mode unless you can live with missing signals which have been removed because they are "redundant" under regular mode.
The way I proposed in my previous message will not work because the trailing stops are set only after the positions have been opened which is inside the CBT loop.
I would assign a special position score to sell signals (for example, 2). And inside CBT, in the signal loop, if the position score is 2, loop through open positions to see if there is an open position of the same symbol in the signal. If there is, you can calculate the trailing stop and compare it with trading range (use pos.GetPrice) to see if the stop has been hit. If it has been hit, call ExitTrade, otherwise skip to next signal. This can be done with a bit complexity, but not very difficult.
----- Original Message -----
From: Ken Close
To: amibroker@xxxxxxxxxxxxxxx
Sent: Wednesday, November 01, 2006 11:20 PM
Subject: RE: [amibroker] Rotational Trading with Trailing Stop Exit - Example???
Mark:
Thanks for the brief suggestion, maybe it will help as I dig further.
The idea is to buy the top four or five ranked funds/ETFs on the buy, set a number of conditions, let the trailing stop control the exits, even if the timing signal goes on a sell, the held funds would not sell then but only if their trailing stops are hit. Seems like that requires the rotational mode, no?
Are there no examples posted anywhere of trading something similar with a CustomBackTester code?
Ken
------------------------------------------------------------------------------
From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of Mark H
Sent: Wednesday, November 01, 2006 3:15 PM
To: amibroker@xxxxxxxxxxxxxxx
Subject: Re: [amibroker] Rotational Trading with Trailing Stop Exit - Example???
This can be done via a special PositionScore you designate which is not used else where:
For example:
stopcond = ...trailing stop exit condition...
PositionScore = ...
PositionScore = IIF(stopcond, 10, PositionScore); // score 10 is used to indicate a trailing stop exit
Inside CBT loop:
for ( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar) )
{
if (sig.PosScore == 10 )
{
pr = ... trailing price ... // see NOTE
bo.ExitTrade(bar,sig.symbol, pr)
}
}
NOTE: The difficult part is passing the trailing price inside the CBT loop. There are a couple ways to do it, such as using static variables, or you can calculate the trailing price
inside CBT loop by using Foreign(sig.symbol,...) to get the price arrays of the symbol etc.
I don't know why you need to use rotational trading. It would be easier to do it under regular mode.
----- Original Message -----
From: Ken Close
To: amibroker@xxxxxxxxxxxxxxx
Sent: Wednesday, November 01, 2006 8:53 AM
Subject: [amibroker] Rotational Trading with Trailing Stop Exit - Example???
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 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 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.amibroker.com/kb/category/afl/custom-backtest/
Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
From: Mark H
To: amibroker@xxxxxxxxxxxxxxx
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
To: amibroker@xxxxxxxxxxxxxxx
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
|