I am overly frustrated with something and am hoping someone can see where my mistake is. I am using the code from the above link as a template to rotate between two etfs. my data file follows this format:
date, weight1, symbol1, weight2, symbol2, BUY
01/03/2008,80,QQQ,20,TLT,1
01/09/2008,,QQQ,,TLT,0
01/09/2008,90,QQQ,10,TLT,1
03/30/2008,,QQQ,,TLT,0
03/30/2008,80,QQQ,20,TLT,1
04/11/2008,,QQQ,,TLT,0
04/11/2008,40,QQQ,60,TLT,1
06/08/2008,,QQQ,,TLT,0
06/08/2008,80,QQQ,20,TLT,1
01/09/2008,,QQQ,,TLT,0
01/09/2008,90,QQQ,10,TLT,1
03/30/2008,,QQQ,,TLT,0
03/30/2008,80,QQQ,20,TLT,1
04/11/2008,,QQQ,,TLT,0
04/11/2008,40,QQQ,60,TLT,1
06/08/2008,,QQQ,,TLT,0
06/08/2008,80,QQQ,20,TLT,1
...
now I know buying and selling on the same day is sloppy and in reality I would just want to change the weight of each allocation on a given day so on 1/3 80%/20% would get rebalanced on 1/9 to 90/10.
my backtest runs and the dates for buys and sells works but the weighting of each investment does not shift from the original 80/20. below is my code. please let me know if you see something that I am missing. I've been looking at it too long.
thx
file = "C:\\qqqtlt.csv";
dt = DateTime();
//
// Initialize variables
Buy = Sell = possize = 0;
//
fh = fopen( file, "r" );
//
if( fh )
{
while( ! feof( fh ) )
{
line = fgets( fh );
for( i=0; i<2; i++ )
//i=0;
{
// get the ticker symbol from the file
sym = StrExtract( line, IIf( !i, 2, 4 ) );
//PositionSize = -StrToNum( StrExtract( line, IIf( !i, 1, 3 ) ) );
// if ticker matches current symbol
//SetForeign( sym );
if ( Name() == sym )
{
// extract data from line of text
//trade = StrToUpper( StrExtract( line, 5 ) ) == "BUY";
trade = StrTonum( StrExtract( line, 5 ) );
trade_datetime = StrToDateTime( StrExtract( line, 0 ) );
price = ValueWhen( dt == trade_datetime, O );
//shares = StrToNum( StrExtract( litimene, 4 ) );
//
//if ( trade == "SELL" )
//if ( StrMatch( trade, "Sell" ) )
if ( !trade )
{
newsell = dt == trade_datetime;
Sell = Sell OR newsell; // combine previous sell signals with new
SellPrice = IIf( newsell, price, SellPrice );
}
//
//if ( trade == "BUY" )
//if ( StrMatch( trade, "BUY" ) )
if( trade )
{
newbuy = dt == trade_datetime;
BuyPrice = IIf( newbuy, price, BuyPrice );
PositionSize = -StrToNum( StrExtract( line, IIf( !i, 1, 3 ) ) );
Buy = Buy OR newbuy; // combine previous buy signals with new
dbg = -StrToNum( StrExtract( line, IIf( !i, 1, 3 ) ) );
}
}
//RestorePriceArrays();
}
}
//
fclose( fh );
}
else
{
Error( "ERROR: file can not be open" );
}
//
//
// Initialize variables
Buy = Sell = possize = 0;
//
fh = fopen( file, "r" );
//
if( fh )
{
while( ! feof( fh ) )
{
line = fgets( fh );
for( i=0; i<2; i++ )
//i=0;
{
// get the ticker symbol from the file
sym = StrExtract( line, IIf( !i, 2, 4 ) );
//PositionSize = -StrToNum( StrExtract( line, IIf( !i, 1, 3 ) ) );
// if ticker matches current symbol
//SetForeign( sym );
if ( Name() == sym )
{
// extract data from line of text
//trade = StrToUpper( StrExtract( line, 5 ) ) == "BUY";
trade = StrTonum( StrExtract( line, 5 ) );
trade_datetime = StrToDateTime( StrExtract( line, 0 ) );
price = ValueWhen( dt == trade_datetime, O );
//shares = StrToNum( StrExtract( litimene, 4 ) );
//
//if ( trade == "SELL" )
//if ( StrMatch( trade, "Sell" ) )
if ( !trade )
{
newsell = dt == trade_datetime;
Sell = Sell OR newsell; // combine previous sell signals with new
SellPrice = IIf( newsell, price, SellPrice );
}
//
//if ( trade == "BUY" )
//if ( StrMatch( trade, "BUY" ) )
if( trade )
{
newbuy = dt == trade_datetime;
BuyPrice = IIf( newbuy, price, BuyPrice );
PositionSize = -StrToNum( StrExtract( line, IIf( !i, 1, 3 ) ) );
Buy = Buy OR newbuy; // combine previous buy signals with new
dbg = -StrToNum( StrExtract( line, IIf( !i, 1, 3 ) ) );
}
}
//RestorePriceArrays();
}
}
//
fclose( fh );
}
else
{
Error( "ERROR: file can not be open" );
}
//
//SetPositionSize( possize, spsShares );
Posted by: "bluesinvestor .." <abmessages@gmail.com>
__._,_.___
Posted by: "bluesinvestor .." <abmessages@gmail.com>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (1) |
Upgrade your account with the latest Yahoo Mail app
Get organized with the fast and easy-to-use Yahoo Mail app. Upgrade today!
**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
.
__,_._,___
EmoticonEmoticon