Advertisement IC Markets

An Event Calendar Workflow That Takes Ten Minutes a Week

Discuss 1-minute to 15-minute price action setups, fading intraday momentum, key support/resistance zones, and proven short-term trading methodologies.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by PTScalper »

Why This Fits The cTrader Workflow Better

Because of how C# handles historical data indexing on cTrader (Calculate runs through every bar historically before hitting the live feed), this script gracefully handles backtesting as well. If you scroll back on your chart, the indicator tracks backward and prints out the exact historical event boxes flawlessly, so you can manually back-test your specific pairs against previous NFP or CPI reactions without extra effort.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Recommended broker for automated trading & scalping IC Markets
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by PTScalper »

If you want to take this to a professional, institutional-grade level, the previous cTrader script was a bit too "quick and dirty." When you load an indicator onto a chart with 100,000+ historical bars, relying on basic while loops to find timestamps will block the UI thread and cause terminal lag.

To make this production-ready, we need to upgrade the architecture.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by PTScalper »

Here is the V2 Institutional Edition for cTrader. I've rewritten the C# backend to include:

1.) Binary Search Indexing O(log N): Instead of scanning bars linearly, it uses a binary search algorithm to instantly locate exact chart timestamps, meaning zero lag even if you zoom back 10 years to backtest NFP reactions.

2.) Strongly Typed State Models: Abstracted the events into an EconomicEvent class. No more messy global variables; the memory and state of each event are managed cleanly.

3.) Alpha Rendering & Vector Graphics: Uses Color.FromArgb() for clean, translucent overlays that don't obscure your price action.

4.) Enum Parameter Mapping: Clean drop-downs in the cTrader UI instead of raw text inputs.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by PTScalper »

The C# cTrader Automate Script (Pro Edition)

Code: Select all

//+------------------------------------------------------------------+
//| EventRiskManager Pro (Institutional Edition)                       |
//| Advanced Risk Window & Volatility Engine                         |
//+------------------------------------------------------------------+
using System;
using System.Collections.Generic;
using cAlgo.API;

namespace cAlgo
{
    public enum RiskAction { Flatten, ReduceSize, Monitor }

    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class EventRiskManagerPro : Indicator
    {
        #region Input Parameters

        [Parameter("Policy: Action To Take", DefaultValue = RiskAction.Flatten, Group = "1. Global Risk Policy")]
        public RiskAction DefaultPolicy { get; set; }

        [Parameter("Pre-Event Blackout (Mins)", DefaultValue = 30, Group = "1. Global Risk Policy")]
        public int MinsBefore { get; set; }

        [Parameter("Post-Event Blackout (Mins)", DefaultValue = 30, Group = "1. Global Risk Policy")]
        public int MinsAfter { get; set; }

        [Parameter("Early Warning Alert (Mins)", DefaultValue = 10, Group = "1. Global Risk Policy")]
        public int AlertMins { get; set; }

        [Parameter("Reaction Measurement (Mins)", DefaultValue = 60, Group = "1. Global Risk Policy")]
        public int ReactionMins { get; set; }

        // --- Event 1 ---
        [Parameter("Enable Event 1", DefaultValue = true, Group = "2. High-Impact Events")]
        public bool Ev1On { get; set; }
        [Parameter("Name", DefaultValue = "US CPI", Group = "2. High-Impact Events")]
        public string Ev1Name { get; set; }
        [Parameter("Time (Server Time: yyyy-MM-dd HH:mm)", DefaultValue = "2026-10-08 14:30", Group = "2. High-Impact Events")]
        public string Ev1TimeStr { get; set; }

        // --- Event 2 ---
        [Parameter("Enable Event 2", DefaultValue = true, Group = "2. High-Impact Events")]
        public bool Ev2On { get; set; }
        [Parameter("Name", DefaultValue = "FOMC Rate Decision", Group = "2. High-Impact Events")]
        public string Ev2Name { get; set; }
        [Parameter("Time (Server Time: yyyy-MM-dd HH:mm)", DefaultValue = "2026-10-08 20:00", Group = "2. High-Impact Events")]
        public string Ev2TimeStr { get; set; }

        #endregion

        // Clean Object-Oriented State Tracking
        private class EconomicEvent
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public DateTime Time { get; set; }
            public bool AlertFired { get; set; }
            public bool ReactionDrawn { get; set; }
        }

        private List<EconomicEvent> _eventRoster;

        protected override void Initialize()
        {
            _eventRoster = new List<EconomicEvent>();

            // Safely parse and register events
            RegisterEvent("1", Ev1On, Ev1Name, Ev1TimeStr);
            RegisterEvent("2", Ev2On, Ev2Name, Ev2TimeStr);

            Print($"EventRiskManager initialized: Tracking {_eventRoster.Count} major events.");
        }

