private static List <PairedOrder> DoPairOrders(InstrumentConversion instrumentConversion, OAOrders OAOrders, List <SierraOrder> sierraOrders) { List <PairedOrder> pairedOrders = new List <PairedOrder>(); SierraOrderFacade sierraChartOrderFacade = new SierraOrderFacade(); List <SierraOrderEntity> sierraChartOrderEntities = sierraChartOrderFacade.LoadMultipleByColumn( SierraOrderFacade.GetPropertyName <SierraOrderEntity>(x => x.SierraInstrumentName), instrumentConversion.SierraInstrumentName); foreach (SierraOrderEntity sierraChartOrderEntity in sierraChartOrderEntities) { PairedOrder pairedOrder = new PairedOrder(); pairedOrder.sierraOrder = sierraOrders.Find(x => x.InternalOrderID == sierraChartOrderEntity.SierraOrderId); pairedOrder.oandaOrder = OAOrders.Find(x => x.Id == sierraChartOrderEntity.OandaOrderId); if (pairedOrder.sierraOrder != null) { pairedOrder.sierraOrderSL = sierraOrders.Find(x => x.ParentInternalOrderID == pairedOrder.sierraOrder.InternalOrderID && x.OrderTypeAsInt == SCT_ORDERTYPE_STOP); pairedOrder.sierraOrderPT = sierraOrders.Find(x => x.ParentInternalOrderID == pairedOrder.sierraOrder.InternalOrderID && x.OrderTypeAsInt == SCT_ORDERTYPE_LIMIT); } if (pairedOrder.oandaOrder != null && pairedOrder.sierraOrder != null && pairedOrder.sierraOrderSL != null) { pairedOrders.Add(pairedOrder); } } return(pairedOrders); }
private static async void ModifyOandaOrder(InstrumentConversion instrumentConversion, long marketOrderId, SierraOrder sierraOrder, double sierraOrderSLPrice, double sierraOrderPTPrice) { try { if (sierraOrder == null) { Log.WriteMessage("ModifyOandaOrder(), sierraOrder == null"); } SierraOrderFacade sierraChartOrderFacade = new SierraOrderFacade(); SierraOrderEntity sierraOrderEntity = sierraChartOrderFacade.LoadByColumn(SierraOrderFacade.GetPropertyName <SierraOrderEntity>(x => x.SierraOrderId), sierraOrder.InternalOrderID); if (sierraOrderEntity == null) { Log.WriteMessage("ModifyOandaOrder(), sierraOrderEntity == null"); } if (sierraOrderEntity.SierraPT != sierraOrderPTPrice || sierraOrderEntity.SierraSL != sierraOrderSLPrice) { if (sierraOrderEntity.OandaPrice == 0) { //sierraOrderEntity.OandaPrice = await SvpOandaV20.Instance.GetActualPrice(InstrumentConversion.SierraChartAccountId, instrumentConversion.OandaInstrumentName); Trade trade = await SvpOandaV20.Instance.GetMarketOrder(InstrumentConversion.SierraChartAccountId, marketOrderId); sierraOrderEntity.OandaPrice = trade.price; } double compensation = (sierraOrder.BuySell == BSE_BUY ? -instrumentConversion.Compensation : instrumentConversion.Compensation); OAOrder newOAMarketOrder = new OAOrder(); newOAMarketOrder.Id = sierraOrderEntity.OandaOrderId; newOAMarketOrder.Instrument = instrumentConversion.OandaInstrumentName; double sl; if (sierraOrderSLPrice != 0) { sl = sierraOrder.AvgFillPrice - sierraOrderSLPrice; } else { sl = instrumentConversion.SL; } newOAMarketOrder.SL = sierraOrderEntity.OandaPrice - /*(sierraOrder.BuySell == BSE_BUY ? sl : -sl)*/ sl + compensation; double pt; if (sierraOrderPTPrice != 0) { pt = sierraOrderPTPrice - sierraOrder.AvgFillPrice; newOAMarketOrder.PT = sierraOrderEntity.OandaPrice + /*(sierraOrder.BuySell == BSE_BUY ? pt : -pt)*/ pt + compensation; } await SvpOandaV20.Instance.ModifyMarketOrder(InstrumentConversion.SierraChartAccountId, newOAMarketOrder); sierraOrderEntity.SierraPT = sierraOrderPTPrice; sierraOrderEntity.SierraSL = sierraOrderSLPrice; sierraChartOrderFacade.Save(sierraOrderEntity); } } catch (Exception ex) { Log.WriteException(ex); throw; } }
private static void MarketUnpairedSierraOrders(InstrumentConversion instrumentConversion, List <SierraOrder> sierraOrders, List <PairedOrder> pairedOrders, OAOrders oaOrders) { SierraOrderFacade sierraChartOrderFacade = new SierraOrderFacade(); List <Task> tasks = new List <Task>(); foreach (SierraOrder sierraOrder in sierraOrders) { SierraOrderEntity sierraOrderEntity = sierraChartOrderFacade.LoadByColumn(SierraOrderFacade.GetPropertyName <SierraOrderEntity>(x => x.SierraOrderId), sierraOrder.InternalOrderID); if (sierraOrderEntity == null && // objednavka nebyla nikdy pouzita sierraOrder.IsOrder && // Neni to SL ani PT pairedOrders.Find(x => x.sierraOrder.InternalOrderID == sierraOrder.InternalOrderID) == null) // neni to sparovana objednavka { OAOrder oaOrder = new OAOrder(); oaOrders.Add(oaOrder); Task task = Task.Run(() => { MarketUnpairedSierraOrder(instrumentConversion, sierraOrder, oaOrder); }); tasks.Add(task); } } Task.WaitAll(tasks.ToArray()); }