Python:语法无效:<string>,第1行,pos 16

时间:2016-11-04 20:54:28

标签: python

我已经在Python中开发了一个代码 - 为了运行程序 - 我需要从命令行中获取一些参数。但我不断得到同样的错误:

Traceback (most recent call last):
File "<string>", line 1, in <fragment>
invalid syntax: <string>, line 1, pos 16

我对我的代码有什么问题我最不了解。所以,我在下面提供我的代码,万一有人可以帮助我:

import QSTK.qstkutil.qsdateutil as du
import QSTK.qstkutil.tsutil as tsu
import QSTK.qstkutil.DataAccess as da
import datetime as dt
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import time
import math
import copy
import QSTK.qstkstudy.EventProfiler as ep
import csv
import sys
import argparse


def readData(li_startDate, li_endDate, ls_symbols):
    #Create datetime objects for Start and End dates (STL)
    dt_start = dt.datetime(li_startDate[0], li_startDate[1], li_startDate[2])
    dt_end = dt.datetime(li_endDate[0], li_endDate[1], li_endDate[2])

    #Initialize daily timestamp: closing prices, so timestamp should be hours=16 (STL)
    dt_timeofday = dt.timedelta(hours=16)

    #Get a list of trading days between the start and end dates (QSTK)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    #Create an object of the QSTK-dataaccess class with Yahoo as the source (QSTK)
    c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)

    #Keys to be read from the data
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

    #Read the data and map it to ls_keys via dict() (i.e. Hash Table structure)
    ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    return [d_data, dt_start, dt_end, dt_timeofday, ldt_timestamps]


def marketsim(cash,orders_file,values_file):

    orders = pd.read_csv(orders_file,index_col='Date',parse_dates=True,header=None)
    ls_symbols = list(set(orders['X.4'].values))
    df_lastrow = len(orders) - 1
    dt_start = dt.datetime(orders.get_value(0, 'X.1'),orders.get_value(0, 'X.2'),orders.get_value(0, 'X.3'))
    dt_end = dt.datetime(orders.get_value(df_lastrow, 'X.1'),orders.get_value(df_lastrow, 'X.2'),orders.get_value(df_lastrow, 'X.3') + 1 )    
    #d_data = readData(dt_start,dt_end,ls_symbols)
    #Initialize daily timestamp: closing prices, so timestamp should be hours=16 (STL)
    dt_timeofday = dt.timedelta(hours=16)

    #Get a list of trading days between the start and end dates (QSTK)
    ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)

    #Create an object of the QSTK-dataaccess class with Yahoo as the source (QSTK)
    c_dataobj = da.DataAccess('Yahoo', cachestalltime=0)

    #Keys to be read from the data
    ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']

    #Read the data and map it to ls_keys via dict() (i.e. Hash Table structure)
    df_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
    d_data = dict(zip(ls_keys, ldf_data))

    ls_symbols.append("_CASH")
    trades = pd.Dataframe(index=list(ldt_timestamps[0]),columns=list(ls_symbols))
    current_cash = cash
    trades["_CASH"][ldt_timestamps[0]] = current_cash
    current_stocks = dict()
    for symb in ls_symbols:
        current_stocks[symb] = 0
        trades[symb][ldt_timestamps[0]] = 0

    for row in orders.iterrows():
        row_data = row[1]
        current_date = dt.datetime(row_data['X.1'],row_data['X.2'],row_data['X.3'],16)
        symb = row_data['X.4']
        stock_value = d_data['close'][symb][current_date]
        stock_amount = row_data['X.6']
        if row_data['X.5'] == "Buy":
            current_cash = current_cash - (stock_value*stock_amount)
            trades["_CASH"][current_date] = current_cash
            current_stocks[symb] = current_stocks[symb] + stock_amount
            trades[symb][current_date] = current_stocks[symb]
        else:
            current_cash = current_cash + (stock_value*stock_amount)
            trades["_CASH"][current_date] = current_cash
            current_stocks[symb] = current_stocks[symb] - stock_amount
            trades[symb][current_date] = current_stocks[symb]


    #trades.fillna(method='ffill',inplace=True)
    #trades.fillna(method='bfill',inplace=False)
    trades.fillna(0)
    #alt_cash = current_cash
    #alt_cash = trades.cumsum()
    value_data = pd.Dataframe(index=list(ldt_timestamps),columns=list("V"))
    value_data = value_data.fillna(0)
    value_data = value_data.cumsum(axis=0)
    for day in ldt_timestamps:
        value = 0
        for sym in ls_symbols:
            if sym == "_CASH":
                value = value + trades[sym][day]
            else:
                value = calue + trades[sym][day]*d_data['close'][sym][day]
        value_data["V"][day] = value

    fileout = open(values_file,"w")
    for row in value_data.iterrows():
        file_out.writelines(str(row[0].strftime('%Y,%m,%d')) + ", " + str(row[1]["V"].round()) + "\n" )

    fileout.close()



def main(argv):

    if len(sys.argv) != 3:
        print "Invalid arguments for marketsim.py. It should be of the following syntax: marketsim.py orders_file.csv values_file.csv"
        sys.exit(0)

    #initial_cash = int (sys.argv[1])
    initial_cash = 1000000
    ordersFile = str(sys.argv[1])
    valuesFile = str(sys.argv[2])

    marketsim(initial_cash,ordersFile,valuesFile)


if __name__ == "__main__":
    main(sys.argv[1:])

我给命令行的输入是:

python marketsim.py orders.csv values.csv

我猜问题可能在于进口或可能在主函数中(包括if def main(argv)

以下)

我必须指出,文件orders.csv和values.csv存在并位于同一文件夹中。

我希望一切都清楚。

所以,我期待着阅读你的答案社区伙伴! :d

谢谢!

0 个答案:

没有答案