Code: Select all
using System;
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class ExecutionCostJournalPro : Indicator
{
// =========================================================================
// PARAMETERS
// =========================================================================
[Parameter("Cheap Spread Ceiling (Pips)", DefaultValue = 0.4, Group = "Microstructure")]
public double CheapSpreadPips { get; set; }
[Parameter("Expensive Spread Floor (Pips)", DefaultValue = 1.1, Group = "Microstructure")]
public double ExpensiveSpreadPips { get; set; }
[Parameter("London Open Hour (UTC)", DefaultValue = 8, Group = "Time Windows")]
public int LonOpenHour { get; set; }
[Parameter("NY Open Hour (UTC)", DefaultValue = 13, Group = "Time Windows")]
public int NyOpenHour { get; set; }
[Parameter("Open Friction Duration (Mins)", DefaultValue = 15, Group = "Time Windows")]
public int OpenDurationMins { get; set; }
// =========================================================================
// ENUMS & STATE
// =========================================================================
public enum CostBucket { Cheap, Normal, Expensive }
public enum SetupType { None, BullishSweep, BearishSweep }
// UI Elements
private Border _hudContainer;
private TextBlock _spreadText;
private TextBlock _costBucketText;
private TextBlock _executionText;
// =========================================================================
// INITIALIZATION
// =========================================================================
protected override void Initialize()
{
BuildControlPanel();
}
// =========================================================================
// CORE CALCULATION LOOP
// =========================================================================
public override void Calculate(int index)
{
if (index < 2) return;
// 1. Evaluate Current Hard-Right-Edge (Live Tick Data)
if (IsLastBar)
{
double liveSpread = Math.Round(Symbol.Spread / Symbol.PipSize, 1);
CostBucket liveCost = EvaluateCostState(Server.Time, liveSpread);
UpdateControlPanel(liveSpread, liveCost);
}
// 2. Evaluate Historical Setup Conditions
SetupType currentSetup = DetectPriceAction(index);
if (currentSetup != SetupType.None)
{
// Proxy historical spread as 0 to force Time-Window evaluation
CostBucket historicalCost = EvaluateCostState(Bars.OpenTimes[index], 0);
DrawSetupTag(index, currentSetup, historicalCost);
}
}
// =========================================================================
// MICROSTRUCTURE LOGIC
// =========================================================================
private CostBucket EvaluateCostState(DateTime time, double spreadPips)
{
// Time-based execution friction takes precedence
bool isHighFrictionWindow = (time.Hour == LonOpenHour || time.Hour == NyOpenHour)
&& time.Minute < OpenDurationMins;
if (isHighFrictionWindow || (spreadPips >= ExpensiveSpreadPips && spreadPips > 0))
return CostBucket.Expensive;
if (spreadPips > 0 && spreadPips <= CheapSpreadPips)
return CostBucket.Cheap;
return CostBucket.Normal;
}
// =========================================================================
// STRATEGY LOGIC (ISOLATED)
// =========================================================================
private SetupType DetectPriceAction(int index)
{
// Replace with your actual edge (Engulfing used as placeholder)
bool isBullish = Bars.ClosePrices[index] > Bars.OpenPrices[index] &&
Bars.ClosePrices[index - 1] < Bars.OpenPrices[index - 1] &&
Bars.ClosePrices[index] > Bars.OpenPrices[index - 1] &&
Bars.OpenPrices[index] < Bars.ClosePrices[index - 1];
bool isBearish = Bars.ClosePrices[index] < Bars.OpenPrices[index] &&
Bars.ClosePrices[index - 1] > Bars.OpenPrices[index - 1] &&
Bars.ClosePrices[index] < Bars.OpenPrices[index - 1] &&
Bars.OpenPrices[index] > Bars.ClosePrices[index - 1];
if (isBullish) return SetupType.BullishSweep;
if (isBearish) return SetupType.BearishSweep;
return SetupType.None;
}
// =========================================================================
// CHART RENDERING (TAGS)
// =========================================================================
private void DrawSetupTag(int index, SetupType setup, CostBucket cost)
{
string objName = $"PA_Tag_{index}";
Color tagColor = GetCostColor(cost);
string routingRule = cost == CostBucket.Expensive ? "LIMIT ONLY" : "MARKET OK";
if (setup == SetupType.BullishSweep)
{
double yPos = Bars.LowPrices[index] - (12 * Symbol.PipSize);
var text = Chart.DrawText(objName, $"BULL\n{routingRule}", index, yPos, tagColor);
text.VerticalAlignment = VerticalAlignment.Top;
text.HorizontalAlignment = HorizontalAlignment.Center;
text.FontSize = 9;
text.IsBold = true;
}
else if (setup == SetupType.BearishSweep)
{
double yPos = Bars.HighPrices[index] + (12 * Symbol.PipSize);
var text = Chart.DrawText(objName, $"BEAR\n{routingRule}", index, yPos, tagColor);
text.VerticalAlignment = VerticalAlignment.Bottom;
text.HorizontalAlignment = HorizontalAlignment.Center;
text.FontSize = 9;
text.IsBold = true;
}
}
// =========================================================================
// UI CONSTRUCTION & UPDATES
// =========================================================================
private void BuildControlPanel()
{
var stackPanel = new StackPanel { Orientation = Orientation.Vertical, Margin = 10 };
// UI Header
stackPanel.AddChild(new TextBlock
{
Text = "EXECUTION MICROSTRUCTURE",
ForegroundColor = Color.White,
FontWeight = FontWeight.Bold,
Margin = new Thickness(0, 0, 0, 5)
});
_spreadText = new TextBlock { ForegroundColor = Color.LightGray, Margin = new Thickness(0, 2, 0, 2) };
_costBucketText = new TextBlock { ForegroundColor = Color.White, FontWeight = FontWeight.Bold };
_executionText = new TextBlock { ForegroundColor = Color.White, Margin = new Thickness(0, 2, 0, 0) };
stackPanel.AddChild(_spreadText);
stackPanel.AddChild(_costBucketText);
stackPanel.AddChild(_executionText);
_hudContainer = new Border
{
BackgroundColor = Color.FromArgb(220, 25, 25, 25), // Semi-transparent dark gray
BorderColor = Color.DimGray,
BorderThickness = 1,
CornerRadius = 3,
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Top,
Margin = "20 20 0 0",
Child = stackPanel
};
Chart.AddControl(_hudContainer);
}
private void UpdateControlPanel(double currentSpread, CostBucket currentCost)
{
_spreadText.Text = $"Live Spread: {currentSpread} pips";
_costBucketText.Text = $"Phase: {currentCost.ToString().ToUpper()}";
_costBucketText.ForegroundColor = GetCostColor(currentCost);
_executionText.Text = currentCost == CostBucket.Expensive
? "ROUTING: LIMIT ORDERS REQUIRED"
: "ROUTING: MARKET ORDERS AUTHORIZED";
_executionText.ForegroundColor = currentCost == CostBucket.Expensive ? Color.Crimson : Color.LimeGreen;
}
private Color GetCostColor(CostBucket cost)
{
switch (cost)
{
case CostBucket.Cheap: return Color.LimeGreen;
case CostBucket.Normal: return Color.DodgerBlue;
case CostBucket.Expensive: return Color.Crimson;
default: return Color.White;
}
}
// =========================================================================
// CLEANUP
// =========================================================================
protected override void OnDestroy()
{
// Prevent memory leaks when changing timeframes
if (_hudContainer != null)
Chart.RemoveControl(_hudContainer);
}
}
}