Skip to content

Commit d9eda31

Browse files
committed
initial
1 parent ee888be commit d9eda31

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*.class
22
*.log
3+
target/
4+
.idea/

build.sbt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name := "qnt"
2+
3+
version := "0.1"
4+
5+
scalaVersion := "2.11.12"
6+
7+
resolvers += "Unidata" at "https://artifacts.unidata.ucar.edu/repository/unidata-all"
8+
libraryDependencies += "edu.ucar" % "cdm-core" % "5.2.0"
9+
libraryDependencies += "edu.ucar" % "netcdf" % "4.3.22"
10+
11+
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
12+
libraryDependencies += "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.10.1"
13+
14+
resolvers += "Sonatype Releases" at "http://oss.sonatype.org/content/repositories/releases"
15+
libraryDependencies += "org.scala-saddle" %% "saddle-core" % "1.3.+"

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 1.3.3

src/main/scala/qnt/data.scala

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package qnt
2+
3+
import java.net.{HttpURLConnection, URL}
4+
import java.time.LocalDate
5+
6+
import com.fasterxml.jackson.databind.ObjectMapper
7+
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper}
8+
import org.saddle.Frame
9+
import org.slf4j.LoggerFactory
10+
import ucar.ma2.ArrayDouble
11+
import ucar.nc2.NetcdfFile
12+
13+
import scala.collection.immutable.{Map => ImmutableMap}
14+
import scala.util.control.NonFatal
15+
16+
case class StockInfo (
17+
id: String,
18+
cik: Option[String],
19+
exchange: String,
20+
symbol: String,
21+
name: String,
22+
sector: Option[String],
23+
props: ImmutableMap[String, Any]
24+
) {
25+
26+
def this(props: Map[String, Any]) = this(
27+
props("id").asInstanceOf[String],
28+
//props("FIGI").asInstanceOf[String],
29+
props.get("cik") match {
30+
case Some(s) => {
31+
val str = s.asInstanceOf[String]
32+
if (str != null && str.length > 0) Some(str) else None
33+
}
34+
case _ => None
35+
},
36+
props("exchange").asInstanceOf[String],
37+
props("symbol").asInstanceOf[String],
38+
props("name").asInstanceOf[String],
39+
{
40+
val s = props("sector").asInstanceOf[String]
41+
if (s == null || s.length < 1) None else Some(s)
42+
},
43+
props - "id" - "cik" - "exchange" - "symbol" - "name" - "sector"
44+
)
45+
46+
}
47+
48+
object data {
49+
50+
def loadStockList(
51+
minDate: LocalDate = LocalDate.of(2007, 1, 1),
52+
maxDate: LocalDate = LocalDate.now()
53+
) : List[StockInfo] = {
54+
55+
val uri = s"assets?min_date=$minDate&max_date=$maxDate"
56+
val dataBytes = loadWithRetry(uri)
57+
if(dataBytes == null) {
58+
return List.empty
59+
}
60+
val lst = OBJECT_MAPPER.readValue[List[Map[String, Any]]](dataBytes)
61+
lst.map(p => new StockInfo(p))
62+
}
63+
64+
def loadStockDailySeries(
65+
ids: List[String],
66+
minDate: LocalDate = LocalDate.of(2007, 1, 1),
67+
maxDate: LocalDate = LocalDate.now()
68+
) : Frame[LocalDate, String, Float] = {
69+
var uri = "data"
70+
var params = Map(
71+
"assets" -> ids,
72+
"min_date" -> minDate.toString,
73+
"max_date" -> maxDate.toString
74+
)
75+
val ad: ArrayDouble = null;
76+
77+
var dataBytes = loadWithRetry(uri, params)
78+
val dataNetcdf = NetcdfFile.openInMemory("data", dataBytes)
79+
null
80+
}
81+
82+
private val LOG = LoggerFactory.getLogger(getClass)
83+
private val RETRIES = 5
84+
private val TIMEOUT = 60*1000
85+
private val OBJECT_MAPPER = new ObjectMapper() with ScalaObjectMapper
86+
87+
OBJECT_MAPPER.registerModule(DefaultScalaModule)
88+
89+
def loadIndexList() = ???
90+
91+
def loadIndexSeries() = ???
92+
93+
private def loadWithRetry(uri: String, dataObj: Any = null): Array[Byte] = {
94+
val urlStr = baseUrl + uri
95+
val url = new URL(urlStr)
96+
for (t <- 1 to RETRIES) {
97+
var exc: Exception = null
98+
var conn: HttpURLConnection = null
99+
try {
100+
conn = url.openConnection().asInstanceOf[HttpURLConnection]
101+
conn.setConnectTimeout(TIMEOUT)
102+
conn.setReadTimeout(TIMEOUT)
103+
conn.setDoOutput(dataObj != null)
104+
conn.setRequestMethod(if(dataObj == null) "GET" else "POST")
105+
conn.setUseCaches(false)
106+
conn.setDoInput(true);
107+
if(dataObj != null) {
108+
val dataBytes = OBJECT_MAPPER.writeValueAsBytes(dataObj)
109+
// conn.setRequestProperty("Content-Type", "application/json; utf-8")
110+
conn.setRequestProperty("Content-Length", Integer.toString(dataBytes.length))
111+
conn.getOutputStream.write(dataBytes)
112+
conn.getOutputStream.flush()
113+
}
114+
val code = conn.getResponseCode
115+
if(code == 404) {
116+
return null
117+
}
118+
if(code != 200) {
119+
throw new IllegalStateException(s"Wrong status code: $code")
120+
}
121+
val is = conn.getInputStream
122+
return is.readAllBytes()
123+
} catch {
124+
case NonFatal(e) => LOG.warn(s"Download exception, URL: $urlStr", e)
125+
} finally {
126+
if (conn != null) conn.disconnect()
127+
}
128+
}
129+
throw new IllegalStateException(s"Data was not loaded, URL: $urlStr")
130+
}
131+
132+
private def baseUrl = {
133+
System.getenv().getOrDefault("DATA_BASE_URL", "http://127.0.0.1:8000/")
134+
}
135+
136+
}
137+

src/main/scala/qnt/rn.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package qnt
2+
3+
4+
import java.time.LocalDate
5+
6+
import qnt.{data => qndata}
7+
8+
9+
object Obj extends App {
10+
val fruits = List("apple", "banana", "lime", "orange")
11+
12+
val fruitLengths = for {
13+
f <- fruits
14+
if f.length > 4
15+
} yield f.length
16+
17+
Console.println(fruitLengths)
18+
19+
val a = List(1,2,3,4)
20+
21+
22+
var s = a.reduce((a1,a2) => a1*a2)
23+
println(s)
24+
25+
26+
val lst = qndata.loadStockList(
27+
LocalDate.of(2007, 1, 1),
28+
LocalDate.of(2019, 7, 7)
29+
)
30+
31+
val ids = lst.take(20).map(i => i.id)
32+
33+
val azaza = qndata.loadStockDailySeries(ids)
34+
35+
lst.filter(e => e.sector.isEmpty).foreach(println)
36+
}

0 commit comments

Comments
 (0)