-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.py
122 lines (91 loc) · 4.32 KB
/
gpt.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
import datetime
import dateutil.relativedelta
import MetaTrader5 as mt5
from datetime import datetime, timedelta
import pandas as pd
import time
import re
import numpy as np
mt5.initialize()
mt5.login(#YOUR LOGIN
)
def order_information(index, instruction, hint):
symbols = ["PLTR", "INTC", "PLUG", "RKLB", "PCG", "UPWK", "SNAP", "LYFT", "STNE", "SOFI", "HOOD", "QS"]
dict = {'PLTR': ['PLTR', 'STK', 'USD', 'SMART', 'NYSE'], 'INTC': ['INTC', 'STK', 'USD', 'SMART', 'NASDAQ'], 'PLUG': ['PLUG', 'STK', 'USD', 'SMART', 'NASDAQ'], 'RKLB': ['RKLB', 'STK', 'USD', 'SMART', 'NASDAQ'], 'PCG': ['PCG', 'STK', 'USD', 'SMART', 'NYSE'], 'UPWK': ['UPWK', 'STK', 'USD', 'SMART', 'NASDAQ'], 'SNAP': ['SNAP', 'STK', 'USD', 'SMART', 'NYSE'], 'LYFT': ['LYFT', 'STK', 'USD', 'SMART', 'NASDAQ'], 'STNE': ['STNE', 'STK', 'USD', 'SMART', 'NASDAQ'], 'SOFI': ['SOFI', 'STK', 'USD', 'SMART', 'NASDAQ'], 'HOOD': ['HOOD', 'STK', 'USD', 'SMART', 'NASDAQ'], 'QS': ['QS', 'STK', 'USD', 'SMART', 'NYSE']}
symbol = symbols[index]
scrape_type = dict[symbol][4]
if scrape_type == "NASDAQ":
scrape_symbol = symbol + ".NAS"
else:
scrape_symbol = symbol + ".NYSE"
current_share = get_current_shares(symbol)
if instruction != "NOTHING":
timeframe = mt5.TIMEFRAME_H1
date_from = datetime.now() - timedelta(days=10)
date_to = datetime.now() + timedelta(days=1)
prices = pd.DataFrame(mt5.copy_rates_range(scrape_symbol, timeframe, date_from, date_to))
prices["time"] = pd.to_datetime(prices['time'], unit = 's')
final_price = prices.iloc[-1]["close"]
string_inst, number_inst = extract_string_and_number(instruction)
owned_quantity = 0
quantity = 0
my_restriction = 100
string_inst2 = None
if current_share == 0:
restriction = (number_inst/100) * my_restriction
quantity = int(restriction // final_price)
if current_share != 0 and hint[1] != 2:
owned_quantity = current_share
multiplier = number_inst/100
quantity = int(np.floor(multiplier * owned_quantity))
if current_share != 0 and hint[1] == 2:
owned_quantity = current_share
if hint[0] == "Strong Long" or hint[0] == "Long" or hint[0] == "LTP1":
string_inst2 = "SELL"
else:
string_inst2 = "BUY"
restriction = (number_inst/100) * my_restriction
quantity = int(restriction // final_price)
counter = read_counter()
quantity_i_have = owned_quantity
quantity_for_share = quantity
order_instruction_first = string_inst2
order_instruction_second = string_inst
orderId = counter
else:
return None, None, None, None, None, None
return dict[symbol], quantity_i_have, quantity_for_share, order_instruction_first, order_instruction_second, orderId
def get_current_shares(symbol):
data = pd.read_csv("possession.csv")
row = data[data['symbol'] == symbol]
if not row.empty:
return row['current_shares'].values[0]
else:
return None
def read_counter():
try:
with open("counter.txt", "r") as file:
counter_value = int(file.read().strip())
return counter_value
except FileNotFoundError:
# If the file doesn't exist, return 0 or handle the exception as needed
return 0
def extract_string_and_number(input_string):
# Define the regular expression pattern to match alphabetic characters and digits
pattern = r'([a-zA-Z]+)(\d+)'
# Use re.match to find the pattern in the input string
match = re.match(pattern, input_string)
# Check if a match is found
if match:
# Extract the string and number parts from the match
extracted_string = match.group(1)
extracted_number = int(match.group(2))
return extracted_string, extracted_number
else:
# If no match is found, return None for both parts
return None, None
my_dict, quantity_i_have, quantity_for_share, order_instruction_first, order_instruction_second, orderId = order_information(2, 'BUY75', ['Short', 2])
if my_dict != None:
if order_instruction_first != None:
print(order_instruction_first, quantity_i_have)
print(order_instruction_second, quantity_for_share)