        public override void Calculate(int index)
        {
            // Only process logic on the closing tick of a bar or real-time updates to save CPU cycles
            if (!IsLastBar && index < Bars.Count - 2) return;

            DateTime currentServerTime = IsLastBar ? Server.Time : Bars.OpenTimes[index];

            foreach (var ev in _eventRoster)
            {
                ProcessEventLifeCycle(ev, currentServerTime);
            }
        }

        private void ProcessEventLifeCycle(EconomicEvent ev, DateTime currentTime)
        {
            DateTime riskStart = ev.Time.AddMinutes(-MinsBefore);
            DateTime riskEnd   = ev.Time.AddMinutes(MinsAfter);
            DateTime rxnEnd    = ev.Time.AddMinutes(ReactionMins);

            // 1. REAL-TIME ALERTING
            if (IsLastBar && !ev.AlertFired)
            {
                DateTime alertThreshold = ev.Time.AddMinutes(-AlertMins);
                if (currentTime >= alertThreshold && currentTime < ev.Time)
                {
                    Notifications.PlaySound(SoundType.Ring);
                    Print($"⚠️ SYSTEM ALERT: {ev.Name} in {AlertMins} mins. Policy: {DefaultPolicy}");
                    ev.AlertFired = true;
                }
            }

            // 2. RISK ZONE RENDERING (Dark Red Overlay)
            // Draw immediately if we cross into the window, or historical backfill
            if ((currentTime >= riskStart && currentTime <= riskEnd) || (currentTime > riskEnd && Chart.FindObject($"ERM_{ev.Id}_Risk") == null))
            {
                DrawRiskOverlay(ev.Id, riskStart, riskEnd);
            }

            // 3. POST-EVENT REACTION MEASUREMENT
            if (currentTime >= rxnEnd && !ev.ReactionDrawn)
            {
                DrawReactionMetrics(ev, riskEnd, rxnEnd);
                ev.ReactionDrawn = true;
            }
        }

        #region Visual Rendering & Math Helpers

        private void DrawRiskOverlay(string id, DateTime start, DateTime end)
        {
            int startIdx = GetClosestBarIndexByBinarySearch(start);
            int endIdx   = GetClosestBarIndexByBinarySearch(end);

            if (startIdx == endIdx) return; // Prevent zero-width boxes

            // Calculate local highs/lows for clean vertical bounding
            double maxH = double.MinValue;
            double minL = double.MaxValue;
            for (int i = startIdx; i <= endIdx; i++)
            {
                if (Bars.HighPrices[i] > maxH) maxH = Bars.HighPrices[i];
                if (Bars.LowPrices[i] < minL) minL = Bars.LowPrices[i];
            }

            double padding = Symbol.PipSize * 15;
            string objName = $"ERM_{id}_Risk";
            
            var box = Chart.DrawRectangle(objName, start, maxH + padding, end, minL - padding, Color.FromArgb(35, Color.Crimson));
            box.IsFilled = true;
            box.IsInteractive = false;
        }

        private void DrawReactionMetrics(EconomicEvent ev, DateTime riskEnd, DateTime reactionEnd)
        {
            int startIdx = GetClosestBarIndexByBinarySearch(ev.Time);
            int endIdx   = GetClosestBarIndexByBinarySearch(reactionEnd);

            if (startIdx >= endIdx) return;

            double maxH = double.MinValue;
            double minL = double.MaxValue;

            for (int i = startIdx; i <= endIdx; i++)
            {
                if (Bars.HighPrices[i] > maxH) maxH = Bars.HighPrices[i];
                if (Bars.LowPrices[i] < minL) minL = Bars.LowPrices[i];
            }

            bool isBullish = Bars.ClosePrices[endIdx] >= Bars.OpenPrices[startIdx];
            Color rxnColor = isBullish ? Color.MediumSeaGreen : Color.IndianRed;
            
            // Draw Outline Box
            var rxnBox = Chart.DrawRectangle($"ERM_{ev.Id}_Rxn", Bars.OpenTimes[startIdx], maxH, Bars.OpenTimes[endIdx], minL, rxnColor);
            rxnBox.IsFilled = false;
            rxnBox.Thickness = 2;
            rxnBox.IsInteractive = false;

            // Draw Clean Meta-Data Text
            double pipRange = Math.Round((maxH - minL) / Symbol.PipSize, 1);
            string txtData  = $" {ev.Name}\n Dir: {(isBullish ? "▲ BULL" : "▼ BEAR")}\n Vol: {pipRange} pips";
            
            var txt = Chart.DrawText($"ERM_{ev.Id}_Txt", txtData, Bars.OpenTimes[endIdx], Bars.ClosePrices[endIdx], rxnColor);
            txt.VerticalAlignment = isBullish ? VerticalAlignment.Bottom : VerticalAlignment.Top;
        }

