Skip to content

Instantly share code, notes, and snippets.

from myapp.models.musicians import Musician
from myapp.models.albums import Album
@mmourafiq
mmourafiq / gist:4198649
Created December 3, 2012 22:18
example yahoo finance
yf = YahooFinance('GOOG,MSFT')
print yf.get_symbols()
>> GOOG+MSFT
yf.add_symbol('AAPL')
print yf.get_symbols()
>> GOOG+AAPL+MSFT
yf.remove_symbol('GOOG')
print yf.get_symbols()
>> AAPL+MSFT
print yf.get_ask()
@mmourafiq
mmourafiq / AAPL_sharp_ratio.py
Created December 19, 2012 23:55
Calculating the sharp ratio for AAPL
import scipy
from yahoo_finance import YahooFinance
VALUE_TYPE = {'Date': 0, 'Open': 1, 'High': 2, 'Low': 3, 'Close': 4, 'Volume': 5, 'Adj_Close': 6}
start_date = "20121101"
end_date = "20121201"
yf = YahooFinance("AAPL")
for (symb, data) in yf.get_historical_prices(start_date, end_date): #get historical data from yahoo finance
@mmourafiq
mmourafiq / portfolio_sharp_ratio.py
Created December 20, 2012 01:29
portfolio sharp ratio
import scipy
from yahoo_finance import YahooFinance
VALUE_TYPE = {'Date': 0, 'Open': 1, 'High': 2, 'Low': 3, 'Close': 4, 'Volume': 5, 'Adj_Close': 6}
start_date = "20121101"
end_date = "20121201"
yf = YahooFinance("AAPL,GOOG,GLD,USO")
percent = scipy.array([0.25, 0.15, 0.4, 0.2]) #investment distribution portfolio
def brute_force(self):
"""
check all possibilities:
1) best solution for combination of 2 stories (if it exists).
2) best solution for combination of 3 stories (if it exists).
.
.
l-1) best solution for combination of l-1 stories (if it exists).
l : being the length of the current stories.
"""
def annealing_simulated(self, T=1000.0,cool=0.35):
"""
perform the annealing simulated algorithm:
1) start with a random solution.
2) move to a neighbour solution.
(favors better solutions, and accepts worst solutions with a certain probabilities
to avoid local minimum until the temperature is totally down)
"""
#order stories based on their proportioned score
ordered_stories = sorted(self._stories, reverse=True)
# -*- coding: utf-8 -*-
'''
Created on Jan 11, 2013
@author: Mourad Mourafiq
About: This is an attempt to solve the Quora challenge Feed Optimizer.
'''
import itertools
import copy
class Story(object):
"""
Story object
@type _cpt: int
@param _cpt: counts the number of instance created.
@type _height: int
@param _height: The stroy's height.
class Solution(object):
"""
Potential solution for the upcoming reload
@type _stories: list
@param _stories: The list of potential items.
@type _len_stories : int
@param _len_stories: The length of the list of stories.
@mmourafiq
mmourafiq / topic.py
Last active December 11, 2015 23:09
class Topic(object):
"""
Topic
@type _id: int
@param _id: the id of the topic
@type _x: float
@param _x: the x coordinate in the plane