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

[amibroker] Re: PairTrading on Amibroker



PureBytes Links

Trading Reference Links

I am a new AB user and new to message groups so if I violate any 
protocols, pls excuse the transgression.

I have been trying to code both approaches suggested in this thread. 
the first one creates a synthetic price and the second one suggested 
by TJ which uses an inverted signals. I prefer to do the first since 
i trade a basket of stocks and use 1 ticker as a hedge stock to 
counterbalance my longs/shorts. IOW, I might be long IBM short QQQQ, 
THEN short WMT long QQQQ,etc. By creating the synthetic price, it is 
much cleaner for performance tracking,etc and pnl since the entire 
pnl (stock + hedge stock)  is embedded in the entry/exit price of the 
stock. I am listing my code below with the hopes of getting help from 
the knowlegable users here. The code works IF there is a single 
signal for each ticker per day. So if IBM has a buy signal Oct 10, 
another stock gets bought oct 11,etc the synthetic buy/sell prices 
gets calcualted perfectly. The problem is if there are 2 or more buy 
signals for that day. The code calculates the correct entry/exit 
prices for the 1st ticker but uses regular close prices for entry 
exit on the 2nd , 3rd....stocks. I don't understand why the inner for 
loop ignores the other symbols after the first and goes to the next 
bar. I am not sure how the advanced portfolio tester works ,but i 
assume if you have a 10 ticker watchlist, it goes to bar 1 for the 
1st ticker, bar 1 for the 2nd ticker, bar 1 for the 3rd ticker, etc 
and once it finishes that bar for that day on ALL the tickers, it 
then and ONLY then goes to the 2nd day for ALL the tickers,etc 

Here is the code. PLease help. Thanks.  


SetCustomBacktestProc("");
if (Status("action") == actionPortfolio)  //main IF
{
bo = GetBacktesterObject();	//  Get backtester object
bo.PreProcess();	//  Do pre-processing (always required)
SPYLastRA=Foreign("SPY","C");

for (i = 1; i < BarCount; i++)	// i  Loop OPEN
{
  for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i)) 
  {	
  if (sig.type==1) //sig loop OPEN
  {   
  SPYLast=SPYLastRA[i];
  HedgeQty= SPYLast/sig.price;
  sig.price=(sig.price*HedgeQty)-(.99*SPYLast);
  foundX=0;
  Currtickr=sig.symbol;
	for (x=i+1;x < BarCount; x++)  //inner loop 2 find close trade
	{  
        for (sig2 = bo.GetFirstSignal(x); sig2; sig2 = 
bo.GetNextSignal(x)) 
        {  
	if ( sig2.type==2 AND CurrTickr==sig2.symbol)
	{  
	SPYLast=SPYLastRA[x];
	foundX=1;
	sig2.price=(sig2.price*HedgeQty)-(.99*SPYLast);
	} 						
}  //close sig 2 close
	if (foundX==1)						
	{						
	x=BarCount;						
	}
}
	
}  //sig loop CLOSE
}	//  sig signal CLOSE
bo.ProcessTradeSignals(i);


}	//  i Loop CLOSE
 bo.PostProcess();	
}  //main IF close


SetPositionSize(100,spsShares);

Buy=RSI(15)<30;
bi = BarIndex();
Sell = bi == LastValue(bi);

Short=0;
Cover=0;




--- In amibroker@xxxxxxxxxxxxxxx, "ang_60" <ima_cons@xxx> wrote:
>
> --- In amibroker@xxxxxxxxxxxxxxx, Howard B <howardbandy@> wrote:
> >
> > Hi Angelo --
> > 
> > Your wrote:
> > "for more clarification, please see a JPEG of an AA results of a 
pair
> > trading strategy, stored in 
http://www.savefile.com/files/1989358.";
> > 
> > That link seems to be broken.
> > 
> > Thanks,
> > Howard
> > 
> > 
> 
> 
> Hi Howard,
> 
> glad you jumped in, eagerly awaiting your new book :).
> Try this link:  http://www.filedropper.com/ambpairtrading_1
> 
> In short: I created a very basic pair system based on TJ code posted
> in this list: UC_5 and BIN_5 are the two legs of the pair and are
> enclosed in a watchlist.
> 
> On 2009/01/30, first trade is opened at 10:05 and closed at 12:50.
> Amibroker treat this as two distinct trades, and so 
> 
> - all statistics based on number/outcome of trades are misleading ( 
%
> winners, avg profit%, avgloss % etc...  );
> 
> - it is really difficult (at least for myself) to correctly manage 
the
> position.... for example as of today I'm not able to test a simple
> "Exit target if there's a 1% profit from the combined position
> (example: 2% prof on long leg, and 1% loss on short leg)".
> 
> Let me know if you need further details, resolving this issue should
> put Amibroker well ahead of competition in testing pair trading 
systems.
>




------------------------------------

**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL 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

*********************************
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/