Hi there,

 

I wanna try a strategy to keep my portfolio in balance the whole time. My portfolio consists of 50% BTC and 50%USDT.

The strategy goes like:

If the price of btc RISES a few % than the ratio in my account isn't 50% - 50% anymore. So I need to SELL some btc for USDT to regain the 50%-50%.

If the price of btc DROPS a few % than the ratio in my account isn't 50% - 50% anymore. So I need to BUY some btc from USDT to regain the 50%-50%.

But the bot only should work when the difference is ≥ 2%.

So the principal of this strategy is that you sell btc when is rises and buy when it drops and in the longrun you should gain BTC and USDT.

The problem is I don't know how i can get the USD value of my btc in portfolio. 

To get the btc balance is like this “_btc = Portfolio.CashBook[valutaBTC].Amount;”

 

using QuantConnect.Securities.Crypto;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using QuantConnect;
using QuantConnect.Parameters;
using QuantConnect.Benchmarks;
using QuantConnect.Brokerages;
using QuantConnect.Util;
using QuantConnect.Interfaces;
using QuantConnect.Algorithm;
using QuantConnect.Algorithm.Framework;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Algorithm.Framework.Alphas;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Algorithm.Framework.Execution;
using QuantConnect.Algorithm.Framework.Risk;
using QuantConnect.Indicators;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Custom;
using QuantConnect.Data.Fundamental;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Notifications;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.Slippage;
using QuantConnect.Scheduling;
using QuantConnect.Securities;
using QuantConnect.Securities.Equity;
using QuantConnect.Securities.Forex;
using QuantConnect.Securities.Interfaces;
using QuantConnect.Python;
using QuantConnect.Storage;
using Newtonsoft.Json;
namespace QuantConnect.Algorithm.CSharp
{
   public class VentralTransdimensionalChamber : QCAlgorithm
   {
     
 /* Tradingbot that keeps the balance between BTC and USDT always 50%
  When Btc balance RISES => SELL btc for Usdt 
  When Btc balance DROPS => BUY Btc with Usdt*/
    
     
    private Dictionary<Symbol, SymbolData> _dataBySymbol;
 private Symbol btcusdt;


       // VARIABELEN DECLAREREN
       public decimal _btc;
 public decimal _usdt;
 
 public decimal _holdingTotal; 
 public decimal _holdingBtc;
 public decimal _holdingUsdt;

 private decimal _percentDifference = .02m;      //Balance difference of 2%

       decimal _minPosition = .0001m;                     // min BTC needed to invest.
       public string valutaUSDT = "USD";
       public string valutaBTC = "BTC";


       //ORDERSIZE
 
 public decimal _usdDifferenceTotal;        //Het USD verschil van de BTC balance en USDT balance
 public decimal _buyingQuantity;        //Bedrag in USD om aan te kopen = de helft van usdDifferenceTotal
 
      
    Resolution res = Resolution.Minute;

 private decimal pipProfit;
       
       
       //INITIALIZE BLOCK
       public override void Initialize()
       {
           SetStartDate(2017, 1, 1); 
           SetEndDate(2019, 1, 1);
           
  SetAccountCurrency(valutaUSDT);
           SetCash("USD", 500);
           
           SetBrokerageModel(BrokerageName.Binance, AccountType.Cash);
        
        btcusdt = AddCrypto("BTCUSDT", Resolution.Minute).Symbol;
           
     _dataBySymbol = new Dictionary<Symbol, SymbolData>();
     
           foreach (var ticker in new [] {"BTCUSDT"}) 
           {                  
                             
            var crypto = AddCrypto(ticker, res);        
            _dataBySymbol.Add(crypto.Symbol, new SymbolData(this, crypto));  
           }
           
           
           SetWarmUp(3 * SymbolData.MinutesOfTrading, res);
       }
           
 private decimal NormalizeQuantity(Symbol symbol, decimal quantity)
 {
  var pip = Securities[symbol].SymbolProperties.MinimumPriceVariation;
  return Math.Round(quantity/pip) * pip;
 }

       // ON DATA BLOCK (waar bestanden/data binnenkomt in het programma)
       public override void OnData(Slice data)
       {
        if (IsWarmingUp) return;
       
        foreach (var kvp in _dataBySymbol)
        {
         var symbolData = kvp.Value;
         if (!symbolData.IsReady)
         {
          continue;
         }
         var symbol = kvp.Key;
         if (!data.Bars.ContainsKey(symbol))
         {
          continue;
         }
         
         
         if (!data.ContainsKey(symbol) | !data.ContainsKey("BTCUSDT"))
         {
          return;
         }
         
         
         //DETERMINE WHAT THE VALUE OF USD IS IN BTC
         var btcAmount = _buyingQuantity / data[btcusdt].Close;
         
         var price = data[symbol].Price;
         
         var invested = Portfolio[symbol].Invested;
         
            _btc = Portfolio.CashBook[valutaBTC].Amount;
            _usdt = Portfolio.CashBook[valutaUSDT].Amount;

   //BUYING PART!!
         if (!invested)
         {
          if (_holdingBtc < _holdingUsdt && _percentDifference >= 2)
          {
           //PLACING ORDER
          var _buyingQuantity = NormalizeQuantity(symbol, btcAmount / data[symbol].Close);
           
     if (_buyingQuantity == 0) continue;
     MarketOrder(symbol, _buyingQuantity);
     
          }
         }
         //SELLING PART!!
   if (invested)
               {
                if (_holdingBtc >_holdingUsdt && _percentDifference >= 2)
                {
                 
                 
                }
                 
               }
        }
       }
   }
   
   public class SymbolData
   {
    private Cash _cash;
    
    public decimal _btc;
 public decimal _usdt;
    
    
    
    
 public static int MinutesOfTrading = 1140;
 int MomentumPercentPeriod = 1440;
 

 
 public decimal Holding => _cash.Amount;
 
 public bool IsReady;
 

 public decimal _usdDifferenceTotal => (_usdt - _btc);
 public decimal _buyingQuantity => (_usdDifferenceTotal/2);
 
    public SymbolData(QCAlgorithm algorithm, Crypto crypto)
    {
     Resolution res = Resolution.Minute;
     _cash = algorithm.Portfolio.CashBook[crypto.BaseCurrencySymbol];

    }
   }
}

 

Thanks in advance!!

 

Greetings David