        // --- Core Optimization: Binary Search Bar Lookup ---
        // Locates a precise timestamp on the chart instantly without freezing the UI.
        private int GetClosestBarIndexByBinarySearch(DateTime targetTime)
        {
            int left = 0;
            int right = Bars.Count - 1;
            
            while (left <= right)
            {
                int mid = left + (right - left) / 2;
                if (Bars.OpenTimes[mid] == targetTime)
                    return mid;
                
                if (Bars.OpenTimes[mid] < targetTime)
                    left = mid + 1;
                else
                    right = mid - 1;
            }
            return right >= 0 ? right : 0; // Return closest previous bar if exact match not found
        }

        private void RegisterEvent(string id, bool isEnabled, string name, string timeStr)
        {
            if (!isEnabled) return;
            if (DateTime.TryParse(timeStr, out DateTime parsedTime))
            {
                _eventRoster.Add(new EconomicEvent { Id = id, Name = name, Time = parsedTime });
            }
            else
            {
                Print($"[Warning] Failed to parse Time for Event {name}. Ensure yyyy-MM-dd HH:mm format.");
            }
        }

        #endregion
    }
}
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by PTScalper »

The Architectural Upgrades Explained:

GetClosestBarIndexByBinarySearch(): This is the real game-changer. Instead of standard API calls that loop backward linearly through every candle (which chokes out the PC when you have years of 1-minute data loaded), this custom binary search cuts the lookup time down to milliseconds, keeping your platform perfectly fluid.

The List<EconomicEvent> Structure: Scaling this up is now effortless. Because the logic is completely decoupled from the parameters, if you want to track 10 events a week, you just add RegisterEvent("3", Ev3On, ...) in the Initialize() block without needing to rewrite any of the actual drawing logic.

CPU Optimization in Calculate(): I added if (!IsLastBar && index < Bars.Count - 2) return;. This stops the indicator from recalculating the exact same math on every single historical tick, executing only at candle closures or the live edge of the market, drastically reducing memory overhead.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by Fairman »

Writing the policy for each event in one line is the bit that makes it useful. No new trades 30 min either side of US CPI is a rule, careful around CPI isn't. In US summer time CPI at 8:30 New York is 1:30pm Lagos, so the line becomes no new entries from 1pm to 2pm, and existing trades either closed or with stops left alone. I also add what happens to open positions, since that's where people improvise. For the Fed statement at 7pm Lagos, my line says flat by 6:45pm on dollar pairs unless the trade is already at breakeven. Writing it on Sunday removes the decision on Wednesday.
It’s Fairman :geek:
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by Fairman »

Logging the first five minutes and first hour after each event builds a really useful reference. After a few months you know which releases actually move your pairs. Keep it simple: event, actual versus forecast, pips moved in five minutes, pips moved in an hour, and whether the first move held or reversed. You'll probably find some releases that the calendar marks as high impact barely move anything most months, while others regularly produce 40 pip moves. You'll also see how often the first spike reverses. That tells you whether to wait and trade the second move or stay out of the pair for the afternoon.
It’s Fairman :geek:
FXS HongKong Scalper
Posts: 547
Joined: Sat Oct 24, 2026 10:15 pm

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by FXS HongKong Scalper »

Tracking whether the first move held is the most useful column in my experience. In our Asian session log, Australian employment data has been one of the more reliable movers for AUD, while some releases marked as high impact rarely moved more than a few pips.

One small addition: record the spread at release time too. A release that moves 20 pips but has a 6 pip spread for the first minute is much less tradable than the pip column suggests. After a few months we had a short list of releases where waiting five minutes and trading the second move was clearly better than trading the first.
FXS Official Team | Hong Kong Session Desk
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: An Event Calendar Workflow That Takes Ten Minutes a Week

Post by Fairman »

Ten minutes a week is about what it should take, and I think that's the reason I've stuck with a version of this when I haven't with fancier routines. Mine goes in a plain note on Sunday evening. Date, time in my own zone, pair affected, and the policy: no trades, half size, or normal.

For the policy, a rule I find useful is that the window depends on the event. For payrolls and central bank decisions I use 30 minutes before and 30 after, with nothing new after the first spike. For second tier data I only avoid the five minutes either side, with half size. Hypothetically, if a week has two big events and four smaller ones, I'd have about three hours of no-entry time out of around 40 trading hours, and it prevents the worst fills of the week.

The note after each event, recording the surprise and the first five minutes against the first hour, is the part that grows into something. Mine now has about 25 entries, and I can see GBPUSD overshoots on UK CPI more often than EURUSD does on German data.

Do you set reminders ten minutes ahead as you describe, or earlier? I find ten isn't enough if I'm in the middle of a trade and need to manage it. And do you keep the entries per pair or all together?
It’s Fairman :geek:
Post Reply