I want it to close all positions, regarless of how they were opened, at 3:55 (=9:55 PM in my timezone).
I have this code in a timer event handler that ticks every second:
if (accountSelector.SelectedAccount != null)
{
DateTime thisDateTime = new DateTime(2017, 05, 25) + new TimeSpan(21, 30, 00); //TODO: Remove hardoding
if ((DateTime.Now > (thisDateTime)) && (accountSelector.SelectedAccount.Name == "MYACCOUNTNUMBER"))
{
//if the end of trading is approaching (5 minutes), flatten everything
foreach (Position thisPosition in accountSelector.SelectedAccount.Positions)
{
if (thisPosition.MarketPosition != MarketPosition.Flat)
{
// do something.
thisPosition.Close();
}
}
}
}
It does trigger and closes the position, but it then triggers many more times, I am guessing that it is triggers until the position closes, but then, as the new positions that are to close the one we're trying to close are opening, it triggers over and over...
There must be a smarter and smoother way than that... Please show me, I am eager to slap my forhead and call myself dumb.
Thank you,
Libor
Comment