Include portfolio details in Explore

I use Amibroker with Norgate Premium EOD data and trade manually using an online broker who provide profit & loss details of my portfolio but I want more.

Is it possible to include date, quantity and cost of my current open trades in an Exploration?

Using the Amibroker account would be an option, I can enter the transactions then display the open positions but can't find any way to link the details to an exploration. I currently import the quantity and cost into the aux1 & aux2 fields on the transaction date but I'm migrating to the new Norgate.com data and these fields are no longer unused.

While I would like to have the date, quantity and cost the important detail is the date because I want to know how long I've held a stock. Holding them for longer than certain periods impacts on any Capital Gains Tax I have to pay. If I decide to sell something which is under performing or because I get a sell signal I will consider delaying if I can half my tax by trading tomorrow or next week.

You could create a copy of your symbol(s) via AddToComposite
Note:Composite's name must include tilde at start of name... so e.g.:

nm = "~" + Name() + "_Trades";

and then you import your trade stuff to the Aux fields (or any other field(s)) of that symbol copy. After creation you call the Aux fields of that copy via Foreign() or SetForeign().

nm = "~" + Name() + "_Trades";

SetForeign( nm );
	frg_Aux1 = Aux1;
	frg_Aux2 = Aux2;
RestorePriceArrays();

Filter = 1;

AddColumn(frg_Aux1,"frg_Aux1", 1.2); 
AddColumn(frg_Aux2,"frg_Aux2", 1.2); 

Or you read&store the data from your account manager's created XML file (file extension *.acx). So that file is human readable and as such you can extract and store the infos from there easily via AFL.

2 Likes

Thanks @fxshrat I'll look at using AddToComposite

I currently don't use the Amibroker account manager and have my data saved in a *.csv file and import the quantity into Aux1 and cost into aux2.

I'm testing using OpenInt (which is unused for shares) rather than Aux1, this means it doesn't have my actual costs but I do know the open, close, high and low for the day and can make some assumptions.

I hoped to use the account manager so I'm not hijacking fields which are intended for other purposes.

I found a solution which works for me.

I want to include details of my current holdings in some of my explorations and was importing the data into the aux1 & aux2 fields but had to find a different method when I changed the source of my data.

After trying a number of things I'm now using the Amibroker notepad to store my trading history which works for me,

  • The notepad entries are loaded automatically then I start Amibroker meaning my trading history is automatically loaded.

  • My trading history is visible when I look at a share rather than just via AFL.

  • Amibroker saves the notepad entries as simple text files meaning I can create, edit & delete them outside of Amibroker. I can also easily save or copy them to another PC.

  • My AFL is a little simpler especially if I only want to display the trade details (i.e. the notepad via the NoteGet function).

From this notepad entry which has the date, trade (buy or sell), quantity and price
image

I can get the following with the AFL below
image

A number of my explorations now include some or all of the following code,

Notepad = Line1 = "" ; // String variables 
TradeDate = at =  Quantity = Price = 0 ; // Numeric variables
Notepad = NoteGet( "" ) ; 
Line1 = StrLeft( Notepad, StrFind( Notepad, "\n" ) ) ; // First line of notepad 

if( StrFind( StrToUpper( Line1), "BUY" ) > 0 ) // If first line includes BUY extract date, quantity and price
	{
		TradeDate = StrToDateTime( StrLeft( Line1, 10 ) ) ;
		at = StrFind( Line1, "@" ) ;
		Quantity = StrToNum( StrMid( Line1, 15, at - 16 ) ) ;
		Price = StrToNum( StrMid( Line1, at + 1 ) ) ;
	}	

Age = IIf( TradeDate <= 0, 0, DateTimeDiff( DateTime(), TradeDate ) / 60 / 60 / 24 ) ; // Calculate age
Profit = iif( Price == 0, 0, ( Close - Price ) / Max( Price, 0.01 ) * 100  ) ; // Calculate profit

Filter = 1 ; 

AddColumn( Close, "Close", 1.2 ) ;
AddTextColumn( Line1, "Line 1", 1.0 ) ; 
AddColumn( TradeDate, "Trade Date", formatDateTime ) ; 
AddColumn( Age, "Age", 1.0 ) ; 
AddColumn( Quantity, "Quantity", 1.0 ) ;
AddColumn( Price, "Purchase Price", 1.2 ) ; 
AddColumn( Profit, "Profit / Loss", 1.2, colorDefault, IIf( Profit > 0, colorGreen, IIf( Profit < 0, colorRed, colorDefault ) ) ) ; 

It only works if the trades are entered in descending date (newest one first) and I use a standardised format. It also doesn't aggregate the quantity when I've made multiple purchases. I could fix this by looping through the notepad lines but I'm most interested in the last trade.

BoNeZ

6 Likes

I like this idea. Would I be correct in understanding that every entry in Notepad would need to be the exact same format for this to work? For instance, If you were to add below your example, a description of why you entered the trade or why you exited, and then added yet another trade afterward, would this exploration still be able to find the required info? That is what I primarily use the Notepad for but love the idea of pulling trade info into an exploration.

On another note (no pun intended), is it possible to write something that would just for example, add a "star" or some other indicator somewhere on the chart, possibly in the heading or something, that would identify that this particular equity indeed has notes in the notepad? I use both the Interpretation Window and the Notepad Window but many times I dont necessarily think to look at the Notepad because it is behind the Interpretation Window. When scrooling through charts it would be cool to have something idenitify that a note existed.

