public static bool CheckMarginAmount(Order order) { double newReqMargin, newInitialOrderMargin, price; double orderPrice; int sign; if (order.OrderAction == "DELETE") // will be handled when exchange sends confirmation { //trader log will be updated when trade is executed return true; } //uses current price for market orders if (!traderMarginAcct.ContainsKey(order.CustomerID)) StartMarginAcct(order.CustomerID); orderPrice = (order.OrderType == "LIMIT" ? order.LimitPrice : order.OrderType == "STOP" ? order.StopPrice : Container.currentPrice); initaltraderMargin temp = (initaltraderMargin)traderMarginAcct[order.CustomerID]; sign = (order.BuySell == "B" ? 1 : -1); newInitialOrderMargin = orderPrice * order.Quantity * 0.02 * sign; newReqMargin = orderPrice * order.Quantity * 0.018 * sign; if (Math.Abs(temp.RequiredMargin + newInitialOrderMargin) < temp.AccountBalance)//probably need to make this work better { price = (temp.Price * temp.Positions + order.Quantity * orderPrice * sign) / (temp.Positions + order.Quantity); double gain = (temp.Price - price) * sign; if (temp.Positions < temp.Positions + order.Quantity * sign) //closing out some positions need to work on this temp.AccountBalance = temp.AccountBalance + gain * (temp.Positions + order.Quantity * sign); temp.Price = price; temp.Positions += order.Quantity * sign; temp.RequiredMargin += newReqMargin * sign; return true; } else { //return trade, insuficient margin return false; } }
public static bool CheckMarginAmount(Order order)// Idealy on open margin information would be loaded from an xml and on close xml would be updated { double newReqMargin, newInitialOrderMargin, price, gain; double orderPrice; int sign; if (order.OrderAction == "DELETE") { //trader log will be updated when trade is executed return(true); } //if no record of trader new account is added with defualt values if (!traderMarginAcct.ContainsKey(order.CustomerID)) { StartMarginAcct(order.CustomerID); } //uses current price for market orders orderPrice = (order.OrderType == "LIMIT" ? order.LimitPrice : order.OrderType == "STOP" ? order.StopPrice : Container.currentPrice); initaltraderMargin temp = (initaltraderMargin)traderMarginAcct[order.CustomerID]; sign = (order.BuySell == "B" ? 1 : -1); newInitialOrderMargin = orderPrice * order.Quantity * 0.02 * sign; newReqMargin = orderPrice * order.Quantity * 0.018 * sign; if (Math.Abs(temp.RequiredMargin + newInitialOrderMargin) < temp.AccountBalance)//checks that the trader has enough available margin { price = (temp.Price * temp.Positions + order.Quantity * orderPrice) / (temp.Positions + order.Quantity); temp.Price = price; temp.Positions += order.Quantity * sign; temp.RequiredMargin = temp.positions * temp.Price * 0.018; return(true); } else { //return trade, insuficient margin return(false); } }
public static void UpdateMarginForDeletedOrder(Order order) { double orderPrice, newReqMargin, price; int sign; orderPrice = (order.OrderType == "LIMIT" ? order.LimitPrice : order.OrderType == "STOP" ? order.StopPrice : Container.currentPrice); initaltraderMargin temp = (initaltraderMargin)traderMarginAcct[order.CustomerID]; sign = (order.BuySell == "B" ? -1 : 1);//revesed since order is being deleted newReqMargin = orderPrice * order.Quantity * 0.018 * sign; price = (temp.Price * temp.Positions + order.Quantity * orderPrice * sign) / (temp.Positions + order.Quantity); temp.Price = price; temp.Positions += order.Quantity * sign; temp.RequiredMargin += newReqMargin * sign; //return true; }
public static void UpdateMarginForExucutedOrder(Order order) { double orderPrice, newReqMargin; int sign; orderPrice = (order.OrderType == "LIMIT" ? order.LimitPrice : order.OrderType == "STOP" ? order.StopPrice : Container.currentPrice); orderPrice = orderPrice - order.executionPrice;//difference between original price and executed price initaltraderMargin temp = (initaltraderMargin)traderMarginAcct[order.CustomerID]; sign = (order.BuySell == "B" ? 1 : -1); if (Math.Abs(temp.Positions) > Math.Abs(temp.Positions + order.Quantity * sign)) //closing out some positions { orderPrice = order.executionPrice; double gain = orderPrice - temp.Price; temp.AccountBalance = temp.AccountBalance + gain * order.executionQuantity; int netPosition = Math.Abs(temp.Positions) - Math.Abs(order.Quantity); temp.Positions += order.Quantity * sign; if (netPosition < 0) { temp.Price = orderPrice; temp.RequiredMargin = orderPrice * temp.Positions * 0.018 * sign; } else { temp.RequiredMargin = temp.Price * temp.Positions * 0.018;// * -sign; } } else { newReqMargin = orderPrice * order.ExecutionQuantity * 0.018 * sign; temp.Price = (temp.Price * temp.Positions + order.ExecutionQuantity * orderPrice) / (temp.Positions); temp.RequiredMargin = temp.Price * temp.positions * 0.018; } }
public static bool CheckMarginAmount(Order order) { double newReqMargin, newInitialOrderMargin, maint, price; int sign; if (order.OrderAction == "DELETE") // will be handled when exchange sends confirmation { //trader log will be updated when trade is executed return(true); } if (!traderMarginAcct.ContainsKey(order.CustomerID)) { StartMarginAcct(order.CustomerID); } initaltraderMargin temp = (initaltraderMargin)traderMarginAcct[order.CustomerID]; sign = (order.BuySell == "B" ? 1 : -1); newInitialOrderMargin = order.LimitPrice * order.Quantity * Convert.ToDouble(ConfigurationManager.AppSettings["initialMargin"]) * sign; newReqMargin = order.LimitPrice * order.Quantity * Convert.ToDouble(ConfigurationManager.AppSettings["maintMargin"]) * sign; if (Math.Abs(temp.RequiredMargin + newInitialOrderMargin) > temp.AccountBalance) //probably need to make this work better { price = (temp.Price * temp.Positions + order.Quantity * order.LimitPrice) / (temp.Positions + order.Quantity); temp.Price = price; temp.Positions += order.Quantity * sign; temp.RequiredMargin += newReqMargin * sign; return(true); } else { //return trade, insuficient margin return(false); } }