1

I would like to do a lot of analysis/performance/statistics on my stock portfolio, which I plan to track with my app. E.g.:

  • Week performance
  • Month performance
  • Year performance
  • Best performer
  • and a lot of other things I can't imagine right now...

Where I'm struggling right now:

- What is a good/the best way to archive this? - Also to show this info on a dashboard

--> I think I should store this information somehow... But how to do this on a daily/weekly/whatever basis?

--> I don't see a way to do such things while runtime?

--> furthermore I need to know, when do do such thinks... It's end of the week so do weekly performance calculations...

--> Maybe there is also an MVP solution, that can evolve into a top-notch solution?

My models are locking like this at the moment - The position is the clamp around my orders and my EOD data. Right now I'm working with yfinance to get range financial data + finnhub API to get real time prices:

class Position(models.Model):
    name = models.CharField(max_length=100)
    shares = models.FloatField(default=0.0)
    symbol = models.CharField(max_length=100, default="")
    transaction_fee_sum = models.FloatField(default=0.0)
    profit = models.FloatField(default=0.0)
    average_price = models.FloatField(default=0.0)
    cost_value = models.FloatField(default=0.0)
    last_price  = models.FloatField(default=0.0)
    position_value = models.FloatField(default=0.0)
    last_update = models.DateTimeField(default=datetime.now(), blank=True)
    position_started = models.DateTimeField(default=datetime.now(), blank=True)
    position_ended = models.DateTimeField(default=datetime.now(), blank=True)
    isin = models.CharField(max_length=12)
    depot = models.ForeignKey("Depot", blank=True, null=True, on_delete=models.SET_NULL)

    def __str__(self):
        return self.name

class PriceHistoryEOD(models.Model):
    price = models.DecimalField(max_digits=8, decimal_places=2)
    position = models.ForeignKey("Position", blank=True, null=True, on_delete=models.CASCADE)
    price_date = models.DateField(default=date.today)

class Inventory_Position(models.Model):
    shares = models.FloatField(default=0.0)
    price = models.FloatField(default=0.0)
    position = models.ForeignKey("Position", blank=True, null=True, on_delete=models.CASCADE)

    def __str__(self):
        return str(self.position.name) + "_Inventory_" + str(self.id)

class Order(models.Model):

    ORDER_PLACES = (
        ('TRADEGATE', 'Tradegate'),
        ('FRANKFURT', 'Frankfurt'),
        ('STUTTGART', 'Stuttgart'),
    )

    ORDER_TYPE = (
        ('buy', 'buy'),
        ('sell', 'sell'),
    )

    name = models.CharField(max_length=100)
    depot = models.ForeignKey("Depot", blank=True, null=True, on_delete=models.SET_NULL)
    position = models.ForeignKey("Position", blank=True, null=True, on_delete=models.SET_NULL)
    ko_identifier = models.BooleanField()
    order_price = models.FloatField()
    quantity = models.FloatField()
    order_value = models.FloatField()
    order_fees = models.DecimalField(decimal_places=2,max_digits=5,default=0.0, null=True)
    order_date = models.DateTimeField('order date')
    order_wkn = models.CharField(max_length=6)
    order_isin = models.CharField(max_length=12)
    order_TYPE = models.CharField(max_length=100, choices=ORDER_TYPE)
    order_place = models.CharField(max_length=100, choices=ORDER_PLACES)

    def __str__(self):
        return self.name
h0ppel
  • 347
  • 2
  • 5
  • 21

1 Answers1

0

I think the easiest way is to create a simple script for each type of analysis you need to do (weekly, monthly...) and then save the values to an sqlite3 DB. You can then import it into your django View as the data context. If you'd like to automate the whole process you can just set up a cronjob through crontab if you're using Linux, and if you're on Windows you can use the Windows task scheduler

Sqlite3 and python: How to work with sqlite3 and Python

If you are not familiar with crontab you can check out one of the many video on Youtube. As for the schedule expressions you can use this website, it makes it so much easier to find the expression you're looking for: https://crontab.guru/

finally one more tip, if you need to get stock prices and you feel like yfinance is kind of slow, you might want to try the Alpaca-trade-API (free API-KEY): https://github.com/alpacahq/alpaca-trade-api-python

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 04 '21 at 02:10