示例#1
0
    public async Task<SysTimeSeries> GetAllocation(ICarbonClient cc_, bool forceRefresh_ = false)
    {
      if (m_alloc!=null && !forceRefresh_)
        return m_alloc.Value;

      m_alloc = new TriedObject<SysTimeSeries>(await SysTimeSeries.GetAsync(string.Format("{0}.allocation", RootName), cc_));

      OnPortfolioChunkUpdated(new PortfolioChunkUpdatedEventArgs(PortfolioChunk.Allocation));

      return m_alloc.Value; 
    }
示例#2
0
    public async Task<SysMeta> GetMeta(ICarbonClient cc_, bool forceRefresh_=false)
    {
      if (m_meta!=null && !forceRefresh_)
        return m_meta.Value;

      m_meta = new TriedObject<SysMeta>(await SysMeta.GetAsync(string.Format("{0}.meta", RootName), cc_));

      OnPortfolioChunkUpdated(new PortfolioChunkUpdatedEventArgs(PortfolioChunk.Meta));

      return m_meta.Value;
    }
示例#3
0
    public async Task<ITRInstrument> TRInstrument(CarbonClient cc_)
    {
      if (m_trInstrument != null)
        return m_trInstrument.Value;

      var name = Name.Contains("#") ? Name.Split('#')[0] : Name;

      // try futures first
      {
        var future = await InstrumentList_Futures.GetByBbgTicker(name, cc_);

        if (future != null)
        {
          m_trInstrument = new TriedObject<ITRInstrument>(future);
          return m_trInstrument.Value;
        }
      }

      // then fx
      {
        var fx = await InstrumentList_FX.GetByName(name, cc_);

        // more than likely we'll have a 3 character currency code coming from fx portfolios.
        // this implies a position in CCY vs USD, so try getting that...
        if (fx == null && name.Length == 3)
        {
          fx = await InstrumentList_FX.GetByName(string.Format("{0}USD", name), cc_);
        }

        if (fx != null)
        {
          m_trInstrument = new TriedObject<ITRInstrument>(fx);
          return m_trInstrument.Value;
        }
      }

      // swaps
      {
        var swap = await InstrumentList_Swaps.GetByBbgTicker(name, cc_);

        if (swap != null)
        {
          m_trInstrument = new TriedObject<ITRInstrument>(swap);
          return m_trInstrument.Value;
        }
      }


      Logger.Info(string.Format("Cannot find equivalent TRS object for [{0}]", Name), typeof(SecMasterEntry));

      m_trInstrument = new TriedObject<ITRInstrument>();
      return m_trInstrument.Value;
    }
示例#4
0
    private static async Task reload(CarbonClient cc_)
    {
      try
      {
        var v = await DataFrameHelper.GetDataFrameByRow<string, Tuple<string, double>>(
          name_: "ssys.dashboard.config.assetorder",
          keyExtractor_: x => x == null ? "blah" : x.ToString(),
          valueExtractor_: (cols, vals) =>
          {
            var assetIndex = cols.ContainsKey("Asset") ? cols["Asset"] : -1;
            var orderIndex = cols.ContainsKey("Order") ? cols["Order"] : -1;

            if (assetIndex == -1 || orderIndex == -1) return null;

            return new Tuple<string, double>(
              item1: vals[assetIndex].ToString(),
              item2: double.Parse(vals[orderIndex].ToString())
              );
          },
          cc_: cc_);

        var dict = new Dictionary<string, double>();

        foreach (var key in v.Keys)
        {
          var kv = v.GetValue(key, 0);
          dict.Add(kv.Item1, kv.Item2);
        }

        m_order = new TriedObject<Dictionary<string, double>>(dict);
      }
      catch (Exception ex_)
      {
        Logger.Error("Error retrieving / building AssetOrder", typeof (AssetOrder), ex_);
        m_order = new TriedObject<Dictionary<string, double>>();
      }
    }
示例#5
0
    public async Task<PortfolioPnlCache> GetHistoricUnAllocatedPnl(CarbonClient cc_, bool forceRefresh_ = false)
    {
      if (m_pnl != null && !forceRefresh_)
        return m_pnl.Value;

      m_pnl =
        new TriedObject<PortfolioPnlCache>(
          new PortfolioPnlCache(
            await SysTimeSeries.GetAsync(PnlMoniker, cc_,PnlMultiplier)));

      OnPortfolioChunkUpdated(new PortfolioChunkUpdatedEventArgs(PortfolioChunk.Pnl));

      return m_pnl.Value;
    }
示例#6
0
    public void Reset()
    {
      m_meta = null;
      m_alloc = null;
      m_pnl = null;
      m_wts = null;
      m_allocatedWts = null;
      m_allocatedPnl = null;
      m_currentExpBackwards = null;
      m_parentPortfolio = null;

      m_childPortfolios = null;
    }