I enter everything into my notepad in the same format but you could use different formats then search each line for a key word ( Buy, Sell, Comment etc) and decide what to do.

I've modified my code to break the individual lines into words which allows me more control. In some explorations I just read the first line which is the most recent trade while in others aggregate multiple buys to calculate my current holdings using the following,

Notepad = NoteGet( "" ) + "\n" ;

for ( n = 1; n < 99; n++ )  // Define Line1 to Line99 as ""
  {
	VarSetText( "Line"+n,"" ) ;
  }

Word1 = Word2 = Word3 = Word4 = Word5 = "" ;
 
Quantity = Price = LineCost = TradeDate = 0 ;

for ( i = 1; i < 99; i++ )  // Read lines
	{ 
		VarSetText( "Line"+i, StrLeft( Notepad, StrFind( Notepad, "\n" ) ) ) ;
		NoteLine = StrReplace( Notepad, "\n", " " ) ; 
		for ( x = 1; x < 6; x++ ) // Read first five words on line
			{
				VarSetText( "Word"+x, StrLeft( NoteLine, StrFind( NoteLine, " " ) -1  ) ) ;     
				NoteLine = StrMid( NoteLine, StrFind( NoteLine, " " ) ) ;
			}
			if( StrToUpper( Word2 ) == "SELL" )
				{
					break ;
				}	
			if( StrToUpper( Word2 ) == "BUY" )
				{
					Quantity = Quantity + StrToNum( Word3 )  ;
					Price = StrToNum( Word5 ) ;
					if( StrToDateTime( Word1 ) > TradeDate )
						TradeDate = StrToDateTime( Word1 ) ;
				}	
		Notepad = StrMid( Notepad, StrFind( Notepad, "\n" ) ) ; 
		if( Quantity > 0 ) 
			LineCost = Price * Quantity ; 
	}

The charts are created using AFL so I'm sure it's possible.

Amibroker does allow you to create accounts where you can enter the transaction history I'm entering into the notepad but I couldn't find a way to access them in an exploration using AFL and the requirement to enter (in my case 30 years of history) in sequential order with limited undo and no editing means it just doesn't work for me. I also like having the details visible in the Notepad window.

2 Likes

Thanks for the response @BoNeZ . This is a more detailed example of how I am using Notepad. Similar but with more information I think. Do you think that with some modifications to your script, I might be able to do something similar with entries like these? I would love to be able to run an exploration on my actual trades like these and run a report similar to the report output from backtesting. At the same time, I like to log my reasons for each move I make. I do record trades in an excel journal too but it would be nice to have them an integral part of Amibroker.

51%20PM

16%20PM

1 Like

Try something like the following,

Notepad = NoteGet( "" ) + "\n" ;

for ( n = 1; n < 99; n++ ) // Define Line1 to Line99 as ""
  {
	VarSetText( "Line"+n,"" ) ;
  }

Word1 = Word2 = Word3 = Word4 = Word5 = Word6 = Comment = "" ;
 
Quantity = Price = TradeDate = 0 ;

for ( i = 1; i < 99; i++ )  // Read up to 99 lines
	{ 
		VarSetText( "Line"+i, StrLeft( Notepad, StrFind( Notepad, "\n" ) ) ) ;
		NoteLine = StrReplace( Notepad, "\n", " " ) ;
		if( StrLen( NotePad ) > 4 )
			Comment = NotePad ; 
		for ( x = 1; x < 7; x++ ) // Read first six words on line
			{
				VarSetText( "Word"+x, StrLeft( NoteLine, StrFind( NoteLine, " " ) -1  ) ) ;     
				NoteLine = StrMid( NoteLine, StrFind( NoteLine, " " ) ) ;
			}
			if( StrToUpper( Word2 ) == Name() AND StrToUpper( Word3 ) == "LONG" )
				{
					Quantity = Quantity + StrToNum( Word4 )  ;
					Price = ( StrToNum( Word6 ) ) ;
				}
			if( StrToUpper( Word2 ) == Name() AND StrToUpper( Word3 ) == "EXIT" )
				{
					Quantity = Quantity - StrToNum( Word4 )  ;
					Price = ( StrToNum( Word6 ) ) ;
				}			
	Notepad = StrMid( Notepad, StrFind( Notepad, "\n" ) ) ; 			
			
	}
	
Filter =   1 ;

AddColumn( Close, "Close", 1.2 ) ; 


AddColumn( Quantity, "Quantity", 1. ) ;  

AddColumn( Price, "Price", 1.2  ) ; 


AddTextColumn( Comment,  "Comment" ) ;


I created the notepad below and the code takes the last line as a comment plus adding the quantity when the third word in the line is "Long" and subtracting when it's "Exit"

image

You still need to change the code for MM/DD/YYY date format and need to consider how to manage when you buy, sell then buy again.

1 Like

Thanks again! I'll play around with it to see what I can do with it. Thanks for your time and giving me a little direction.

Having the details of my current open positions plus history of past trades available via AFL opens possibilities.

My broker can tell my the profit / loss of my current holdings but I can now make better comparisons. If two stocks are both up / down the same percent are they performing the same? Have I held one twice as long, has one stagnated recently while the other raced ahead. Or I can compare the performance of stocks I've held for similar times (i.e. Greater than a week, month, quarter etc).

I can also test my trading decisions. Would they have performed better of worse If I had entered or exited earlier or later.

For now I'm using the notepad to store my trading history.

1 Like