0

I'm looking to call the ATR function from this scala wrapper for ta-lib. But I can't figure out how to use wrapper correctly.

package io.github.patceev.talib

import com.tictactec.ta.lib.{Core, MInteger, RetCode}

import scala.concurrent.Future

object Volatility {
    def ATR(
        highs: Vector[Double],
        lows: Vector[Double],
        closes: Vector[Double],
        period: Int = 14
    )(implicit core: Core): Future[Vector[Double]] = {
        val arrSize = highs.length - period + 1

        if (arrSize < 0) {
            Future.successful(Vector.empty[Double])
        } else {
            val begin = new MInteger()
            val length = new MInteger()
            val result = Array.ofDim[Double](arrSize)

            core.atr(
                0, highs.length - 1, highs.toArray, lows.toArray, closes.toArray,
                period, begin, length, result
            ) match {
                case RetCode.Success =>
                    Future.successful(result.toVector)
                case error =>
                    Future.failed(new Exception(error.toString))
            }
        }
    }
}

Would someone be able to explain how to use function and print out the result to the console.

Many thanks in advance.

3
  • 1
    What's the error that you are seeing? Does it complain about a missing implicit? Commented Mar 1, 2020 at 10:34
  • @stefanobaghino Yes, exactly Commented Mar 1, 2020 at 10:54
  • @stefanobaghino To be precise. code Error:(40, 6) could not find implicit value for parameter core: com.tictactec.ta.lib.Core ATR(highs,lows,closes,14) code Error:(40, 6) not enough arguments for method ATR: (implicit core: com.tictactec.ta.lib.Core)scala.concurrent.Future[Vector[Double]]. Unspecified value parameter core. ATR(highs,lows,closes,14) Commented Mar 1, 2020 at 11:30

1 Answer 1

1

Regarding syntax, Scala is one of many languages where you call functions and methods passing arguments in parentheses (mostly, but let's keep it simple for now):

def myFunction(a: Int): Int = a + 1

myFunction(1) // myFunction is called and returns 2

On top of this, Scala allows to specify multiple parameters lists, as in the following example:

def myCurriedFunction(a: Int)(b: Int): Int = a + b

myCurriedFunction(2)(3) // myCurriedFunction returns 5

You can also partially apply myCurriedFunction, but again, let's keep it simple for the time being. The main idea is that you can have multiple lists of arguments passed to a function.

Built on top of this, Scala allows to define a list of implicit parameters, which the compiler will automatically retrieve for you based on some scoping rules. Implicit parameters are used, for example, by Futures:

// this defines how and where callbacks are run
// the compiler will automatically "inject" them for you where needed
implicit val ec: ExecutionContext = concurrent.ExecutionContext.global

Future(4).map(_ + 1) // this will eventually result in a Future(5)

Note that both Future and map have a second parameter list that allows to specify an implicit execution context. By having one in scope, the compiler will "inject" it for you at the call site, without having to write it explicitly. You could have still done it and the result would have been

Future(4)(ec).map(_ + 1)(ec)

That said, I don't know the specifics of the library you are using, but the idea is that you have to instantiate a value of type Core and either bind it to an implicit val or pass it explicitly.

The resulting code will be something like the following

val highs: Vector[Double] = ???
val lows: Vector[Double] = ???
val closes: Vector[Double] = ???
implicit val core: Core = ??? // instantiate core
val resultsFuture = Volatility.ATR(highs, lows, closes) // core is passed implicitly
for (results <- resultsFuture; result <- results) {
  println(result)
}

Note that depending on your situation you may have to also use an implicit ExecutionContext to run this code (because you are extracting the Vector[Double] from a Future). Choosing the right execution context is another kind of issue but to play around you may want to use the global execution context.


Extra

Regarding some of the points I've left open, here are some pointers that hopefully will turn out to be useful:

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I still have trouble calling the functions, I have tried your solution but I can't even get the code to compile. Here is the library in question: github.com/patceev/ta-lib-scala. Maybe I should have posted the full wrapper code from the beginning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.