Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Fixed the DCA calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
coltoneshaw committed Dec 7, 2021
1 parent 112ae0a commit 035a7cc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/app/Pages/ActiveDeals/ActiveDeals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const columnList = [
id: 'created_at',
name: 'Duration'
},
{
id: 'bought_average_price',
name: 'Average Price'
},
{
id: 'current_price',
name: 'Current Price'
Expand Down
10 changes: 10 additions & 0 deletions src/app/Pages/ActiveDeals/Components/DealsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ function DealsTable({ data, selectedColumns }: { data: object[], selectedColumns
return < span className=" monospace-cell">{getDateString(cell.value)}</span>
}
},
{
Header: 'Average Price',
accessor: 'bought_average_price',
align: 'flex-end',
maxWidth: 120,
sortType: (rowA: any, rowB: any, columnId: string) => sortMe(rowA, rowB, columnId),
Cell: ({ cell }: any) => {
return < span className=" monospace-cell">{formatCurrency([cell.row.original.from_currency], cell.value, true).metric}</span>
}
},
{
Header: 'Current',
accessor: 'current_price',
Expand Down
55 changes: 37 additions & 18 deletions src/app/Pages/ActiveDeals/Components/SubrowTabs/DCA.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Grid, TextField } from "@mui/material";
import React, { useEffect, useState } from "react";
import { parseNumber } from '@/utils/number_formatting'
import { formatCurrency, supportedCurrencies } from '@/utils/granularity'

const calcTpPrice = (currentPrice: number, tpPrice: number) => ((tpPrice - currentPrice) / currentPrice) * 100

const calculate = (addFunds: number, atPrice: number, tpPercent: number, ordersData: any) => {
type calc = {
addFunds: number
atPrice: number
tpPercent: number
}

const exchangeFee = 0.001

const calcNew = ({addFunds, atPrice, tpPercent}: calc, ordersData: any, currentPrice: string) => {
const filteredData = ordersData.filter((r: any) => r.order_type === 'BUY' && r.status_string === 'Filled')
const totalCrypto = filteredData.reduce((r: number, c: any) => {
return r + parseFloat(c.quantity)
Expand All @@ -17,41 +23,54 @@ const calculate = (addFunds: number, atPrice: number, tpPercent: number, ordersD
}, 0) + addFunds

const average = totalSpent / totalCrypto
const tpAt = average + (average * tpPercent / 100)
const tpAt = ( average * ( ( tpPercent / 100) + exchangeFee ) ) + average

return {
average,
tpAt,
gainRequired: calcTpPrice(atPrice, tpAt)
gainRequired: ( ( ( tpAt / average ) - 1) + ((average / +currentPrice) -1) ) * 100
}
}

function DCA({ row, ordersData }: any) {

type row = {
bought_average_price: number,
take_profit: number
current_price: number
}

const calcOriginal = ({ bought_average_price, take_profit, current_price}: row ) => {
const average = bought_average_price;
const tpPercent = take_profit / 100;
const tpAt = average * ( 1 + tpPercent + exchangeFee);
return {
average,
tpAt,
gainRequired: ( ( ( tpAt / average ) - 1) + ((average / current_price) -1) ) * 100
}

}

function DCA({ row, ordersData }: any) {

const origCalc = calculate(0, row.original.current_price, row.original.take_profit, ordersData)

const origCalc = calcOriginal(row.original)
const [newCalc, setNewCalc] = useState(() => ({ average: origCalc.average, tpAt: origCalc.tpAt, gainRequired: origCalc.gainRequired }))

const [addFunds, setAddFundsField] = useState<number>(0);
const [atPrice, setAtPrice] = useState<number>(parseFloat(row.original.current_price));
const [tpPercent, setTpPercent] = useState<number>(parseFloat(row.original.take_profit));


// const formatCurrencyLocally = (value:number) => formatCurrency([row.original.from_currency], value).metric

useEffect(() => {
if (isNaN(addFunds)) {
setAddFundsField(0)
return
}
setNewCalc(calculate(addFunds, atPrice, tpPercent, ordersData))

}, [addFunds, atPrice, tpPercent])



// It's receiving the price in atPrice and using that as the TP price, I believe
setNewCalc(calcNew({ addFunds, atPrice, tpPercent }, ordersData, row.original.current_price))

}, [addFunds, atPrice, tpPercent])

return (
<div className="flex-column" style={{ width: '100%' }}>
Expand Down Expand Up @@ -80,9 +99,9 @@ function DCA({ row, ordersData }: any) {
<tr>
<th></th>
<th>Buy average</th>
<th >Take Profit At</th>
<th >Take Profit percent</th>
<th>Gain to TP</th>
<th>Take profit at</th>
<th>Take profit percent</th>
<th>Distance from TP</th>
</tr>
</thead>
<tbody className="dcaCalcTable">
Expand Down
9 changes: 6 additions & 3 deletions src/app/Pages/ActiveDeals/Components/SubrowTabs/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,25 @@ const OrderTimeline = ({ row, ordersData }: { row: { original: Type_Deals }, ord
average_price: row.original.current_price,
}

const { max_safety_orders, safety_order_volume, martingale_step_coefficient, martingale_volume_coefficient, completed_safety_orders_count, safety_order_step_percentage } = row.original
const { max_safety_orders, safety_order_volume, martingale_step_coefficient, martingale_volume_coefficient, safety_order_step_percentage } = row.original
const basePrice = ordersData.find(o => o.deal_order_type === 'Base') || { rate: 0 };
// const baseSafety = ordersData.find(o => o.deal_order_type === 'Safety') || { rate: 0 };
const placedSafeties = ordersData.filter(o => o.deal_order_type === 'Safety' && o.status_string != 'Cancelled');


const safetyArray = calc_SafetyArray(safety_order_volume, max_safety_orders, completed_safety_orders_count, martingale_volume_coefficient, martingale_step_coefficient, safety_order_step_percentage)
const safetyArray = calc_SafetyArray(safety_order_volume, max_safety_orders, martingale_volume_coefficient, martingale_step_coefficient, safety_order_step_percentage)
.filter(so => so.so_count > placedSafeties.length)
.map(so => {
const rate = basePrice.rate - ((so.deviation / 100) * basePrice.rate)
const quantity = so.volume / rate;

return {
order_id: so.so_count + 'safety',
order_type: 'BUY',
deal_order_type: 'Safety',
status_string: 'Future',
rate,
quantity: so.volume / rate,
quantity,
total: so.volume,
created_at: '',
updated_at: '',
Expand Down
11 changes: 9 additions & 2 deletions src/utils/formulas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,17 @@ const calc_dropMetrics = (bankRoll:number, botData:Type_Query_bots[]) => {
});
}

const calc_SafetyArray = (safety_order_volume:number , max_safety_orders:number , completed_safety_orders:number , martingale_volume_coefficient:number, martingale_step_coefficient:number, safety_order_step_percentage:number ) => {
type safetyArray = {
so_count: number
deviation: number
volume: number
}
const calc_SafetyArray = (safety_order_volume:number , max_safety_orders:number, martingale_volume_coefficient:number, martingale_step_coefficient:number, safety_order_step_percentage:number) => {
// setting the initial drawdown value.
let drawdown = +safety_order_step_percentage
let prevDeviation = +safety_order_step_percentage

const safetyArray = <any[]>[]
const safetyArray = <safetyArray[]>[]

safetyArray.push({
so_count: 1,
Expand All @@ -194,6 +199,7 @@ const calc_SafetyArray = (safety_order_volume:number , max_safety_orders:number
let so_deviation = (prevDeviation * martingale_step_coefficient);
const volume = safety_order_volume * martingale_volume_coefficient ** (so_count - 1)


drawdown += so_deviation
prevDeviation = so_deviation

Expand All @@ -205,6 +211,7 @@ const calc_SafetyArray = (safety_order_volume:number , max_safety_orders:number

safetyArray.push(safetyObject)
}

return safetyArray
}

Expand Down

0 comments on commit 035a7cc

Please sign in to comment.