Syntax
public static void SaveClosedTrades(this WealthScript obj, string path, bool savePositionType = false)
public void SaveClosedTrades( string path, bool savePositionType = false )
Parameter Description
path | Path to the resulting CSV file |
Description
A simple procedure that dumps closed position data to a CSV (comma-separated) file.
The format is:
SymbolName,EntryDate,EntryPrice,EntrySignal,ExitDate,ExitPrice,ExitSignal,The value of Indicator of choice at EntryDate
It's possible to introduce custom output formats by modifying the method body and re-compiling the assembly on your own.
Optional argument "savePositionType" allows to save the Position type (Long, Short)
Note: also see
SaveTrades that makes possible to save both closed and open trades, or just open/closed.
Example
This is an example code that illustrates how to call the procedure inside your Strategy:
Example using C# extension methods:
using System;
using System.Text;
using WealthLab;
using WealthLab.Indicators;
namespace WealthLab.Strategies
{
public class MyStrategy : WealthScript
{
protected override void Execute()
{
for(int bar = 45; bar < Bars.Count; bar++)
{
if (IsLastPositionActive)
{
if ( CrossUnder( bar, RSI.Series( Close, 14 ), 70 ) )
SellAtMarket( bar+1, Position.AllPositions, "exit" );
}
else
{
if ( CrossOver( bar, RSI.Series( Close, 14 ), 50 ) )
if( BuyAtMarket( bar+1, "RSI" ) != null )
// Store indicator data in the position's tag property
LastPosition.Tag = Bars.FormatValue( RSI.Series( Close, 14 )[bar] );
}
}
// Specify destination file
string path = @"C:\temp\Trades.csv";
// Call SaveClosedTrades, passing the destination file
this.SaveClosedTrades( path );
}
}
}
Legacy syntax example:
using System;
using System.Text;
using WealthLab;
using WealthLab.Indicators;
using Community.Components; // SaveClosedTrades here
/*** Requires installation of Community.Components Extension from www.wealth-lab.com > Extensions ***/
namespace WealthLab.Strategies
{
public class MyStrategy : WealthScript
{
protected override void Execute()
{
// Create an instance of the Utility class and pass WealthScript as "this"
Utility u = new Utility( this );
for(int bar = 45; bar < Bars.Count; bar++)
{
if (IsLastPositionActive)
{
if ( CrossUnder( bar, RSI.Series( Close, 14 ), 70 ) )
SellAtMarket( bar+1, Position.AllPositions, "exit" );
}
else
{
if ( CrossOver( bar, RSI.Series( Close, 14 ), 50 ) )
if( BuyAtMarket( bar+1, "RSI" ) != null )
// Store indicator data in the position's tag property
LastPosition.Tag = Bars.FormatValue( RSI.Series( Close, 14 )[bar] );
}
}
// Specify destination file
string path = @"C:\temp\Trades.csv";
// Call SaveClosedTrades, passing the destination file
u.SaveClosedTrades( path );
}
}
}