CGC_Codes

trading sim/account manager

Jun 7th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using QuantSys.MarketData;
  4. using QuantSys.TradeEngine.Simulation.Account;
  5. using QuantSys.TradeEngine.Simulation.Account.Order;
  6. using QuantSys.TradeEngine.Simulation.Performance;
  7. using QuantSys.Visualization.Highstocks;
  8.  
  9. namespace QuantSys.TradeEngine.AccountManagement
  10. {
  11.  
  12.     public class AccountManager : IAccountManager
  13.     {
  14.         public Portfolio Portfolio { get { return _portfolio; } }
  15.         public List<HighstockFlag> Flags { get { return _flags; } }
  16.         public List<double> Trades { get { return _tradeProfits; } }
  17.         public double CurrentBalance { get { return _portfolio.CurrentBalance; } }
  18.         public double CurrentEquity { get { return _portfolio.CurrentEquity; } }
  19.  
  20.         private BacktestPerformance _performance;
  21.  
  22.         private readonly Portfolio _portfolio;
  23.  
  24.         private Dictionary<Symbol, StopOrder> _outStandingStopOrders;
  25.         private Dictionary<Symbol, LimitOrder> _outStandingLimitOrders;
  26.  
  27.         private List<HighstockFlag> _flags;
  28.         private List<double> _tradeProfits;
  29.  
  30.         private Dictionary<Symbol, Tick> currentOffers;
  31.         public AccountManager()
  32.         {
  33.             _portfolio = new Portfolio();
  34.             _flags = new List<HighstockFlag>();
  35.             _outStandingStopOrders = new Dictionary<Symbol, StopOrder>();
  36.             _outStandingLimitOrders = new Dictionary<Symbol, LimitOrder>();
  37.             _tradeProfits = new List<double>();
  38.             _performance = new BacktestPerformance();
  39.             currentOffers = new Dictionary<Symbol, Tick>();
  40.         }
  41.  
  42.         #region StopOrder Logic
  43.  
  44.         public StopOrder GetStopOrder(Symbol s)
  45.         {
  46.             if (_outStandingStopOrders.ContainsKey(s))
  47.             {
  48.                 return _outStandingStopOrders[s];
  49.             }
  50.  
  51.             return null;
  52.         }
  53.         public void PlaceStopOrder(StopOrder stop)
  54.         {
  55.             _outStandingStopOrders.Add(stop.Symbol, stop);
  56.         }
  57.         public void ModifyStopOrder(Tick stop, double newPrice)
  58.         {
  59.             if (_outStandingStopOrders.ContainsKey(stop.Symbol))
  60.             {
  61.                 _outStandingStopOrders[stop.Symbol].TriggerPrice = newPrice;
  62.             }
  63.  
  64.  
  65.  
  66.         }
  67.         private void CloseStopOrder(Tick t, StopOrder s)
  68.         {
  69.             _tradeProfits.Add(_portfolio.ClosePosition(t, s.TriggerPrice));
  70.  
  71.             _outStandingStopOrders.Remove(s.Symbol);
  72.  
  73.  
  74.            
  75.             if (_outStandingLimitOrders.ContainsKey(t.Symbol))
  76.                 _outStandingLimitOrders.Remove(t.Symbol);
  77.  
  78.  
  79.            
  80.             _flags.Add(new HighstockFlag(
  81.                                 "CS",
  82.                                 "Closed Stop Order",
  83.                                 t.Time
  84.                             ));
  85.         }
  86.  
  87.         public void ModifyTrailingStop(Tick stop, double trailSize)
  88.         {
  89.             if (_outStandingStopOrders.ContainsKey(stop.Symbol))
  90.             {
  91.                 _outStandingStopOrders[stop.Symbol].Trailing = true;
  92.                 _outStandingStopOrders[stop.Symbol].TrailSize = trailSize;
  93.             }
  94.         }
  95.         #endregion
  96.  
  97.  
  98.         #region LimitOrder Logic
  99.  
  100.         public LimitOrder GetLimitOrder(Symbol s)
  101.         {
  102.             if (_outStandingLimitOrders.ContainsKey(s))
  103.             {
  104.                 return _outStandingLimitOrders[s];
  105.             }
  106.  
  107.             return null;
  108.         }
  109.  
  110.         public void PlaceLimitOrder(LimitOrder limit)
  111.         {
  112.             _outStandingLimitOrders.Add(limit.Symbol, limit);
  113.         }
  114.  
  115.         public void ModifyLimitOrder(Tick limit, double newPrice)
  116.         {
  117.             if (_outStandingLimitOrders.ContainsKey(limit.Symbol))
  118.             {
  119.                 _outStandingLimitOrders[limit.Symbol].TriggerPrice = newPrice;
  120.             }
  121.         }
  122.  
  123.         private void CloseLimitOrder(Tick t, LimitOrder l)
  124.         {
  125.             _tradeProfits.Add(_portfolio.ClosePosition(t, l.TriggerPrice));
  126.  
  127.             _outStandingLimitOrders.Remove(l.Symbol);
  128.  
  129.            
  130.             if (_outStandingStopOrders.ContainsKey(t.Symbol))
  131.                 _outStandingStopOrders.Remove(t.Symbol);
  132.  
  133.  
  134.            
  135.             _flags.Add(new HighstockFlag(
  136.                                 "CL",
  137.                                 "Closed Limit Order",
  138.                                 t.Time
  139.                             ));
  140.         }
  141.  
  142.         private void ModifyTrailingLimit(double trailSize)
  143.         {
  144.             throw new NotImplementedException();
  145.         }
  146.  
  147.         #endregion
  148.  
  149.         public void IncreasePosition(Symbol t, double size)
  150.         {
  151.             if (!currentOffers.ContainsKey(t)) return;
  152.             _portfolio.IncreasePosition(currentOffers[t], size);
  153.         }
  154.  
  155.         public void ReducePosition(Symbol t, double size)
  156.         {
  157.             if (!currentOffers.ContainsKey(t)) return;
  158.             _portfolio.ReducePosition(currentOffers[t], size);
  159.         }
  160.  
  161.         public void ClosePosition(Symbol s)
  162.         {
  163.             if (!currentOffers.ContainsKey(s)) return;
  164.             Tick t = currentOffers[s];
  165.  
  166.             _tradeProfits.Add(_portfolio.ClosePosition(t,
  167.                 (_portfolio.Positions[t.Symbol].isLong ? t.BidClose : t.AskClose)));
  168.  
  169.        
  170.             if (_outStandingStopOrders.ContainsKey(t.Symbol))
  171.                 _outStandingStopOrders.Remove(t.Symbol);
  172.  
  173.             if (_outStandingLimitOrders.ContainsKey(t.Symbol))
  174.                 _outStandingLimitOrders.Remove(t.Symbol);
  175.  
  176.            
  177.             _flags.Add(new HighstockFlag(
  178.                                 "C",
  179.                                 "Closed Order",
  180.                                 t.Time
  181.                             ));
  182.         }
  183.  
  184.  
  185.         public void OnTick(params Tick[] ticks)
  186.         {
  187.  
  188.  
  189.             foreach (Tick t in ticks)
  190.             {
  191.                 if (!currentOffers.ContainsKey(t.Symbol))
  192.                     currentOffers.Add(t.Symbol, t);
  193.                 else
  194.                     currentOffers[t.Symbol] = t;
  195.             }
  196.  
  197.             foreach (Tick t in ticks)
  198.             {
  199.  
  200.  
  201.                 if (_outStandingStopOrders.ContainsKey(t.Symbol))
  202.                 {
  203.                     StopOrder stopOrder = _outStandingStopOrders[t.Symbol];
  204.  
  205.                    
  206.                     if (stopOrder.Trailing) stopOrder.RecalculateTrail(t);
  207.  
  208.                    
  209.                     if (stopOrder.IsBuyStop())
  210.                     {
  211.                        
  212.                         if (t.BidLow <= stopOrder.TriggerPrice)
  213.                         {
  214.                             CloseStopOrder(t, stopOrder);
  215.                         }
  216.                     }
  217.  
  218.                    
  219.                     else if (stopOrder.IsSellStop())
  220.                     {
  221.                         if (t.AskHigh >= stopOrder.TriggerPrice)
  222.                         {
  223.                             CloseStopOrder(t, stopOrder);
  224.  
  225.                         }
  226.                     }
  227.                 }
  228.  
  229.  
  230.  
  231.  
  232.                 if (_outStandingLimitOrders.ContainsKey(t.Symbol))
  233.                 {
  234.                     LimitOrder limitOrder = _outStandingLimitOrders[t.Symbol];
  235.  
  236.                    
  237.                     if (limitOrder.Trailing) limitOrder.RecalculateTrail(t);
  238.  
  239.                    
  240.                     if (limitOrder.IsBuyLimit())
  241.                     {
  242.                        
  243.                         if (t.BidClose >= limitOrder.TriggerPrice)
  244.                         {
  245.                             CloseLimitOrder(t, limitOrder);
  246.                         }
  247.                     }
  248.  
  249.                    
  250.                     else if (limitOrder.IsSellLimit())
  251.                     {
  252.                         if (t.AskClose <= limitOrder.TriggerPrice)
  253.                         {
  254.                             CloseLimitOrder(t, limitOrder);
  255.                         }
  256.                     }
  257.                 }
  258.  
  259.             }
  260.  
  261.  
  262.  
  263.             _portfolio.AdjustPortfolioEquityAndMargin(ticks);
  264.  
  265.         }
  266.  
  267.  
  268.         public bool ExistsPositionForSymbol(Symbol s)
  269.         {
  270.             return _portfolio.ExistsPositionForSymbol(s);
  271.         }
  272.  
  273.         public bool ExistsLongPositionForSymbol(Symbol s)
  274.         {
  275.             return _portfolio.ExistsPositionForSymbol(s) && _portfolio[s].Side.Equals(Position.PositionSide.Long);
  276.         }
  277.  
  278.         public bool ExistsShortPositionForSymbol(Symbol s)
  279.         {
  280.             return _portfolio.ExistsPositionForSymbol(s) && _portfolio[s].Side.Equals(Position.PositionSide.Short);
  281.         }
  282.  
  283.  
  284.         public void PlaceMarketOrder(Symbol sym, int size, Position.PositionSide side, double stopPips = double.NaN, double LimitPips = double.NaN)
  285.         {
  286.             if (!currentOffers.ContainsKey(sym)) return;
  287.  
  288.             double orderPrice = side.Equals(Position.PositionSide.Long)
  289.                 ? currentOffers[sym].AskClose
  290.                 : currentOffers[sym].BidClose;
  291.  
  292.  
  293.             _portfolio.TakePosition(sym, orderPrice, side, size, currentOffers[sym].Time);
  294.  
  295.  
  296.             if (!stopPips.Equals(double.NaN))
  297.             {
  298.                 StopOrder stopOrder = new StopOrder(sym, side, ((side.Equals(Position.PositionSide.Long)) ? orderPrice - stopPips : orderPrice + stopPips));
  299.                 PlaceStopOrder(stopOrder);
  300.             }
  301.  
  302.             if (!LimitPips.Equals(double.NaN))
  303.             {
  304.                 LimitOrder limitOrder = new LimitOrder(sym, side, ((side.Equals(Position.PositionSide.Long)) ? orderPrice + LimitPips : orderPrice - LimitPips));
  305.                 PlaceLimitOrder(limitOrder);
  306.             }
  307.  
  308.  
  309.             _flags.Add(new HighstockFlag(
  310.                     (side == Position.PositionSide.Long) ? "B" : "S",
  311.                     ((side == Position.PositionSide.Long) ? "Bought " : "Sold ") + size + " on " + side + " at " + orderPrice + " on " + currentOffers[sym].Time.ToString(),
  312.                     currentOffers[sym].Time
  313.                 ));
  314.  
  315.         }
  316.  
  317.         public void ModifyStopOrder()
  318.         {
  319.             throw new NotImplementedException();
  320.         }
  321.  
  322.         public void SetTrailingStop()
  323.         {
  324.             throw new NotImplementedException();
  325.         }
  326.  
  327.         public void PlaceStopOrder()
  328.         {
  329.             throw new NotImplementedException();
  330.         }
  331.  
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment