Project: Print Trade List to csv

Questions about MultiCharts and user contributed studies.
User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Project: Print Trade List to csv

Postby MAtricks » 03 Mar 2015

Since exporting the trade list takes so long, I'd like to add a "Printer" strategy to the window of each strategy. This Printer would write out a trade list similar to what we receive in the Performance Reports. However, some entries will be reversals, some will be limits fills, stop fills, and some exits will flatten the position 0. In a reversal I'd like to separate the Exit order contracts and exit prices from the Entry order contracts and entry prices. Sample from performance reports:

Image

Has anyone worked on something similar? I think that a project like this might help those who are wanting a separate form of exporting the trade lists instead of waiting 2343242 hours for the export a Performance Report.

I started working on this and realized that our key words do not help much with this project. I would love for someone with more know-how to lend a hand at jump starting this project. Here was my start (which is incorrectly constructed):

Code: Select all

VARIABLES:
intrabarpersist MPos( 0 ),
Logger("C:\Users\Name\Desktop\TradeList"),
vShares( 0 ) ;

MPos = MarketPosition ;
vShares = MPos-MPos[1] ;

Once begin
Print(File(Logger), "Order;DateTime;Contracts;Price;Profit" ) ;
end ;


//Reverse to Long
if MPos[1]<0 and MPos>0 then begin
Print(File(Logger),"Short Exit",";",ExitDateTime(0),";",vShares,";",ExitPrice(0),";",PositionProfit(1) ) ;
Print(File(Logger),"Long Entry",";",EntryDateTime(0),";",vShares,";",EntryPrice(0),";",0 ) ;
end else
//Reverse to Short
if MPos[1]>0 and MPos<0 then begin
Print(File(Logger),"Long Exit",";",ExitDateTime(0),";",vShares,";",ExitPrice(0),";",PositionProfit(1) ) ;
Print(File(Logger),"Short Entry",";",EntryDateTime(0),";",vShares,";",EntryPrice(0),";",0 ) ;
end else
//Entry Long
if MPos[1]=0 and MPos>0 then
Print(File(Logger),"Long Entry",";",EntryDateTime(0),";",vShares,";",EntryPrice(0),";",0 )
else
//Enter Short
if MPos[1]=0 and MPos<0 then
Print(File(Logger),"Short Entry",";",EntryDateTime(0),";",vShares,";",EntryPrice(0),";",0 )
else
//Exit Long
if MPos[1]>0 and MPos=0 then
Print(File(Logger),"Long Exit",";",ExitDateTime(0),";",vShares,";",ExitPrice(0),";",PositionProfit(1) )
else
//Exit Short
if MPos[1]<0 and MPos=0 then
Print(File(Logger),"Short Exit",";",ExitDateTime(0),";",vShares,";",ExitPrice(0),";",PositionProfit(1) ) ;
Attachments
2015-03-03_17-26-53.png
(18.92 KiB) Downloaded 1180 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Project: Print Trade List to csv

Postby JoshM » 04 Mar 2015

However, some entries will be reversals, some will be limits fills, stop fills, and some exits will flatten the position 0. In a reversal I'd like to separate the Exit order contracts and exit prices from the Entry order contracts and entry prices.
I think you can retrieve this with the PosTrade* keywords. I've done it like this:

Code: Select all

entryType = PosTradeEntryCategory(1, y);
typeOfEntryOrder = PosTradeOrderCategory(entryType);
(...)
exitType = PosTradeExitCategory(1, y);
typeOfExitOrder = PosTradeOrderCategory(exitType);
With the `PosTradeOrderCategory()` function implemented as follows:

Code: Select all

Inputs:
OrderCategory(NumericSimple);

switch (OrderCategory) begin

case 3:
PosTradeOrderCategory = "MKT";

case 2:
PosTradeOrderCategory = "LMT";

case 1:
PosTradeOrderCategory = "STP";

case 4:
PosTradeOrderCategory = "MOC";

case 5:
PosTradeOrderCategory = "MOO";

case 8:
PosTradeOrderCategory = "STPLMT";

default:
PosTradeOrderCategory = "N/A";

end;
Order names (like "LE-Reveral" or "LE-MAcross") can be retrieved with a code like the following:

Code: Select all

nameOfEntry = PosTradeEntryName(1, y);
(...)
nameOfExit = PosTradeExitName(1, y);
The `y` variable in these snippets is by the way a looping variable, defined as:

Code: Select all

numTradesPos = PosTradeCount(1);
for y = 0 to (numTradesPos - 1) begin

// access PosTrade info here

end;
Has anyone worked on something similar?
I did, but it's over 1500 lines long (backtesting, optimizing, and walk-forward), not my best work, a couple of years old, and filled with non-English comments. I don't think I'll help anyone by dumping that here. :) Do you have specific parts you need help with?
I started working on this and realized that our key words do not help much with this project.
I think you can practically calculate/export anything, including things like MFE, MAE, stop-loss, profit target, ATR at entry (for an indication of volatility), trade number, entry date, time in trade, number of parameters, parameter names (for keeping track of optimisations), etc. It depends on how much you want to code and how much info is needed.

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Re: Project: Print Trade List to csv

