Trade File Import Indicator

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
k-hen
Posts: 8
Joined: 08 Dec 2009

Trade File Import Indicator

Postby k-hen » 16 Jun 2010

Hi All,

I wanted to be able to review my trading activity in MultiCharts so I set to work on this indicator or rather a series of indicators. Since my trade history is stored in a database - I'm able to write this data to a file and import it using ELCollections. I would really appreciate any feedback that anyone has to offer and as I incorporate the changes and suggestions I'll post the updated code back to this thread.

A word of caution that this indicator is pretty far from complete and definitely needs some performance tuning and debugging - but I figure that it's better to get it out there and get feedback early before it takes a full re-write.

Also, I don't have access to the TS forums - so if anyone has any additional ELCollections info/documentation please let me know, thanks!

The import file looks like this (I made this data up here):

Code: Select all

:Bar,symbol,side,shares,price,type
1100611.094545,JPM,B,100,38.7900,S
1100611.143044,MSFT,T,100,25.8600,S
1100611.150145,JPM,S,100,32.7900,S
1100611.152256,MSFT,B,100,22.8800,S
:Bar is the ADE BarID (EL date/time)
Type is for custom tagging different prints (order type, category, etc)

And here's the code for the indicator:

Code: Select all

//note: you probably don't want this to update every tick - check the item settings
//note: there's an issue comparing EL BarTimes so datetimes are used instead
//note: the input file is expected to be ordered by date/time

//todo: need to check for memory/performance usage and improvements
//todo: need to handle different item types
//todo: use TZOffset() functionality
//todo: file refresh isn't working properly

inputs:
my_symbol("JPM") //'all' or individual symbol
;

