changeset 113:16f8514cc5e6

Implement insider transactions
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Mon, 01 Feb 2021 20:19:32 +0100
parents cd6177f15ded
children 4c172ff9af20
files LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Jobs/NormalizeData.swift lazybear/Jobs/ReadJson.swift lazybear/Models/ApiModel.swift lazybear/Models/CompanyModel.swift lazybear/Models/HistoricalPricesModel.swift lazybear/Models/InsiderTransactionModel.swift lazybear/Models/QuoteModel.swift lazybear/Views/Company.swift lazybear/Views/InsiderTransactions.swift lazybear/Views/Stock.swift lazybear/Views/TransactionRow.swift
diffstat 13 files changed, 144 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj	Mon Feb 01 19:15:26 2021 +0100
+++ b/LazyBear.xcodeproj/project.pbxproj	Mon Feb 01 20:19:32 2021 +0100
@@ -13,6 +13,9 @@
 		9520F0B225C712D000692610 /* LineChartShape.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9520F0B125C712D000692610 /* LineChartShape.swift */; };
 		9520F0B525C7131300692610 /* LineChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9520F0B425C7131300692610 /* LineChart.swift */; };
 		95363CDC25C8741900B74131 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95363CDB25C8741900B74131 /* Request.swift */; };
+		95363CE225C87CF000B74131 /* InsiderTransactionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95363CE125C87CF000B74131 /* InsiderTransactionModel.swift */; };
+		95363CE625C87FEC00B74131 /* InsiderTransactions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95363CE525C87FEC00B74131 /* InsiderTransactions.swift */; };
+		95363CEA25C8858800B74131 /* TransactionRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95363CE925C8858800B74131 /* TransactionRow.swift */; };
 		954D992525A2123B001F7F60 /* HistoricalPricesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */; };
 		954DDF0425C456E800848A4B /* QuoteModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954DDF0325C456E800848A4B /* QuoteModel.swift */; };
 		95612C512598D48200F7698F /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95612C4F2598D48200F7698F /* SearchBar.swift */; };
@@ -55,6 +58,9 @@
 		9520F0B125C712D000692610 /* LineChartShape.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LineChartShape.swift; path = lazybear/Views/LineChartShape.swift; sourceTree = SOURCE_ROOT; };
 		9520F0B425C7131300692610 /* LineChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LineChart.swift; path = lazybear/Views/LineChart.swift; sourceTree = SOURCE_ROOT; };
 		95363CDB25C8741900B74131 /* Request.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Request.swift; path = lazybear/Network/Request.swift; sourceTree = SOURCE_ROOT; };
+		95363CE125C87CF000B74131 /* InsiderTransactionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsiderTransactionModel.swift; path = lazybear/Models/InsiderTransactionModel.swift; sourceTree = SOURCE_ROOT; };
+		95363CE525C87FEC00B74131 /* InsiderTransactions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsiderTransactions.swift; path = lazybear/Views/InsiderTransactions.swift; sourceTree = SOURCE_ROOT; };
+		95363CE925C8858800B74131 /* TransactionRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TransactionRow.swift; path = lazybear/Views/TransactionRow.swift; sourceTree = SOURCE_ROOT; };
 		954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HistoricalPricesModel.swift; path = lazybear/Models/HistoricalPricesModel.swift; sourceTree = SOURCE_ROOT; };
 		954DDF0325C456E800848A4B /* QuoteModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = QuoteModel.swift; path = lazybear/Models/QuoteModel.swift; sourceTree = SOURCE_ROOT; };
 		95612C4F2598D48200F7698F /* SearchBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = "<group>"; };
@@ -128,6 +134,7 @@
 				954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */,
 				95FE646A25C30B880052832E /* ApiModel.swift */,
 				954DDF0325C456E800848A4B /* QuoteModel.swift */,
+				95363CE125C87CF000B74131 /* InsiderTransactionModel.swift */,
 			);
 			path = Models;
 			sourceTree = "<group>";
@@ -153,6 +160,8 @@
 				95F6C30825BAF7C2003CF389 /* DateSelection.swift */,
 				9520F0B125C712D000692610 /* LineChartShape.swift */,
 				9520F0B425C7131300692610 /* LineChart.swift */,
