From 8de173121c78516d8c6683baf878bdd3892f3cd6 Mon Sep 17 00:00:00 2001 From: mnsrulz Date: Tue, 9 Apr 2024 04:36:56 +0000 Subject: [PATCH 01/13] added the call option price calculation --- src/components/mystocks.tsx | 5 +- src/components/stock-options-view.tsx | 123 ++++++++++++++++---------- 2 files changed, 77 insertions(+), 51 deletions(-) diff --git a/src/components/mystocks.tsx b/src/components/mystocks.tsx index 9ab91ce..61254e2 100644 --- a/src/components/mystocks.tsx +++ b/src/components/mystocks.tsx @@ -1,16 +1,13 @@ 'use client'; import * as React from 'react'; -import { RemoveItemFromMyList, SearchTickerItem, useMyStockList } from '../lib/socket'; +import { SearchTickerItem } from '../lib/socket'; import DeleteIcon from '@mui/icons-material/Delete'; import InfoIcon from '@mui/icons-material/Info'; import { DataGrid, GridActionsCellItem, GridColDef, useGridApiRef } from '@mui/x-data-grid'; -import { Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Button } from '@mui/material'; import { useState } from 'react'; -import { StockOptionsView } from './stock-options-view'; import { StockTickerView } from './stock-ticker'; import AddTradeIcon from '@mui/icons-material/Add'; import { AddTradeDialog } from './add-trade'; -import Link from 'next/link'; import { GridLinkAction } from './GridLinkAction'; diff --git a/src/components/stock-options-view.tsx b/src/components/stock-options-view.tsx index b3ab60e..e1fc724 100644 --- a/src/components/stock-options-view.tsx +++ b/src/components/stock-options-view.tsx @@ -13,7 +13,7 @@ interface ITickerProps { } type NumberRange = { start: number, end: number } -type IStrikePriceSliderPorps = { allStrikePricesValues: number[], onChange: (v: NumberRange) => void } +type IStrikePriceSliderPorps = { allStrikePricesValues: number[], onChange: (v: NumberRange) => void, currentPrice: number } const StrikePriceSlider = (props: IStrikePriceSliderPorps) => { const { allStrikePricesValues, onChange } = props; @@ -29,17 +29,35 @@ const StrikePriceSlider = (props: IStrikePriceSliderPorps) => { }); }; + //todoo + // function calculateValue(value: number) { + // return 2 ** value; + // } + +/* + + 144 + + 0 200 + 20 70 120 160 180 + + + +*/ + return - Strike Price Range: + Strike Price Range: {strikePriceRange[0]} - {strikePriceRange[1]} + 'Temperature range'} value={strikePriceRange} onChange={handleChange} - valueLabelDisplay="on" + //valueLabelDisplay="on" min={minStrikePrice} max={maxStrikePrice} marks={strikePriceMarks} step={null} + // scale={calculateValue} //getAriaValueText={valuetext} /> @@ -71,7 +89,6 @@ export const StockOptionsView = (props: ITickerProps) => { })).filter(n => n.value >= strikePriceRange.start && n.value <= strikePriceRange.end); workingStrikePrices.sort(function (a, b) { return a.value - b.value; }).forEach(s => { - columns.push({ field: s.strikePrice, width: 10, headerName: `${parseFloat(s.strikePrice)}`, @@ -101,11 +118,25 @@ export const StockOptionsView = (props: ITickerProps) => { o[s.strikePrice] = price && (() => { switch (valueMode) { case 'TOTAL_RETURN': - return (data.currentPrice > s.value ? price : (price - (s.value - data.currentPrice))) / s.value; + /* + 135 + 138 --> 1.10 + + */ + if (putCallTabValue == 'PUT') { + return (data.currentPrice > s.value ? price : (price - (s.value - data.currentPrice))) / s.value; + } else { + return (price / data.currentPrice); + } case 'ANNUAL_RETURN': - const sellCost = (data.currentPrice > s.value ? price : (price - (s.value - data.currentPrice))); - const risk = s.value; - return (sellCost / risk) * (365 / numberofdays); + if (putCallTabValue == 'PUT') { + const sellCost = (data.currentPrice > s.value ? price : (price - (s.value - data.currentPrice))); + const risk = s.value; + return (sellCost / risk) * (365 / numberofdays); + } else { + const sellCost = (data.currentPrice < s.value ? price : (price - (data.currentPrice - s.value))); + return (sellCost / data.currentPrice) * (365 / numberofdays); + } default: return price } @@ -120,7 +151,7 @@ export const StockOptionsView = (props: ITickerProps) => { Age */} - + Price Mode setPriceMode(e.target.value as PriceModeType)} > diff --git a/src/lib/socket.ts b/src/lib/socket.ts index f67a885..234f90f 100644 --- a/src/lib/socket.ts +++ b/src/lib/socket.ts @@ -116,12 +116,25 @@ type StockPriceData = { } } +export type NumberRange = { start: number, end: number } + export const useOptionTracker = (symbol: string) => { const [data, setOd] = useState(); const [isLoading, setIsLoading] = useState(false); + const [strikePriceRange, setStrikePriceRange] = useState({ start: 0, end: Number.MAX_VALUE }); + useEffect(() => { setIsLoading(true); - ky(`/api/symbols/${symbol}/options/analyze`).json().then(r => setOd(r)).finally(() => setIsLoading(false)); + ky(`/api/symbols/${symbol}/options/analyze`).json().then(r => { + setOd(r); + const { currentPrice } = r; + const thresholdValue = currentPrice * 0.1; + debugger; + setStrikePriceRange({ + start: Math.round(currentPrice - thresholdValue), + end: Math.round(currentPrice + thresholdValue) + }); + }).finally(() => setIsLoading(false)); // socket.emit('options-subscribe-request', item); // socket.on(`options-subscribe-response`, setOd); // return () => { @@ -129,7 +142,7 @@ export const useOptionTracker = (symbol: string) => { // socket.off('options-subscribe-response', setOd); // } }, []); - return { data, isLoading }; + return { data, isLoading, strikePriceRange, setStrikePriceRange }; }