CGC_Codes

trading sim/ portfolio

Jun 7th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using QuantSys.MarketData;
  4.  
  5. namespace QuantSys.TradeEngine.Simulation.Account
  6. {
  7.     public class Portfolio
  8.     {
  9.         public const double STARTING_BALANCE = 10000;
  10.         public const double MARGIN_REQUIREMENT = 0.2;
  11.  
  12.         public int NumPositions
  13.         {
  14.             get { return _positions.Count; }
  15.         }
  16.  
  17.         public Position this[Symbol symbol]
  18.         {
  19.             get { return _positions[symbol]; }
  20.         }
  21.  
  22.         public Dictionary<Symbol, Position> Positions { get { return _positions; } }
  23.  
  24.         private Dictionary<Symbol, Position> _positions;
  25.  
  26.         public Portfolio(double startingBalance = STARTING_BALANCE)
  27.         {
  28.             CurrentBalance = startingBalance;
  29.             StartingBalance = startingBalance;
  30.             CurrentMargin = startingBalance;
  31.             CurrentEquity = startingBalance;
  32.             _positions = new Dictionary<Symbol, Position>();
  33.         }
  34.  
  35.         public double CurrentEquity { get; set; }
  36.         public double CurrentBalance { get; set; }
  37.         public double CurrentMargin { get; set; }
  38.         public double StartingBalance { get; set; }
  39.  
  40.  
  41.         public bool ExistsPositionForSymbol(Symbol s)
  42.         {
  43.             return _positions.ContainsKey(s);
  44.         }
  45.  
  46.         public bool TakePosition(Symbol s, double price, Position.PositionSide side, double size, DateTime date)
  47.         {
  48.  
  49.  
  50.             _positions.Add(s, new Position(s, price, size, side, date));
  51.  
  52.  
  53.             return true;
  54.         }
  55.  
  56.        
  57.         public double ClosePosition(Tick t, double price)
  58.         {
  59.             if (ExistsPositionForSymbol(t.Symbol))
  60.             {
  61.                 double changeInBalance = _positions[t.Symbol].ClosePosition(t, price);
  62.                 _positions.Remove(t.Symbol);
  63.                 AdjustPortfolioBalance(changeInBalance);
  64.                 return changeInBalance;
  65.             }
  66.             return 0;
  67.         }
  68.  
  69.         public bool ReducePosition(Tick t, double size)
  70.         {
  71.             if (ExistsPositionForSymbol(t.Symbol))
  72.             {
  73.                 double changeInBalance = _positions[t.Symbol].ReducePosition(t, size);
  74.                 AdjustPortfolioBalance(changeInBalance);
  75.                 return true;
  76.             }
  77.             return false;
  78.         }
  79.  
  80.         public bool IncreasePosition(Tick t, double size)
  81.         {
  82.             if (ExistsPositionForSymbol(t.Symbol))
  83.             {
  84.                 _positions[t.Symbol].IncreasePosition(t, size);
  85.                 return true;
  86.             }
  87.             return false;
  88.         }
  89.  
  90.         private void AdjustPortfolioBalance(double change)
  91.         {
  92.             CurrentBalance += change;
  93.         }
  94.  
  95.  
  96.        
  97.         public void AdjustPortfolioEquityAndMargin(params Tick[] ticks)
  98.         {
  99.             double tempEquity = CurrentBalance;
  100.  
  101.             foreach (Tick t in ticks)
  102.             {
  103.                 if (ExistsPositionForSymbol(t.Symbol))
  104.                 {
  105.                     tempEquity += _positions[t.Symbol].Size *
  106.                                   ((_positions[t.Symbol].Side.Equals(Position.PositionSide.Long))
  107.                                       ? t.BidClose - _positions[t.Symbol].PositionPrice
  108.                                       : _positions[t.Symbol].PositionPrice - t.AskClose);
  109.                 }
  110.             }
  111.  
  112.             CurrentEquity = tempEquity;
  113.  
  114.            
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment