Close Position on the next bar
Close Position on the next bar
22 Jun 2016, 00:33
Hi,
I have a strategy which opens a position OnBar (daily) and the signal should only be valid for 1 day, meaning I am closing the position at the end of the day. I am using the following logic which works fine in Backtesting however does not work in live trading. Do you see any issues which this code? Thanks in advance!
private bool HasPosition(Symbol symbol)
{
foreach (Position position in Positions)
{
if (position.SymbolCode == symbol.Code)
return true;
}
return false;
}
protected override void OnBar()
{
///Exit on Close
var position = Positions.Find("mylabel");
if (HasPosition(Symbol))
{
ClosePosition(position);
}
}