variables:
file_dir("C:\_KTrade\TradeData\Fills\") //directory of files to read
, debug(true) //controls print output
/////////////////////////////////////////////////////////////////////////////////////
, pn("zz_ReadItems") //process name
, ds(FormatDate("yyyyMMdd", ELDateToDateTime(currentdate))) //datestamp
, ts(FormatTime("HHmmss", ELTimeToDateTime_s(currenttime_s))) //timestamp
, lm(ds+"."+ts+":"+pn+":"+symbol+": ") //log_message for debugging
, e_file(file_dir+"(all).csv"), cfile(file_dir+ symbol +".csv") //filename
, e_map(MapSC.Share("AllItemsMap")) //shared map
, e_bar_list(ListN.New) //list of bar ids in file (date/time)
, e_symbol_list(ListS.New) //list of symbols
, e_type_list(ListS.New) //for assigning custom attributes to trade circumstances/etc.
, e_side_list(ListS.New) //buy/sell
, e_shares_list(ListN.New) //# shares/contracts
, e_price_list(ListN.New) //price
, e_index(1) //index of item in map
, e_datetime(0) //datetime of current item
, e_date(0) //date of current item
, e_time(0) //time of current item
, e_barid(0) //barID of the current item
, e_symbol("") //current item symbol
, e_type("") //current item type
, e_side("") //current item side
, e_shares(0) //current item shares
, e_price(0) //current item price

, e_bar_itemcount(0) //number of items within this bar
, e_bar_netshares(0) //number of shares within this bar
, e_bar_sumproduct(0) //sumproduct of shares/prices for this bar
, e_bar_totalshares(0) //total number of shares within this bar
, e_bar_wavgprice(0) //weighted average price of items in the bar
, e_bar_netpos(0) //net position
, use_symbol(false) //whether the current symbol is valid to display


, this_bar_datetime(0), prior_bar_datetime(0) //datetime of current bar/prior bar for comparisons
, arrow_id(0), arrow_price(0), arrow_style(3), arrow_color(0) //default arrow info
, arrow_text(""), arrow_text_color(white), arrow_down(true) //default arrow info
, arrow_price_offset(0) //default arrow info
;

if barnumber=1 or LastBarOnChart then begin //or LastBarOnChart -- need to reload this file (compare the file lengths)
cleardebug;
e_index = 1; //reset the map index

if ADE.FileExists(e_file) = false then begin;
if debug then print(lm+"'"+ e_file +"' does not exist");
end else begin
if debug then print(lm+"'"+ e_file +"' DOES exist");
value1 = MapSC.ReadFile(e_map, e_file);
if debug then print(lm+"done reading file, getting lists from map");

e_bar_list = MapSC.Get(e_map, ":Bar");
e_symbol_list = MapSC.Get(e_map, "symbol");
e_type_list = MapSC.Get(e_map, "type");
e_side_list = MapSC.Get(e_map, "side");
e_shares_list = MapSC.Get(e_map, "shares");
e_price_list = MapSC.Get(e_map, "price");

//get the first item from the file(for before the loop)
e_barid = ListN.Get(e_bar_list, e_index);
e_datetime = ELDateToDateTime(ADE.BarDate(e_barid))
+ ELTimeToDateTime(ADE.BarTime(e_barid));
if debug then print(lm+"initial map retrieval is complete");
end;
end;

//get the datetime of the current bar and prior bar (to compare to print)
this_bar_datetime = eldatetodatetime(date) + eltimetodatetime(time);
prior_bar_datetime = eldatetodatetime(date[1]) + eltimetodatetime(time[1]);

//reset item counts for this bar
e_bar_itemcount=0;
e_bar_netshares=0;
e_bar_sumproduct=0;
e_bar_totalshares=0;
e_bar_wavgprice=0;


//for this bar, loop through map items until one fits before this bar
while(e_datetime <= this_bar_datetime
and e_index <= ListN.Count(e_bar_list)
) begin

e_barid=ListN.Get(e_bar_list, e_index);
e_date=ADE.BarDate(e_barid);
e_time=ADE.BarTime(e_barid);
e_datetime=ELDateToDateTime(e_date) + ELTimeToDateTime(e_time);
e_symbol=ListS.Get(e_symbol_list, e_index);
e_type=ListS.Get(e_type_list, e_index);
e_side=ListS.Get(e_side_list, e_index);
e_shares=ListN.Get(e_shares_list, e_index);
e_price=ListN.Get(e_price_list, e_index);

if my_symbol = "ALL" or e_symbol = my_symbol then begin
use_symbol=true;
end else begin
use_symbol=false;
end;

if debug then begin
print(lm+" e_barid: "+ NumToStr(e_barid,4)
+ ", e_dt: "+ NumToStr(e_datetime,6)
+ ", tb_dt: "+ NumToStr(this_bar_datetime,6)
+ ", pb_dt: "+ NumToStr(prior_bar_datetime,6)
);
end; //debug

if e_datetime > this_bar_datetime then begin
//the item belongs after the current bar, continue to the next bar
//print(lm+" item is after this bar");
break;
end else begin
if(e_datetime < prior_bar_datetime
or use_symbol = false
) then begin
//the current item occured on or before this bar, move the next item
//print(lm+" item is before the prior bar");
end else begin
//print belongs in this bar, print/display it
//print(lm+" *** item here: "+ hp_symbol );
if debug then begin
print(lm+" e_barid: "+ NumToStr(e_barid,4)
+ ", e_type: "+ e_type
+ ", e_side: "+ e_side
+ ", e_shares: "+ NumToStr(e_shares,0)
+ ", e_price: "+ NumToStr(e_price,2)
+ ", e_date: "+ NumToStr(e_date,0)
+ ", e_time: "+ NumToStr(e_time,0)
);
end; //debug
e_bar_itemcount=e_bar_itemcount+1; //increment the item count
// calculate the weighted average of items in the bar
e_bar_sumproduct=e_bar_sumproduct+(e_shares*e_price);
e_bar_totalshares=e_bar_totalshares + e_shares;
e_bar_wavgprice=e_bar_sumproduct/e_bar_totalshares;
if e_side = "S" or e_side = "T" then begin //sell or short
e_bar_netshares = e_bar_netshares - e_shares;
end else begin //long or covering position
e_bar_netshares = e_bar_netshares + e_shares;
end; //side
e_bar_netpos = e_bar_netpos + e_bar_netshares;
end; //print before prior bar
end; //print after this bar
e_index = e_index + 1; //move to next item
end; //items loop


if e_bar_itemcount > 0 then begin;
if debug then begin
print(lm+"bn: "+ NumToStr(barnumber,0)
+ ", e_bar_itemcount: "+ NumToStr(e_bar_itemcount,0)
+ ", e_barnetshares: "+ NumToStr(e_bar_netshares,0)
+ ", e_barwavgprice: "+ NumToStr(e_bar_wavgprice,4)
);
end; //debug
arrow_style=3; //filled
arrow_text=NumToStr(e_bar_netshares/1000,1);
arrow_price_offset=.05;

if e_bar_netshares = 0 then begin //buy-sell within bar netted out to 0, grey bar
arrow_color=lightgray;
arrow_down=true;
arrow_price=low[0]-arrow_price_offset;
end else begin
if e_bar_netshares > 0 then begin //this bar netted in a long or covering position
arrow_color=green;
arrow_down=false;
arrow_price=low[0]-arrow_price_offset;
end else begin //this bar netted in a sell or shorting position
arrow_color=red;
arrow_down=true;
arrow_price=high[0]+arrow_price_offset;
end; //cp_barnetshares > 0
end;//cp_barnetshares = 0

arrow_id = arw_new_s(date, time_s, arrow_price, arrow_down);
arw_setstyle(arrow_id, arrow_style);
arw_setcolor(arrow_id, arrow_color);
arw_settext(arrow_id, arrow_text);
arw_settextcolor(arrow_id, arrow_text_color);

Plot1(e_bar_netpos, "Net Position");
end else begin

//nothing occurred in this bar,
//but may need to add value to plot here

end;

Plot2(0, "Zero Line");

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Trade File Import Indicator

Postby TJ » 07 Nov 2010

Nice work.
Thanks for sharing.

bomberone1
Posts: 310
Joined: 02 Nov 2010
Has thanked: 26 times
Been thanked: 23 times

Re: Trade File Import Indicator

Postby bomberone1 » 21 Dec 2010

Are there any upgrade?


Return to “User Contributed Studies and Indicator Library”