I am trying to build a strategy that will reliably trade at the close of the bar, however I'm running into some random... discrepancies, i.e. I'm telling the timer to print each 20 seconds or so a warning, and 2 seconds before the bar close to print out something else.
What happens, is that it sometimes, quite inconsistently, doesn't print the warnings every 20 secods, and skips directly to printing the message at the end of the bar.
Would anyone have any idea why this behavior?
Please find the relevant pieces of code below, inspired greatly by the BarTimer indicator:
private DateTime lastTimePlot = Cbi.Globals.MinDate; private System.Windows.Forms.Timer timer; private int iSecondsLeft = 0; private bool bCloseEnough = false; private bool bSendWarning = false; protected override void Initialize() { CalculateOnBarClose = false; } protected override void OnStartUp() { if (timer == null) { timer = new System.Windows.Forms.Timer(); timer.Interval = 1000; timer.Tick += new EventHandler(OnTimerTick); timer.Enabled = true; } } protected override void OnTermination() { if (timer != null) { timer.Enabled = false; timer = null; } } private void OnTimerTick(object sender, EventArgs e) { if (Historical) return; /*if (DateTime.Now.Subtract(lastTimePlot).Seconds >= 1) {*/ lastTimePlot = DateTime.Now; TimeSpan barTimeLeft = Bars.GetTime(Bars.Count - 1).Subtract(Now); //iSecondsLeft = (barTimeLeft.Ticks < 0 ? 0 : Convert.ToInt16(barTimeLeft.Minutes * 60 + barTimeLeft.Seconds)); iSecondsLeft = Convert.ToInt16(barTimeLeft.Minutes * 60 + barTimeLeft.Seconds); bCloseEnough = (Math.Abs(iSecondsLeft) < 2 ? true : false); bSendWarning = (iSecondsLeft > 20 && iSecondsLeft % 20 < 5 ? true : false); if (bCloseEnough) Print("Do the trade in the 4 seconds of the bar around the close! --> " + iSecondsLeft + "//" + Time[0] + "|| The time now is = " + DateTime.Now); if (bSendWarning) Print("Send a warning, every 20 seconds or so --> " + iSecondsLeft + "//" + Time[0] + "|| The time now is = " + DateTime.Now); //} } private DateTime Now { get { DateTime now = (Bars.MarketData.Connection.Options.Provider == Cbi.Provider.Replay ? Bars.MarketData.Connection.Now : DateTime.Now); if (now.Millisecond > 0) now = Cbi.Globals.MinDate.AddSeconds((long) Math.Floor(now.Subtract(Cbi.Globals.MinDate).TotalSeconds)); return now; } }
Please also find the content of the output window below:
Send a warning, every 20 seconds or so --> 44//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:16 Send a warning, every 20 seconds or so --> 43//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:17 Send a warning, every 20 seconds or so --> 42//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:18 Send a warning, every 20 seconds or so --> 41//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:19 Send a warning, every 20 seconds or so --> 40//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:20 Send a warning, every 20 seconds or so --> 24//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:36 Send a warning, every 20 seconds or so --> 23//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:37 Send a warning, every 20 seconds or so --> 22//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:38 Send a warning, every 20 seconds or so --> 21//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:39 Do the trade in the 4 seconds of the bar around the close! --> 1//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:51:59 Do the trade in the 4 seconds of the bar around the close! --> 0//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:52:00 Do the trade in the 4 seconds of the bar around the close! --> -1//19/03/2012 16:52:00|| The time now is = 19/03/2012 16:52:01 Send a warning, every 20 seconds or so --> 42//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:18 Send a warning, every 20 seconds or so --> 41//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:19 Send a warning, every 20 seconds or so --> 40//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:20 Send a warning, every 20 seconds or so --> 24//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:36 Send a warning, every 20 seconds or so --> 23//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:37 Send a warning, every 20 seconds or so --> 22//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:38 Send a warning, every 20 seconds or so --> 21//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:39 Do the trade in the 4 seconds of the bar around the close! --> 1//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:53:59 Do the trade in the 4 seconds of the bar around the close! --> 0//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:54:00 Do the trade in the 4 seconds of the bar around the close! --> -1//19/03/2012 16:54:00|| The time now is = 19/03/2012 16:54:01 Do the trade in the 4 seconds of the bar around the close! --> 1//19/03/2012 16:55:00|| The time now is = 19/03/2012 16:54:59 Do the trade in the 4 seconds of the bar around the close! --> 0//19/03/2012 16:55:00|| The time now is = 19/03/2012 16:55:00 Do the trade in the 4 seconds of the bar around the close! --> -1//19/03/2012 16:55:00|| The time now is = 19/03/2012 16:55:01 Send a warning, every 20 seconds or so --> 24//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:36 Send a warning, every 20 seconds or so --> 23//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:37 Send a warning, every 20 seconds or so --> 22//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:38 Send a warning, every 20 seconds or so --> 21//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:39 Do the trade in the 4 seconds of the bar around the close! --> 1//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:55:59 Do the trade in the 4 seconds of the bar around the close! --> 0//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:56:00 Do the trade in the 4 seconds of the bar around the close! --> -1//19/03/2012 16:56:00|| The time now is = 19/03/2012 16:56:01 Send a warning, every 20 seconds or so --> 44//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:16 Send a warning, every 20 seconds or so --> 43//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:17 Send a warning, every 20 seconds or so --> 42//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:18 Send a warning, every 20 seconds or so --> 41//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:19 Send a warning, every 20 seconds or so --> 40//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:20 Send a warning, every 20 seconds or so --> 24//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:36 Send a warning, every 20 seconds or so --> 22//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:38 Send a warning, every 20 seconds or so --> 21//19/03/2012 16:57:00|| The time now is = 19/03/2012 16:56:39
D.
Comment