private void dgvClosedTrades_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { DataGridViewRow row = ((DataGridView)sender).Rows[e.RowIndex]; TradeModel trade = (TradeModel)DataGridHelper.GetCell(row.Cells, "Trade"); SetColor(row, trade); }
private void PerformSell(TradeModel trade, bool fromSignal) { OrderModel order = null; var stopwatch = new Stopwatch(); long elapsedTime = 0; txtLogs.AppendText($"{DateTime.Now} - {trade.CurrencyPair} => SELL {(fromSignal ? "(signal)" : "(tol)")} ...{Environment.NewLine}"); trade.Status = EnumStatus.SELLING; dgvCurrentTrades.Refresh(); stopwatch.Start(); order = ExecuteOrder.ExecuteBuySell(OrderType.Sell, trade.CurrencyPair, trade.Amount, RealTrade); stopwatch.Stop(); elapsedTime = stopwatch.ElapsedMilliseconds; txtLogs.AppendText($"{DateTime.Now} - {trade.CurrencyPair} => OK SOLD ({elapsedTime / 1000} secs){Environment.NewLine}"); trade.Status = EnumStatus.SOLD; trade.Action = "BUY"; DisposeSubscribeTicker(trade); AddClosedTrade(trade); trade.Ticker = null; trade.Status = EnumStatus.WAITING; dgvCurrentTrades.Refresh(); }
void dataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { DataGridViewRow row = ((DataGridView)sender).Rows[e.RowIndex]; TradeModel trade = (TradeModel)DataGridHelper.GetCell(row.Cells, "Trade"); SetColor(row, trade); }
private void SetColor(DataGridViewRow row, TradeModel trade) { if (trade.Ticker != null) { if (trade.Ticker.Profit >= 5) { row.DefaultCellStyle.BackColor = Color.MediumPurple; } else if (trade.Ticker.Profit >= 1) { row.DefaultCellStyle.BackColor = Color.Lime; } else if (trade.Ticker.Profit >= 0) { row.DefaultCellStyle.BackColor = Color.LightGreen; } else { row.DefaultCellStyle.BackColor = Color.LightPink; } } else { row.DefaultCellStyle.BackColor = Color.White; } }
private void AnalyseSignal(TradeModel trade, SignalModel signal) { if (signal.OrderType == OrderType.Buy) { if (trade.Status == EnumStatus.WAITING) { PerformBuy(trade); } else if (trade.Status == EnumStatus.BOUGHT) { txtLogs.AppendText($"{DateTime.Now} - {signal.CurrencyPair} => BUY - Already bought{Environment.NewLine}"); } } else if (signal.OrderType == OrderType.Sell) { if (trade.Status == EnumStatus.BOUGHT) { PerformSell(trade, true); } else if (trade.Status == EnumStatus.WAITING) { txtLogs.AppendText($"{DateTime.Now} - {signal.CurrencyPair} => SELL - Already sold{Environment.NewLine}"); } } }
private Subject <SignalModel> SetManualSignal() { Subject <SignalModel> manualSignal = new Subject <SignalModel>(); this.dgvCurrentTrades.CellContentClick += (object sender, DataGridViewCellEventArgs e) => { var senderGrid = (DataGridView)sender; if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) { TradeModel trade = (TradeModel)DataGridHelper.GetCell(dgvCurrentTrades.CurrentRow.Cells, "Trade"); OrderType orderType = OrderType.Buy; if (trade.Action == OrderType.Buy.ToString().ToUpper()) { orderType = OrderType.Buy; } else if (trade.Action == OrderType.Sell.ToString().ToUpper()) { orderType = OrderType.Sell; } SignalModel signal = new SignalModel() { CurrencyPair = trade.CurrencyPair, OrderType = orderType }; manualSignal.OnNext(signal); } }; return(manualSignal); }
public TradeModel(TradeModel trade) { CurrencyPair = trade.CurrencyPair; Status = trade.Status; ToleranceBuy = trade.ToleranceBuy; Tolerance = trade.Tolerance; Multiplicator = trade.Multiplicator; Amount = trade.Amount; PricePaid = trade.PricePaid; TolAjusted = trade.TolAjusted; TimeBuy = trade.TimeBuy; Action = trade.Action; if (trade.Ticker != null) { Ticker = new TickerModel() { BaseCurrencyTotal = trade.Ticker.BaseCurrencyTotal, HighestPrice = trade.Ticker.HighestPrice, HighestProfit = trade.Ticker.HighestProfit, HPDiff = trade.Ticker.HPDiff, LastTicker = trade.Ticker.LastTicker, PriceLast = trade.Ticker.PriceLast, Profit = trade.Ticker.Profit, UsdtValue = trade.Ticker.UsdtValue }; } }
private void DisposeSubscribeTicker(TradeModel trade) { if (_subsTickers.TryGetValue(trade.CurrencyPair, out IDisposable subscribe)) { _subsTickers.Remove(trade.CurrencyPair); subscribe.Dispose(); } }
private void btnStartBot_Click(object sender, EventArgs e) { TradeModel trade = new TradeModel() { CurrencyPair = CurrencyPair, Status = EnumStatus.WAITING, Amount = GetBotAmount(), ToleranceBuy = double.Parse(lblValueToleranceBuy.Text), Tolerance = double.Parse(Utilities.TryParseDouble(BotLosstolerance).ToString()), Multiplicator = double.Parse(Utilities.TryParseDouble(BotMultiplicator).ToString()), Action = "BUY" }; AddBot(trade); }
private void UpdateCurrentTrades(TradeModel trade) { FlatTradeModel flatTrade = _blCurrentTrades.Where(x => object.Equals(x.Trade.CurrencyPair, trade.CurrencyPair)).First(); flatTrade.Trade = trade; dgvCurrentTrades.Refresh(); if (CheckTolerance(trade)) { PerformSell(trade, false); } CalculTotalProfit(); }
private bool CheckTolerance(TradeModel trade) { if (trade.Ticker == null) { return(false); } trade.TolAjusted = trade.Tolerance + (trade.Ticker.HighestProfit * trade.Multiplicator); trade.TolAjusted = trade.TolAjusted > trade.Tolerance / 4 ? trade.Tolerance / 4 : trade.TolAjusted; if (trade.Ticker.Profit < double.Parse(lblValueToleranceBuy.Text) || trade.Ticker.HPDiff < trade.TolAjusted) { System.Media.SystemSounds.Exclamation.Play(); return(true); } return(false); }
private void AddBot(TradeModel trade) { if (!_blCurrentTrades.Select(x => x.CurrencyPair).Contains(trade.CurrencyPair.ToString())) { FlatTradeModel flatTrade = new FlatTradeModel(trade); _blCurrentTrades.Add(flatTrade); Subject <SignalModel> manualSignal = SetManualSignal(); _signal = new Signal(trade.CurrencyPair); _subsSignals.Add(trade.CurrencyPair, _signal.DataSource .Merge(manualSignal) .ObserveOn(WindowsFormsSynchronizationContext.Current) .Subscribe(x => AnalyseSignal(trade, x))); } else { txtLogs.AppendText($"Bot {trade.CurrencyPair} => Already running{Environment.NewLine}"); } }
private void PerformBuy(TradeModel trade) { OrderModel order = null; var stopwatch = new Stopwatch(); long elapsedTime = 0; txtLogs.AppendText($"{DateTime.Now} - {trade.CurrencyPair} => BUY ...{Environment.NewLine}"); trade.Status = EnumStatus.BUYING; dgvCurrentTrades.Refresh(); stopwatch.Start(); order = ExecuteOrder.ExecuteBuySell(OrderType.Buy, trade.CurrencyPair, 0, RealTrade); stopwatch.Stop(); elapsedTime = stopwatch.ElapsedMilliseconds; txtLogs.AppendText($"{DateTime.Now} - {trade.CurrencyPair} => OK BOUGHT ({elapsedTime / 1000} secs){Environment.NewLine}"); trade.Status = EnumStatus.BOUGHT; trade.PricePaid = order.PricePerCoin; trade.TolAjusted = trade.Tolerance; trade.TimeBuy = DateTime.Now; trade.Action = "SELL"; dgvCurrentTrades.Refresh(); _ticker = new Ticker(trade.CurrencyPair, trade.Amount, trade.PricePaid, 0); if (!_subsTickers.TryGetValue(trade.CurrencyPair, out IDisposable subscribe)) { _subsTickers.Add(trade.CurrencyPair, (_ticker.DataSource .ObserveOn(WindowsFormsSynchronizationContext.Current) .Subscribe(x => { trade.Ticker = x; UpdateCurrentTrades(trade); }))); } }
public FlatTradeModel(TradeModel trade) { Trade = new TradeModel(trade); }
private void AddClosedTrade(TradeModel trade) { FlatTradeModel flatTrade = new FlatTradeModel(trade); _blClosedTrades.Add(flatTrade); }