[SOLVED] Division gives random numbers in Pine Script


This Content is from Stack Overflow. Question asked by Czár Iván

Please find the code snippet below. I try to declare few variables, assign value and then divide certain numbers. But the result is completely random.

I put the values of the variables into a table for debugging but I cannot understand what the problem could be. I mean all the values assigned to the variables are correct but the result of the division is rather random. (see attached screenshots)

For testing I used 1000 USD for staring capital, so the result should be 500/price, in case of BTC around 0,025 (@20K), in case of ADA around 10 000-11 000 (@0.45-0.50). Screenshot1 Screenshot2

varip float startingCapital = input.float(defval=1000.0, title="Starting capital", minval=15)
varip float StartingBaseSize = na
varip float StartingCurrencySize = na
varip float BasePrice = na
varip float CurrencyPrice = na

BasePrice := request.security(syminfo.basecurrency + "USDT", "D", close)
CurrencyPrice := request.security(syminfo.currency + "USDT", "D", close)

if syminfo.currency == "USDT" or syminfo.currency == "USD"
    CurrencyPrice := 1.0

if na(StartingBaseSize) == true
    StartingBaseSize := (startingCapital/BasePrice)/2
if na(StartingCurrencySize) == true
    StartingCurrencySize := (startingCapital/CurrencyPrice)/2


If I understand correctly the request.security comes back with an array and in that array at the last index is the most current close value.

temp = request.security_lower_tf("BTCUSD", "60", close)
BasePrice := array.get(temp, array.size(temp)-1)

This Question was asked in StackOverflow by Czár Iván and Answered by Czár Iván It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?