S15 Bull Test
S15 Bull Test
"""
Modified with Buying Power based on Available Fund on 4/26/2017, which is 10 times
Printing Context
Execute NEW trade at 10:00 AM Daily, Assign Stop Loss at 10:05 AM , Check Log file at 10:10 AM
"""
# Global data lists used for various purposes within the algorithm are below
Global_Stock_List = []
def initialize(context):
#####################################################################
set_slippage(slippage.FixedSlippage(spread=0.00))
set_commission(commission.PerShare(cost=0.00, min_trade_cost=0.0))
fetch_csv(https://docs.google.com/spreadsheets/d/e/2PACX-1vS3fgFw3AoYVqLdLG3NFLhAUBqD0JyDLLIgSsmLlmKZ4cw46qhMTGKHaBdd-w8t2bEnxT-
9AS3wawsT/pub?gid=0&single=true&output=csv',
date_column = 'Date',
date_format = '%m/%d/%y')
schedule_function(execute_trade,
date_rules.every_day(),
time_rules.market_open(hours=Hours_After_Open, minutes=Minutes_After_Open))
# The function below schedules the ordering of the initial stop loss orders
schedule_function(order_initial_stops,
date_rules.every_day(),
3
time_rules.market_open(hours=Hours_After_Open, minutes=Minutes_After_Open+5))
schedule_function(log_new_stocks,
date_rules.every_day(),
time_rules.market_open(hours=0, minutes=40))
schedule_function(refresh_stops,
date_rules.every_day(),
time_rules.market_open(hours=0, minutes=2))
Step_1_Percent = context.Step_1_Percent
Step_1_New_Stop = context.Step_1_New_Stop
Step_2_Percent = context.Step_2_Percent
Step_2_New_Stop = context.Step_2_New_Stop
Step_3_Percent = context.Step_3_Percent
Step_3_New_Stop = context.Step_3_New_Stop
Step_4_Percent = context.Step_4_Percent
Step_4_New_Stop = context.Step_4_New_Stop
Step_5_Percent = context.Step_5_Percent
Step_5_New_Stop = context.Step_5_New_Stop
Step_Dictionary = {} # Create empty dictionary for assessing the step up of each stock
orders = get_open_orders(stock) # determines if each stock in the portfolio has an open order
if Current_Price >= (Cost_Basis + Cost_Basis*Step_1_Percent) and Current_Price < (Cost_Basis + Cost_Basis*Step_2_Percent): # measure for step 1 increase
4
elif Current_Price >= (Cost_Basis + Cost_Basis*Step_2_Percent) and Current_Price < (Cost_Basis + Cost_Basis*Step_3_Percent): # measure for step 2 increase
elif Current_Price >= (Cost_Basis + Cost_Basis*Step_3_Percent) and Current_Price < (Cost_Basis + Cost_Basis*Step_4_Percent): # measure for step 3 increase
else: # if no step up
for stock, value in Step_Dictionary.items(): # Iterate through stock and values in step dictionary
if Shares_Owned > 0: # second filter to ensure stocks are owned and positive
if value == 0: # if no step up
pass # do nothing
Daily_Volume_Threshold = context.Daily_Volume_Threshold
Minute_Volume_Threshold = context.Minute_Volume_Threshold
Buying_Power = context.account.available_funds*10
Min_Price_Threshold = context.Min_Price_Threshold
Max_Price_Threshold = context.Max_Price_Threshold
############################################################################
# Determine how much cash is available for trading before making decisions
#Starting_Cash = context.portfolio.cash
Starting_Cash = context.account.available_funds+Buying_Power
#Starting_Cash = context.portfolio.cash
#print Starting_Cash
print Starting_Cash
Available_Funds = context.account.available_funds
6
print "Available_Funds"
print Available_Funds
Buying_Power = context.account.buying_power
print Buying_Power
Cushion = context.account.cushion
print "Cushion"
print Cushion
#Credit_Utilized = context.portfolio.positions_value
Share_Quantity[stock] = data.current(stock, 'Shares') # pull stock specific shares from csv and append to the Share Quantity Dictionary
Share_Priority[stock] = data.current(stock, 'Sort') # Append share priority to the Share Priority Dictionary.
Daily_Quantity = len(Share_Priority)
#print len(Share_Priority)
#print len(Sorted_Priority)
shares = int(Share_Quantity[stock])
Daily_Volume = data.history(stock, 'volume', 2, '1d') # pull prior day volume for each stock.
print "Stock"
print stock
print Daily_Volume[-1]
print Daily_Volume[0]
Minute_Volume = data.history(stock, 'volume', 2, '1m') # pull prior minute volume for each stock.
print "Stock"
print stock
print Minute_Volume[0]
print Minute_Volume[-1]
Last_Price = data.history(stock,'price', 15,'1m')[0] # pull last price 15 minutes prior for each stock
log.info(Last_Price)
print Last_Price
log.info(Price)
print Price
# Check that the stock to trade meets the price and volume requirements
if Daily_Volume[-2] >= Daily_Volume_Threshold and Minute_Volume[0] >= Minute_Volume_Threshold and Price >= Min_Price_Threshold and Price <=
Max_Price_Threshold:
Total_Cost = Price * shares # Estimate the cash needed for the pending trade
#print Total_Cost
Cash_Used.append(Total_Cost) # append daily cash used list for measuring cumulative cash
#print np.sum(Cash_Used)
elif Starting_Cash - np.sum(Cash_Used) < 0: # If not enough cash for full trade, follow steps below
pass
else:
Partial_Flag = Partial_Flag + 1 # Set the flag to not buy for rest of day
Step_1_Percent = context.Step_1_Percent
def log_new_stocks(context, data): # Function for logging new stocks each day
Global_Stock_List.append(stock) # Append global stock list with all new and prior stocks
print "Global_Stock_List"
print Global_Stock_List
print "Context"
print context,data