I manually check for exit conditions in my strategy, and one line before calling ExitLong/Short(), I call a method that exports the values. For the entry price, I use Position.AveragePrice. For time of entry, I use Time[BarsSinceEntry()-1]. When I view the exported results, the entry times I get vs the trade info in the Trades tab of strategy analyzer are close, but off by a few seconds. The entry prices are off significantly, by 6+ points. Interestingly, even though the entry/ exit prices are off by several points, the profit in points appears to be accurate for all of them (at least the 10 or so I checked).
So my questions are, why the wide discrepancy, and what am I doing wrong, and/ or what can be done to fix it?
As an example, based on info in the Trades tab, I enter short @ 1302.5 at 1/26/12, 10:00:37 AM. In my exported info, I enter short @ 1308.75 at 1/26/12, 10:01:00 AM. My code is below..
public class EminiRenko3Bars013013 : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
MarketDataExporter exp;
string tableName;
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
tableName = "EminiRenko3Bars013013";
exp = new MarketDataExporter(tableName);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBars[0] <= BarsRequired) return;
if(Position.MarketPosition == MarketPosition.Flat && Time[0].Hour != 12 && (ToTime(Time[0])<=160000))
{
if(Close[0] > Open[0] && Close[1] > Open[1] && Close[2] > Open[2] && Close[3] < Open[3])
{
EnterLong();
}
else if(Close[0] < Open[0] && Close[1] < Open[1] && Close[2] < Open[2] && Close[3] > Open[3])
{
EnterShort();
}
}
else
{
if(Position.MarketPosition == MarketPosition.Long && Close[0] < Open[0] && Close[1] < Open[1])
{
WriteStatsToDb();
ExitLong();
}
else if(Position.MarketPosition == MarketPosition.Short && Close[0] > Open[0] && Close[1] > Open[1])
{
WriteStatsToDb();
ExitShort();
}
}
}
void WriteStatsToDb()
{
//Entry time variables
string longOrshort = Position.MarketPosition.ToString();
double entryPrice = Position.AvgPrice;
double exitPrice = Close[0];
double profitInPoints;
if(Position.MarketPosition == MarketPosition.Long)
profitInPoints = exitPrice - entryPrice;
else
profitInPoints = entryPrice - exitPrice;
DateTime entryDate = Time[BarsSinceEntry() - 1];
DateTime exitDate = Time[0];
double ema5 = EMA(5)[BarsSinceEntry() - 1];
double ema10 = EMA(10)[BarsSinceEntry() - 1];
double ema20 = EMA(20)[BarsSinceEntry() - 1];
double ema50 = EMA(50)[BarsSinceEntry() - 1];
double adx5 = ADX(5)[BarsSinceEntry() - 1];
double adx10 = ADX(10)[BarsSinceEntry() - 1];
double adx20 = ADX(20)[BarsSinceEntry() - 1];
double adx50 = ADX(50)[BarsSinceEntry() - 1];
double linReg5 = LinReg(5)[BarsSinceEntry() - 1];
double linReg10 = LinReg(10)[BarsSinceEntry() - 1];
double linReg20 = LinReg(20)[BarsSinceEntry() - 1];
double linReg50 = LinReg(50)[BarsSinceEntry() - 1];
double rsi14Per3AvgRsi = RSI(14,3)[BarsSinceEntry() - 1];
double rsi14Per3AvgAvg = RSI(14,3).Avg[BarsSinceEntry() - 1];
double daysHigh = High[HighestBar(High,Bars.BarsSinceSession - 1)];
double daysLow = Low[LowestBar(Low,Bars.BarsSinceSession - 1)];
exp.Add("LongOrShort;Char;" + longOrshort + "!" +
"EntryPrice;Decimal;" + entryPrice + "!" +
"ExitPrice;Decimal;" + exitPrice + "!" +
"ProfitInPoints;Decimal;" + profitInPoints + "!" +
"EntryDate;Date;" + entryDate + "!" +
"ExitDate;Date;" + exitDate + "!" +
"Ema5;Decimal;" + ema5 + "!" +
"Ema10;Decimal;" + ema10 + "!" +
"Ema20;Decimal;" + ema20 + "!" +
"Ema50;Decimal;" + ema50 + "!" +
"Adx5;Decimal;" + adx5 + "!" +
"Adx10;Decimal;" + adx10 + "!" +
"Adx20;Decimal;" + adx20 + "!" +
"Adx50;Decimal;" + adx50 + "!" +
"LinReg5;Decimal;" + linReg5 + "!" +
"LinReg10;Decimal;" + linReg10 + "!" +
"LinReg20;Decimal;" + linReg20 + "!" +
"LinReg50;Decimal;" + linReg50 + "!" +
"Rsi14Per3AvgRsi;Decimal;" + rsi14Per3AvgRsi + "!" +
"Rsi14Per3AvgAvg;Decimal;" + rsi14Per3AvgAvg + "!" +
"DaysHigh;Decimal;" + daysHigh + "!" +
"DaysLow;Decimal;" + daysLow);
}
protected override void OnTermination()
{
exp.WriteMarketDataRecordsToDb();
}

Comment