-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction.py
179 lines (140 loc) · 7.44 KB
/
action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TadawulStocks v 0.1 - action.py
# Copyright (C) <2014> mad_dev(A'mmer Almadani)
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL A'MMER ALMADANI BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Report any issues with this script to <[email protected]>
from __future__ import absolute_import
import npyscreen
from market.TadawulStocks import *
from refresh import SetConfig, GetConfig, SetLast, Config
import re
__author__ = 'Amer Almadani'
__email__ = '[email protected]'
class TestApp(npyscreen.NPSAppManaged):
def main(self):
npyscreen.setTheme(npyscreen.Themes.TransparentThemeLightText)
buy_sell = npyscreen.Form(name = "Buy & Sell Stocks",)
pick = buy_sell.add(npyscreen.TitleFixedText, name = "Please choose an option",)
option= buy_sell.add(npyscreen.TitleSelectOne, max_height =-2, value = [1,], name="Option",
values = ["Buy","Sell"], scroll_exit=True)
buy_sell.edit()
if str(option.get_selected_objects()) == "['Buy']":
self.buy()
elif str(option.get_selected_objects()) == "['Sell']":
self.sell()
def buy(self):
stocks = GetList('All')
my_stocks = GetConfig('code')[0]
my_shares = GetConfig('shares')[1]
config = Config('Get')
buy_log = open('log/buy.log', 'a')
code_list = npyscreen.Form(name = "Buy Code Or List",)
pick = code_list.add(npyscreen.TitleFixedText, name = "Please choose an option",)
option = code_list.add(npyscreen.TitleSelectOne, max_height =-2, value = [1,], name="Option",
values = ["Tadawul Code","Choose From List"], scroll_exit=True)
code_list.edit()
if str(option.get_selected_objects()) == "['Choose From List']":
list_ = npyscreen.Form(name = "Buy - List",)
stock = list_.add(npyscreen.TitleSelectOne, max_height=30, value = [1,], name="Stocks", values = stocks, scroll_exit=True)
shares = list_.add(npyscreen.TitleText, name = "Shares:",)
price = list_.add(npyscreen.TitleText, name = "Price:",)
date = list_.add(npyscreen.TitleDateCombo, name = "Date:")
list_.edit()
code = str(stock.get_selected_objects())
code_re = re.findall('[0-9]{1,}', code)[0]
if re.findall('[\w]{1,}', code)[0] in config.sections():
section = re.findall('[\w]{1,}', code)[0]
aval = Config('Get').get(section, 'shares')
current_shares = int(aval) + int(shares.value)
SetLast('Shares', current_shares, section)
SetLast('Last_Buying_Price', price.value, section)
SetLast('Last_Purchase_Date', date.value, section)
else:
SetConfig(stock_name=GetData(code=code_re, sector=TadawulStocks('All'))[0],
code=code_re,
shares=shares.value,
last_buying_price=price.value,
last_purchase_date=date.value,
last_price=1)
stock_code = code_re
elif str(option.get_selected_objects()) == "['Tadawul Code']":
code = npyscreen.Form(name = "Buy - List",)
stock = code.add(npyscreen.TitleText, name = "Code:",)
shares = code.add(npyscreen.TitleText, name = "Shares:",)
price = code.add(npyscreen.TitleText, name = "Price:",)
date = code.add(npyscreen.TitleDateCombo, name = "Date:",)
code.edit()
if re.findall('[\w]{1,}', GetData(code=stock.value, sector=TadawulStocks('All'))[0])[0] in config.sections():
section = re.findall('[\w]{1,}', GetData(code=stock.value, sector=TadawulStocks('All'))[0])[0]
aval = Config('Get').get(section, 'shares')
current_shares = int(aval) + int(shares.value)
SetLast('Shares', current_shares, section)
SetLast('Last_Buying_Price', price.value, section)
SetLast('Last_Purchase_Date', date.value, section)
else:
SetConfig(stock_name=GetData(code=stock.value, sector=TadawulStocks('All'))[0],
code=stock.value,
shares=shares.value,
last_buying_price=price.value,
last_purchase_date=date.value,
last_price=1)
stock_code = stock.value
total = float(shares.value) * float(price.value)
string = "%s - You Bought %s shares of %s for %s SAR per share. %s SAR in total." %(date.value, shares.value, GetData(code=stock_code, sector=TadawulStocks('All'))[0], price.value, str(total))
buy_log.write(string+'\n')
buy_log.close()
def sell(self):
my_stocks = GetConfig('code')[0]
my_shares = GetConfig('shares')[1]
sell_log = open('log/sell.log', 'a')
# stock_shares = []
# for stocks, share in zip(my_stocks, my_shares):
# stock_shares.append(stocks + ' - Shares = ' +share)
sell = npyscreen.Form(name = "Sell",)
pick = sell.add(npyscreen.TitleFixedText, name = "Select a stock to sell",)
option = sell.add(npyscreen.TitleSelectOne, max_height =5, value = [1,], name="My Stocks",
values = my_stocks, scroll_exit=True)
shares = sell.add(npyscreen.TitleText, name = "Shares:", rely=9)
price = sell.add(npyscreen.TitleText, name = "Price:",)
date = sell.add(npyscreen.TitleDateCombo, name = "Date:",)
sell.edit()
selected_stock = str(option.get_selected_objects())
stock = re.findall('[\w]{1,}', selected_stock)
for i in stock:
try:
aval = Config('Get').get(i, 'shares')
current_shares = int(aval) - int(shares.value)
except:
current_shares = int(aval) - int(0)
#self.Notify('No value was set')
SetLast('Shares', current_shares, i)
total = float(shares.value) * float(price.value)
string = '%s - You Sold %s shares of %s for %s SAR. You made %s SAR' %(date.value, shares.value, i, price.value, total)
sell_log.write(string+'\n')
sell_log.close()
def Notify(self, msg):
npyscreen.notify_confirm(msg, title='Error')
if __name__ == "__main__":
#npyscreen.setTheme(npyscreen.Themes.ColorfulTheme)
App = TestApp()
while 1:
try:
App.run()
except KeyboardInterrupt:
break
print("Goodbye")