{
  "$schema": "./.schemas/page.schema.json",
  "title": "Stocks",
  "icon": "chart.line.uptrend.xyaxis",
  "sources": {
    "finnhub": {
      "url": "https://finnhub.io",
      "secret": "finnhub",
      "auth": {
        "type": "header",
        "name": "X-Finnhub-Token"
      },
      "every": "5m",
      "requests": {
        "market": "/api/v1/stock/market-status?exchange=US",
        "quotes": {
          "path": "/api/v1/quote?symbol={entry}",
          "each_entry": {
            "list": "stock-watchlist",
            "max": 20,
            "pattern": "[A-Z0-9][A-Z0-9.\\-]{0,11}"
          }
        },
        "profiles": {
          "path": "/api/v1/stock/profile2?symbol={entry}",
          "each_entry": {
            "list": "stock-watchlist",
            "max": 20,
            "pattern": "[A-Z0-9][A-Z0-9.\\-]{0,11}"
          }
        }
      },
      "derive": [
        {
          "name": "rows",
          "value": {
            "each": "quotes",
            "lookup": {
              "from": "profiles",
              "by": "entry",
              "on": "entry",
              "as": "profile"
            },
            "fields": {
              "entry": "entry",
              "error": "error",
              "name": "profile.answer.name",
              "currency": "profile.answer.currency",
              "c": "answer.c",
              "dp": "answer.dp",
              "t": "answer.t"
            }
          }
        },
        {
          "name": "moves",
          "value": {
            "each": "quotes",
            "where": {
              "path": "answer.t",
              "gt": 0
            },
            "fields": {
              "entry": "entry",
              "dp": "answer.dp",
              "size": {
                "if": {
                  "path": "answer.dp",
                  "lt": 0
                },
                "then": {
                  "multiply": [
                    "answer.dp",
                    -1
                  ]
                },
                "else": "answer.dp"
              },
              "low": {
                "multiply": [
                  {
                    "divide": [
                      {
                        "subtract": [
                          "answer.l",
                          "answer.pc"
                        ]
                      },
                      "answer.pc"
                    ]
                  },
                  100
                ],
                "round": 2
              },
              "high": {
                "multiply": [
                  {
                    "divide": [
                      {
                        "subtract": [
                          "answer.h",
                          "answer.pc"
                        ]
                      },
                      "answer.pc"
                    ]
                  },
                  100
                ],
                "round": 2
              }
            }
          }
        },
        {
          "name": "moved",
          "value": {
            "each": "moves",
            "sort": {
              "by": "size",
              "order": "desc"
            },
            "limit": 14,
            "fields": {
              "entry": "entry",
              "dp": "dp",
              "low": "low",
              "high": "high"
            }
          }
        },
        {
          "name": "priced",
          "value": {
            "each": "quotes",
            "where": {
              "path": "answer.t",
              "gt": 0
            },
            "lookup": {
              "from": "profiles",
              "by": "entry",
              "on": "entry",
              "as": "profile"
            },
            "fields": {
              "entry": "entry",
              "price": "answer.c",
              "at": "answer.t",
              "d": "answer.d",
              "dp": "answer.dp",
              "currency": {
                "if": {
                  "path": "profile.answer.currency",
                  "exists": true
                },
                "then": "profile.answer.currency",
                "else": {
                  "template": "USD"
                }
              }
            }
          }
        },
        {
          "name": "quoted",
          "value": {
            "count": "moves"
          }
        },
        {
          "name": "listed",
          "value": {
            "count": "rows"
          }
        }
      ],
      "collect": {
        "dataset": "finnhub-prices",
        "each": "priced",
        "key": "{entry}",
        "sample": {
          "every": "5m"
        },
        "fields": {
          "price": "price",
          "quoted_at": "at",
          "currency": "currency",
          "d": "d",
          "dp": "dp"
        },
        "readers": [
          "finnhub-symbol"
        ]
      }
    }
  },
  "cards": [
    {
      "id": "market",
      "type": "status",
      "width": "full",
      "label": "Market",
      "state": "unknown",
      "source": {
        "ref": "finnhub",
        "map": {
          "label": {
            "if": {
              "path": "market.session",
              "in": [
                "pre-market",
                "post-market"
              ]
            },
            "then": {
              "path": "market.session",
              "values": {
                "pre-market": "Pre-market",
                "post-market": "After hours"
              }
            },
            "else": {
              "if": {
                "path": "market.isOpen",
                "equals": true
              },
              "then": {
                "template": "Market open"
              },
              "else": {
                "if": {
                  "path": "market.holiday",
                  "exists": true
                },
                "then": {
                  "template": "Closed for {market.holiday}"
                },
                "else": {
                  "template": "Market closed"
                }
              }
            }
          },
          "state": {
            "if": {
              "path": "market.isOpen",
              "equals": true
            },
            "then": {
              "template": "ok"
            },
            "else": {
              "template": "off"
            }
          },
          "icon": {
            "if": {
              "path": "market.isOpen",
              "not": true
            },
            "then": {
              "template": "moon.fill"
            }
          }
        }
      }
    },
    {
      "id": "views",
      "type": "tabs",
      "tabs": [
        {
          "label": "Watchlist",
          "cards": [
            {
              "id": "watchlist",
              "type": "tiles",
              "max_items": 12,
              "items": [],
              "source": {
                "ref": "finnhub",
                "map": {
                  "items": {
                    "each": "rows",
                    "limit": 60,
                    "fields": {
                      "label": "entry",
                      "value": {
                        "if": {
                          "any": [
                            {
                              "path": "currency",
                              "exists": false
                            },
                            {
                              "path": "currency",
                              "equals": "USD"
                            }
                          ]
                        },
                        "then": {
                          "if": {
                            "path": "error",
                            "exists": true
                          },
                          "then": {
                            "if": {
                              "path": "error",
                              "contains": "sends the first"
                            },
                            "then": {
                              "template": "Past the first 20"
                            },
                            "else": {
                              "if": {
                                "path": "error",
                                "contains": "can't send"
                              },
                              "then": {
                                "template": "Check this symbol"
                              },
                              "else": {
                                "template": "Didn't load"
                              }
                            }
                          },
                          "else": {
                            "if": {
                              "all": [
                                {
                                  "path": "error",
                                  "exists": false
                                },
                                {
                                  "path": "t",
                                  "exists": true
                                },
                                {
                                  "path": "t",
                                  "gt": 0
                                }
                              ]
                            },
                            "then": {
                              "template": "${c|round:2}"
                            },
                            "else": {
                              "template": "No quote"
                            }
                          }
                        },
                        "else": {
                          "if": {
                            "path": "error",
                            "exists": true
                          },
                          "then": {
                            "if": {
                              "path": "error",
                              "contains": "sends the first"
                            },
                            "then": {
                              "template": "Past the first 20"
                            },
                            "else": {
                              "if": {
                                "path": "error",
                                "contains": "can't send"
                              },
                              "then": {
                                "template": "Check this symbol"
                              },
                              "else": {
                                "template": "Didn't load"
                              }
                            }
                          },
                          "else": {
                            "if": {
                              "all": [
                                {
                                  "path": "error",
                                  "exists": false
                                },
                                {
                                  "path": "t",
                                  "exists": true
                                },
                                {
                                  "path": "t",
                                  "gt": 0
                                }
                              ]
                            },
                            "then": {
                              "template": "{c|round:2} {currency}"
                            },
                            "else": {
                              "template": "No quote"
                            }
                          }
                        }
                      },
                      "change": {
                        "if": {
                          "all": [
                            {
                              "path": "error",
                              "exists": false
                            },
                            {
                              "path": "t",
                              "exists": true
                            },
                            {
                              "path": "t",
                              "gt": 0
                            }
                          ]
                        },
                        "then": "dp"
                      },
                      "detail": "name",
                      "url": {
                        "template": "legato://page/finnhub-symbol?symbol={entry|raw}"
                      }
                    }
                  }
                }
              }
            },
            {
              "id": "watchlist-cut",
              "type": "text",
              "tone": "info",
              "body": "The first 60 symbols on your list.",
              "source": {
                "ref": "finnhub",
                "when": {
                  "path": "listed",
                  "gt": 60
                },
                "map": {
                  "body": {
                    "template": "The first 60 of the {listed} symbols on your list."
                  }
                }
              }
            }
          ]
        },
        {
          "label": "Today's moves",
          "cards": [
            {
              "id": "moves",
              "type": "ranges",
              "label": "Against yesterday's close",
              "unit": "%",
              "items": [
                {
                  "label": "—",
                  "low": 0,
                  "high": 0
                }
              ],
              "source": {
                "ref": "finnhub",
                "when": {
                  "path": "quoted",
                  "gt": 0
                },
                "map": {
                  "items": {
                    "each": "moved",
                    "sort": {
                      "by": "dp",
                      "order": "desc"
                    },
                    "fields": {
                      "label": "entry",
                      "low": "low",
                      "high": "high",
                      "now": "dp"
                    }
                  },
                  "detail": {
                    "if": {
                      "path": "quoted",
                      "gt": 14
                    },
                    "then": {
                      "template": "14 furthest of {quoted}"
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    }
  ]
}
