class AccountMetrics:
 
    def __init__(self,con):
        self.account_manager=AccountManager(con)
        self.security_data=SecurityData(con)

    def compare_vs_sharpe(self, acct1, acct2, acct2_vals): #acct1=S&P500 (unless comparing against S&P 500, where acct1=4% fixed rate); acct2=strategy
        acct1_returns = self.all_returns(acct1)
        acct2_returns = self.all_returns(acct2)
        acct1_total = sum(acct1_returns)
        acct2_total = sum(acct2_returns)
        stdev_val = stdev(acct2_vals)
        sharpe = (acct2_total - acct1_total) / stdev_val
        return sharpe
 
    def month_returns(self,account_id,as_of_date):
        my_stocks=self.account_manager.get_holdings(account_id,as_of_date)
        stock_ids=[ h[0] for h in my_stocks ]
        stock_returns=self.security_data.get_returns(stock_ids,as_of_date)
        my_value=0
        my_base=0
        for i in range(len(my_stocks)):
            if stock_returns[i] is not None:
                my_value=my_value+my_stocks[i][1]*(1+stock_returns[i])
            else:
                my_value=my_value+my_stocks[i][1]
            my_base=my_base+my_stocks[i][1]
        return (my_value - my_base)        
 
    def all_returns(self,account_id):
        dates=self.security_data.get_dates()
        annual=[]
        for d in dates:
            month=self.month_returns(account_id,d)
            annual.append(month)
        return annual

    def show_stats(self,vals):
        mean_val=mean(vals);
        variance_val=variance(vals)
        stdev_val=stdev(vals)
        mdd_val=mdd(vals)
        print("Average Return: %s" %mean_val)
        #print("The variance: %s" %variance_val)
        #print("The stdev: %s" %stdev_val)
        print("Maximum drawdown: %s" %mdd_val)
import math
from pprint import pprint
import random
from security_data import SecurityData
from account_manager import AccountManager
from account_metrics import AccountMetrics

'''
This file sets up a new account for the following trading strategy:
Invest a fixed amount (e.g. 100) of fund into each security where eq>75, arm>90, and rv>50. Only hold those stocks.
At the end of each month, replace the stocks in the account by investing a fixed amount (e.g., 100) of fund into new stocks that have the above signals.
'''

if __name__=='__main__':
    con = sqlite3.connect('qse_test.db')
    security_data = SecurityData(con)
    account_manager = AccountManager(con)
    account_metrics = AccountMetrics(con)
    dates = security_data.get_dates()
    new_id=account_manager.new_account()
    #print new_id
    rows=[]

    def signal_selection(eq,arm,rv):
        if eq>75 and arm>90 and rv>50:
            return True
        else:
            return False

    for d in dates:
        signals = security_data.get_security_signals(d,
'''
This file sets up a new account for the following trading strategy:
Invest 100 each month in the sp500 except during summer month. Simulates how fund managers may not want 
to invest investing in any stocks in the summer months of May, June, July, and August, so they only 
resume investing in stocks in September.

If there are gains for a stock in a month, sell the gain portion to bring the investment in the stock 
back to the fixed amount for the next month.
 
If there are losses for a stock in a month, add fund for the loss portion to buy the stock to bring 
the investment to the stock back to the fixed amount for the next month.
'''

if __name__=='__main__':
    con = sqlite3.connect('qse_test.db')
    security_data = SecurityData(con)
    account_manager = AccountManager(con)
    account_metrics = AccountMetrics(con)
    dates = security_data.get_dates()
    new_id=account_manager.new_account()
    #print new_id
    rows=[]

    for d in dates:
        summer_months = ["2008-05-31", "2008-06-30", "2008-07-31", "2008-08-31"]
        if d in summer_months:
            continue
        print d
        sp500_ids=security_data.get_sp500(d)
        for id in sp500_ids:
            row=[new_id, id, 100, d]
Electronic Equipment & Instruments
 
If there are gains for a stock in a month, sell the gain portion to bring the investment in the stock 
back to the fixed amount for the next month.
 
If there are losses for a stock in a month, add fund for the loss portion to buy the stock to bring 
the investment to the stock back to the fixed amount for the next month.
'''
 
if __name__=='__main__':
 
    industry_name="Electronic Equipment & Instruments"
    industry_code="452030"
 
    con = sqlite3.connect('qse_test.db')
    security_data = SecurityData(con)
    account_manager = AccountManager(con)
    account_metrics = AccountMetrics(con)
    dates = security_data.get_dates()
    stock_ids=security_data.get_security()
 
    stock_info=security_data.get_company_info(stock_ids, ['ticker', 'company_name', 'gics_code', 'gics_name'])
 
    new_id=account_manager.new_account()
    #print new_id
    industry_ids=[]
    for i in range(len(stock_ids)):
        if stock_info[i][2]==industry_code:
            industry_ids.append(stock_ids[i])
    if len(industry_ids)<1:
        print("The database contains no stocks in the sector: %s" %industry_name)
 def __init__(self,con):
     self.account_manager=AccountManager(con)
     self.security_data=SecurityData(con)
from security_data import SecurityData
from account_manager import AccountManager
from account_metrics import AccountMetrics
 
'''
This file sets up a new account for the following trading strategy:
Invest a fixed amount (e.g. 100) of fund into 500 randomly chosen securities.
 
At the end of each month, replace the entire holdings in the account by investing the fixed 
amount of fund to a new set of 500 randomly selected stocks.
 
'''
 
if __name__=='__main__':
    con = sqlite3.connect('qse_test.db')
    security_data = SecurityData(con)
    account_manager = AccountManager(con)
    account_metrics = AccountMetrics(con)
    dates = security_data.get_dates()
    stock_ids=security_data.get_security()
    
    if len(stock_ids)<=500:
        print "There are insufficient number of securities in the database for this trading strategy."
        exit(1)
 
    new_id=account_manager.new_account()
    #print new_id
 
    rows=[]
 
    for d in dates: