Skip to content

Commit

Permalink
default threshold to 10 percent for range
Browse files Browse the repository at this point in the history
  • Loading branch information
mnsrulz committed Apr 13, 2024
1 parent e6b0bd0 commit 9c291eb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 deletions.
73 changes: 32 additions & 41 deletions src/components/stock-options-view.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import * as React from 'react';
import { useOptionTracker } from '../lib/socket';
import { NumberRange, useOptionTracker } from '../lib/socket';
import { GridColDef, DataGrid, gridClasses } from '@mui/x-data-grid';
import { Box, FormControl, InputLabel, MenuItem, Paper, Select, Slider, Stack, Tab, Tabs } from '@mui/material';
import { Box, FormControl, Grid, InputLabel, MenuItem, Paper, Select, Slider, Stack, Tab, Tabs } from '@mui/material';
import { useState } from 'react';
import dayjs from 'dayjs';
import { percentageFormatter } from '@/lib/formatters';
Expand All @@ -12,64 +12,51 @@ interface ITickerProps {
symbol: string
}

type NumberRange = { start: number, end: number }
type IStrikePriceSliderPorps = { allStrikePricesValues: number[], onChange: (v: NumberRange) => void, currentPrice: number }

type IStrikePriceSliderPorps = { allStrikePricesValues: number[], onChange: (v: NumberRange) => void, currentPrice: number, strikePriceRange: NumberRange }

const StrikePriceSlider = (props: IStrikePriceSliderPorps) => {
const { allStrikePricesValues, onChange } = props;
const { allStrikePricesValues, onChange, strikePriceRange } = props;
const t = [strikePriceRange.start, strikePriceRange.end];
const [minStrikePrice, maxStrikePrice] = [Math.min.apply(null, allStrikePricesValues), Math.max.apply(null, allStrikePricesValues)];
// const [minStrikePrice, maxStrikePrice] = [currentPrice - thresholdValue, currentPrice + thresholdValue];
const strikePriceMarks = allStrikePricesValues.map(m => ({ value: m }));
const [strikePriceRange, setStrikePriceRange] = useState([minStrikePrice, maxStrikePrice]);
const handleChange = (e: Event, v: number | number[]) => {
const value = v as number[];
setStrikePriceRange(value);
onChange({
start: value[0],
end: value[1]
});
};

//todoo
// function calculateValue(value: number) {
// return 2 ** value;
// }

/*
144
0 200
20 70 120 160 180
*/

return <Stack spacing={2} direction="row" sx={{ mb: 1 }} alignItems="center">
<div>Strike Price Range: {strikePriceRange[0]} - {strikePriceRange[1]}</div>

<Slider
getAriaLabel={() => 'Strike price'}
value={strikePriceRange}
onChange={handleChange}
//valueLabelDisplay="on"
min={minStrikePrice}
max={maxStrikePrice}
marks={strikePriceMarks}
step={null}
return <div>
<div>Strike Price Range: {t[0]} - {t[1]}</div>
<Stack direction="row" sx={{ m: 1 }} alignItems="center">
<Grid item>
</Grid>
<Slider
getAriaLabel={() => 'Strike price'}
value={t}
onChange={handleChange}
valueLabelDisplay="auto"
min={minStrikePrice}
max={maxStrikePrice}
marks={strikePriceMarks}
step={null}
// scale={calculateValue}
//getAriaValueText={valuetext}
/>
</Stack>
//getAriaValueText={valuetext}
/>
</Stack>
</div>
}
type PriceModeType = 'LAST_PRICE' | 'BID_PRICE' | 'ASK_PRICE'
type ValueModeType = 'PRICE' | 'ANNUAL_RETURN' | 'TOTAL_RETURN'

const numberFormatter = (v: string) => v && Number(v);
const todaysDate = dayjs();
export const StockOptionsView = (props: ITickerProps) => {
const { data, isLoading } = useOptionTracker(props.symbol);
const [strikePriceRange, setStrikePriceRange] = useState<NumberRange>({ start: 0, end: Number.MAX_VALUE });
const { data, isLoading, strikePriceRange, setStrikePriceRange } = useOptionTracker(props.symbol);

const [putCallTabValue, setPutCallTabValue] = useState<'PUT' | 'CALL'>('PUT');
const [priceMode, setPriceMode] = useState<PriceModeType>('LAST_PRICE');
const [valueMode, setValueMode] = useState<ValueModeType>('PRICE');
Expand Down Expand Up @@ -151,7 +138,11 @@ export const StockOptionsView = (props: ITickerProps) => {
<InputLabel htmlFor="demo-customized-textbox">Age</InputLabel>
<BootstrapInput id="demo-customized-textbox" />
</FormControl> */}
<StrikePriceSlider currentPrice={data.currentPrice} allStrikePricesValues={allStrikePricesValues} onChange={setStrikePriceRange} />
<StrikePriceSlider currentPrice={data.currentPrice}
allStrikePricesValues={allStrikePricesValues}
onChange={setStrikePriceRange}
strikePriceRange={strikePriceRange}
/>
<FormControl sx={{ m: 1 }} variant="standard">
<InputLabel>Price Mode</InputLabel>
<Select value={priceMode} onChange={(e, v) => setPriceMode(e.target.value as PriceModeType)} >
Expand Down
17 changes: 15 additions & 2 deletions src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,33 @@ type StockPriceData = {
}
}

export type NumberRange = { start: number, end: number }

export const useOptionTracker = (symbol: string) => {
const [data, setOd] = useState<OptionsData>();
const [isLoading, setIsLoading] = useState(false);
const [strikePriceRange, setStrikePriceRange] = useState<NumberRange>({ start: 0, end: Number.MAX_VALUE });

useEffect(() => {
setIsLoading(true);
ky(`/api/symbols/${symbol}/options/analyze`).json<OptionsData>().then(r => setOd(r)).finally(() => setIsLoading(false));
ky(`/api/symbols/${symbol}/options/analyze`).json<OptionsData>().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 () => {
// socket.emit('options-unsubscribe-request', item);
// socket.off('options-subscribe-response', setOd);
// }
}, []);
return { data, isLoading };
return { data, isLoading, strikePriceRange, setStrikePriceRange };
}


Expand Down

0 comments on commit 9c291eb

Please sign in to comment.