Search code examples
pythonpython-3.xlogicpercentage

python program for the DCA logic


I'm working on a DCA trading project. I have some amount 'X' and I need to divide that 'X' amount to 'Y' parts. Each 'Y' part should have scale or ratio of 'Z'. What I want output is 'Y' parts with divided amounts of 'X'

X = 1000
Y = 4
Z = 2

then output will be

66.66, 133.33, 266.66, 533.33

Explanation: Each preceding number is double or 2 times (Z) of the previous number and are divided as 4 parts (Y) and their total sum is almost 1000 (X)

Example 2:

X = 1000
Y = 5 
Z = 1

Then output is

200, 200, 200, 200, 200

Explanation: Each preceding number is same or 1 times (Z) of the previous number and are divided as 5 parts (Y) and their total sum is almost 1000 (X)

Example 3:

X = 1000
Y = 6
Z = 1.5

Then output is

48.12, 72.18, 108.27, 162.4, 243.6, 365.41

Explanation: Each preceding number is almost 1.5 times (Z) of the previous number and are divided as 6 parts (Y) and their total sum is almost 1000 (X)


Solution

  • You could do:

    X = 1000
    Y = 4
    Z = 2
    
    n_parts = sum([Z ** i for i in range(Y)])
    
    part_size = X / n_parts
    
    output = [part_size * Z ** i for i in range(Y)]
    
    print(output)
    

    Output:

    [66.66666666666667, 133.33333333333334, 266.6666666666667, 533.3333333333334]
    

    This code finds the "part size", and then multiplies each output by this to the power of i, so the outputs consecutively are Z times bigger than the last.