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

[amibroker] Problem with AB to TradeSim Code - Please Help!!



PureBytes Links

Trading Reference Links

Hello All,

If anyone can help with this problem, I'd be really grateful as I am 
completely stuck!!!

Up to recently, I have been using the older version of the code for 
transfering the Trades Data from AB to TradeSim. This code was the 
one that was set-up for Long Trades only. (I have posted it below)

However, now that I have begun to develop the short side of my 
system, I decided to start using the newer code which was posted on 
here (TradesimFns(rev 1.5).zip), which allows both Long & Short 
Trades. (again, I have posted it below)

I have only included some sample code for LONG trades. However, when 
I carry out an exploration and then view the results in 
TradeSim, I get different results compared to when I use the older 
version of the code!!!???!!! In my mind, there should be no 
difference.

The problem I have now is that I do not know which version of the 
code gives the correct results, so am reluctant to keep developing.

When I compare individual Trade details in each report, the entry & 
exit dates and Entry & Exit prices are the same, as would be expected.

However, when I compare Total Trade duration, Trades taken, as well 
as other stats these are different!!! 

Any ideas, as I really don't know how to proceed.

Many Thanks,

Chorlton



***** OLDER Version of the code *****

// System Name:  TEST

SetTradeDelays(1, 0, 0, 0);

////////// BUY CONDITIONS //////////

Buy	= L>Ref(H,-1);
BuyPrice = Open;

////////// SELL CONDITIONS //////////

ExitPrice= LLV(Low,2);

stop[0] = Close[0];
for( i = 1 ; i < BarCount; i++)
{ 
 if( Close[i] > stop[i-1]) 
 { 
  stop[i] = Max( ExitPrice[i], stop[i-1] ); 
 }
 else { stop[i] = ExitPrice[i]; }
}

Sell = L <= Ref(stop,-1);  // close long positions on 31 Dec 2004 if 
stop not already activated;

SellPrice = Ref(stop,-1);  // Trailing Stop is being monitored 
IntraDay. 

// Plots only the first occurance of each Signal until the opposite 
signal is shown
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

//// *********  EXPLORER OUTPUT FOR TRADESIM *********   ////

// Trades may need to be delayed
Buy = Ref(Buy,-1); //Sell = Ref(Sell, -1); Sell Cond should NOT be 
delayed as Exit is based on Previous Trailing Stop value and will 
therefore be exited on the same bar as the Sell Signal

Cover = 0; Short = 0;
TradeStop = LLV(Low,2);     // Initial stop for TradeSim

Equity( 1 ); // evaluates trades and stops 
Filter = Sell;// OR Cover;  // Not set up for short trades at this 
stage

IsLong = Sell; 

dt = DateTime(); 

// Determine values for Explorer
EntryDate = IIf( IsLong, ValueWhen( Buy, dt ), ValueWhen( Short, 
dt ) ); 
ExitDate = dt; 
EntryPrice = IIf( IsLong, ValueWhen( Buy, BuyPrice ), ValueWhen( 
Short, ShortPrice ) );  // Set buy and sell prices to Open for 
TradeSim
ExitPrice = IIf( IsLong, SellPrice, CoverPrice ); 
EntryLow = IIf( IsLong, ValueWhen( Buy, L ), ValueWhen( Short, L ) ); 
EntryHigh = IIf( IsLong, ValueWhen( Buy, H ), ValueWhen( Short, 
H ) ); 
EntryVolume = IIf( IsLong, ValueWhen( Buy, Ref(V,-1) ), ValueWhen( 
Short, Ref(V, -1) ) ); 
InitialStop = IIf( IsLong, ValueWhen( Buy, TradeStop ), ValueWhen( 
Short, TradeStop ) ); 
ExitLow = L; 
ExitHigh = H;


SetOption("NoDefaultColumns", True ); 

AddTextColumn( Name(), "Symbol" ); 
AddColumn( IIf( IsLong, 76, 83 ), "Long/Short", formatChar ); // L or 
S letter 
AddColumn( EntryDate, "EntryDate", formatDateTime); 
AddColumn( ExitDate, "ExitDate", formatDateTime); 
AddColumn( InitialStop, "Initial Stop", 1.3 ); 
AddColumn( EntryPrice, "EntryPrice", 1.3 ); 
AddColumn( ExitPrice, "ExitPrice", 1.3 ); 
AddColumn( EntryLow, "EntryLow", 1.3 ); 
AddColumn( EntryHigh, "EntryHigh", 1.3 ); 
AddColumn( L, "ExitLow", 1.3 ); 
AddColumn( H, "ExitHigh", 1.3 ); 
AddColumn( EntryVolume, "EntryVolume", 0.0);




