Close Position on the next bar

Created at 22 Jun 2016, 00:33
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
DE

DELETED_USER

Joined 08.04.2016 Blocked

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);
            }

}