示例#7
0
    public async Task<IEnumerable<Portfolio>> GetChildren(ICarbonClient cc_, bool recurse_ = false, bool forceRefresh_=false, bool isInitialLoad_=false )
    {
      if (m_childPortfolios!=null && !forceRefresh_)
        return m_childPortfolios.Value;

      var meta = await GetMeta(cc_, forceRefresh_);

      if (meta != null && meta.Data != null && meta.Data.Length > 0)
      {
        var list = new List<Portfolio>();
        var portfolioNames = meta.Data.Where(x => x.IsVisible).Select(x => x.Model).Distinct().ToArray();

#if DEBUG

        // temporary thing to only go so deep into the tree structure

        //if (RootName.ToCharArray().Where(x => x == '.').Count() >= 2)
        //  portfolioNames = new string[0];

#endif

        foreach (string t in portfolioNames)
        {
          list.Add(new Portfolio(string.Format("{0}.{1}", RootName, t),
            combineAllocation(AllocationToThis, await GetAllocationTo(t, cc_, forceRefresh_)), PnlMultiplier, this));
        }

        m_childPortfolios = new TriedObject<List<Portfolio>>(list);

        foreach (var child in m_childPortfolios.Value)
        {
          OnChildPortfolioPopulated(new ChildPortfolioPopulatedEventArgs(child,isInitialLoad_));
        }

        // do we want to recurse down the tree?
        if (recurse_ && m_childPortfolios != null && m_childPortfolios.Value.Count > 0)
        {
          // if we're on the initial load, then don't want to go more than 
          if(!(isInitialLoad_ && RootName.ToCharArray().Where(x=>x=='.').Count()>=2))
            foreach (var p in m_childPortfolios.Value)
              await p.GetChildren(cc_: cc_, recurse_: recurse_, isInitialLoad_: isInitialLoad_);
        }

        return m_childPortfolios.Value;
      }

      return m_childPortfolios==null ? null : m_childPortfolios.Value;
    }
示例#8
0
    public async Task<double> ContributionToParentTargetVol(CarbonClient cc_, bool forceRefresh_ = false)
    {
      if (m_contributionToParentTargetVol != null && !forceRefresh_)
        return m_contributionToParentTargetVol.Value;

      if (m_parentPortfolio == null) return 0d;

      var cov = await Covariances.Instance().Get(DateTime.Today, cc_, 10);

      if (cov == null)
      {
        m_contributionToParentTargetVol = new TriedObject<double>(0d);
        return 0d;
      }

      CovContToRiskResult myContToRisk, parentContToRisk;

      // try and get 'mine'
      {
        var myExposures = await GetLatestAllocatedExposure(cc_, forceRefresh_);

        if (myExposures == null || myExposures.Item2 == null || myExposures.Item2.SumAbs().IsZero())
        {
          m_contributionToParentTargetVol = new TriedObject<double>(0d);
          return 0d;
        }

        myContToRisk = cov.CalcContToRisk(myExposures.Item1, myExposures.Item2);
      }

      // try and get parent
      {
        var parentexposures = await m_parentPortfolio.GetLatestAllocatedExposure(cc_, forceRefresh_);

        if (parentexposures == null || parentexposures.Item2 == null || parentexposures.Item2.SumAbs().IsZero())
        {
          m_contributionToParentTargetVol = new TriedObject<double>(0d);
          return 0d;
        }

        parentContToRisk = cov.CalcContToRisk(parentexposures.Item1, parentexposures.Item2);
      }


      var sum = 0d;
      for (int i = 0; i < myContToRisk.ExposureNames.Length; ++i)
      {
        if (myContToRisk.NetExposures[i].IsZero())
          continue;

        // need to find index of myexposure in parentExposure

        for (int j = 0; j < parentContToRisk.ExposureNames.Length; ++j)
        {
          if (String.Compare(parentContToRisk.ExposureNames[j], myContToRisk.ExposureNames[i], StringComparison.OrdinalIgnoreCase) == 0)
          {
            sum += parentContToRisk.ContToRisk[j] * (myContToRisk.NetExposures[i] / parentContToRisk.NetExposures[j]);
            break;
          }
        }
      }

      m_contributionToParentTargetVol = new TriedObject<double>(sum);

      return m_contributionToParentTargetVol.Value;
    }
示例#9
0
    public async Task<double> GetLatestTargetVol(CarbonClient cc_, bool forceRefresh_ = false)
    {
      if (m_targetVol != null && !forceRefresh_)
        return m_targetVol.Value;

      var cov = await Covariances.Instance().Get(date_: DateTime.Today, cc_: cc_, maxBehind_: 10);
      var exp = await GetAllocatedExposure(cc_);
      double targetVol = double.NaN;
      if (cov != null && exp != null)
      {
        targetVol = cov.CalcLatestVol(exp);
      }

      m_targetVol = new TriedObject<double>(targetVol);

      return m_targetVol.Value;
    }