***** NEWER Version of the code *****

// Settings
//Capital	= 10000000;
dly			= 1; //trade delay
//SetPositionSize(35000, spsValue);
//SetOption("NoDefaultColumns", True );
//SetOption("InitialEquity",Capital);
//SetOption("AllowPositionShrinking", False ); 
//SetOption("MaxOpenPositions", 2000 );
//SetOption( "PriceBoundChecking", 1 );
//SetOption("AllowSameBarExit", 1 );
//SetOption( "CommissionMode", 2 );  
//SetOption( "CommissionAmount", 0 );//nil commission
//SetOption( "UsePrevBarEquityForPosSizing", 1 ); //not rqd
SetTradeDelays( 1, 0, 0, 0);
//allow long & Short positions for symbol at same time
//that is, does not remove redundant signals
//SetBacktestMode( backtestRegularRawMulti );


global TradedVol, InitialStop, TradeRank;

// Optional Tradesim field declarations - MUST have or export failure
InitialStop	= Null;
TradedVol		= Null;
TradeRank		= Null;
PointValue		= 0; //overrides AA.settings
InitialMargin	= 0; //overrides AA.settings

Valid			=  Open>0 AND High>0 AND Low>0 AND 
Close>0; //OHLC > 0


/*********************************************************************
**********/
/* END of DECLARATIONS */
/*********************************************************************
**********/

// LONG System
BSig		= L>Ref(H,-1);

Initial = LLV(Low,2);
stop[0] = Close[0];
for( i = 1 ; i < BarCount; i++)
{ 
 if( Close[i] > stop[i-1]) 
 { 
  stop[i] = Max( initial[i], stop[i-1] ); 
 }
 else { stop[i] = initial[i]; }
}

SSig	=  L <= Ref(stop, -1);

Buy			= Ref(BSig,-dly) AND Valid;
Sell		=  SSIG; // Ref(SSig,-dly);  Sell Cond should NOT be 
delayed as Exit is based on Previous Trailing Stop value and will 
therefore be exited on the same bar as the Sell Signal

InitStopL	= LLV(Low,2);


// SHORT system
ShtSig		= 0;
CSig		= 0;
Short		= 0; //Ref(ShtSig,-dly) AND Valid;
Cover		= 0; //Ref(CSig,-dly);
InitStopS	= 0; // Ref( C + 1.25*ATR(10), -dly);

TradedVol	= V;


// Entry/Exit prices
BuyPrice		= Open;
SellPrice		=  Ref(stop,-1);
ShortPrice		= Open;
CoverPrice		= Open;


/*********************************************************************
**********/
/* END SYSTEM */
/*********************************************************************
**********/


// Plots only the first occurance of each Signal until the opposite 
signal is shown
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
BSig= ExRem( BSig, SSig );
SSig = ExRem( SSig , BSig);

// These 3 lines are for plotting the Buy and Sell Arrows on the Chart
Plot( C, " Close Price", colorGrey50, styleBar );
PlotShapes( shapeUpArrow*BSig, colorGreen, 0, L, -12 );
PlotShapes( shapeDownArrow*SSig, colorRed, 0, H, -12 );



