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 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()); }
private static async void MarketUnpairedSierraOrder(InstrumentConversion instrumentConversion, SierraOrder sierraOrder, OAOrder oaOrder) { SierraOrderFacade sierraChartOrderFacade = new SierraOrderFacade(); OAOrder oaMarketOrder = new OAOrder(); oaMarketOrder.Instrument = instrumentConversion.OandaInstrumentName; oaMarketOrder.Units = sierraOrder.BuySell == BSE_BUY ? sierraOrder.OrderQuantity : -sierraOrder.OrderQuantity; oaMarketOrder.Id = await SvpOandaV20.Instance.CreateMarketOrder(InstrumentConversion.SierraChartAccountId, oaMarketOrder.Instrument, oaMarketOrder.Units); SierraOrderEntity sierraChartOrderEntity = new SierraOrderEntity(); sierraChartOrderEntity.SierraOrderId = sierraOrder.InternalOrderID; sierraChartOrderEntity.OandaOrderId = oaMarketOrder.Id; sierraChartOrderEntity.SierraInstrumentName = instrumentConversion.SierraInstrumentName; sierraChartOrderEntity.SierraPrice = sierraOrder.AvgFillPrice; sierraChartOrderFacade.Save(sierraChartOrderEntity); oaOrder.Id = oaMarketOrder.Id; oaOrder.Units = oaMarketOrder.Units; oaOrder.Instrument = instrumentConversion.OandaInstrumentName; }
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; } }
/// <summary> /// Lze zrusit PT a nasledne lze uzavrit pozici uzavrenim SL. /// Vhodne napriklad pri rychlem pohybu. /// </summary> public static void MakePosition(string sierraInstrumentName, int sierraPosition, List <SierraOrder> sierraOrders) { try { if (runned) { return; } if (counter++ < 2) { return; } counter = 0; runned = true; if (string.IsNullOrWhiteSpace(sierraInstrumentName)) { Log.WriteMessage("sierraInstrumentNamed = " + sierraInstrumentName); return; } if (SierraChartWrongPosition(sierraPosition, sierraOrders)) { Log.WriteMessage("SierraChartWrongPosition"); return; } if (sierraInstrumentName.IndexOf("[Sim]", StringComparison.InvariantCultureIgnoreCase) == 0) { sierraInstrumentName = sierraInstrumentName.Substring("[Sim]".Length); } sierraInstrumentName = sierraInstrumentName.Substring(0, 2); InstrumentConversion instrumentConversion = InstrumentConversion.InstrumentConversions.Find(x => x.SierraInstrumentName == sierraInstrumentName); if (instrumentConversion == null) { Log.WriteMessage("sierraInstrumentName" + sierraInstrumentName); return; } OAOrders oaOrders = SvpOandaV20.Instance.GetMarketOrders(InstrumentConversion.SierraChartAccountId, instrumentConversion.OandaInstrumentName); List <PairedOrder> pairedOrders = DoPairOrders(instrumentConversion, oaOrders, sierraOrders); CloseUnpairedOandaOrders(oaOrders, pairedOrders); MarketUnpairedSierraOrders(instrumentConversion, sierraOrders, pairedOrders, oaOrders); pairedOrders = DoPairOrders(instrumentConversion, oaOrders, sierraOrders); ModifyPairedSierraOrders(instrumentConversion, pairedOrders); if (sierraPosition == 0 && (int)oaOrders.Sum(x => x.Units) == 0) { SierraOrderFacade.DeleteAllS(instrumentConversion.SierraInstrumentName); } } catch (AggregateException aggregateException) { foreach (Exception ex in aggregateException.InnerExceptions) { Log.WriteException(ex); } throw aggregateException; } catch (Exception ex) { Log.WriteException(ex); throw ex; } finally { runned = false; } }