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

Re: [amibroker] hedging - is it possible to control individual exits in CB?



PureBytes Links

Trading Reference Links

hi,
 
last year I have been playing with the CBT and you can do lots with it but it takes time to learn. I suggest that when you are having difficulties understanding what is happening you write output to a file. Below I have an example that I used to understand what is going on. When you call for this code in your main program (a portfolio type system) basicly nothing additional happens to what the normal backtest engine would already have done for you without this code (if i remember correctly) except signals are written to a file signals.txt.  Maybe it helps.
 
rgds, Ed
 
 
 // this leaves your system unchanged


SetOption("UseCustomBacktestProc", True
);

if( Status("action") == actionPortfolio
) {

   fh =
fopen("c:\\signals.txt", "w"
);

   bo =
GetBacktesterObject
();
   bo.PreProcess();
// Initialize backtester

   
   
for(bar=0; bar<BarCount; bar++) {
   
           
// loop through signals

           
for
( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar) ) {
        
              
if( fh ) fputs( "  " + "\n"
, fh );
              
// first handle exit signals

              
if (sig.IsExit() AND sig.Price != -1 ) {
           
                 
// Exit Signal

                 bo.ExitTrade(bar,sig.symbol,sig.Price);
                 
if( fh ) fputs( NumToStr(bar) + sig.Symbol + "\n", fh );
              
              }
           
           }

           
// update stats after closing trades

           bo.UpdateStats(bar,
1 );
      
           bContinue =
True
;
           
for ( sig=bo.GetFirstSignal(bar); sig AND
bContinue; sig=bo.GetNextSignal(bar)) {
        
              
// enter new trade when scale-in signal is found

              
// and we don�t have already open position for given symbol

              
if (sig.IsEntry() AND sig.Price != -1 AND IsNull( bo.FindOpenPos( sig.Symbol ) ) ) {
           
                 
// Entry Signal

                 
if( bo.EnterTrade(bar, sig.symbol, sig.IsLong(), sig.Price,sig.PosSize) == 0 ) {
              
                    
// if certain trade can not be entered due to insufficient funds

                    
// or too small value (less than �MinPositionValue�) or
                    
// to few shares (less than �MinShares"
                    
// then do NOT process any further signals
                    bContinue =
False;
                 
                 }
              
              }
           
           }

           bo.UpdateStats(bar,
1); // MAE/MFE is updated when timeinbar is set to 1.

           bo.UpdateStats(bar,
2);  
   
   }
  
      bo.PostProcess();
// Finalize backtester

  

}
 
 
----- Original Message -----
From: tiedemj
Sent: Saturday, August 16, 2008 11:07 AM
Subject: [amibroker] hedging - is it possible to control individual exits in CB?

Hello

In order to hedge Forex trading, I'm running two independent trading
systems. One performs well in certain situations, the other
under "opposite" conditions.

However, the two systems will often provide "opposite" tradesignals
(long/short), even in the same bar. I still want to take the two
trades, as they have different exit criteria.

In order to control multiple trades on the same bar independently,
I've been trying to use "backtestRegularRawmulti" with custom
backtester. Entering multiple trades on a bar works fine this way
(using positionScore to indicate to the custom backtester if two
positions in the same direction should be taken).

However, I have a hard time figuring out a way to control the exits.
bo.ExitTrade() seem to exit the first (last?) entered open position
and have no parameter to indicate what position to close - so unless
the trades come in a certain order (they don't), the wrong trades are
being closed out. So I've been trying to use sigScaleIn/Out in
various ways, but with no luck.

Also, I've been trying to use the applyStop(stopModeBars) as a
trigger mechanism (but pos.barsInTrade is a readOnly property - so
I've not been able to force an exit this way on an individual open
position).

Is there a way to control exits independently? Like if the trade
object had a pos.ExitTrade() method, or by some other means?

Regards
Jens Tiedemann

__._,_.___

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html




Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___