Click or drag to resize

Position chart

The graphic component OptionPositionChart - is the chart showing the position and the options “Greeks” relating to the underlying asset.

The following is the example SampleOptionQuoting, in which this chart is used. The source code of the example can be found in the folder Samples/Options/SampleOptionQuoting.

option volsmile

SampleOptionQuoting example

  1. The OptionPositionChart element is added to the XAML code and the PosChart name is assigned to it:

    XAML
    <Window x:Class="OptionCalculator.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:loc="clr-namespace:StockSharp.Localization;assembly=StockSharp.Localization"
            xmlns:xaml="http://schemas.stocksharp.com/xaml"
            xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
            Title="{x:Static loc:LocalizedStrings.XamlStr396}" Height="400" Width="1030">
        <Grid Margin="5,5,5,5">
    
          .........................................................
    
          <xaml:OptionPositionChart x:Name="PosChart" Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="6" />
    
      </Grid>
    </Window>
  2. The RithmicTrader connection is created in the C# code:

    C#
    Connector = new RithmicTrader
    {
    
    };
  3. To set the initial control settings:

    1. To reset to zero options positions collections Positions and the underlying instrument AssetPosition of control;
    2. To redraw the chart with the initial values Refresh(Decimal, Decimal, DateTimeOffset, DateTimeOffset);
    3. To specify messages provider for market data and instruments.
    C#
    PosChart.Positions.Clear();
    PosChart.AssetPosition = null;
    PosChart.Refresh(1, 1, default(DateTimeOffset), default(DateTimeOffset));
    
    PosChart.MarketDataProvider = Connector;
    PosChart.SecurityProvider = Connector;
  4. Upon getting the instruments to add underlying assets to the list:

    C#
    Connector.NewSecurities += securities =>
            _assets.AddRange(securities.Where(s => s.Type == SecurityTypes.Future));
  5. Upon changing the Level1 of the underlying instrument or options, as well as getting a new trade to set the _isDirty flag. This allows to call the RefreshChart method (see below) in the timer event (the code is omitted) to redraw the chart. Thus we control the frequency of redrawing:

    C#
    Connector.SecuritiesChanged += securities =>
    {
        if ((PosChart.AssetPosition != null && securities.Contains(PosChart.AssetPosition.Security)) || PosChart.Positions.Cache.Select(p => p.Security).Intersect(securities).Any())
            _isDirty = true;
    };
    
    Connector.NewTrades += trades =>
    {
        var assetPos = PosChart.AssetPosition;
        if (assetPos != null && trades.Any(t => t.Security == assetPos.Security))
            _isDirty = true;
    };
  6. In the new position occurrence event handler to update the Positions and AssetPosition properties of the control. In this case, as well as upon position change, to call redraw the chart:

    C#
    Connector.NewPositions += positions => this.GuiAsync(() =>
    {
        var asset = SelectedAsset;
    
        if (asset == null)
            return;
    
        var assetPos = positions.FirstOrDefault(p => p.Security == asset);
        var newPos = positions.Where(p => p.Security.UnderlyingSecurityId == asset.Id).ToArray();
    
        if (assetPos == null && newPos.Length == 0)
            return;
    
        if (assetPos != null)
            PosChart.AssetPosition = assetPos;
    
        if (newPos.Length > 0)
            PosChart.Positions.AddRange(newPos);
    
        RefreshChart();
    });
    
    Connector.PositionsChanged += positions => this.GuiAsync(() =>
    {
        if ((PosChart.AssetPosition != null && positions.Contains(PosChart.AssetPosition)) || positions.Intersect(PosChart.Positions.Cache).Any())
            RefreshChart();
    });
  7. The method calls the redraw of the chart:

    C#
    private void RefreshChart()
    {
        var asset = SelectedAsset;
        var trade = asset.LastTrade;
    
        if (trade != null)
            PosChart.Refresh(trade.Price, asset.PriceStep ?? 1m, TimeHelper.Now, asset.ExpiryDate ?? DateTimeOffset.Now.Date + TimeSpan.FromDays(1));
    }
See Also

Other Resources