public List <string> GetOpenedOrdersChangeNotifications() { List <string> res = new List <string>(); if (PrevOpenedOrders == null) { PrevOpenedOrders = new OpenedOrderInfo[0]; } for (int i = 0; i < PrevOpenedOrders.Length; i++) { var po = PrevOpenedOrders[i]; var oo = OpenedOrders.FirstOrDefault(o => o.OrderId == PrevOpenedOrders[i].OrderId); if (oo == null) { res.Add("Order " + po.OrderId + " completely filled"); } else if (oo.Amount != po.Amount) { res.Add("Order " + po.OrderId + " partially filled"); } } for (int i = 0; i < OpenedOrders.Count; i++) { var oo = OpenedOrders[i]; var po = PrevOpenedOrders.FirstOrDefault(p => p.OrderId == oo.OrderId); if (po == null) { res.Add("New order '" + oo.OrderId + "' added. " + oo.TickerName + " rate = " + oo.ValueString + " amount = " + oo.AmountString); } } PrevOpenedOrders = new OpenedOrderInfo[OpenedOrders.Count]; OpenedOrders.CopyTo(PrevOpenedOrders); return(res); }
protected virtual void UpdateCurrentLoss() { if (StrategyData.Count > 0) { CombinedStrategyDataItem last = (CombinedStrategyDataItem)StrategyData.Last(); last.CurrentLoss = Math.Min(last.CurrentLoss, OpenedOrders.Sum(o => o.CurrentLoss)); } }
protected virtual OpenPositionInfo OpenShortPosition(Ticker ticker, string mark, double value, double amount, bool allowTrailing, bool checkForMinValue, double trailingStopLossPc, double minProfitPc) { double spent = ticker.SpentInBaseCurrency(value, amount); if (1.05 * spent > GetMaxAllowedShortDeposit()) { return(null); } if (checkForMinValue && spent < MinDepositForOpenPosition) { return(null); } TradingResult res = MarketSell(ticker, value, amount); if (res == null) { return(null); } OpenPositionInfo info = new OpenPositionInfo() { Ticker = ticker, DataItemIndex = StrategyData.Count - 1, Time = DataProvider.CurrentTime, Type = OrderType.Sell, Spent = spent + CalcFee(res.Total), AllowTrailing = allowTrailing, StopLossPercent = trailingStopLossPc, OpenValue = res.Value, OpenAmount = res.Amount, Amount = res.Amount, Mark = mark, AllowHistory = (DataProvider is SimulationStrategyDataProvider), Total = res.Total, MinProfitPercent = minProfitPc, CloseValue = value * (1 + minProfitPc * 0.01), }; info.UpdateCurrentValue(DataProvider.CurrentTime, res.Value); OpenedOrders.Add(info); OrdersHistory.Add(info); OnOpenShortPosition(info); UpdateMaxAllowedShortDeposit(-info.Spent); IOpenedPositionsProvider provider = (IOpenedPositionsProvider)StrategyData.Last(); provider.OpenedPositions.Add(info); provider.AddMark(mark); Save(); return(info); }
protected virtual OpenPositionInfo OpenLongPosition(string mark, double value, double amount, bool allowTrailing, bool checkForMinValue, double trailingStopLossPc, double minProfitPc) { if (1.05 * value * amount > MaxAllowedDeposit) { return(null); } if (checkForMinValue && value * amount < MinDepositForOpenPosition) { return(null); } TradingResult res = MarketBuy(value, amount); if (res == null) { return(null); } OpenPositionInfo info = new OpenPositionInfo() { DataItemIndex = StrategyData.Count - 1, Time = DataProvider.CurrentTime, Type = OrderType.Buy, Spent = res.Total + CalcFee(res.Total), AllowTrailing = allowTrailing, StopLossPercent = trailingStopLossPc, OpenValue = res.Value, OpenAmount = res.Amount, Amount = res.Amount, Mark = mark, AllowHistory = (DataProvider is SimulationStrategyDataProvider), Total = res.Total, MinProfitPercent = minProfitPc, CloseValue = value * (1 + minProfitPc * 0.01), }; info.UpdateCurrentValue(DataProvider.CurrentTime, res.Value); OpenedOrders.Add(info); OrdersHistory.Add(info); OnOpenLongPosition(info); MaxAllowedDeposit -= info.Spent; IOpenedPositionsProvider provider = (CombinedStrategyDataItem)StrategyData.Last(); provider.OpenedPositions.Add(info); provider.AddMark(mark); Save(); return(info); }
protected void CloseShortPosition(OpenPositionInfo info, TradingResult res) { if (res == null) { return; } double earned = res.Total - CalcFee(res.Total); MaxAllowedShortDeposit += earned; info.UpdateCurrentValue(DataProvider.CurrentTime, res.Value); info.Earned += earned; info.Amount -= res.Amount; info.Total -= res.Total; info.CloseValue = res.Value; //CombinedStrategyDataItem item = (CombinedStrategyDataItem)StrategyData.FirstOrDefault(i => ((CombinedStrategyDataItem)i).Time == info.CandlestickTime); //if(item != null) { // item.Closed = true; // item.CloseLength = ((CombinedStrategyDataItem)StrategyData.Last()).Index - item.Index; //} IOpenedPositionsProvider last = (IOpenedPositionsProvider)StrategyData.Last(); double fee = 0.075 / 100.0; double profit = ((1 / info.CloseValue) - (1 / info.OpenValue)) * res.Amount; double openFee = fee * (1 / info.OpenValue) * res.Amount; // info.Ticker.Total(info.OpenValue, res.Amount); double closeFee = fee * (1 / info.CloseValue) * res.Amount; // info.Ticker.Total(info.CloseValue, res.Amount); profit -= openFee + closeFee; profit *= Short.UsdTicker.OrderBook.Bids[0].Value; Earned += profit; if (info.Amount < 0.000001) { OpenedOrders.Remove(info); last.ClosedPositions.Add(info); info.CloseTime = DataProvider.CurrentTime; } //last.ClosedOrder = true; //last.Value = Ticker.OrderBook.Bids[0].Value; //if(item != null) // item.Profit = profit; //last.AddMark("Close " + info.Mark); }
protected bool AlreadyOpenedPingPongPosition() { if (OpenedOrders.Count > 0 && OpenedOrders.Last().CurrentValue > OpenedOrders.Last().StopLoss) { return(true); } CombinedStrategyDataItem last = (CombinedStrategyDataItem)StrategyData.Last(); foreach (OpenPositionInfo info in OpenedOrders) { if (info.Mark != "PP") { continue; } SRValue res = SRIndicator2.FindSupportByTime(info.CandlestickTime); if (res != null && info.Type == OrderType.Buy && (SRIndicator2.BelongsSameSupportLevel(res) || last.Index - info.DataItemIndex < 5)) { return(true); } } return(false); }
protected void CloseLongPosition(OpenPositionInfo info, TradingResult res) { if (res == null) { return; } double earned = res.Total - CalcFee(res.Total); MaxAllowedDeposit += earned; info.UpdateCurrentValue(DataProvider.CurrentTime, res.Value); info.Earned += earned; info.Amount -= res.Amount; info.Total -= res.Total; info.CloseValue = res.Value; //StatisticalArbitrageHistoryItem item = (StatisticalArbitrageHistoryItem)StrategyData.FirstOrDefault(i => ((StatisticalArbitrageHistoryItem)i).Time == info.CandlestickTime); //if(item != null) { // item.Closed = true; // item.CloseLength = ((CombinedStrategyDataItem)StrategyData.Last()).Index - item.Index; //} IOpenedPositionsProvider last = StrategyData.Last() as IOpenedPositionsProvider; if (info.Amount < 0.000001) { OpenedOrders.Remove(info); last.ClosedPositions.Add(info); info.CloseTime = DataProvider.CurrentTime; Earned += info.Earned - info.Spent; } //last.ClosedOrder = true; //last.Value = Ticker.OrderBook.Bids[0].Value; //if(item != null) // item.Profit = earned - info.Spent; //last.AddMark("Close " + info.Mark); }
protected override void CloseLongPosition(OpenPositionInfo info) { TradingResult res = MarketSell(Ticker.OrderBook.Bids[0].Value, info.Amount); if (res != null) { double earned = res.Total - CalcFee(res.Total); MaxAllowedDeposit += earned; info.UpdateCurrentValue(DataProvider.CurrentTime, res.Value); info.Earned += earned; info.Amount -= res.Amount; info.Total -= res.Total; info.CloseValue = res.Value; CombinedStrategyDataItem item = (CombinedStrategyDataItem)StrategyData.FirstOrDefault(i => ((CombinedStrategyDataItem)i).Time == info.CandlestickTime); if (item != null) { item.Closed = true; item.CloseLength = ((CombinedStrategyDataItem)StrategyData.Last()).Index - item.Index; } CombinedStrategyDataItem last = (CombinedStrategyDataItem)StrategyData.Last(); if (info.Amount < 0.000001) { OpenedOrders.Remove(info); last.ClosedPositions.Add(info); info.CloseTime = DataProvider.CurrentTime; Earned += info.Earned - info.Spent; } last.ClosedOrder = true; last.Value = Ticker.OrderBook.Bids[0].Value; if (item != null) { item.Profit = earned - info.Spent; } last.AddMark("Close " + info.Mark); } }