Hi There,
I just wanted to input the data of the highest and lowest exchange rate from the last 24 hours into 2 variables. So i could use them for my trading bot. Is there a way to get the data into a variable?
Thanks in advantage!
Kind regards,
David
Derek Melchin
Hi Vronghel,
We can utilize the Minimum and Maximum indicators to accomplish this.
class MultidimensionalUncoupledCircuit(QCAlgorithm): def Initialize(self): self.SetStartDate(2020, 1, 16) self.SetEndDate(2020, 1, 20) self.SetCash(100000) symbol = self.AddForex("EURUSD", Resolution.Minute, Market.Oanda).Symbol mins_per_trading_day = 60 * 6.5 self.min = self.MIN(symbol, mins_per_trading_day, Resolution.Minute) self.max = self.MAX(symbol, mins_per_trading_day, Resolution.Minute)
See the attached backtest for reference. Also, consider reviewing our documentation on indicators.
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 a lot for your comment/Solution.
From were do you get the numbers 60 * 6.5.
This mean that we get the highest or lowest exhange range of the last 6.5 hours?
grts,
David
Vronghel
Here is some code were i get stuck.
I get the following error: "Runtime Error: Attempted to divide by zero".
What is wrong in the code so I can fix it?
namespace QuantConnect.Algorithm.CSharp
{
public class VentralTransdimensionalChamber : QCAlgorithm
{
// VARIABELEN DECLAREREN
private string _ticker = "ETHUSD"; //virtueel paar - tracks the current USD value of 1 ether.
private int _startingCash = 1000;
private decimal _weight = .05m; ///Percentage van portfolio dat je wilt investeren
private decimal _targetPercent = .01m; //10%
private decimal _stopPercent = .04m; //verkopen bij de 2% verlies
int _maxPosition = 1000; // Max USD invested
int _minPosition = 100; // min USD needed to invest.
public decimal _usd; //Amount of USD in our Cashbook.
Maximum _maxExchange;
Minimum _minExchange;
//PROGRAMMA VARIABELEN
public decimal _price;
public decimal _holding; //Het aantal Ether we in onze portfolio hebben zitten.
public string _baseSymbol; //vertegenwoordigt de basesymbool dat we houden ETH
public decimal _targetPrice;
public decimal _stopPrice;
public decimal _koers;
//INITIALIZE BLOCK
public override void Initialize()
{
SetStartDate(2017, 1, 1);
SetEndDate(2018, 1, 1);
SetCash(_startingCash);
var _crypto = AddCrypto(_ticker, Resolution.Minute);
_baseSymbol = _crypto.BaseCurrencySymbol;
SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);
}
// ON DATA BLOCK (waar bestanden/data binnenkomt in het programma)
public override void OnData(Slice data)
{
int _minutesOfTrading = 1440;
_maxExchange = MAX(_ticker, _minutesOfTrading, Resolution.Minute);
_minExchange = MIN(_ticker, _minutesOfTrading, Resolution.Minute);
decimal _factor = (((_maxExchange- _minExchange)/2)/_maxExchange)*100;
decimal _average = (_maxExchange / _minExchange);
_price = data[_ticker].Price;
if (!Portfolio.Invested && _factor >=3)
{
SetHoldings(_ticker, _weight); // om een order te maken ==>percentage van portfolio dat je wilt investeren
Debug("Purchased Stock"); // Als het order gemaakt is word dit bevestigd door de debug
//Log($"{_maxExchange}");
_targetPrice = _price + (_price * _targetPercent); //Bepalen welke verkoopprijs bij winst (huidige prijs + 10%)
_stopPrice = _price - (_price * _stopPercent); //bepalen welke verkoopprijs bij verlies (huidige - 2%)
}
if (Portfolio.Invested)
{
_holding = Portfolio.CashBook[_baseSymbol].Amount;
if(_price >= _targetPrice)
{
Sell(_ticker, _holding);
}
if(_price < _stopPrice)
{
Sell(_ticker, _holding);
}
}
}
}
}
Thanks in advance.
Greeting,
David
Derek Melchin
Hi David,
The number 60*6.5=390 was selected for the `period` argument of the indicators because the market is open for 6.5 hours in a typical trading day. Using this argument, the indicators contain the min/max over the last 24 hours. If instead we use 60*24=1440, the indicators will contain the min/max over the last 1440/390=3.7 trading days.
The division by zero error occurs in the algorithm above because of this line
decimal _average = (_maxExchange / _minExchange);
Since `_minExchange` is being constructed on each call to OnData, it is never warmed up. Indicators that are not warm, return a value of 0.
To resolve this, we should move
int _minutesOfTrading = 1440; _maxExchange = MAX(_ticker, _minutesOfTrading, Resolution.Minute); _minExchange = MIN(_ticker, _minutesOfTrading, Resolution.Minute);
to the Initialize method. We should also add
SetWarmUp(_minutesOfTrading, Resolution.Minute);
to Initialize and
if (!_minExchange.IsReady) return;
to the top of the OnData method.
See the attached backtest for reference. Also, consider reviewing our documentation on initializing indicators.
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 a lot. Saved me a lot of time!! :-)
Greetings,
David
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!