Skip to main content

Examples Library

These examples are starting patterns for HyperionX scripts. They are intentionally small so the structure is clear.

Indicator: Close Momentum

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using System.Xml.Serialization;
using HyperionX.Chart.Classes;
using HyperionX.Chart.Enums;
using HyperionX.Core.Attributes;
using HyperionX.Core.DataCalc;
using HyperionX.Core.Enums;

namespace HyperionX.Custom.Indicators;

public class CloseMomentum : Indicator
{
private Plot _plot;

[HyperionXProperty]
[Display(Name = "Period", GroupName = "Parameters", Order = 1)]
public int Period { get; set; }

[Browsable(false)]
[XmlIgnore]
public Series<double> Momentum { get; set; }

public override void OnStateChanged()
{
if (State == State.SetDefaults)
{
Name = "Close Momentum";
Version = "1.0";
Period = 10;
_plot = new Plot(Colors.DeepSkyBlue, "Momentum", 2, PlotLineType.Solid, PlotChartType.Linear);
AddPanePlot(_plot);
}
else if (State == State.Configured)
{
Momentum = new Series<double>();
AddSeries(Momentum);
_plot.DataSource = Momentum;
}
}

public override void OnBarUpdate()
{
if (CurrentBar < Period + 1)
return;

Momentum[0] = Close[0] - Close[Period];
}
}

Indicator: HUD Label

using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using HyperionX.Chart.Enums;
using HyperionX.Core.Attributes;
using HyperionX.Core.Enums;

namespace HyperionX.Custom.Indicators;

public class PriceHud : Indicator
{
[HyperionXProperty]
[Display(Name = "Show Spread", GroupName = "Parameters", Order = 1)]
public bool ShowSpread { get; set; }

public override void OnStateChanged()
{
if (State == State.SetDefaults)
{
Name = "Price HUD";
Version = "1.0";
ShowSpread = true;
}
}

public override void OnBarUpdate()
{
if (CurrentBar < 1)
return;

string text = ShowSpread
? $"Last {Close[0]:0.####} Spread {Ctx.MarketData.Spread:0.####}"
: $"Last {Close[0]:0.####}";

Draw.FixedText(
this,
"price-hud",
text,
ChartHudAnchor.TopRight,
textBrush: Brushes.White,
backgroundBrush: Brushes.Black,
borderBrush: Brushes.DeepSkyBlue);
}
}

Strategy: Moving Average Cross

using System.ComponentModel.DataAnnotations;
using HyperionX.Core.Attributes;
using HyperionX.Core.Enums;
using HyperionX.Core.Market;
using HyperionX.Custom.Indicators;

namespace HyperionX.Custom.Strategies;

public class MovingAverageCrossStrategy : Strategy
{
private SMA _fast;
private SMA _slow;

[HyperionXProperty]
[Display(Name = "Fast Period", GroupName = "Parameters", Order = 1)]
public int FastPeriod { get; set; }

[HyperionXProperty]
[Display(Name = "Slow Period", GroupName = "Parameters", Order = 2)]
public int SlowPeriod { get; set; }

[HyperionXProperty]
[Display(Name = "Quantity", GroupName = "Parameters", Order = 3)]
public double Quantity { get; set; }

public override void OnStateChanged()
{
if (State == State.SetDefaults)
{
Name = "Moving Average Cross Strategy";
Version = "1.0";
FastPeriod = 9;
SlowPeriod = 21;
Quantity = 1;
}
else if (State == State.Configured)
{
_fast = SMA(FastPeriod);
_slow = SMA(SlowPeriod);
}
}

public override void OnBarUpdate()
{
if (CurrentBar < SlowPeriod + 2)
return;

bool crossedUp = _fast[0] > _slow[0] && _fast[1] <= _slow[1];
bool crossedDown = _fast[0] < _slow[0] && _fast[1] >= _slow[1];

if (crossedUp && LastPosition.MarketPosition == MarketPosition.Flat)
EnterLong(Quantity, "MA Long");

if (crossedDown && LastPosition.MarketPosition == MarketPosition.Long)
ExitLong(LastPosition.Quantity, "MA Exit", "MA Long");
}
}

Strategy: Bracket Protection

public override void OnBarUpdate()
{
if (CurrentBar < 20)
return;

if (LastPosition.MarketPosition == MarketPosition.Flat && Close[0] > Close[1])
{
SetStopLossTicks("Breakout", 20);
SetProfitTargetTicks("Breakout", 40);
EnterLong(1, "Breakout");
}
}

Market Depth Read

public override void OnBarUpdate()
{
if (CurrentBar < 1)
return;

var depth = Ctx.MarketData.GetDepth(10);
if (!depth.HasDepth)
return;

Print($"Bid {depth.BestBid} Ask {depth.BestAsk}");
}

What To Test

For every example or imported script:

  • Build in Code Lab.
  • Load on a test chart.
  • Check the log.
  • Verify early bars.
  • Verify output while panning and changing timeframes.
  • For strategies, test playback/simulation before live.