public override void ProcessOrders(List <Order> orders) { //cycle through orders that match our symbol list foreach (var order in orders.Where(x => _symbols.Contains(x.Instrument.UnderlyingSymbol) && x.OrderReference.Contains("ETF-Swing"))) { string orderInstrument = order.Instrument.UnderlyingSymbol; //look for an open trade that fits the pattern, was opened before this orders, and uses the same instrument Trade trade = OpenTrades .Where(x => x.Name.StartsWith("ETF-Swing") && x.DateOpened < order.TradeDate && x.Orders.Any(o => o.Instrument.UnderlyingSymbol == orderInstrument)) .OrderByDescending(x => x.DateOpened) .FirstOrDefault(); if (trade != null) { //if such a trade exists, add this order to it SetTrade(order, trade); } else { //if the trade does not exist, create it var side = order.BuySell == "BUY" ? "Long" : "Short"; trade = CreateTrade($"ETF-Swing {orderInstrument} {side} {order.TradeDate:yyyy--MM-dd}"); SetTrade(order, trade); Log($"Created new trade for {orderInstrument}"); } } }
public void MoveTrades() { foreach (var t in OpenTrades.ToList()) { if (t.Trade.CloseDateTime != null) { MoveOpenToClose(t); } } foreach (var t in OrderTradesAsc.ToList()) { if (t.Trade.EntryDateTime != null && t.Trade.CloseDateTime == null) { MoveOrderToOpen(t); } else if (t.Trade.EntryDateTime != null && t.Trade.CloseDateTime != null) { MoveOrderToClosed(t); } } }
public override void ExecuteStrategy() { // Collect data FetchExchangeData(Exchange.ExchangeConfiguration.CoinPairs); // Farming logic: //========================================================== // For each open trade foreach (var openTrade in OpenTrades) { if (openTrade.Type == TradeType.Buy) { //TODO: what should happen here? } else { //TODO: what should happen here? } } // Get all coin pairs that do NOT have open orders var nonOpenCoinPairs = Exchange.ExchangeConfiguration.CoinPairs.FindAll(coinPair => !OpenTrades.Contains(new Trade() { CoinPair = coinPair })); // For each coin pair not in open orders foreach (var coinPair in nonOpenCoinPairs) { // If last trade was a Buy var lastTrade = LastTrades.Find(trade => trade.CoinPair == coinPair); if (lastTrade.Type == TradeType.Buy) { var positiveDistance = (lastTrade.Price / 100) * double.Parse(StrategyConfiguration.Configurations["PositivePercentage"]); var negativeDistance = (lastTrade.Price / 100) * double.Parse(StrategyConfiguration.Configurations["NegativePercentage"]); // If last trade price is higher than last buy price + positive distance if (Tickers.Find(ticker => ticker.CoinPair == coinPair).LastTradePrice > (lastTrade.Price + positiveDistance)) { // Create trailing stop at configured TrailingStopPercent of difference var difference = (lastTrade.Price + positiveDistance) - Tickers.Find(ticker => ticker.CoinPair == coinPair).LastTradePrice; var trailingStopDistance = (difference / 100) * double.Parse(StrategyConfiguration.Configurations["TrailingStopPercent"]); Exchange.CreateTrailingStop(coinPair, trailingStopDistance, InvestedBalances.Find(balance => balance.CoinPair == coinPair).Available); } // If current price is lower than last buy price - negative distance else if (Tickers.Find(ticker => ticker.CoinPair == coinPair).LastTradePrice < (lastTrade.Price - negativeDistance)) { // Create market sell order to cut our losses Exchange.CreateMarketSell(coinPair, InvestedBalances.Find(balance => balance.CoinPair == coinPair).Available); } } else { // Create buy order at a percentage below last trade price (to prevent creating a taker order and paying more fee's) //TODO: complete below //Exchange.CreateBuyOrder(coinPair, ); } } }