示例#10
0
    public async Task<PortfolioPnlCache> GetCurrentAllocatedExposureHistPnl(CarbonClient cc_, bool forcerefresh_ = false)
    {
      if (m_currentExpBackwards != null && !forcerefresh_)
        return m_currentExpBackwards.Value;

      var exp = await GetLatestAllocatedExposure(cc_, forcerefresh_);

      // if we haven't got exposure
      if (exp == null || exp.Item2 == null)
      {
        m_currentExpBackwards = new TriedObject<PortfolioPnlCache>();
        return null;
      }

      var listOfColumns = new List<string>();
      var listOfDailyReturns = new List<DatedDataCollectionGen<double>>();

      for (int i = 0; i < exp.Item2.Length; ++i)
      {
        var exposureName = exp.Item1[i];
        var exposure = exp.Item2[i];

        if (exposure.IsZero())
          continue;

        listOfColumns.Add(exposureName);

        // get the security master
        var secMaster = await SecMasters.Get(exposureName, cc_);

        // find equivalent TRS object
        var trInstrument = (secMaster == null) ? null : await secMaster.TRInstrument(cc_);

        // get TRS object's history
        var series = trInstrument == null ? null : await trInstrument.GetTimeSeries(cc_);

        if (series == null)
        {
          Logger.Error(string.Format("Could not find total return series for [{0}]", exposureName), typeof(Portfolio));
          continue;
        }

        var dailyIncrements = series.ToReturns();

        if (secMaster.Invert)
          dailyIncrements = dailyIncrements.MultiplyBy(-1d);

        listOfDailyReturns.Add(dailyIncrements.MultiplyBy(exposure));
      }

      var con = new ConstructGenGen<DateTime, double>(listOfColumns.ToArray());
      for (int i = 0; i < con.ArrayLength; ++i)
        con.SetColumnValues(i, listOfDailyReturns[i].Dates, listOfDailyReturns[i].Data);

      con.Keys.Sort((a, b) => a.CompareTo(b));

      m_currentExpBackwards = new TriedObject<PortfolioPnlCache>(new PortfolioPnlCache(new SysTimeSeries { Data = con }));

      return m_currentExpBackwards.Value;
    }
示例#11
0
    public async Task<SysTimeSeries> GetAllocatedExposure(CarbonClient cc_, bool forceRefresh_ = false)
    {
      if (m_allocatedWts != null && !forceRefresh_)
        return m_allocatedWts.Value;

      var wts = await GetUnAllocatedExposure(cc_, forceRefresh_);

      if (wts == null || !wts.HasData) return null;

      if (AllocationToThis == null)
      {
        m_allocatedWts = new TriedObject<SysTimeSeries>(wts);
      }
      else
      {
        var multipliedOut = wts.MultiplyOut(AllocationToThis, 0);
        m_allocatedWts = new TriedObject<SysTimeSeries>(multipliedOut);
      }

      return m_allocatedWts.Value;
    }
示例#12
0
    public async Task<SysTimeSeries> GetUnAllocatedExposure(CarbonClient cc_, bool forceRefresh_ = false)
    {
      if (m_wts!=null && !forceRefresh_)
        return m_wts.Value;

      m_wts =
        new TriedObject<SysTimeSeries>(
          await SysTimeSeries.GetAsync(string.Format("{0}.model.exposure", RootName), cc_));


      if (m_wts == null || m_wts.Value==null || !m_wts.Value.HasData)
        Logger.Error(string.Format("Could not retrieve exposure for [{0}]", RootName), typeof (Portfolio));
      else
      {
        m_wts.Value.RemoveValuesGreaterThanOrEqualToToday();

        Logger.Info(
          string.Format("Portfolio exposure for [{0}] last saved for {1}", RootName,
            m_wts.Value.Data.LastKey().ToString("dd-MMM-yyyy")), typeof (Portfolio));
      }

      OnPortfolioChunkUpdated(new PortfolioChunkUpdatedEventArgs(PortfolioChunk.Exposure));

      return m_wts.Value;
    }
示例#13
0
    public async Task<PortfolioPnlCache> GetHistoricAllocatedPnl(CarbonClient cc_, bool forceRefresh_ = false)
    {
      if (m_allocatedPnl != null && !forceRefresh_)
        return m_allocatedPnl.Value;

      var pnl = await GetHistoricUnAllocatedPnl(cc_, forceRefresh_);

      if (pnl == null || !pnl.HasData) return null;

      if (AllocationToThis == null)
      {
        m_allocatedPnl = new TriedObject<PortfolioPnlCache>(pnl);
      }
      else
      {
        var multipliedOut = pnl.TimeSeries.MultiplyOut(AllocationToThis,-1);
        m_allocatedPnl = new TriedObject<PortfolioPnlCache>(new PortfolioPnlCache(multipliedOut));
      }

      return m_allocatedPnl.Value;
    }