/// <summary> /// Clears the state items associated with an inactive thrust. /// </summary> /// <param name="chart">The chart containing the thrust pattern to be purged.</param> /// <param name="thrust">The inactive thrust pattern to be purged.</param> /// <returns></returns> protected virtual async Task PurgeThrust(Chart chart, Thrust thrust) { // kill and save the signal MCE.Signal signal = await SubscriptionCaller.Instance().GetSignalAsync(thrust.SignalID); signal.Active = false; await SubscriptionCaller.Instance().UpdateSignalAsync(signal); // clear the chart chart.Patterns.RemoveAll(p => p.Type == MACC.Constants.SignalType.Thrust); Orders.Remove(Orders.FirstOrDefault(o => ((MarketMinerOrder)o).SignalID == signal.SignalID)); Trades.Remove(Trades.FirstOrDefault(t => ((MarketMinerTrade)t).SignalID == signal.SignalID)); }
/// <summary> /// Handler for newly discovered thrust patterns. /// </summary> /// <param name="chart">The chart containing the thrust pattern.</param> /// <param name="thrust">The discovered or updated thrust pattern.</param> protected virtual async void HandleNewThrust(Chart chart, Thrust thrust) { // new signal // save the signal MCE.Signal signal = GetSignalFromThrust(thrust); signal = await SubscriptionCaller.Instance().UpdateSignalAsync(signal); // place the order signal.StrategyTransactionID = await CreateEntryOrder(signal, chart, thrust); // share with others SubscriptionCaller.Instance().PushSignalToSubscribersAsync(signal); // update the thrust thrust.SignalID = signal.SignalID; thrust.StrategyTransactionID = signal.StrategyTransactionID; }
/// <summary> /// Creates a Signal object from the supplied Thrust object. /// </summary> /// <param name="thrust"></param> /// <returns>The Signal object created.</returns> protected virtual MCE.Signal GetSignalFromThrust(Thrust thrust) { // find the productID var product = SubscriptionCaller.Instance().GetProductByName(thrust.GetProductName()); if (product == null) { throw new NotFoundException(string.Format("Product ({0}) not found for thrust signal.", thrust.GetProductName())); } // save the signal var signal = new MCE.Signal() { ProductID = product.ProductID }; signal.InjectWith(thrust); return(signal); }
/// <summary> /// /// </summary> /// <param name="signal"></param> /// <param name="instrument"></param> /// <param name="retracement"></param> /// <returns></returns> protected virtual async Task <int> CreateEntryOrder(MCE.Signal signal, Chart chart, Thrust thrust) { StrategyTransaction transaction = null; Tuple <double, double, double> orderPrices = GetOrderPrices(chart.HistoricBidAskSpread, signal.Side, thrust); double entryPrice = orderPrices.Item1; double stopLossPrice = orderPrices.Item2; double takeProfitPrice = orderPrices.Item3; int tradeUnits = await TradeUnitsCapacity(chart.Instrument, entryPrice, stopLossPrice); if (tradeUnits > 0) { // 12 hours for now .. how should this change? .. read from metaSetting? string expiry = MAOE.Utilities.GetTimeAsXmlSerializedUtc(DateTime.UtcNow.AddHours(12)); string type = "limit"; string side = signal.Side == MACC.Constants.SignalSide.Buy ? "buy" : "sell"; int places = ((FibonacciRetracement)thrust.Study).LevelPlaces(); // create order var orderData = new Dictionary <string, string> { { "instrument", chart.Instrument }, { "units", tradeUnits.ToString() }, { "side", side }, { "type", type }, { "expiry", expiry }, { "price", Math.Round(entryPrice, places).ToString() }, { "stopLoss", Math.Round(stopLossPrice, places).ToString() }, { "takeProfit", Math.Round(takeProfitPrice, places).ToString() } }; PostOrderResponse response = await PlaceLimitOrder(orderData); if (response.orderOpened != null && response.orderOpened.id > 0) { MarketMinerOrder orderOpened = new MarketMinerOrder(response.orderOpened); orderOpened.SignalID = signal.SignalID; transaction = CreateEntryTransaction(chart.Instrument, orderOpened.side, response.time, type, response.price.GetValueOrDefault(), orderOpened.takeProfit, orderOpened.stopLoss, orderOpened.id.ToString()); transaction = await StrategyCaller.Instance().SaveStrategyTransactionAsync(transaction, MarketMiner.Common.Constants.Brokers.OANDA); orderOpened.StrategyTransactionID = transaction.StrategyTransactionID; // add entry trans id to the signal signal.StrategyTransactionID = transaction.StrategyTransactionID; await SubscriptionCaller.Instance().UpdateSignalAsync(signal); AddOrUpdateAlgorithmOrder(orderOpened); DateTime transactionTime = Convert.ToDateTime(response.time).ToUniversalTime(); thrust.SetOrUpdatePrices(entryPrice, takeProfitPrice, stopLossPrice, places, transactionTime); } } else { string missedTradeReason = Utilities.GetMissedTradeReason(tradeUnits); AddAlgorithmMessage(string.Format("Missed trade: {0} for signalId: {1}", missedTradeReason, signal.SignalID), true, TraceEventType.Information); } return(transaction == null ? 0 : transaction.StrategyTransactionID); }