{
  "$schema": "./.schemas/page.schema.json",
  "title": "Stock",
  "icon": "chart.line.uptrend.xyaxis",
  "sidebar": false,
  "params": {
    "symbol": {
      "pattern": "[A-Z0-9][A-Z0-9.\\-]{0,11}",
      "example": "AAPL"
    }
  },
  "title_from": {
    "ref": "stock",
    "value": {
      "if": {
        "path": "profile.name",
        "exists": true
      },
      "then": "profile.name",
      "else": {
        "template": "{param.symbol}"
      }
    }
  },
  "sources": {
    "stock": {
      "url": "https://finnhub.io",
      "secret": "finnhub",
      "auth": {
        "type": "header",
        "name": "X-Finnhub-Token"
      },
      "every": "5m",
      "requests": {
        "quote": "/api/v1/quote?symbol={param.symbol}",
        "profile": "/api/v1/stock/profile2?symbol={param.symbol}",
        "earnings": "/api/v1/calendar/earnings?symbol={param.symbol}&from={date.today}&to={date.days_ahead_120}",
        "year": "/api/v1/stock/metric?symbol={param.symbol}&metric=all"
      },
      "derive": [
        {
          "name": "next_earnings",
          "value": {
            "first": "date",
            "of": "earnings.earningsCalendar",
            "sort": "date"
          }
        },
        {
          "name": "next_hour",
          "value": {
            "first": "hour",
            "of": "earnings.earningsCalendar",
            "sort": "date"
          }
        },
        {
          "name": "c_times_3",
          "value": {
            "multiply": [
              "quote.c",
              3
            ]
          }
        },
        {
          "name": "year_low",
          "value": {
            "if": {
              "path": "year.metric.52WeekLow",
              "lt": {
                "answer": "quote.l"
              }
            },
            "then": "year.metric.52WeekLow",
            "else": "quote.l"
          }
        },
        {
          "name": "year_high",
          "value": {
            "if": {
              "path": "year.metric.52WeekHigh",
              "gt": {
                "answer": "quote.h"
              }
            },
            "then": "year.metric.52WeekHigh",
            "else": "quote.h"
          }
        },
        {
          "name": "cap_t",
          "value": {
            "divide": [
              "profile.marketCapitalization",
              1000000
            ],
            "round": 2
          }
        },
        {
          "name": "cap_b",
          "value": {
            "divide": [
              "profile.marketCapitalization",
              1000
            ],
            "round": 1
          }
        },
        {
          "name": "cap_m",
          "value": {
            "path": "profile.marketCapitalization",
            "round": 0
          }
        }
      ]
    },
    "samples": {
      "dataset": "finnhub-prices",
      "key": "{param.symbol}",
      "derive": [
        {
          "name": "first_at",
          "value": {
            "first": "at",
            "of": "kept.rows"
          }
        },
        {
          "name": "last_at",
          "value": {
            "first": "at",
            "of": "kept.rows",
            "sort": {
              "by": "at",
              "order": "desc"
            }
          }
        },
        {
          "name": "last_age",
          "value": {
            "since": "last_at"
          }
        },
        {
          "name": "currency",
          "value": {
            "first": "row.currency",
            "of": "kept.rows",
            "sort": {
              "by": "at",
              "order": "desc"
            }
          }
        },
        {
          "name": "last_d",
          "value": {
            "first": "row.d",
            "of": "kept.rows",
            "sort": {
              "by": "at",
              "order": "desc"
            }
          }
        },
        {
          "name": "last_dp",
          "value": {
            "first": "row.dp",
            "of": "kept.rows",
            "sort": {
              "by": "at",
              "order": "desc"
            }
          }
        },
        {
          "name": "d_abs",
          "value": {
            "multiply": [
              "last_d",
              -1
            ]
          }
        },
        {
          "name": "dp_abs",
          "value": {
            "multiply": [
              "last_dp",
              -1
            ]
          }
        }
      ]
    },
    "news": {
      "url": "https://finnhub.io",
      "secret": "finnhub",
      "auth": {
        "type": "header",
        "name": "X-Finnhub-Token"
      },
      "every": "30m",
      "requests": {
        "news": "/api/v1/company-news?symbol={param.symbol}&from={date.days_ago_2}&to={date.today}"
      }
    }
  },
  "cards": [
    {
      "id": "day",
      "type": "sparkline",
      "label": "Price",
      "unit": "USD",
      "change": false,
      "points": [],
      "source": {
        "ref": "samples",
        "when": {
          "value": {
            "count": "kept.rows"
          },
          "gte": 2
        },
        "map": {
          "label": {
            "if": {
              "any": [
                {
                  "path": "currency",
                  "exists": false
                },
                {
                  "path": "currency",
                  "equals": "USD"
                }
              ]
            },
            "then": {
              "if": {
                "path": "last_d",
                "gt": 0
              },
              "then": {
                "template": "Price · +${last_d|round:2} · +{last_dp|round:2}% today"
              },
              "else": {
                "if": {
                  "path": "last_d",
                  "lt": 0
                },
                "then": {
                  "template": "Price · −${d_abs|round:2} · −{dp_abs|round:2}% today"
                },
                "else": {
                  "if": {
                    "path": "last_d",
                    "exists": true
                  },
                  "then": {
                    "template": "Price · no change today"
                  },
                  "else": {
                    "template": "Price"
                  }
                }
              }
            },
            "else": {
              "if": {
                "path": "last_d",
                "gt": 0
              },
              "then": {
                "template": "Price · +{last_d|round:2} {currency} · +{last_dp|round:2}% today"
              },
              "else": {
                "if": {
                  "path": "last_d",
                  "lt": 0
                },
                "then": {
                  "template": "Price · −{d_abs|round:2} {currency} · −{dp_abs|round:2}% today"
                },
                "else": {
                  "if": {
                    "path": "last_d",
                    "exists": true
                  },
                  "then": {
                    "template": "Price · no change today"
                  },
                  "else": {
                    "template": "Price"
                  }
                }
              }
            }
          },
          "unit": {
            "if": {
              "any": [
                {
                  "path": "currency",
                  "exists": false
                },
                {
                  "path": "currency",
                  "equals": "USD"
                }
              ]
            },
            "then": {
              "template": "USD"
            },
            "else": {
              "template": "{currency}"
            }
          },
          "points": {
            "each": "kept.rows",
            "buckets": 144,
            "keep": "last",
            "value": "row.price"
          },
          "times": {
            "each": "kept.rows",
            "buckets": 144,
            "keep": "last",
            "value": "at"
          },
          "start_label": {
            "template": "{first_at|ago}"
          },
          "end_label": {
            "if": {
              "path": "last_age",
              "gt": 900
            },
            "then": {
              "template": "{last_at|ago}"
            },
            "else": {
              "template": "Now"
            }
          }
        }
      }
    },
    {
      "id": "day-wait",
      "type": "text",
      "label": "Price",
      "body": "The price line comes from the prices Legato keeps every 5 minutes for the symbols on your watchlist.",
      "source": {
        "ref": "samples",
        "when": {
          "value": {
            "count": "kept.rows"
          },
          "lt": 2
        }
      }
    },
    {
      "id": "no-quote",
      "type": "text",
      "tone": "info",
      "label": "No quote",
      "body": "Finnhub has no quote for this symbol.",
      "source": {
        "ref": "stock",
        "when": {
          "path": "quote.t",
          "equals": 0
        }
      }
    },
    {
      "id": "range",
      "type": "ranges",
      "label": "Range",
      "unit": "USD",
      "items": [
        {
          "label": "Today",
          "low": 0,
          "high": 0
        },
        {
          "label": "52 weeks",
          "low": 0,
          "high": 0
        }
      ],
      "source": {
        "ref": "stock",
        "when": {
          "all": [
            {
              "path": "quote.t",
              "gt": 0
            },
            {
              "path": "year.metric.52WeekLow",
              "exists": true
            },
            {
              "path": "year.metric.52WeekHigh",
              "exists": true
            },
            {
              "path": "year.metric.52WeekLow",
              "lte": {
                "answer": "quote.c"
              }
            },
            {
              "path": "year.metric.52WeekHigh",
              "lte": {
                "answer": "c_times_3"
              }
            }
          ]
        },
        "map": {
          "unit": {
            "if": {
              "any": [
                {
                  "path": "profile.currency",
                  "exists": false
                },
                {
                  "path": "profile.currency",
                  "equals": "USD"
                }
              ]
            },
            "then": {
              "template": "USD"
            },
            "else": {
              "template": "{profile.currency}"
            }
          },
          "items[0].low": "quote.l",
          "items[0].high": "quote.h",
          "items[0].now": "quote.c",
          "items[1].low": "year_low",
          "items[1].high": "year_high",
          "items[1].now": "quote.c"
        }
      }
    },
    {
      "id": "range-today",
      "type": "ranges",
      "label": "Range",
      "unit": "USD",
      "items": [
        {
          "label": "Today",
          "low": 0,
          "high": 0
        }
      ],
      "source": {
        "ref": "stock",
        "when": {
          "all": [
            {
              "path": "quote.t",
              "gt": 0
            },
            {
              "any": [
                {
                  "path": "year.metric.52WeekLow",
                  "exists": false
                },
                {
                  "path": "year.metric.52WeekHigh",
                  "exists": false
                },
                {
                  "path": "year.metric.52WeekLow",
                  "gt": {
                    "answer": "quote.c"
                  }
                },
                {
                  "path": "year.metric.52WeekHigh",
                  "gt": {
                    "answer": "c_times_3"
                  }
                }
              ]
            }
          ]
        },
        "map": {
          "unit": {
            "if": {
              "any": [
                {
                  "path": "profile.currency",
                  "exists": false
                },
                {
                  "path": "profile.currency",
                  "equals": "USD"
                }
              ]
            },
            "then": {
              "template": "USD"
            },
            "else": {
              "template": "{profile.currency}"
            }
          },
          "items[0].low": "quote.l",
          "items[0].high": "quote.h",
          "items[0].now": "quote.c"
        }
      }
    },
    {
      "id": "facts",
      "type": "facts",
      "items": [
        {
          "key": "Quoted",
          "value": "—"
        },
        {
          "key": "Previous close",
          "value": "—"
        },
        {
          "key": "Open",
          "value": "—"
        },
        {
          "key": "Next earnings",
          "value": "—"
        },
        {
          "key": "Market cap",
          "value": "—"
        },
        {
          "key": "Industry",
          "value": "—"
        }
      ],
      "source": {
        "ref": "stock",
        "when": {
          "path": "quote.t",
          "gt": 0
        },
        "map": {
          "items[0].value": {
            "template": "{quote.t|ago}"
          },
          "items[1].value": {
            "if": {
              "any": [
                {
                  "path": "profile.currency",
                  "exists": false
                },
                {
                  "path": "profile.currency",
                  "equals": "USD"
                }
              ]
            },
            "then": {
              "template": "${quote.pc|round:2}"
            },
            "else": {
              "template": "{quote.pc|round:2} {profile.currency}"
            }
          },
          "items[2].value": {
            "if": {
              "any": [
                {
                  "path": "profile.currency",
                  "exists": false
                },
                {
                  "path": "profile.currency",
                  "equals": "USD"
                }
              ]
            },
            "then": {
              "template": "${quote.o|round:2}"
            },
            "else": {
              "template": "{quote.o|round:2} {profile.currency}"
            }
          },
          "items[3].value": {
            "if": {
              "path": "next_earnings",
              "exists": false
            },
            "then": {
              "template": "None in the next 4 months"
            },
            "else": {
              "if": {
                "path": "next_hour",
                "equals": "bmo"
              },
              "then": {
                "template": "{next_earnings|date}, before the open"
              },
              "else": {
                "if": {
                  "path": "next_hour",
                  "equals": "amc"
                },
                "then": {
                  "template": "{next_earnings|date}, after the close"
                },
                "else": {
                  "template": "{next_earnings|date}"
                }
              }
            }
          },
          "items[4].value": {
            "if": {
              "any": [
                {
                  "path": "profile.currency",
                  "exists": false
                },
                {
                  "path": "profile.currency",
                  "equals": "USD"
                }
              ]
            },
            "then": {
              "if": {
                "path": "profile.marketCapitalization",
                "exists": false
              },
              "then": {
                "template": "—"
              },
              "else": {
                "if": {
                  "path": "profile.marketCapitalization",
                  "gte": 1000000
                },
                "then": {
                  "template": "${cap_t}T"
                },
                "else": {
                  "if": {
                    "path": "profile.marketCapitalization",
                    "gte": 1000
                  },
                  "then": {
                    "template": "${cap_b}B"
                  },
                  "else": {
                    "template": "${cap_m}M"
                  }
                }
              }
            },
            "else": {
              "if": {
                "path": "profile.marketCapitalization",
                "exists": false
              },
              "then": {
                "template": "—"
              },
              "else": {
                "if": {
                  "path": "profile.marketCapitalization",
                  "gte": 1000000
                },
                "then": {
                  "template": "{cap_t}T {profile.currency}"
                },
                "else": {
                  "if": {
                    "path": "profile.marketCapitalization",
                    "gte": 1000
                  },
                  "then": {
                    "template": "{cap_b}B {profile.currency}"
                  },
                  "else": {
                    "template": "{cap_m}M {profile.currency}"
                  }
                }
              }
            }
          },
          "items[5].value": {
            "if": {
              "path": "profile.finnhubIndustry",
              "exists": true
            },
            "then": "profile.finnhubIndustry",
            "else": {
              "template": "—"
            }
          }
        }
      }
    },
    {
      "id": "news",
      "type": "list",
      "label": "News",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "news",
        "empty": {
          "when": {
            "value": {
              "count": "news"
            },
            "equals": 0
          },
          "text": "No news in the last two days"
        },
        "map": {
          "items": {
            "each": "news",
            "sort": {
              "by": "datetime",
              "order": "desc"
            },
            "limit": 4,
            "fields": {
              "text": {
                "template": "{headline|clip:120}"
              },
              "detail": {
                "template": "{source} · {datetime|ago}"
              },
              "url": "url"
            }
          }
        }
      }
    }
  ]
}