Postby MAtricks » 04 Mar 2015

JoshM,

Thank you for the detailed response. I didn't know about some of those keywords so this helps a lot. Also, thank you for the idea of a switch for the order type.

My goal is to replicate the Trade List tab which is exported with a backtest--not completely, but the important parts being:
  • Order type (Long Entry, Long Exit, Short Entry, Short Exit)
    Date
    Time
    Number of Contracts (Entry contracts separated from Exit contracts)
    Price of Executions
    Profit of round turn or at each exit
Export from MC backtest which is exactly what I'd like to Print with this strategy:
Image

The difficult part is separating the reversals into buys & sells and sellshorts and buytocovers.

This all would be written in a *separate* strategy which is also applied to the chart which you would like to pull the tradelist from.

Mark from hightick.com and whos also popular on the TS forum as MarkSanDiego has created something which is much more than I'd want, but its written in OOEL... :( http://hightick.com/TS/Strate ... index.html
Attachments
2015-03-04_8-57-47.png
(44.52 KiB) Downloaded 1158 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Project: Print Trade List to csv

Postby JoshM » 06 Mar 2015

Code: Select all

if MPos[1]<0 and MPos>0 then begin
(...)
if MPos[1]>0 and MPos<0 then begin
(...)
if MPos[1]=0 and MPos>0 then
(...)
if MPos[1]=0 and MPos<0 then
(...)
if MPos[1]>0 and MPos=0 then
(...)
if MPos[1]<0 and MPos=0 then
Does this catch all trades for you? It doesn't for me; trades that are reversed on the same bar are missed by it.

But my approach with IOG isn't working either, unless backtesting with a 1 tick Bar Magnifier is okay, but then one of the goals of this project (faster backtesting and exporting) will probably not be achieved.

justme
Posts: 56
Joined: 08 Jan 2015
Has thanked: 9 times
Been thanked: 3 times

Re: Project: Print Trade List to csv

Postby justme » 07 Mar 2015

Hey guys-

it's not the MarketPosition change that matters here But the TotalTrades change that matters. This should capture all the trades.

Add something like this to the bottom of your code.

Code: Select all

var: Intrabarpersist PriorTotTrades(0),Decimals(4), LogPath("C:\Trades.csv");

if LastBarOnChart then begin

If PriorTotTrades<>Totaltrades then begin

value1=totalTrades-PriorTotTrades;

While Value1 >0 begin
FileAppend(LogPath,symbol +","+ FormatDate( "yyyyMMdd", ElDateToDateTime( entrydate(Value1) )) +","+ numtostr(entrytime(Value1),0) +","+ numtostr(MarketPosition(Value1),0) +","+ numtostr(entryPrice(Value1),Decimals) +","+ numtostr(entryPrice(Value1),Decimals) +","+ FormatDate( "yyyyMMdd", ElDateToDateTime( exitdate(Value1) )) +","+ numtostr(exittime(Value1),0) +","+ numtostr(exitPrice(Value1),Decimals) +"," + Numtostr(PositionProfit(Value1),2) + newline);

Value1 = Value1-1;
End;
PriorTotTrades=Totaltrades;

end;

end;
if anyone improves upon it, please post it.

Gordon

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Project: Print Trade List to csv

Postby JoshM » 07 Mar 2015

it's not the MarketPosition change that matters here But the TotalTrades change that matters. This should capture all the trades.
Thanks for making that point :), it remembered me of the difference between trades and positions, which don't always are the same.
Export from MC backtest which is exactly what I'd like to Print with this strategy:
Image
How do you want to format the table in the csv file? In the Performance Report there are two rows for each trade (entry and exit), which we can also do with outputting of course. But the 'Profit ($)' column combines two cells -- which we cannot do with outputting to a file.

Two possible workarounds (there may be more):
* Export the profit only for the second trade, and an empty cell for the first trade.
* Put one trade on one line of the outputted file, like so:

Code: Select all

Type;Signal;EntryDate;EntryTime;ExitDate;ExitTime;EntryPrice;ExitPrice;Contracts;Profit/loss
ExitLong;Buy;3/5/2015;11:15:00;3/5/2015;12:45:00;3591.8;3601.8;1;15.26
ExitLong;Buy;3/5/2015;12:00:00;3/5/2015;12:45:00;3596.8;3601.8;1;7.63
ExitShort;Short;3/5/2015;18:00:00;3/5/2015;19:30:00;3616.8;3615.8;1;1.53
ExitShort;Short;3/6/2015;08:30:00;3/6/2015;10:00:00;3614.8;3612.8;1;3.05
ExitShort;Short;3/6/2015;11:00:00;3/6/2015;11:30:00;3617.8;3616.8;1;1.52
ExitShort;Buy;3/6/2015;11:30:00;3/6/2015;11:45:00;3616.8;3616.8;1;0.00
ExitShort;Short;3/6/2015;11:45:00;3/6/2015;12:30:00;3616.8;3618.8;1;-3.05
ExitShort;Short;3/6/2015;12:15:00;3/6/2015;12:30:00;3617.8;3618.8;1;-1.52
ExitLong;Buy;3/6/2015;12:30:00;3/6/2015;14:00:00;3618.8;3623.8;1;7.62
(This messes up the 'Type' column)

