# You must import sys to be able to convert the arguments # tickers must be specified as below as must the frequency (in seconds) # Everything is case sensitive import sys tickers=['IBM','CSCO','MSFT'] frequency=30 # The next part converts the arguments into variables that are easier to use # the arguments are passed as follows: # argv[0] is the name of your python file # argv[1] is the number of arguments per ticker, as explained below # then comes the data for the first ticker: bid, ask, last, q0,q2,..., # In the current implementation, # bid=latest bid # ask=latest ask # last=last traded price # q0=cash quantity # q1=margin quantity # q2=short quantity # q3=cash available (in the currency that the ticker trades in) # q4=VWAP of your position # q5=transaction cost per trade # note that there are 7 elements of the quantity, numbered 0 to 6 # this is followed by the data for the second ticker # the next part converts the arguments into last prices and position data by ticker # the first is the number of arguments per ticker nArg=int(sys.argv[1]) # in the future, we may pass more arguments per ticker; that is why we made this variable i=1 bid=dict() ask=dict() last=dict() qty=dict() for tkr in tickers: i=i+1 bid[tkr]=float(sys.argv[i]) i=i+1 ask[tkr]=float(sys.argv[i]) i=i+1 last[tkr]=float(sys.argv[i]) qty[tkr]=list() for j in range(nArg): i=i+1 qty[tkr].append(float(sys.argv[i])) # the following 'constant' declarations refer to the different parts of qty for nArgs=7 (the current implementation) cCashQty=0 cMarginQty=1 cShortQty=2 cCashAvailable=3 cCVAP=4 cTransCost=5 # the trades are returned in a dictionary called returnstring. returnString=dict() # each element of returnString is a key-value pair of the format returnString[ticker]=action/qty # action is one of: cashbuy cashsell marginbuy marginsell shortsale shortcover # qty is a number. # Example: returnString['IBM']="cashbuy/10" means exactly what it says: buy 10 shares for cash. # any order that is valid will be executed. # So if you have cashbuy orders for multiple securities, they will all be executed at once. # This allows you to trade baskets of securities. # You can develop any trading strategy you like. Your Python script can download data, # and use any algorithm you like. # IMPORTANT NOTE: if you use Python packages, make sure that your Windows PATH is set correctly. # See https://docs.python.org/3/using/windows.html for help on installing Python correctly and setting the PATH. # You can test your script without trading from the FTS Real Time Client. # the next part shows how to create a constant mix strategy and return the trades you want to execute. # It invests $100000 in each of the three tickers # It does not check that you have enough cash available for the trades, you can add features like that if you like. dollarAmount=100000.0 nTrade=0 for tkr in tickers: if last[tkr]>0: if qty[tkr][cCashQty]*last[tkr] < dollarAmount: nTrade=dollarAmount / last[tkr] - qty[tkr][cCashQty] returnString[tkr]='cashbuy/' + str(int(nTrade)) elif qty[tkr][cCashQty]*last[tkr] > dollarAmount: nTrade= qty[tkr][cCashQty] - dollarAmount / last[tkr] returnString[tkr]='cashsell/' + str(int(nTrade)) else: returnString[tkr]='blank' # the next statement writes out the result and is read by the FTS Real Time Client and trades are executed print(returnString); # this should only be the ONLY print statement