Exemplo n.º 1
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.º 2
0
 public void ApiEvent(int code, Object order)
 {
     Order fullSpecifiedOrder;
     Order convertedOrder;
     switch (code)
     {
         case Presets.ORDER_FILLED:
                 convertedOrder = order as Order;
                 fullSpecifiedOrder = _localDb.ReturnFromQueue(convertedOrder.lOrderId);
                 if (fullSpecifiedOrder != null)
                 {
                     fullSpecifiedOrder.Price = (convertedOrder).Price;
                     eventListener.Invoke(fullSpecifiedOrder);
                 }
                 break;
         case Presets.ORDER_NOT_FILLED:
                 convertedOrder = order as Order;
                 fullSpecifiedOrder = _localDb.ReturnFromQueue(convertedOrder.lOrderId);
                 if (fullSpecifiedOrder != null)
                 {
                     fullSpecifiedOrder.Comment = convertedOrder.Comment;
                     Console.WriteLine("Order not filled " + fullSpecifiedOrder.strOrderId);
                     errorListsner.Invoke(fullSpecifiedOrder);
                 }
                 break;
         case Presets.ORDER_REJECTED:
                 convertedOrder = order as Order;
                 fullSpecifiedOrder = _localDb.ReturnFromQueue(convertedOrder.lOrderId);
                 if (fullSpecifiedOrder != null)
                 {
                     fullSpecifiedOrder.Comment = convertedOrder.Comment;
                     Console.WriteLine("Order rejected " + fullSpecifiedOrder.strOrderId);
                     errorListsner.Invoke(fullSpecifiedOrder);
                 }
                 break;
         case Presets.LOGGINED:
             {
                 Console.WriteLine("Logined");
             }
             break;
         default:
             {
                 Order order1 = new Order();
                 order1.Comment = "Unknow error";
                 errorListsner.Invoke(order1);
                 break;
             }
     }
 }
Exemplo n.º 3
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.º 4
0
 /// <summary>
 /// Add an open Order object to the database
 /// </summary>
 /// <param name="order"></param>
 public void AddOpen(Order order)
 {
     Add(order);
 }
Exemplo n.º 5
0
 public void AddOrderToHistory(String userId, Order order)
 {
     _localDb.OrderToHistory(userId, order);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Add an order to the database
 /// </summary>
 /// <param name="order">Order object to add to database</param>
 private void Add(Order order)
 {
     _localDb.InsertToQueue(order, userID);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Add a close order to the database.\n Important! The direction of order will chnaged 
 /// </summary>
 /// <param name="order">an Order object</param>
 public void AddClose(Order order)
 {
     Order order2 = order.Clone();
     order2.Direction = (order.Direction == 0 ? 1 : 0);
     Add(order2);
 }
Exemplo n.º 8
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.º 9
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.º 10
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.º 11
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.º 12
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.º 13
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();
     }
 }
Exemplo n.º 14
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();
     }
 }