SyntaxError:打印功能中的语法无效

时间:2019-10-10 10:36:46

标签: python

我正在MacbookAir上处理以下脚本,目前尚不清楚此语法错误来自何处,并尝试在Google上搜索为什么打印功能中的=符号会中断。

我知道可以打印不同的功能,并尝试了许多功能。但不清楚我是否使用了正确的Python版本(已安装2和3)。

你能帮忙吗?

61行出现错误:

print("The interest rate is too high to trade {}".format(total_profit) , end="\n", file=output_file)

脚本:

## Initial values from the Python script 
principal=1000
coupon=0.06
frequency=2
r=0.03
transaction_fee = 0.003*principal

## Amendments to the variables as per question 7
probabilitypayingout=0.85
probabolilitynotpayingout=0.15
notpayingoutfullamount=200
maturity=7
market_price=1070
avoidtradingaboveinterestrate=0.02 

#!/usr/bin/env python3.7
import numpy as np

# Open a file to store output
output_file = open("outputfile.txt", "w")

# print variables of this case
print("The variables used for this calculation are: \n - Probability of paying out the full principal {}.".format(probabilitypayingout), "\n - Probability of paying out partial principal {}.".format(probabolilitynotpayingout), "\n - Amount in case of paying out partial principal {}.".format(notpayingoutfullamount), "\n - Market price bond {}.".format(market_price), "\n - Bond maturity in years {}.".format(maturity), "\n - Coupon rate bond {}.".format(coupon), "\n - Principal bond {}.".format(principal), "\n - Frequency coupon bond {}.".format(frequency) , "\n - Risk free rate {}.".format(r) , "\n - Avoid trading aboe interest rate {}.".format(avoidtradingaboveinterestrate), "\n \n"   )

# calculate true value and decide whether to trade     

true_price=0
principalpayout=(probabilitypayingout*principal)+(probabolilitynotpayingout*notpayingoutfullamount)

for t in range(1,maturity*frequency+1):
  if t<(maturity*frequency):
    true_price = true_price + (coupon/frequency)*principal/(1+(r/frequency))**t  # Present value of coupon payments
  else:
    true_price = true_price + (coupon/frequency)*principal/(1+(r/frequency))**t + principalpayout/(1+(r/frequency))**t  # Present value of coupons and principal

print("The price of the bond according to the pricing model is {}, while the current market price is {}.".format(true_price, market_price)) 

if true_price-transaction_fee>market_price:
  profit = true_price-transaction_fee-market_price
  print("The trade is executed and if the pricing model is correct, the profit will be {}".format(profit), "after deduction of trading fees.")  
else:
  print("The trade was not executed, because the expected profit after transaction fees is negative.")  

# Fifth, mimic changes in market conditions by adjusting the interest rate and market price. The indented code below the "for" line is repeated 1,000 times. 
total_profit=0

for n in range(0,1000):
  # Adds some random noise to the interest rate and market price, so each changes slightly (each time code is executed, values will differ because they are random)
  change_r=np.random.normal(0,0.015)  
  change_market_price=np.random.normal(0,40) 
  r_new = r + change_r
  market_price_new = market_price + change_market_price

  # Sixth, execute trading algorithm using new values  
  true_price_new=0


if r_new>avoidtradingaboveinterestrate:


    print("The interest rate is too high to trade {}".format(total_profit) , end="\n", file=output_file)
    output_file.close()

else:
    for t in range(1,maturity*frequency+1):
        if t<(maturity*frequency):
          true_price_new = true_price_new + (coupon/frequency)*principal/(1+(r_new/frequency))**t
        else:
          true_price_new = true_price_new + (coupon/frequency)*principal/(1+(r_new/frequency))**t + principalpayout/(1+(r_new/frequency))**t

    if true_price_new-transaction_fee>market_price_new:
        trading_profit = true_price_new-transaction_fee-market_price_new
        total_profit = total_profit + trading_profit
      print("The trade was executed and is expected to yield a profit of {}. The total profit from trading is {}.".format(trading_profit,total_profit), end="\n", file=output_file    )

print("The total profit from trading is {}".format(total_profit), end="\n", file=output_file)
output_file.close()

2 个答案:

答案 0 :(得分:0)

  

print(“利率太高,无法交易{}”。format(total_profit),end =“ \ n”,file = output_file)

Python 3语法。运行python3 your_file.py而不是python your_file.py

答案 1 :(得分:0)

我认为您没有使用正确的python版本。 Mac OS上的默认python版本仍然是python 2.x,要使用python 3.x,您必须使用“ python3”调用解释器。尝试以下命令:

python3 PythonMIFIAT.py