-2

さて、私はpythonを初めて使用します。これについて学校でクラスを受講していますが、これについて少し混乱しています。株式の売買によるトランザクションを計算するプログラム/スクリプトを作成していますが、何らかの理由で、「残高」変数を時間の経過とともに蓄積し、株式の売買から減算および加算することができません。これがコードです。どんな入力でも素晴らしいでしょう ^.^

def main():

    #Below is the variables used in the context

    balance = float(input("Starting cash amount? "))
    numtrades = int(input("Number of trades for today?"))
    print('You do not own any shares, but have', balance, 'in cash')
    buy = 0
    sell = 0
    price_per_share = 0
    transaction_amount = 0
    transaction_amount_buy = 0
    transaction_amount_sell = 0


    #Below is the prompt for the first transaction, which requires a buy

    num_shares = int(input('Number of shares to buy?'))
    price_per_share = float(input('Price per share?'))
    print(num_shares, "shares for $",price_per_share, "per share cost $",price_per_share * num_shares)
    buy = buy + num_shares
    transaction_amount_buy = transaction_amount_buy + (num_shares * price_per_share)
    print("Currently holding", buy, "and have $", balance - transaction_amount_buy , "in cash")


    if balance < transaction_amount :

            print('You do not have sufficient funds to purchase', num_shares, 'for $', price_per_share, 'per share.')
            print('Your current balance is', balance, ', but', num_shares,' x ', price_per_share,' = ', num_shares * price_per_share)
            print("Currently holding", buy, "and have $", balance - transaction_amount , "in cash")




    #Below is the loop counter for each trade, along with if statements for buy/sell


    for i in range(numtrades):
        print('Trade number', (i + 2), end = "")
        action = input(' (buy/sell)?')
        if action == 'buy':
            num_shares = int(input('Number of shares to buy?'))
        if action == 'sell':
            num_shares = int(input('Number of shares to sell?'))
        price_per_share = float(input('Price per share?'))
        print('Transaction', (i+2))
        print('Transaction type is', action)


        if action == 'buy':
            print(num_shares, "shares for $",price_per_share, "per share cost $",price_per_share * num_shares)
            buy = buy + num_shares
            transaction_amount_buy = transaction_amount_buy + (num_shares * price_per_share)

        if action == 'sell':
            print(num_shares, 'shares for $',price_per_share, 'per share worth $',price_per_share * num_shares)
            sell = sell + num_shares
            transaction_amount_sell = transaction_amount_sell + (num_shares * price_per_share)

        transaction_amount = transaction_amount_buy - transaction_amount_sell

        if balance < transaction_amount :

            print('You do not have sufficient funds to purchase', num_shares, 'for $', price_per_share, 'per share.')
            print('Your current balance is', balance - transaction_amount, ', but', num_shares,' x ', price_per_share,' = ', num_shares * price_per_share)
            print("Currently holding", buy, "and have $", balance - transaction_amount , "in cash")


        if balance > transaction_amount :
            print("Currently holding", buy, "and have $", balance - transaction_amount , "in cash")

 main()
4

3 に答える 3

1
transaction_amount_buy = transaction_amount_buy + (num_shares * price_per_share)
balance = balance - transaction_amount_buy
print("Currently holding", buy, "and have $", balance , "in cash")

これで最初のセクションが機能するようになり、残りのセクションが続くはずです...

于 2013-09-19T03:31:54.927 に答える
1

この問題を一連の小さな問題に分解することをお勧めします。それは、小さな問題だけを扱う関数を作成することによって行います。すべての関数を main に接続する前に、各関数を機能させます。

例: 何かを購入したかどうかに関係なく、残高があります。購入または販売を選択します。何を買ったり売ったりしていますか?販売している場合、あなたはそれを所有していますか?そうでない場合、それは大丈夫ですか?所有していないものを売る(ショートする)のは別物ですか?

簡単に始めましょう。バナナのみを売買しているとします。バナナを100本買うのとバナナを100本売るのとでは違いますか? 買いが 0 より大きい数値で、売りが 0 未満の数値である場合は、そうではありません。

つまり、(現金)残高があります。あなたが「買う」とき、あなたはコストを返します。残高はコスト分だけ減らさなければなりません。販売する場合、残高はコスト分だけ増加する必要があります。

うーん.. 所有していないものを販売できますか? 残高だけでなく、「ポジション」、つまり売買される商品の保有も追跡する必要があるようです。

所有している以上に販売できない場合、または残高よりも高価なバナナを購入できない場合は、これらの問題を処理する方法が必要です。

于 2013-09-19T03:40:45.963 に答える