+				95363CE525C87FEC00B74131 /* InsiderTransactions.swift */,
+				95363CE925C8858800B74131 /* TransactionRow.swift */,
 			);
 			path = Views;
 			sourceTree = "<group>";
@@ -306,7 +315,9 @@
 				9520F0B525C7131300692610 /* LineChart.swift in Sources */,
 				9597CE0425C1DFE7004DDFED /* LogoPlaceholder.swift in Sources */,
 				95FE646B25C30B880052832E /* ApiModel.swift in Sources */,
+				95363CE625C87FEC00B74131 /* InsiderTransactions.swift in Sources */,
 				95F6C30525BAF599003CF389 /* CompanyHeader.swift in Sources */,
+				95363CEA25C8858800B74131 /* TransactionRow.swift in Sources */,
 				95612C512598D48200F7698F /* SearchBar.swift in Sources */,
 				95E411BE25BEEA6C00A9C23F /* WatchlistRow.swift in Sources */,
 				95AD892425C5D8A200BCE8E4 /* AddWatchlist.swift in Sources */,
@@ -330,6 +341,7 @@
 				95E411A725BEE03000A9C23F /* Watchlist.swift in Sources */,
 				95825AFC25C7255600465409 /* NormalizeData.swift in Sources */,
 				95363CDC25C8741900B74131 /* Request.swift in Sources */,
+				95363CE225C87CF000B74131 /* InsiderTransactionModel.swift in Sources */,
 				95F7CAF625ADC7B7009E0E7C /* LazyBear.xcdatamodeld in Sources */,
 				95FE646825C2DC580052832E /* WatchlistCompany+CoreDataProperties.swift in Sources */,
 			);
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- a/lazybear/Jobs/NormalizeData.swift	Mon Feb 01 19:15:26 2021 +0100
+++ b/lazybear/Jobs/NormalizeData.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -5,7 +5,7 @@
 //  Created by Dennis Concepción Martín on 31/1/21.
 //
 
