dimanche 3 octobre 2021

Design a JSON structure to create a display

I'm requesting for help in creating a JSON structure that will help in creating a display in Command Prompt using python. I have a draft JSON structure that is given below. I can change the below structure to best suit the display.

Plan is to use {}.format() to create the display.

[
    {
        "Acc1": [
            {
                "clientId": "Acc1",
                "Strategy": "MONTHLY_IRON_CONDOR",
                "StrategyList": [
                    "MONTHLY_IRON_CONDOR",
                    "MONTHLY_IRON_CONDOR",
                    "MANUAL"
                ],
                "strikeType": "PE",
                "CurrentM2M": 1850.0,
                "MaxStopLoss": 24195.0,
                "MaxProfit": 8065.0
            },
            {
                "clientId": "Acc1",
                "Strategy": "MONTHLY_IRON_CONDOR",
                "StrategyList": [
                    "MONTHLY_IRON_CONDOR",
                    "MONTHLY_IRON_CONDOR",
                    "MANUAL"
                ],
                "strikeType": "CE",
                "CurrentM2M": -8075.0,
                "MaxStopLoss": 36060.0,
                "MaxProfit": 12020.0
            }
        ]
    },
    {
        "Acc2": [
            {
                "clientId": "Acc2",
                "Strategy": "LONGTERM_STRANGLE",
                "StrategyList": [
                    "LONGTERM_STRANGLE",
                    "LONGTERM_STRANGLE"
                ],
                "strikeType": "PE",
                "CurrentM2M": 405.0,
                "MaxStopLoss": 21735.0,
                "MaxProfit": 7245.0
            },
            {
                "clientId": "Acc2",
                "Strategy": "LONGTERM_STRANGLE",
                "StrategyList": [
                    "LONGTERM_STRANGLE",
                    "LONGTERM_STRANGLE"
                ],
                "strikeType": "CE",
                "CurrentM2M": -3105.0,
                "MaxStopLoss": 36015.0,
                "MaxProfit": 12005.0
            }
        ]
    }
]

Output Structure:

     Strategies                            Acc1                           Acc2
                                   P&L  |   SL   |   MaxPL        P&L  |   SL   |   MaxPL
MONTHLY_IRON_CONDOR ---->    PE   1850.0  24195.0  8065.0
                             CE   -8075.0  36060.0  12020.0

LONGTERM_STRANGLE ------>                                     PE   405.0  21735.0  7245.0
                                                              CE   -3105.0  36015.0  12005.0

I've already tried the extract the data best out of the above JSON using the python code given below;

import json
import os
import random
import sys
import time

import pandas as pd

home = os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.realpath(__file__))))
sys.path.append(home)

home = os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.realpath(__file__))))
tmp_path = str(os.path.join(home, "tmp"))

# Set base path:--------------------------------------------------------
sys.path.append(home)
os.chdir(home)


def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')


a = 0
while True:
    clear_screen()

    positionsList = None

    if (os.path.isfile(os.path.join(tmp_path, "input"))):
        with open(os.path.join(tmp_path, "input"), "r") as f:
            positionsList = json.loads(f.read(), strict=False)

    clientIdList = []

    headerClientId = "       Strategies                          " # This variable will hold the top Header line that will contain dynamic 
                                                                   # number of Accounts
    headerNames = "                                   "            # This variable will hold the text "P&L  |   SL   |   MaxPL" for all 
                                                                     the accounts
    monthlyIC = " "                                                # Strategy Specific data for MONTHLY_IRON_CONDOR
    longtermStrangle = " "                                         # Strategy Specific data for LONGTERM_STRANGLE
    noData = "                               "                     # Filler when there is no data for a specific strategy for a given 
                                                                     account

    for client in positionsList:
        values = list(client.values())[0]
        flg = False
        for value in values:
            for clientId in client.keys():
                if flg == False:
                    clientIdList.append(clientId)
                    headerClientId = headerClientId + "{}                         "
                    headerNames = headerNames + "P&L  |   SL   |   MaxPL        "
                    flg = True

                availableStrategies = ["MONTHLY_IRON_CONDOR",'INTRADAY_STRANGLE", "LONGTERM_STRANGLE", "PUT_SPREAD"]

                unusedStrategies = list(
                    set(availableStrategies) - set(value['StrategyList']))      # Helps to fill "noData" for these strategies for each 
                                                                                # Account

                if Strategy.monthlyIronCondor.value in unusedStrategies and "\n" not in monthlyIC:
                    monthlyIC = monthlyIC + noData
                elif Strategy.longTermStrangle.value in unusedStrategies and "\n" not in longtermStrangle:
                    longtermStrangle = longtermStrangle + noData

                if value['Strategy'] == Strategy.monthlyIronCondor.value:
                    monthlyIC = monthlyIC + "" + str(value['strikeType']) + "   " + str(value['CurrentM2M']) + \
                        "  " + str(value['MaxStopLoss']) + \
                        "  " + str(value['MaxProfit'])
                if value['Strategy'] == Strategy.longTermStrangle.value:
                    longtermStrangle = longtermStrangle + "" + str(value['strikeType']) + "   " + str(value['CurrentM2M']) + \
                        "  " + str(value['MaxStopLoss']) + \
                        "  " + str(value['MaxProfit'])
                if monthlyIC.replace(' ', '', -1) != '':
                    monthlyIC = monthlyIC + "\n                             "
                if longtermStrangle.replace(' ', '', -1) != '':
                    longtermStrangle = longtermStrangle + "\n                             "
                # if longtermStrangle.replace(' ', '', -1) == '':
                #    longtermStrangle = longtermStrangle + noData

    headerClientId = headerClientId.format(clientIdList[0], clientIdList[1])
    print(headerClientId, end='\n\r')
    print(headerNames, end='\n\r')

    print("MONTHLY_IRON_CONDOR ---->   " + monthlyIC, end='\n\r')
    print("LONGTERM_STRANGLE ------>   " + longtermStrangle, end='\n\r')

    time.sleep(1)
    a += 1

Aucun commentaire:

Enregistrer un commentaire