Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rylorin committed Jan 10, 2025
1 parent 22b141c commit 191b247
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 127 deletions.
10 changes: 10 additions & 0 deletions src/app/components/Portfolio/Setting/SettingEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const SettingEdit: FunctionComponent<Props> = ({ ..._rest }): React.ReactNode =>
<Field name="minPremium" type="number" />
</Text>
</Flex>

<Flex justifyContent="center" gap="2">
<Text w="180px" as="b" textAlign="right">
CSP strat.:
Expand All @@ -92,6 +93,14 @@ const SettingEdit: FunctionComponent<Props> = ({ ..._rest }): React.ReactNode =>
<Field name="navRatio" type="number" />
</Text>
</Flex>
<Flex justifyContent="center" gap="2">
<Text w="180px" as="b" textAlign="right">
CSP Delta:
</Text>
<Text w="200px">
<Field name="ccDelta" type="number" />
</Text>
</Flex>
<Flex justifyContent="center" gap="2">
<Text w="180px" as="b" textAlign="right">
CSP Roll:
Expand All @@ -104,6 +113,7 @@ const SettingEdit: FunctionComponent<Props> = ({ ..._rest }): React.ReactNode =>
))}
</Field>
</Flex>

<Flex justifyContent="center" gap="2">
<Text w="180px" as="b" textAlign="right">
Covered calls strat.:
Expand Down
6 changes: 6 additions & 0 deletions src/app/components/Portfolio/Setting/SettingShow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const SettingShow: FunctionComponent<SettingShowProps> = ({ ..._rest }): React.R
{cspStrategy2String(thisItem.cspStrategy)}
</Text>
</Flex>
<Flex justifyContent="center" gap="2">
<Text w="180px" as="b" textAlign="right">
CSP delta:
</Text>
<Number textAlign="left" w="120px" value={thisItem.cspDelta} decimals={2} color={"-"} />
</Flex>
<Flex justifyContent="center" gap="2">
<Text w="180px" as="b" textAlign="right">
CSP Roll:
Expand Down
5 changes: 5 additions & 0 deletions src/app/components/Portfolio/Setting/SettingsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const SettingsTable: FunctionComponent<Props> = ({ content, ..._rest }): React.R
<Td isNumeric>
<Number value={item.navRatio} isPercent color="-" />
</Td>
<Td isNumeric>
<Number value={item.cspDelta} decimals={2} color="-" />
</Td>
<Td>{strategy2String(item.rollPutStrategy)}</Td>
<Td>{strategy2String(item.ccStrategy)}</Td>
<Td isNumeric>
Expand Down Expand Up @@ -98,6 +101,8 @@ const SettingsTable: FunctionComponent<Props> = ({ content, ..._rest }): React.R
<Td></Td>
<Td></Td>
<Td></Td>
<Td></Td>
<Td></Td>
<Td>
<Link to={SettingLink.toSettingNew(portfolioId)} as={RouterLink}>
<IconButton aria-label="Add setting" icon={<PlusSquareIcon />} size="xs" variant="ghost" />
Expand Down
4 changes: 2 additions & 2 deletions src/bots/csp.bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class SellCashSecuredPutBot extends ITradingBot {
console.log("parameter.underlying.price:", parameter.underlying.livePrice);
console.log(
"free_for_this_symbol in currency:",
Math.round(free_for_this_symbol * this.base_rates[parameter.underlying.currency]),
Math.round(free_for_this_symbol * this.baseRates[parameter.underlying.currency]),
);
// RULE 7: stock price is lower than previous close
const options =
Expand Down Expand Up @@ -164,7 +164,7 @@ export class SellCashSecuredPutBot extends ITradingBot {
for (const option of all_options) {
// RULE 6: check overall margin space
const stock = await StockContract.findByPk(option.stock.id);
if (option.strike * option.multiplier * this.base_rates[option.contract.currency] < max_for_all_symbols) {
if (option.strike * option.multiplier * this.baseRates[option.contract.currency] < max_for_all_symbols) {
if (option.impliedVolatility! > stock!.historicalVolatility!) {
// RULE 1: implied volatility > historical volatility
// const expiry: Date = new Date(option.lastTradeDate);
Expand Down
2 changes: 1 addition & 1 deletion src/bots/importer.bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ export class ImporterBot extends ITradingBot {
case StatementTypes.InterestStatement:
return InterestStatement.findOrCreate({
where: { id: statement.id },
defaults: { id: statement.id, country: "" },
defaults: { id: statement.id, country: this.portfolio.country },
}).then(([_interestStatement, _created]) => statement);

case StatementTypes.CashStatement:
Expand Down
40 changes: 24 additions & 16 deletions src/bots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ITradingBot extends EventEmitter {
public get portfolio(): Portfolio {
return this._portfolio;
}
protected base_rates: number[] = [];
protected baseRates: number[] = [];

constructor(app: MyTradingBotApp, api: IBApiNext, account: string) {
super();
Expand Down Expand Up @@ -199,8 +199,8 @@ export class ITradingBot extends EventEmitter {
} else throw Error("portfolio not found");
})
.then((currencies) => {
for (const currency of currencies) this.base_rates[currency.currency] = currency.rate;
this.base_rates[this.portfolio.baseCurrency] = 1.0;
for (const currency of currencies) this.baseRates[currency.currency] = currency.rate;
this.baseRates[this.portfolio.baseCurrency] = 1.0;
});
}

Expand Down Expand Up @@ -335,15 +335,15 @@ export class ITradingBot extends EventEmitter {
result.quantity += position.quantity * opt.multiplier;
result.value +=
(position.quantity * opt.multiplier * position.contract.livePrice) /
this.base_rates[position.contract.currency];
this.baseRates[position.contract.currency];
result.engaged +=
(position.quantity * opt.multiplier * opt.strike) / this.base_rates[position.contract.currency];
(position.quantity * opt.multiplier * opt.strike) / this.baseRates[position.contract.currency];
result.risk +=
(position.quantity *
opt.multiplier *
opt.strike *
(opt.delta ? opt.delta : opt.callOrPut == OptionType.Call ? +0.5 : -0.5)) /
this.base_rates[position.contract.currency];
this.baseRates[position.contract.currency];
result.options.push(opt);
}
}
Expand All @@ -362,16 +362,24 @@ export class ITradingBot extends EventEmitter {
};
if (short && !long) where.quantity = { [Op.lt]: 0 };
if (!short && long) where.quantity = { [Op.gt]: 0 };
return Position.findAll({
where: where,
include: {
model: Contract,
where: {
secType: IbSecType.OPT,
return (
Position.findAll({
where: where,
include: {
model: Contract,
where: {
secType: IbSecType.OPT,
},
},
},
}).then(async (positions: Position[]) => this.sumOptionsPositionsSynthesisInBase(positions, underlying, right));
})
// .then(async (positions: Position[]) => {
// console.log('getOptionsPositionsSynthesisInBase',positions);
// return positions;
// })
.then(async (positions: Position[]) => this.sumOptionsPositionsSynthesisInBase(positions, underlying, right))
);
} else {
logger.error(MODULE + ".getOptionsPositionsSynthesisInBase", "Portfolio not loaded");
return Promise.resolve({
engaged: 0,
value: 0,
Expand Down Expand Up @@ -530,7 +538,7 @@ export class ITradingBot extends EventEmitter {
currency: currency,
},
}).then((balance) => {
if (balance) return balance.quantity / this.base_rates[balance.currency];
if (balance) return balance.quantity / this.baseRates[balance.currency];
else return 0;
});
}
Expand All @@ -539,7 +547,7 @@ export class ITradingBot extends EventEmitter {
return Balance.findAll({ where: { portfolio_id: this.portfolio.id } }).then((balances) => {
return balances.reduce(
(p, b) => {
p.quantity += b.quantity / this.base_rates[b.currency];
p.quantity += b.quantity / this.baseRates[b.currency];
return p;
},
{ quantity: 0, currency: this.portfolio.baseCurrency },
Expand Down
Loading

0 comments on commit 191b247

Please sign in to comment.