-import SwiftUI
+import Foundation
 
 func normalize(_ data: [Double]) -> [DataPoint] {
     var normalData = [DataPoint]()
--- a/lazybear/Jobs/ReadJson.swift	Mon Feb 01 19:15:26 2021 +0100
+++ b/lazybear/Jobs/ReadJson.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -5,7 +5,7 @@
 //  Created by Dennis Concepción Martín on 31/12/20.
 //
 
-import SwiftUI
+import Foundation
 
 // With this function I parse the local JSON file to read it and create a list with its items.
 let companiesData: [CompanyModel] = load("companies.json")
--- a/lazybear/Models/ApiModel.swift	Mon Feb 01 19:15:26 2021 +0100
+++ b/lazybear/Models/ApiModel.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -5,7 +5,7 @@
 //  Created by Dennis Concepción Martín on 28/1/21.
 //
 
-import SwiftUI
+import Foundation
 
 struct ApiModel {
     var key: String?
--- a/lazybear/Models/CompanyModel.swift	Mon Feb 01 19:15:26 2021 +0100
+++ b/lazybear/Models/CompanyModel.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -5,7 +5,7 @@
 //  Created by Dennis Concepción Martín on 31/12/20.
 //
 
-import SwiftUI
+import Foundation
 
 struct CompanyModel: Hashable, Codable {
     var symbol: String
--- a/lazybear/Models/HistoricalPricesModel.swift	Mon Feb 01 19:15:26 2021 +0100
+++ b/lazybear/Models/HistoricalPricesModel.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -5,7 +5,7 @@
 //  Created by Dennis Concepción Martín on 3/1/21.
 //
 
-import SwiftUI
+import Foundation
 
 struct HistoricalPricesModel: Codable {
     var date: String
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lazybear/Models/InsiderTransactionModel.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -0,0 +1,22 @@
+//
+//  InsiderTransactions.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 1/2/21.
+//
+
+import Foundation
+
+struct InsiderTransactionModel: Codable, Hashable {
+    var conversionOrExercisePrice: Float?
+    var directIndirect: String?
+    var filingDate: String?
+    var fullName: String?
+    var postShares: Int?
+    var reportedTitle: String?
+    var transactionCode: String?
+    var transactionDate: String?
+    var transactionPrice: Float?
+    var transactionShares: Int?
+    var transactionValue: Double?
+}
--- a/lazybear/Models/QuoteModel.swift	Mon Feb 01 19:15:26 2021 +0100
+++ b/lazybear/Models/QuoteModel.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -5,7 +5,7 @@
 //  Created by Dennis Concepción Martín on 29/1/21.
 //
 
-import SwiftUI
+import Foundation
 
 struct QuoteModel: Codable {
     var latestPrice: Float
--- a/lazybear/Views/Company.swift	Mon Feb 01 19:15:26 2021 +0100
+++ b/lazybear/Views/Company.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -19,8 +19,11 @@
             ScrollView {
                 VStack(alignment: .leading) {
                     Stock(name: name, symbol: symbol, lineChartHeight: geo.size.height*0.2)
-                        .environment(\.managedObjectContext, persistenceController.container.viewContext)
+                        .padding(.bottom)
+                    
+                    InsiderTransactions(symbol: symbol)
                 }
+                .environment(\.managedObjectContext, persistenceController.container.viewContext)
             }
         }
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lazybear/Views/InsiderTransactions.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -0,0 +1,50 @@
+//
+//  Insiders.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 1/2/21.
+//
+
+import SwiftUI
+
+struct InsiderTransactions: View {
+    var symbol: String
+    @EnvironmentObject var apiAccess: ApiAccess
+    
+    // <--------- API Job --------->
+    @State private var url = String() {
+        didSet { request(url: url, model: [InsiderTransactionModel].self) { self.data = $0 } }}
+    
+    @State private var data = [InsiderTransactionModel]()
+    // <--------- API Job --------->
+    
+    var body: some View {
+        VStack(alignment: .leading) {
+            Text("Insider transactions")
+                .font(.title)
+                .fontWeight(.semibold)
+                .padding([.leading, .bottom])
+            
+            ForEach(data, id: \.self) { data in
+                TransactionRow(data: data)
+            }
+        }
+        .onAppear { getUrl() }
+    }
+    
+    private func getUrl() {
+        // 1 -> Sandbox / 2 -> Production
+        let baseUrl = apiAccess.results[1].url ?? ""
+        let token = apiAccess.results[1].key ?? ""
+        let path = "/stable/stock/\(symbol)/insider-transactions?token="
+        
+        self.url = baseUrl + path + token
+        print(url)
+    }
+}
+
+struct InsiderTransactions_Previews: PreviewProvider {
+    static var previews: some View {
+        InsiderTransactions(symbol: "aapl")
+    }
+}
--- a/lazybear/Views/Stock.swift	Mon Feb 01 19:15:26 2021 +0100
+++ b/lazybear/Views/Stock.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -72,7 +72,6 @@
         let path = "/stable/stock/\(symbol)/chart/\(range)?chartCloseOnly=true&includeToday=false&token="
         
         self.url = baseUrl + path + token
-        print(self.url)
    }
     
     private func colorLineChart(prices: [Double]) -> Bool {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lazybear/Views/TransactionRow.swift	Mon Feb 01 20:19:32 2021 +0100
@@ -0,0 +1,50 @@
+//
+//  TransactionRow.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 1/2/21.
+//
+
+import SwiftUI
+
+struct TransactionRow: View {
+    @State var data: InsiderTransactionModel
+    
+    var body: some View {
+        VStack {
+            HStack(alignment: .top) {
+                VStack(alignment: .leading) {
+                    Text(data.fullName ?? "-".capitalized)
+                        .fontWeight(.semibold)
+                    
+                    Text(data.transactionDate ?? "-")
+                        .font(.subheadline)
+                }
+                Spacer()
+                Text("$\(data.transactionValue ?? 0, specifier: "%.2f")")
+                    .fontWeight(.semibold)
+            }
+            .padding([.leading, .trailing])
+            
+            Divider()
+        }
+    }
+}
+
+struct TransactionRow_Previews: PreviewProvider {
+    static var previews: some View {
+        TransactionRow(data: InsiderTransactionModel(
+                                conversionOrExercisePrice: 100,
+                                directIndirect: "D",
+                                filingDate: "2020-10-09",
+                                fullName: "Tim",
+                                postShares: 100,
+                                reportedTitle: "Some reported title",
+                                transactionCode: "B",
+                                transactionDate: "2020-10-09",
+                                transactionPrice: 50,
+                                transactionShares: 1000,
+                                transactionValue: 50000)
+        )
+    }
+}