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