I'm trying to make a menu GUI using pyqt5, the menu includes drinks and other stuff.
In front of each menu item there is a checkbox, when it is checked its price will be added to the bill.
self.latte_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget) #latte_box is the name of the checkbox
self.latte_box.setText("")
self.latte_box.setObjectName("latte_box")
self.horizontalLayout.addWidget(self.latte_box)
self.latte_box.stateChanged.connect(self.order)
self.cookie_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget_3)
self.cookie_box.setText("")
self.cookie_box.setObjectName("cookie_box")
self.horizontalLayout_3.addWidget(self.cookie_box)
self.cookie_box.stateChanged.connect(self.order)
bill = 0 #the bill variable
def order(self):
if self.latte_box.isChecked():
bill += 2.85
else:
bill -= 2.85
if self.cookie_box.isChecked():
bill += 1.50
else:
bill -= 1.50
latte_box and cookie_box are the checkboxes of 2 items on the list with the prices of $2.85 and $1.50.
So when the user check the box, the price of the item will be added to the bill, but in case of an error the user will just uncheck the box and the price of the item will be removed from the bill.
The problem here is that all the items run through the method (order), and whether the box is checked the price is added, and if its not checked the price is removed.
How can only the boxes that are checked or unchecked run through the method, and the untouched ones remain still.. ?
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.latte_box = QtWidgets.QCheckBox()
self.latte_box.setText("2.85")
self.latte_box.stateChanged.connect(lambda state, cb=self.latte_box: self.order(state, cb))
self.cookie_box = QtWidgets.QCheckBox()
self.cookie_box.setText("1.50")
self.cookie_box.stateChanged.connect(lambda state, cb=self.cookie_box: self.order(state, cb))
self.label = QLabel()
self.layout = QGridLayout(self)
self.layout.addWidget(self.latte_box, 0, 0)
self.layout.addWidget(self.cookie_box, 0, 1)
self.layout.addWidget(self.label, 1, 0)
self.bill = 0
def order(self, state, cb):
if cb is self.latte_box:
if state:
self.bill += 2.85
else:
self.bill -= 2.85
elif cb is self.cookie_box:
if state:
self.bill += 1.50
else:
self.bill -= 1.50
self.label.setText(f'{abs(self.bill):.2f}')
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
form = Window()
form.show()
sys.exit(app.exec_())
Related
I'm currently trying to put together some code that backtests a simple trading strategy involving sequencing through time series price data, incrementally fitting an ARIMA model, making future price predictions, and then either adding a share if the price is predicted to increase, or selling all accumulated shares if the price is predicted to go down. Currently, it's returning nan values for the projected returns from trades and appears to only be selling somehow.
I've attached my code below. There's just a few simple functions for calculating a sharpe ratio and then the main function for running backtests.
import yfinance as yf
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.arima.model import ARIMA
import numpy as np
import seaborn as sns
from tqdm import tqdm
import pandas as pd
from statsmodels.tools.sm_exceptions import ValueWarning, HessianInversionWarning, ConvergenceWarning
import warnings
#in practice do not supress these warnings, they carry important information about the status of your model
warnings.filterwarnings('ignore', category=ValueWarning)
warnings.filterwarnings('ignore', category=HessianInversionWarning)
warnings.filterwarnings('ignore', category=ConvergenceWarning)
tickerSymbol = 'SPY'
data = yf.Ticker(tickerSymbol)
prices = data.history(start='2017-01-01', end='2019-01-01').Close
returns = prices.pct_change().dropna()
def std_dev(data):
# Get number of observations
n = len(data)
# Calculate mean
mean = sum(data) / n
# Calculate deviations from the mean
deviations = sum([(x - mean)**2 for x in data])
# Calculate Variance & Standard Deviation
variance = deviations / (n - 1)
s = variance**(1/2)
return s
# Sharpe Ratio From Scratch
def sharpe_ratio(data, risk_free_rate=0):
# Calculate Average Daily Return
mean_daily_return = sum(data) / len(data)
print(f"mean daily return = {mean_daily_return}")
# Calculate Standard Deviation
s = std_dev(data)
# Calculate Daily Sharpe Ratio
daily_sharpe_ratio = (mean_daily_return - risk_free_rate) / s
# Annualize Daily Sharpe Ratio
sharpe_ratio = 252**(1/2) * daily_sharpe_ratio
return sharpe_ratio
def run_simulation(returns, prices, amt, order, thresh, verbose=True, plot=True):
if type(order) == float:
thresh = None
sum_list = []
events_list = []
sharpe_list = []
init_amt = amt
#go through dates
for date, r in tqdm (returns.iloc[14:].items(), total=len(returns.iloc[14:])):
#if you're currently holding the stock, sell it
#get data til just before current date
curr_data = returns[:date]
if type(order) == tuple:
try:
#fit model
model = ARIMA(curr_data, order=order).fit(maxiter=200)
#get forecast
pred = model.forecast()[0][0]
except:
pred = thresh - 1
#if you predict a high enough return and not holding, buy stock
if ((type(order) == float and np.random.random() < order)
or (type(order) == tuple and pred > thresh)):
buy_price = prices.loc[date]
events_list.append(('b', date))
int_buy_price = int(buy_price)
sum_list.append(int_buy_price)
if verbose:
print('Bought at $%s'%buy_price)
print('Predicted Return: %s'%round(pred,4))
print('Actual Return: %s'%(round(ret, 4)))
print('=======================================')
continue
#if you predict below the threshold return, sell the stock
if ((type(order) == float and np.random.random() < order)
or (type(order) == tuple and thresh > pred)
or (order == 'last' and curr_data[-1] > 0)):
sell_price = prices.loc[date]
total_return = len(sum_list) * sell_price
ret = (total_return-sum(sum_list))/sum(sum_list)
amt *= (1+ret)
events_list.append(('s', date, ret))
sharpe_list.append(ret)
sum_list.clear()
if verbose:
print('Sold at $%s'%sell_price)
print('Predicted Return: %s'%round(pred,4))
print('Actual Return: %s'%(round(ret, 4)))
print('=======================================')
if verbose:
sharpe = sharpe_ratio(sharpe_list, risk_free_rate=0)
print('Total Amount: $%s'%round(amt,2))
print(f"Sharpe Ratio: {sharpe}")
#graph
if plot:
plt.figure(figsize=(10,4))
plt.plot(prices[14:])
y_lims = (int(prices.min()*.95), int(prices.max()*1.05))
shaded_y_lims = int(prices.min()*.5), int(prices.max()*1.5)
for idx, event in enumerate(events_list):
plt.axvline(event[1], color='k', linestyle='--', alpha=0.4)
if event[0] == 's':
color = 'green' if event[2] > 0 else 'red'
plt.fill_betweenx(range(*shaded_y_lims),
event[1], events_list[idx-1][1], color=color, alpha=0.1)
tot_return = round(100*(amt / init_amt - 1), 2)
sharpe = sharpe_ratio(sharpe_list, risk_free_rate=0)
tot_return = str(tot_return) + '%'
plt.title("%s Price Data\nThresh=%s\nTotal Amt: $%s\nTotal Return: %s"%(tickerSymbol, thresh, round(amt,2), tot_return), fontsize=20)
plt.ylim(*y_lims)
plt.show()
print(sharpe)
return amt
# A model with a dth difference to fit and ARMA(p,q) model is called an ARIMA process
# of order (p,d,q). You can select p,d, and q with a wide range of methods,
# including AIC, BIC, and empirical autocorrelations (Petris, 2009).
for thresh in [0.001]:
run_simulation(returns, prices, 100000, (7,1,7), thresh, verbose=True)
I've discovered that it's failing to fit the ARIMA model for some reason. Not converging to a solution, I guess. I'm not sure why though because it was fitting it just fine when I was running a different strategy using the same data and order.
I'm learning Django and I'm trying to make a Cart, which the customer can get and item and add it in his/her order row and then the order will be submitted. so my teacher said use def initiate(customer), and I don't understand how to use it. Can someone please explain it to me? Thank you.
here is the code I'm working on it:
User = get_user_model()
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=Product, related_name="User")
phone = models.CharField(max_length=20)
address = models.TextField()
balance = models.IntegerField(default=20000)
def deposit(self, amount):
self.balance += amount
self.save()
def spend(self, amount):
if amount > self.balance:
raise ValueError
self.balance -= amount
self.save()
class OrderRow(models.Model):
product = models.ManyToManyField(Product)
order = models.ForeignKey('Order', on_delete=models.CASCADE)
amount = models.IntegerField()
class Order(models.Model):
# Status values. DO NOT EDIT
STATUS_SHOPPING = 1
STATUS_SUBMITTED = 2
STATUS_CANCELED = 3
STATUS_SENT = 4
customer = models.ForeignKey('Customer', on_delete=models.SET_NULL)
order_time = models.DateTimeField(auto_now=True)
total_price = Sum(F('amount') * F('product__price'))
status = models.IntegerField(choices=status_choices)
#staticmethod
def initiate(customer):
Order.initiate(User)
def add_product(self, product, amount):
Order.status = 1
OrderRow.product = Product.objects.get(id=product.id)
print(product.id)
if OrderRow.objects.filter(product=product).exists():
preexisting_order = OrderRow.objects.get(product=product, order=self)
preexisting_order.amount += 1
preexisting_order.save()
else:
new_order = OrderRow.objects.create(
product=product,
cart=self,
amount=1,
)
new_order.save()
You are probably supposed to create a new Order associated with this customer. Something along the following lines:
#classmethod
def initiate(cls, customer):
return cls.objects.create(customer=customer, status=cls.STATUS_SHOPPING)
There are some other issues with your code. You cannot use SET_NULL if the fk is not nullable:
customer = models.ForeignKey('Customer', on_delete=models.SET_NULL, null=true)
There should not be multiple products per row:
class OrderRow(models.Model):
product = models.ForeignKey(Product) # not many2many!
# ...
Also, your add_product needs quite some fixing:
def add_product(self, product, amount):
self.status = self.STATUS_SHOPPING # the instance is self + use your descriptive variables
print(product.id)
# filter only rows in the current order!
if self.orderrow_set.filter(product=product).exists():
# fix naming: this is a row, not an order
preexisting_order_row = self.orderrow_set.get(product=product)
preexisting_order_row.amount += amount # why +1, you are adding amount
preexisting_order_row.save()
else:
new_order_row = OrderRow.objects.create(
product=product,
order=self,
amount=amount,
) # create saves already
I don't understand how to recall the value from the entry grid, I got the array of the entries
import tkinter as tk
import sys
class Main:
# >>>>> Init <<<<<
def __init__(self, master):
self.master = master
self.entries = []
# >>>>> Menu <<<<<
def menu(self):
self.master.geometry("400x150")
self.master.title("Score Calculator")
main_label = tk.Label(self.master, text="Score program")
main_button1 = tk.Button(self.master, text="Enter score", command=self.judge_score)
main_button2 = tk.Button(self.master, text="Calculate results", command=self.calculate_results)
main_button3 = tk.Button(self.master, text="Save and Quit", command=self.save_results)
main_button4 = tk.Button(self.master, text="Show current results", command=self.show_result)
main_label.pack()
main_button1.pack()
main_button2.pack()
main_button3.pack()
main_button4.pack()
# >>>>> Score <<<<<
def score(self):
window = tk.Tk()
# >>> The 5*6 grid <<<
for x in range(6):
for i in range(5):
entry = tk.Entry(window, width=4).grid(column=i, row=x)
self.entries.append(entry)
So this part has the score grid that needs to be able to output a value when the value is typed in I will add the button for confirming results but for now I need to be able to recall
root = tk.Tk()
app = Main(root)
app.menu()
root.mainloop()
print(app.entries)
The following code works as intended:
class Item(object):
def __init__(self, unq_id, name, price, qty, measure):
self.unq_id = unq_id
self.product_name = name
self.price = price
self.qty = qty
self.measure = measure
class Cart(object):
def __init__(self):
self.content = dict()
def __format__(self, format_type):
if format_type == 'short':
return ', '.join(item.product_name for item in self.content.values())
elif format_type == 'long':
return '\n'.join(f'\t\t{item.qty:2} {item.measure:7} {item.product_name:12} # '
f'${item.price:1.2f} ... ${item.qty * item.price:1.2f}'
for item in self.content.values())
def add(self, item):
if item.unq_id not in self.content:
self.content.update({item.unq_id: item})
return
for k, v in self.content.get(item.unq_id).items():
if k == 'unq_id':
continue
elif k == 'qty':
total_qty = v.qty + item.qty
if total_qty:
v.qty = total_qty
continue
self.remove_item(k)
else:
v[k] = item[k]
def get_total(self):
return sum([v.price * v.qty for _, v in self.content.items()])
def get_num_items(self):
return sum([v.qty for _, v in self.content.items()])
def remove_item(self, key):
self.content.pop(key)
if __name__ == '__main__':
item1 = Item(1, "Cucumbers", 1., 1, 'kg')
item2 = Item(2, "Tissues", 2., 2, 'dozen')
item3 = Item(3, "Tomatoes", 3., 5, 'pound')
item4 = Item(4, "Toothpaste", 1., 5, 'box')
cart = Cart()
cart.add(item1)
cart.add(item2)
cart.add(item3)
cart.add(item4)
print("Your cart contains: {0:short}".format(cart))
# cart.remove_item(1)
print()
print("Your cart contains: \n {0:long}".format(cart))
print()
print("The total number of items in your cart is: ", cart.get_num_items())
print()
print("The total cost of the items in your cart is: ", cart.get_total())
print()
cart.remove_item(3)
print("Your cart contains: {0:short}".format(cart))
print()
print("Your cart contains: \n {0:long}".format(cart))
print()
print("The total number of items in your cart is: ", cart.get_num_items())
print()
print("The total cost of the items in your cart is: ", cart.get_total())
My question is that PyCharm is crying at me regarding this piece of code:
print("Your cart contains: \n {0:long}".format(cart))
PyCharm says I am using an "unsupported format character '|' " <-- Looks like a vertical bar inside of PyCharm. Since everything is working I am not sure what PyCharm is complaining about. I would like to know what PyCharm is objecting to.
Output from above code:
Your cart contains: Cucumbers, Tissues, Tomatoes, Toothpaste
Your cart contains:
1 kg Cucumbers # $1.00 ... $1.00
2 dozen Tissues # $2.00 ... $4.00
5 pound Tomatoes # $3.00 ... $15.00
5 box Toothpaste # $1.00 ... $5.00
The total number of items in your cart is: 13
The total cost of the items in your cart is: 25.0
Your cart contains: Cucumbers, Tissues, Toothpaste
Your cart contains:
1 kg Cucumbers # $1.00 ... $1.00
2 dozen Tissues # $2.00 ... $4.00
5 box Toothpaste # $1.00 ... $5.00
The total number of items in your cart is: 8
The total cost of the items in your cart is: 10.0
Process finished with exit code 0
That vertical bar is the lowercase letter L, which it found in the beginning of the format string long. It's complaining since PyCharm only recognises a certain subset of characters (namely https://docs.python.org/2/library/string.html#format-specification-mini-language), as it doesn't know to search for operator overloading of __format__.
Unfortunately, my best advice is to add a noinspection clause for PyCharm to pick up:
# noinspection PyStringFormat
print("Your cart contains: \n {0:long}".format(cart))
I'm building a web app with django. I use postgresql for the db. The app code is getting really messy(my begginer skills being a big factor) and slow, even when I run the app locally.
This is an excerpt of my models.py file:
REPEATS_CHOICES = (
(NEVER, 'Never'),
(DAILY, 'Daily'),
(WEEKLY, 'Weekly'),
(MONTHLY, 'Monthly'),
...some more...
)
class Transaction(models.Model):
name = models.CharField(max_length=30)
type = models.IntegerField(max_length=1, choices=TYPE_CHOICES) # 0 = 'Income' , 1 = 'Expense'
amount = models.DecimalField(max_digits=12, decimal_places=2)
date = models.DateField(default=date.today)
frequency = models.IntegerField(max_length=2, choices=REPEATS_CHOICES)
ends = models.DateField(blank=True, null=True)
active = models.BooleanField(default=True)
category = models.ForeignKey(Category, related_name='transactions', blank=True, null=True)
account = models.ForeignKey(Account, related_name='transactions')
The problem is with date, frequency and ends. With this info I can know all the dates in which transactions occurs and use it to fill a cashflow table. Doing things this way involves creating a lot of structures(dictionaries, lists and tuples) and iterating them a lot. Maybe there is a very simple way of solving this with the actual schema, but I couldn't realize how.
I think that the app would be easier to code if, at the creation of a transaction, I could save all the dates in the db. I don't know if it's possible or if it's a good idea.
I'm reading a book about google app engine and the datastore's multivalued properties. What do you think about this for solving my problem?.
Edit: I didn't know about the PickleField. I'm now reading about it, maybe I could use it to store all the transaction's datetime objects.
Edit2: This is an excerpt of my cashflow2 view(sorry for the horrible code):
def cashflow2(request, account_name="Initial"):
if account_name == "Initial":
uri = "/cashflow/new_account"
return HttpResponseRedirect(uri)
month_info = {}
cat_info = {}
m_y_list = [] # [(month,year),]
trans = []
min, max = [] , []
account = Account.objects.get(name=account_name, user=request.user)
categories = account.categories.all()
for year in range(2006,2017):
for month in range(1,13):
month_info[(month, year)] = [0, 0, 0]
for cat in categories:
cat_info[(cat, month, year)] = 0
previous_months = 1 # previous months from actual
next_months = 5
dates_list = month_year_list(previous_month, next_months) # Returns [(month,year)] from the requested range
m_y_list = [(date.month, date.year) for date in month_year_list(1,5)]
min, max = dates_list[0], dates_list[-1]
INCOME = 0
EXPENSE = 1
ONHAND = 2
transacs_in_dates = []
txs = account.transactions.order_by('date')
for tx in txs:
monthyear = ()
monthyear = (tx.date.month, tx.date.year)
if tx.frequency == 0:
if tx.type == 0:
month_info[monthyear][INCOME] += tx.amount
if tx.category:
cat_info[(tx.category, monthyear[0], monthyear[1])] += tx.amount
else:
month_info[monthyear][EXPENSE] += tx.amount
if tx.category:
cat_info[(tx.category, monthyear[0], monthyear[1])] += tx.amount
if monthyear in lista_m_a:
if tx not in transacs_in_dates:
transacs_in_dates.append(tx)
elif tx.frequency == 4: # frequency = 'Monthly'
months_dif = relativedelta.relativedelta(tx.ends, tx.date).months
if tx.ends.day < tx.date.day:
months_dif += 1
years_dif = relativedelta.relativedelta(tx.ends, tx.date).years
dif = months_dif + (years_dif*12)
dates_range = dif + 1
for i in range(dates_range):
dt = tx.date+relativedelta.relativedelta(months=+i)
if (dt.month, dt.year) in m_y_list:
if tx not in transacs_in_dates:
transacs_in_dates.append(tx)
if tx.type == 0:
month_info[(fch.month,fch.year)][INCOME] += tx.amount
if tx.category:
cat_info[(tx.category, fch.month, fch.year)] += tx.amount
else:
month_info[(fch.month,fch.year)][EXPENSE] += tx.amount
if tx.category:
cat_info[(tx.category, fch.month, fch.year)] += tx.amount
import operator
thelist = []
thelist = sorted((my + tuple(v) for my, v in month_info.iteritems()),
key = operator.itemgetter(1, 0))
thelistlist = []
for atuple in thelist:
thelistlist.append(list(atuple))
for i in range(len(thelistlist)):
if i != 0:
thelistlist[i][4] = thelistlist[i-1][2] - thelistlist[i-1][3] + thelistlist[i-1][4]
list = []
for el in thelistlist:
if (el[0],el[1]) in lista_m_a:
list.append(el)
transactions = account.transactions.all()
cats_in_dates_income = []
cats_in_dates_expense = []
for t in transacs_in_dates:
if t.category and t.type == 0:
if t.category not in cats_in_dates_income:
cats_in_dates_income.append(t.category)
elif t.category and t.type == 1:
if t.category not in cats_in_dates_expense:
cats_in_dates_expense.append(t.category)
cat_infos = []
for k, v in cat_info.items():
cat_infos.append((k[0], k[1], k[2], v))
Depends on how relevant App Engine is here. P.S. If you'd like to store pickled objects as well as JSON objects in the Google Datastore, check out these two code snippets:
http://kovshenin.com/archives/app-engine-json-objects-google-datastore/
http://kovshenin.com/archives/app-engine-python-objects-in-the-google-datastore/
Also note that the Google Datastore is a non-relational database, so you might have other trouble refactoring your code to switch to that.
Cheers and good luck!