spotware / openapi-proto-messages

Open API Proto Messages
https://openapi.ctrader.com
MIT License
21 stars 9 forks source link

P&L calculation pseudo-code correction #4

Closed Vasile-Peste closed 2 years ago

Vasile-Peste commented 2 years ago

Hello, at https://spotware.github.io/open-api-docs/profit-loss-calculation/ the following pseudo code is indicated to calculate rate for converting P&L to deposit currency

fromAsset = "JPY"
toAsset = "USD"

// rate will be our conversion result
rate = 1

// symbols are the ProtoOASymbolsForConversionRes symbols field
symbols[] = ProtoOASymbolsForConversionRes(fromAsset,toAsset)

asset = fromAsset 

FOR symbol in symbols:
    IF symbol.baseAsset == asset:
        rate = rate * symbol.price
        asset = symbol.quoteAsset
    ELSE:
        rate = rate * 1 / symbol.price
        asset = symbol.baseAsset

However from the pseudo code indicated it's not clear to me if symbol.price represents the bid price or the ask price of the symbol? I should consider it the ask price since I'm supposed to buy the deposit currency with the symbol quote asset? For now this is my solution but I'm not 100% sure if it's totally correct.

fromAsset = "JPY"
toAsset = "USD"

// rate will be our conversion result
rate = 1

// symbols are the ProtoOASymbolsForConversionRes symbols field
symbols[] = ProtoOASymbolsForConversionRes(fromAsset,toAsset)

asset = fromAsset 

FOR symbol in symbols:
    IF positionDirection == SHORT:
        closePrice = symbol.ask
    ELSE:
        closePrice = symbol.bid

    IF symbol.baseAsset == asset:
        rate = rate * closePrice
        asset = symbol.quoteAsset
    ELSE:
        rate = rate * 1 / closePrice
        asset = symbol.baseAsset
amusleh-spotware-com commented 2 years ago

Fixed, thanks!