Writing to File: Output occasionally duplicates row
Author: jedediah
Creation Date: 4/16/2012 4:25 PM
profile picture

jedediah

#1
O.K., bear with me, I'm not a programmer and I've only been hacking around at this thing for a couple of months. I wrote a stategy that writes the trades to a .csv file. It all works except for the end of the day exit. Occasionally, the end of day exit writes the same trade twice to the .csv file. It does not always do it, but for some trades, it always does it. For example, using the last two years of IWM, 3-minute data, I get duplicates on 1/13/2011 and 2/18/2011. I tried using the WLP6 debugger to see if it was looping twice during these trades, but it was not. So, I then used VS2010 to debug. I set break points at SellAtClose and PrintDebug, and later I set a breakpoint at csv.WriteLine. I stepped through the program at these points to see if csv.WriteLine was executing twice at any point, but it was not. I put the section of code where I think the problem is occurring in bold italic. Any help would be greatly appreciated. Thanks in advance, guys.

A.J.


CODE:
Please log in to see this code.
profile picture

Eugene

#2
The first error here is a violation of the rule saying that "don't code what you can beg, borrow, or steal (c)" ;)

SaveTrades from Community Components

Feel free to modify the source as you like to include the desired extra fields.

Other obvious errors include:

* wrapping the main trading loop inside a StreamWriter block
* checking a Position's properties after it may have already been exited without if( p != null)
* assuming that the current Position is exited when calling csv.WriteLine w/o a preceding "if"
profile picture

Eugene

#3
P.S. The SaveTrades method, as can be seen in its Wiki usage sample, uses a different pattern - so the 3 issues simply won't raise if you use SaveTrades.
profile picture

jedediah

#4
O.K., Eugene. I just downloaded the src from the wiki (I'm assuming this is the latest version), and, when I get a chance I'll poke around and see if I can get it to work. When I build the solution do I just target the WealthLab directory? Will I be able to pass my dataseries values from the trades to the SaveTrades method? Is it always wrong to wrap the main trading loop inside another code block? As far as "checking a Position's properties after it may have already been exited", I thought that LastPosition was the last created position regardless of it being open or closed. I thought that I could check its properties as long as the position I wanted was the last position....is that wrong? Again, I'm really sorry about the questions; I'm not a programmer, but I'm trying. I was really just looking for a simple fix along the lines of StreamWriter so I can get to analyzing the backtest. Thanks, again.
profile picture

jedediah

#5
I found the save trades method. It looks like I can just add my variables to trades.Write. Do I just build the solution in the WealthLab directory and replace the existind dll (backing up the existing dll, of course)? I really don't want to crash the program.
profile picture

Eugene

#6
Sure you can add your variables, but make sure the solution is compiled for .NET 2.0 (or 3.5) framework version. With WL up to 6.3 inclusive, .NET4 targeted assemblies (created by VS2010 by default) will not load and work.

p.s. It's not always wrong to wrap the main loop, rather when writing to a file - because it's easy to err with arguments and overwrite the file instead of appending etc.
profile picture

jedediah

#7
Thanks Eugene. I tried calling the SaveTrades method (I think that's the right lingo). It dumps 200+ thousand trades to the file for 1 year's worth of data of IWM. The system only generates 28 trades. I tried altering my main loop with an if(BuyAtMarket (bar+1,"") !=null) statement, but that didn't help. What am I doing wrong, now? I bolded the main loop.

CODE:
Please log in to see this code.


profile picture

Eugene

#8
Don't call SaveTrades from inside the main loop. You've placed it there along with PlotSeries/HideVolume.
profile picture

jedediah

#9
Thanks. That seems to have fixed the problem. I even had to move PlotSeries out of there. I may have another question later about compiling the dll. I ran into a problem with the FileVersion, I believe. Anyway, if I can't figure it out, I'll post the question here. Thanks, again, for your help.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).