Exemple #1
0
        private void SellAsset(IIndexedOhlcv indexedCandle, decimal premium, IDictionary <IEnumerable <IOhlcv>, decimal> assetCashMap, IList <Transaction> transactions)
        {
            if (assetCashMap.TryGetValue(indexedCandle.BackingList, out _))
            {
                var nextCandle      = indexedCandle.Next;
                var lastTransaction = transactions.LastOrDefault(t => t.OhlcvList.Equals(indexedCandle.BackingList));
                if (lastTransaction == default)
                {
                    return;
                }

                var     quantity          = lastTransaction.Quantity;
                decimal cashWhenSellAsset = nextCandle.Open * quantity - premium;

                // EUR/USD (1€ = 1000$) ; flat exchange fee ratio percent = 0.1
                // you sell 1.999€
                // Total 1999€, fee = 1.999, net = 1997.001 €
                cashWhenSellAsset -= _flatExchangeFee * cashWhenSellAsset;

                decimal profitLossRatio = (cashWhenSellAsset - lastTransaction.AbsoluteCashFlow) / lastTransaction.AbsoluteCashFlow;
                assetCashMap[indexedCandle.BackingList] += cashWhenSellAsset;

                transactions.Add(new Transaction(indexedCandle.BackingList, nextCandle.Index, nextCandle.DateTime, TransactionType.Sell, quantity, cashWhenSellAsset));
                OnSold?.Invoke(indexedCandle.BackingList, nextCandle.Index, nextCandle.DateTime, nextCandle.Open, quantity, cashWhenSellAsset, assetCashMap[indexedCandle.BackingList], profitLossRatio);
            }
        }
Exemple #2
0
        private void SellAsset(IndexedCandle indexedCandle, decimal premium, IDictionary <IEnumerable <Candle>, decimal> assetCashMap, IList <Transaction> transactions)
        {
            if (assetCashMap.TryGetValue(indexedCandle.BackingList, out _))
            {
                var nextCandle      = indexedCandle.Next;
                var lastTransaction = transactions.LastOrDefault(t => t.Candles.Equals(indexedCandle.BackingList));

                decimal cashIn  = nextCandle.Open * lastTransaction.Quantity - premium;
                decimal plRatio = (cashIn - lastTransaction.AbsoluteCashFlow) / lastTransaction.AbsoluteCashFlow;
                assetCashMap[indexedCandle.BackingList] += cashIn;

                transactions.Add(new Transaction(indexedCandle.BackingList, nextCandle.Index, nextCandle.DateTime, TransactionType.Sell, lastTransaction.Quantity, cashIn));
                OnSold?.Invoke(indexedCandle.BackingList, nextCandle.Index, nextCandle.DateTime, nextCandle.Open, lastTransaction.Quantity, cashIn, assetCashMap[indexedCandle.BackingList], plRatio);
            }
        }
Exemple #3
0
        private void SellAsset(IIndexedOhlcv indexedCandle, IFeeCalculator calculator, IDictionary <IEnumerable <IOhlcv>, decimal> assetCashMap, IList <Transaction> transactions)
        {
            if (assetCashMap.TryGetValue(indexedCandle.BackingList, out _))
            {
                var nextCandle      = indexedCandle.Next;
                var lastTransaction = transactions.LastOrDefault(t => t.OhlcvList.Equals(indexedCandle.BackingList));
                if (lastTransaction == default)
                {
                    return;
                }

                var transaction = calculator.SellAsset(indexedCandle, lastTransaction);

                assetCashMap[indexedCandle.BackingList] += transaction.AbsoluteCashFlow;
                decimal profitLossRatio = (transaction.AbsoluteCashFlow - lastTransaction.AbsoluteCashFlow) / lastTransaction.AbsoluteCashFlow;

                transactions.Add(transaction);
                OnSold?.Invoke(indexedCandle.BackingList, nextCandle.Index, nextCandle.DateTime, nextCandle.Open, transaction.Quantity, transaction.AbsoluteCashFlow, assetCashMap[indexedCandle.BackingList], profitLossRatio);
            }
        }
Exemple #4
0
        private void SellAsset(IIndexedOhlcv indexedCandle, decimal premium, IDictionary <IEnumerable <IOhlcv>, decimal> assetCashMap, IList <Transaction> transactions)
        {
            if (assetCashMap.TryGetValue(indexedCandle.BackingList, out _))
            {
                var nextIOhlcvData  = indexedCandle.Next;
                var lastTransaction = transactions.LastOrDefault(t => t.IOhlcvDatas.Equals(indexedCandle.BackingList));

                var     baseCurrencyQte  = lastTransaction.Quantity;
                decimal quoteCurrencyQte = nextIOhlcvData.Open * baseCurrencyQte - premium;

                // EUR/USD (1€ = 1000$) ; flat exchange fee ratio percent = 0.1
                // you sell 1.999€
                // Total 1999€, fee = 1.999, net = 1997.001 €
                quoteCurrencyQte -= _flatExchangeFee * quoteCurrencyQte;

                decimal plRatio = (quoteCurrencyQte - lastTransaction.AbsoluteCashFlow) / lastTransaction.AbsoluteCashFlow;
                assetCashMap[indexedCandle.BackingList] += quoteCurrencyQte;

                transactions.Add(new Transaction(indexedCandle.BackingList, nextIOhlcvData.Index, nextIOhlcvData.DateTime, TransactionType.Sell, baseCurrencyQte, quoteCurrencyQte));
                OnSold?.Invoke(indexedCandle.BackingList, nextIOhlcvData.Index, nextIOhlcvData.DateTime, nextIOhlcvData.Open, baseCurrencyQte, quoteCurrencyQte, assetCashMap[indexedCandle.BackingList], plRatio);
            }
        }