Exemplo n.º 1
0
 ///<summary>
 ///Add an order to the current position. Throw an Exception if the position and order not compatibles
 ///</summary>
 ///<param name="order"> an Order object </param>
 ///<returns>Order object if after adding, the position 
 ///changes his direction. In this case the qty of position will be 0 (position closed),
 ///and the Order object represent the carry of position. Otherwise return null.</returns>
 public Order addOrderUntilClose(Order order)
 {
     Order resultOrder = null;
     if (!this.symbol.Equals(order.Symbol)) throw new Exception("The orders symbol and positions symbol not compatible");
     if (this.direction == order.Direction)
     {
         this.qty += order.Lots;
     }
     else
     {
         if (this.qty > order.Lots)
         {
             this.qty -= order.Lots;
         }
         else
         {
             resultOrder = new Order();
             resultOrder.Symbol = this.symbol;
             resultOrder.Direction = order.Direction;
             resultOrder.Lots = order.Lots - this.qty;
             this.qty = 0.0;
         }
     }
     return resultOrder;
 }
Exemplo n.º 2
0
 // Return null if order filtered and cannot return
 public Order FilterOrder(Order order)
 {
     for (int i = 0; i < cumulatedPositons.Count; ++i)
     {
         if (cumulatedPositons[i].symbol == order.Symbol)
         {
             Order resultOrder = cumulatedPositons[i].addOrderUntilClose(order);
             if (resultOrder != null)
             {
                 cumulatedPositons.RemoveAt(i);
             }
             return resultOrder;
         }
     }
     return order;
 }
Exemplo n.º 3
0
 ///<summary>
 ///Add an order to current position
 ///throws Exception when the Order object symbol not compatible with current positions symbol
 ///</summary>
 ///<param name = "order"> order - an Order object</param>
 public void addOrder(Order order)
 {
     if (!this.symbol.Equals(order.Symbol)) throw new Exception("The orders symbol and positions symbol not compatible");
     if (this.direction == order.Direction)
     {
         this.qty += order.Lots;
     }
     else
     {
         this.qty -= order.Lots;
         if (this.qty < 0.0)
         {
             this.direction = order.Direction;
             this.qty *= -1.0;
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// This constructor creates a position by order
 /// </summary>
 /// <param name="order"> order which open the position</param>
 public Position(Order order)
 {
     this.symbol = order.Symbol;
     this.qty = order.Lots;
     this.direction = order.Direction;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Convert current posiiton to order
 /// </summary>
 /// <returns>an order that represent tjis position</returns>
 public Order ToOrder()
 {
     Order o = new Order();
     o.Symbol = symbol;
     o.Direction = this.direction;
     o.Lots = this.qty;
     return o;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Compare two orders
 /// </summary>
 /// <param name="otherOrder">an other order to compare</param>
 /// <returns>return "true" if this orders equal "else" otherwise</returns>
 public bool compare(Order otherOrder)
 {
     return (otherOrder.symbol.Equals(this.symbol) &&
             otherOrder.lots == this.Lots &&
             otherOrder.direction == this.direction);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Create an independent copy of this Order object
 /// </summary>
 /// <returns>An independent Order object</returns>
 public Order Clone()
 {
     Order clonedOrder = new Order();
     clonedOrder.Comment   = (String)this.Comment.Clone();
     clonedOrder.Direction = this.Direction;
     clonedOrder.FromID    = this.FromID;
     clonedOrder.lOrderId  = this.lOrderId;
     clonedOrder.Lots      = this.Lots;
     clonedOrder.OrderID   = this.OrderID;
     clonedOrder.PrevScanTime = this.PrevScanTime;
     clonedOrder.Price     = this.Price;
     clonedOrder.Symbol    = (String)this.Symbol.Clone();
     clonedOrder.Time      = this.Time;
     return clonedOrder;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Insert an Order to a queue, that phisicaly stored in a database table
 /// </summary>
 /// <param name="order">an Order object to store</param>
 /// <param name="userId">a user ID wich order will be stores</param>
 public void InsertToQueue(Order order, String userId)
 {
     lock (_sqlLock)
     {
         order.Time = DateTime.Now;
         StringBuilder sqlString = new StringBuilder();
         sqlString.Append(
             "Insert Into Orders(\"OrderId\",\"Direction\",\"Symbol\",\"Lots\",\"OrderTime\", \"Price\", \"FromID\", \"UserID\", \"PrevScanTime\")");
         sqlString.Append("VALUES ('").Append(order.strOrderId).Append("', ").Append(order.Direction).Append(
             ", '");
         sqlString.Append(order.Symbol).Append("',").Append(order.Lots.ToString("G", CultureInfo.InvariantCulture));
         sqlString.Append(", '").Append(order.Time.ToString("yyyy-MM-dd HH:mm:ss")).Append("', ");
         sqlString.Append(order.Price.ToString("G", CultureInfo.InvariantCulture)).Append(", ");
         sqlString.Append(order.FromID).Append(", '").Append(userId).Append("', '").Append(
             order.PrevScanTime.ToString("yyyy-MM-dd HH:mm:ss"));
         sqlString.Append("')");
         SqlCommand command = new SqlCommand(sqlString.ToString(), _connection);
         command.ExecuteNonQuery();
         command.Dispose();
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Convert datas from Reader to an Order object
 /// </summary>
 /// <param name="reader">a non empty reader</param>
 /// <returns>na Order object instance</returns>
 private Order ReaderToOrder(SqlDataReader reader)
 {
     Order order = new Order();
     order.OrderID = Convert.ToInt64(reader["ORDERID"]);
     order.Direction = Convert.ToInt32(reader["DIRECTION"]);
     order.Symbol = reader["SYMBOL"].ToString();
     order.Lots = Convert.ToDouble(reader["LOTS"]);
     order.Time = Convert.ToDateTime(reader["OrderTime"]);
     order.Price = Convert.ToDouble(reader["PRICE"]);
     order.FromID = Convert.ToInt64(reader["FROMID"]);
     order.PrevScanTime = Convert.ToDateTime(reader["PrevScanTime"]);
     return order;
 }
Exemplo n.º 10
0
 /// <summary>
 /// This method write an order to History table
 /// </summary>
 /// <param name="userId">user ID</param>
 /// <param name="order">anOrder object to write to database</param>
 public void OrderToHistory(String userId, Order order)
 {
     lock (_sqlLock)
     {
         StringBuilder sqlString = new StringBuilder();
         sqlString.Append("INSERT INTO HISTORY (\"USERID\", \"SYSTEMID\", \"DIRECTION\", \"SYMBOL\", \"VOLUME\", \"PRICE\")\n VALUES (");
         sqlString.Append(" '").Append(userId).Append("', ").Append(order.FromID).Append(", ").Append(
             order.Direction).Append(", '").Append(order.Symbol).Append("', ");
         sqlString.Append(Convert.ToString(order.Lots, CultureInfo.InvariantCulture)).Append(", ");
         sqlString.Append(Convert.ToString(order.Price, CultureInfo.InvariantCulture)).Append(");");
         SqlCommand sqlCommads = new SqlCommand(sqlString.ToString(), _connection);
         Console.WriteLine(sqlCommads.CommandText);
         sqlCommads.ExecuteNonQuery();
         sqlCommads.Dispose();
     }
 }