// Exploration or Scan
if( Status("Action")==actionExplore OR Status("Action")==actionScan )
{
 Filter	= Buy OR Sell OR Short OR Cover;

 InTradeL	= Flip(Buy,Sell);
 OutTradeL	= Flip(Sell,Buy);
 InLng		= Ref(intradeL,-1);
 OutLng	= Ref(OutTradeL,-1);

 InTradeS	= Flip(Short,Cover);
 OutTradeS	= Flip(Cover,Short);
 InSht		= Ref(intradeS,-1);
 OutSht	= Ref(OutTradeS,-1);

 TSDate	= DateNum() + 19000000; //in YYYYMMDD format

 EntryDate	= IIf( InLng, ValueWhen(Buy, TSDate),		
	//undefined placeholder
				IIf( InSht, ValueWhen(Short, TSDate), 
Null ) );

 ExitDate	= IIf( OutLng, ValueWhen(Sell, TSDate),	
	//undefined placeholder
				IIf( OutSht, ValueWhen(Cover, 
TSDate), Null ) );

 EntryP	= IIf( InLng, ValueWhen(Buy,BuyPrice),		//undefined 
placeholder
				IIf( InSht, ValueWhen
(Short,ShortPrice ), Null ) );

 ExitP		= IIf( OutLng, ValueWhen(Sell,SellPrice),	
	//undefined placeholder
				IIf( OutSht, ValueWhen
(Cover,CoverPrice ), Null ) );

 EntryLoP	= IIf( InLng, ValueWhen(Buy OR Short, L), 0 );
	//placeholder=0
 EntryHiP	= IIf( InLng, ValueWhen(Buy OR Short, H), 0 );
	//placeholder=0

 ExitLoP	= IIf( OutLng, ValueWhen(Sell OR Cover, L), 0);
	//placeholder=0
 ExitHiP	= IIf( OutLng, ValueWhen(Sell OR Cover, H), 0);
	//placeholder=0


 NumBars	= IIf( InLng, IIf( IsNull(BarsSince(Buy)),0, BarsSince
(Buy)),
			IIf( InSht, IIf( IsNull(BarsSince(Short)),0, 
BarsSince(Short)), 0));

 frm1		= LastValue( IIf( EntryLoP > 0, 1.4, 1.0) );
 frm2		= LastValue( IIf( EntryHiP > 0, 1.4, 1.0) );
 frm3		= LastValue( IIf( ExitLoP > 0, 1.4, 1.0) );
 frm4		= LastValue( IIf( ExitHiP > 0, 1.4, 1.0) );
 frm5		= LastValue( IIf( MarginDeposit > 0, 1.2, 1.0) );

 //By running an explore, the columns below can be exported to 
Tradesim
 AddTextColumn( Name(), "Symbol", 1.0, colorDefault, colorDefault, 
55 );
 AddColumn(	IIf( Buy OR Sell, Asc("L"),
				IIf( Short OR Cover, Asc("S"), 
Null ) ),"Posn", formatChar,
				colorDefault, colorDefault,40);
 AddColumn( EntryDate, "Entry Date", 1.0, colorDefault, colorDefault, 
70 );
 AddColumn( ExitDate, "Exit Date", 1.0, colorDefault, colorDefault, 
70 );
 AddColumn(InitialStop,"Stop",1.4, colorDefault, colorDefault,65);
 AddColumn( EntryP, "Entry Price", 1.4, colorDefault, colorDefault, 
65 );
 AddColumn( ExitP, "Exit Price", 1.4, colorDefault, colorDefault, 
65 );
 AddColumn( EntryLoP, "Entry Low", frm1, colorDefault, colorDefault, 
65 );
 AddColumn( EntryHiP, "Entry High", frm2, colorDefault, colorDefault, 
65 );
 AddColumn( ExitLoP, "Exit Low", frm3, colorDefault, colorDefault, 
65 );
 AddColumn( ExitHiP, "Exit High", frm4, colorDefault, colorDefault, 
65 );
 AddColumn( TradedVol, "TradedVol", 1.0, colorDefault, colorDefault, 
65);
 AddColumn( TradeRank, "TradedRank", 1.0, colorDefault, colorDefault, 
55);
 AddColumn( PointValue, "Point Value", 1.0, colorDefault, 
colorDefault, 65);
 AddColumn( 
MarginDeposit,"InitialMargin",frm5,colorDefault,colorDefault,70 );
}

/*********************************************************************
**********/
/* TRADESIM EXPORT*/
/*********************************************************************
**********/

// TradesimFns(rev x.x)
#include_once "C:\Program 
Files\AmiBroker\Formulas\Include\AB_to_TradeSim\TradeSimFns(rev 
1.5).afl";

// call the TradeSim function, using defaults
// output to c:\Temp\ABtoTS-TradeFile.trt
//////////////////////////////////////////////////////////////////////
// Procedure ABtoTS( SigDelay, msDir, msFileName, sMode ): TextFile
//////////////////////////////////////////////////////////////////////
if( 	Status("action")==actionScan
	OR	Status("action")==actionExplore
	OR	Status("action")==actionBacktest )
{
 ABtoTS( 0, "", "" , "" ); //defaults
}
/*********************************************************************
**********/
/*********************************************************************
**********/



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

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
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/