Hi Everyone,
I just want to know what's the best way to place an order for crypto. Its not SetHoldings but MarketOrder.
But how can I define the correct size I want for each order.
In my case i want to start with a value of circa 10USD for each order
But I'm not sure what value I need to enter of _weight to get an amount of circa 10USD for each Order.
For exmample: I got 0,12 BTC (1367,36USD) but I want an amount for eacht order circa of 10USD.
I thought I needed to give _weight the value of .009m (equals 0.9%) and 0.9% of 0.12BTC (1367,36USD) is a USD value of circa 10USD.
Is this correct?
//_weight has a value of .008m = same as 0.9%?
var qnty =(_btc, _weight) / price);
and the market order.
MarketOrder(symbol, qnty, price);
Is this the correct way to do it?
Thanks in advance,
David
Derek Melchin
Hi Vronghel,
The number we pass to the MarketOrder method represents the quantity of the base currency we'd like to purchase. We can calculate this number based on the value of the quote currency though. For example, if we want to purchase ETH using $x USD worth of BTC, the quantity of ETHBTC to purchase is
quantity = x / data[self.btcusd].Price / data[self.ethbtc].Price
See the attached backtest and plots for reference.
We recommend reviewing this crypto trading course.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Vronghel
Then it is goin to be like this?
//Calculating the quantity to buy. var qnty = 15 / data["USDBTC"].Price / data[symbol].Price; //15 USD //Placing order. MarketOrder(symbol, qnty);
Greetings David
Vronghel
Hi Derek,
Thanks for the answer.
I tried to add it to my code but there is an error
Runtime Error: The ticker USDBTC was not found in the SymbolCache. Use the Symbol object as key instead
I don't want to place the ticker USDBTC in my library cause I only want the data out of it and don't want to trade it.
Check code below. I just omitted a big part of the code because of the irrelevance.
using QuantConnect.Securities.Crypto; using QuantConnect.Data; using QuantConnect.Data.Market; using QuantConnect.Indicators; namespace QuantConnect.Algorithm.CSharp { public class VentralTransdimensionalChamber : QCAlgorithm { // Trading bot that trades with the highest and lowest exchange rate of the last 24 hours. // Buying determined bacause the factor and the current price and selling price determined by the Macd. private Dictionary<Symbol, SymbolData> _dataBySymbol; // VARIABELEN DECLAREREN private decimal _weight = .006m; ///Percentage van portfolio dat je wilt investeren // % of portfolio for every trade. private decimal usd_trade_amount = 15; private decimal _targetPercent = .01m; //1% winst dan verkopen // Sell when 1% profit private decimal _stopPercent = .10m; //verkopen bij de 10% verlies / Sell when loss is 10% //decimal _maxPosition = .006m; // Max USD invested decimal _minPosition = .07m; // min USD needed to invest. public decimal _btc; public string valuta = "BTC"; public decimal _holding; public string usdbtc = "USDBTC"; Resolution res = Resolution.Minute; //MACD variabelen public int RollingWindow = 5; RollingWindow<IndicatorDataPoint> _macdWin; MovingAverageConvergenceDivergence _macd; public int fast = 12; //12 public int slow = 26; //26 public int sig = 9; //9 decimal macd_change; //INITIALIZE BLOCK public override void Initialize() { SetStartDate(2017, 1, 1); SetEndDate(2018, 1, 1); SetCash("BTC", 1.0m); SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash); _dataBySymbol = new Dictionary<Symbol, SymbolData>(); foreach (var ticker in new [] {"ETHBTC", "XRPBTC","NEOBTC", "OMGBTC","XMRBTC"}) //"XRPBTC","ETHBTC","DSHBTC", "VETBTC", "NEOBTC", "OMGBTC" { //coins wel maar niet in lijst Gerry //XMRBTC var crypto = AddCrypto(ticker, res); _dataBySymbol.Add(crypto.Symbol, new SymbolData(this, crypto)); } SetWarmUp(3 * SymbolData.MinutesOfTrading, res); //SetWarmUp(SymbolData.MinutesOfTrading, Resolution.Minute); } // 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; } var price = data[symbol].Price; var invested = Portfolio[symbol].Invested; _btc = Portfolio.CashBook[valuta].Amount; if (!invested && symbolData.Factor >= 3 && symbolData.LowestAverage >= price && _btc > _minPosition) { if (symbolData.MacdChange >= 0) { //Calculating the quantity to buy. var qnty = usd_trade_amount / data[usdbtc].Price / data[symbol].Price; //usd_trade_amount has value 15 Log($"Bestelhoeveelheid {qnty}"); //Placing order. MarketOrder(symbol, qnty); symbolData.TargetPrice = price * (1 + _targetPercent); //Bepalen welke verkoopprijs bij winst (huidige prijs + 1%) // determine wich selling price at profit symbolData.StopPrice = price * (1 - _stopPercent); //bepalen welke verkoopprijs bij verlies (huidige -10 %) // determine wich selling price at loss. } } if (invested) { if (price >= symbolData.TargetPrice) { MarketOrder(symbol, symbolData.Holding); } if (price < symbolData.StopPrice) { MarketOrder(symbol, symbolData.Holding); } } } }
Thanks in advance,
David
Derek Melchin
Hi Vronghel,
Our data library doesn't have data for USDBTC, we should use BTCUSD instead.
The quantity formula depends on the currency we're trading. For example:
// Buy $10 of BTC MarketOrder(btcusd, usdAmount / data[btcusd].Close); // Buy $10 of ETH MarketOrder(ethusd, usdAmount / data[ethusd].Close);
See the attached backtest for reference.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Vronghel
Hi Derek,
I didn't told it right I think. I'm trading altcoins with bitcoin and I'm not trading with USD or EUR.
I wanna place orders in BTC but with a USD value of circa 10 usd.
10 usd = 0.00076 btc
But for example my portfolio is 0.12 btc and I want every order in btc but with a value of circa 10 usd.
But I want to calculate the amount of bitcoin from my portfolio. So 0.12 btc * 0.0065 (weight = percentage of port I want to use for an order) = 0.00078btc and 0.00078 btc = 10.23 usd.
But how do I need to implement this in my code?
Cause when I implement it in my code I get errors like this the whole time.
"Backtest Handled Error: Order Error: id: 1186, Insufficient buying power to complete order (Value:10007413447.3689), Reason: Your portfolio holds 1 BTC, 0 BTC of which are reserved for open orders, but your Buy order is for 1008180202.36 ETH. Your order requires a total value of 11140391.236078 BTC, but only a total value of 0.996047 BTC is available."
Thanks in advance!
David
Derek Melchin
Hi Vronghel,
Thanks for clarifying. Since the price of BTCUSD changes over time, we shouldn't use a fixed weight of the portfolio to determine order size. Instead, if we want each order to be a value of $10USD, we can create a class member
private decimal usdAmount = 10.0m;
Because we are purchasing with bitcoins, we need to determine what the value of $10USD is in BTC. We can do this with
decimal btcAmount = usdAmount / data[btcusd].Close;
Now since we need to pass the value of the base cryptocurrency to the MarketOrder method, we need to convert this BTC value to the base cryptocurrency. For example:
// Buy $10USD worth of ETH with BTC MarketOrder(ethbtc, btcAmount / data[ethbtc].Close); // Buy $10USD worth of XRP with BTC MarketOrder(xrpbtc, btcAmount / data[xrpbtc].Close);
See the attached backtest plots and logs for reference.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Vronghel
Hi Derek,
I tried Implementing the conversion in my code. But it creates a lot of errors after a while.
Backtest Handled Error: Order Error: id: 60582, Insufficient buying power to complete order (Value:0.4221786), Reason: Your portfolio holds 0.0703376365 BTC, 0 BTC of which are reserved for open orders, but your Buy order is for 39.04 ETH. Your order requires a total value of 0.42217856 BTC, but only a total value of 0.07007472 BTC is available.
I seems like i'm only buying and not selling any order I think.
Is the buying order or the selling order not correct?
Thanks in advance,
David
Vronghel
Hi Derek,
An other question: you said fixed weight of the portfolio shouldn't be used Since the price of BTCUSD changes over time.
It's simply technically not possible or this isn't the "usual" way to determin the size of an order?
Thanks in advance,
David
Derek Melchin
Hi Vronghel,
These error message is thrown because the algorithm is only purchasing, never selling. To resolve this, we can replace
MarketOrder(symbol, symbolData.Holding);
with
Liquidate(symbol);
See the attached backtest for reference.
Position sizing based on the size of the portfolio is indeed a usual way to size orders. However, if the goal is to position size based on $10USD, we shouldn't use a fixed weight of the portfolio. Either way is possible to implement.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Vronghel
Hi Derek,
Thanks for the respons. But I don't want to liquidate all my open order at the same time. I only want to sell the order that gets triggered by the StopPrice or the TargetPrice
I dont know if this is possible?
Thanks in advance,
David
Derek Melchin
Hi Vronghel,
To clarify, the algorithm above only enters positions in a symbol when it's not currently invested in that symbol. Once invested, it waits for the price of that symbol to pass the target or stop price. At that point, it liquidates the security. Is this the intended behavior?
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Vronghel
Hi Derek,
Thanks for your answer! You made made it al clear
Vronghel
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!