LEAN is the open source
algorithmic trading engine powering QuantConnect. Founded in 2013 LEAN has been built by a
global community of 80+ engineers and powers more than a dozen hedge funds today.
Alpha League Competition: $1,000 Weekly Prize Pool
Qualifying Alpha Streams Reentered Weekly Learn
more
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.
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.
Derek Melchin
STAFF Pro
,
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.
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
2k Pro
,
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
0
Vronghel
2k Pro
,
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 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";
_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 (price < symbolData.StopPrice)
{
MarketOrder(symbol, symbolData.Holding);
}
}
}
}
Thanks in advance,
David
0
Derek Melchin
STAFF Pro
,
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
1
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
2k Pro
,
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
0
Derek Melchin
STAFF Pro
,
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
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
1
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
2k Pro
,
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
0
Vronghel
2k Pro
,
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
0
Derek Melchin
STAFF Pro
,
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
1
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
2k Pro
,
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
0
Derek Melchin
STAFF Pro
,
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
1
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.
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.
Loading...
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!