I think the best option might be the one that works the best with your existing Excel sheets, or however you want to process the trades.

User avatar
MAtricks
Posts: 789
Joined: 09 Apr 2012
Has thanked: 286 times
Been thanked: 288 times

Re: Project: Print Trade List to csv

Postby MAtricks » 08 Mar 2015

How do you want to format the table in the csv file? In the Performance Report there are two rows for each trade (entry and exit), which we can also do with outputting of course. But the 'Profit ($)' column combines two cells -- which we cannot do with outputting to a file.

Two possible workarounds (there may be more):
* Export the profit only for the second trade, and an empty cell for the first trade.
* Put one trade on one line of the outputted file, like so:
Thank you for helping on this.

I think that the P/L difference should be calculated at the trade exit and should be 0 or blank for the entry.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Project: Print Trade List to csv

Postby JoshM » 21 Mar 2015

Here's what I have so far:

Code: Select all

Inputs:
FileLocation("C:\Temp\");

Variables:
fileName(
Text(FileLocation, "\",
GetSymbolName, "_",
FormatDate("d-M-yy", ComputerDateTime), "_",
FormatTime("HH;mm;ss", ComputerDateTime))),
posType(""),
outputStr(""),
tmpOutputStr(""),
sep(";"), // The separator for the text file
IntraBarPersist prevMP(0),
MP(0),
IntraBarPersist prevTrades(0),
tradesDone(0),
decimals(Round(Log(PriceScale) / Log(10), 0)),

x(0), y(0), z(0) // Reserved for loops
;

once ClearDebug;

once (BarStatus(1) = 2) begin

// Generate header of file
outputStr = Text(
"Type", sep,
"Signal", sep,
"Date", sep,
"Time", sep,
"Price", sep,
"Contracts", sep,
"Profit", NewLine);

end;


MP = MarketPosition(0) * CurrentContracts;

if ((prevMP < 0) and (MP > 0)) or
((prevMP > 0) and (MP < 0)) or
((prevMP <> 0) and (MP = 0)) then begin

tradesDone = TotalTrades - prevTrades;

tmpOutputStr = "";

// Type
if (MP = 0) and (prevMP > 0) then
posType = "ExitLong"
else if (MP = 0) and (prevMP < 0) then
posType = "ExitShort"
else if (MP > 0) and (prevMP = 0) then
posType = "EnterLong"
else if (MP < 0) and (prevMP = 0) then
posType = "EnterShort";

// Loop through trades done
// *This won't 'catch' open trades
for x = 0 to tradesDone - 1 begin

// Create table's entry row
if (MP > 0) and (prevMP = 0) then
posType = "EnterLong"
else if (MP < 0) and (prevMP = 0) then
posType = "EnterShort";

tmpOutputStr = Text(
postype, // Type
sep,
// PosTradeEntryName(1, x), // Signal name
sep,
FormatDate("M/d/yyyy", PosTradeEntryDateTime(1, x)), // Entry date
sep,
FormatTime("HH:mm:ss", PosTradeEntryDateTime(1, x)), // Entry time
sep,
NumToStr(PosTradeEntryPrice(1, x), decimals), // Entry price
sep,
NumToStr(PosTradeSize(1, x), 0), // Contracts
sep,
"", // Profit = blank for entry
NewLine
);

// Create table's exit row
if (MP = 0) and (prevMP > 0) then
posType = "ExitLong"
else if (MP = 0) and (prevMP < 0) then
posType = "ExitShort";

tmpOutputStr = Text(tmpOutputStr,

posType, // Type
sep,
// PosTradeExitName(1, x), // Signal name
sep,
FormatDate("M/d/yyyy", PosTradeExitDateTime(1, x)), // Exit date
sep,
FormatTime("HH:mm:ss", PosTradeExitDateTime(1, x)), // Exit time
sep,
NumToStr(PosTradeExitPrice(1, x), decimals), // Exit price
sep,
NumToStr(PosTradeSize(1, x), 0), // Contracts
sep,
NumToStr(PosTradeProfit(1, x), 2), // Profit
NewLine
);

Print(tmpOutputStr);

end;

end;

prevMP = mp;
prevTrades = TotalTrades;
It's not fully complete yet, but perhaps it can help others. Due to time constraints I don't think I have time soon to complete it myself.


Return to “MultiCharts”