oversite_scrye/scrye/controller2_back2.py
2023-07-08 10:09:12 -07:00

317 lines
12 KiB
Python

import sys
import math
from pywinauto import application
from pywinauto import mouse
from pywinauto.keyboard import send_keys
import time
import cv2
import numpy as np
from scipy.signal import argrelextrema
import requests
sys.stdout = open('output.txt', 'w')
import sqlite3
con = sqlite3.connect("oversite.db", isolation_level=None)
cur = con.cursor()
hist ={}
buy_reset = { "w":0, "lb": 0, "b":0, "g": 0, "br": 0}
sell_reset = { "r":0, "p": 0, "l": 0, "t": 0, "th":0}
#cl = { 'buy': [ { "w":1, "lb": 1, "b":1, "g": 0, "br": 0}, { "w":1, "lb": 1, "b":1, "g": 1, "br": 0}, { "w":1, "lb": 1, "b":1, "g": 0, "br": 1} ], "sell": [ { "r":1, "p": 1, "l": 1, "t": 0, "th":0}, { "r":1, "p": 1, "l": 1, "t": 1, "th":0}, { "r":1, "p": 1, "l": 1, "t": 1, "th":1} , {"r": 1, "p":1, "l": 1, "t":0, "th":1}] }
cl = { 'buy': [ { "w":1, "lb": 1, "b":1 }, { "w":1, "lb": 1, "b":1, "g": 1}, { "w":1, "lb": 1, "b":1, "br": 1} ], "sell": [ { "r":1, "p": 1, "l": 1}, { "r":1, "p": 1, "l": 1, "t": 1 }, { "r":1, "p": 1, "l": 1, "t": 1, "th":1} , {"r": 1, "p":1, "l": 1, "th":1}] }
def ptlist(mask,tc):
intersection_points = np.where(mask> 0)
pts_list = [[c, r] for r, c in zip(*intersection_points)]
blue = {}
for r, c in zip(*intersection_points):
if c in blue:
continue
#blue[c] = {f"r{r}": tc}
blue[c] = {r: tc}
print(f"{tc}")
print(blue)
print(f"{tc}")
return(blue, pts_list)
LIME = [( 0,255,0), (0,255,0)]
BLACK = [(42,42,165),(42,42,165)]
BLUE = [(255,0,0),(255,0,0)]
RED = [(0,0,255),(0,0,255)]
GREEN = [(35,142,107),(35,142,107)]
WHITE = [(32,165,218),(32,165,218)]
TAN = [(179,222,245),(179,222,245)]
LBLUE = [(255,191,0 ), (255, 191,0 )]
PINK = [(226,43,138),(226, 43, 138)]
THISTLE =[(216,191,216), (216,191,216)]
app = application.Application(backend="uia").connect(path=r"C:\Program Files (x86)\Traders Way MetaTrader 4\terminal.exe")
print("started")
state = {}
from pywinauto.timings import Timings
from pywinauto.timings import wait_until
print("getting")
Timings.slow()
x = app.window().children()
WS = ""
for i in x:
if 'Workspace' in i.texts():
WS = i
while 1 > 0:
if 1> 0:
for j in WS.children():
sys.stdout.write(f"\t{j}" + '\n')
print(f"\t{j}")
n = j.texts()[0].split(",")
instr = f"{n[0][0]}{n[0][1]}{n[0][2]}_{n[0][3]}{n[0][4]}{n[0][5]}"
j.set_focus()
j.maximize()
if n[0] not in hist:
hist[n[0]] = ""
state["b"] = buy_reset.copy()
state["s"] = sell_reset.copy()
time.sleep(2)
print(n)
sys.stdout.write(f"n" + '\n')
j.capture_as_image().save(f"pair_{n[0]}.png")
img = cv2.imread(f"pair_{n[0]}.png")
ih, iw, ia = img.shape
img4 = cv2.imread(f"pair_{n[0]}.png")
print("plines")
black_mask = cv2.inRange(img, BLACK[0], BLACK[1])
thistle_mask = cv2.inRange(img, THISTLE[0], THISTLE[1])
green_mask = cv2.inRange(img, GREEN[0], GREEN[1])
blue_mask = cv2.inRange(img, BLUE[0], BLUE[1])
lime_mask = cv2.inRange(img, LIME[0], LIME[1])
red_mask = cv2.inRange(img, RED[0], RED[1])
white_mask = cv2.inRange(img, WHITE[0], WHITE[1])
tan_mask = cv2.inRange(img, TAN[0], TAN[1])
lblue_mask = cv2.inRange(img, LBLUE[0], LBLUE[1])
pink_mask = cv2.inRange(img, PINK[0], PINK[1])
# Adjust according to your adjacency requirement.
kernel = np.ones((3, 3), dtype=np.uint8)
# Dilating masks to expand boundary.
black_mask = cv2.dilate(black_mask, kernel, iterations=1)
green_mask = cv2.dilate(green_mask, kernel, iterations=1)
blue_mask = cv2.dilate(blue_mask, kernel, iterations=1)
lime_mask = cv2.dilate(lime_mask, kernel, iterations=1)
red_mask = cv2.dilate(red_mask, kernel, iterations=1)
white_mask = cv2.dilate(white_mask, kernel, iterations=1)
tan_mask = cv2.dilate(tan_mask, kernel, iterations=1)
pink_mask = cv2.dilate(pink_mask, kernel, iterations=1)
lblue_mask = cv2.dilate(lblue_mask, kernel, iterations=1)
thistle_mask = cv2.dilate(thistle_mask, kernel, iterations=1)
cv2.imwrite(f"pair_{n[0]}_red.png", red_mask)
combo_mask = cv2.add(red_mask, white_mask)
cv2.imwrite(f"pair_{n[0]}_combo.png", combo_mask)
cv2.imwrite(f"pair_{n[0]}_white.png", white_mask)
cv2.imwrite(f"pair_{n[0]}_blue.png", blue_mask)
cv2.imwrite(f"pair_{n[0]}_green.png", green_mask)
cv2.imwrite(f"pair_{n[0]}_black.png", black_mask)
cv2.imwrite(f"pair_{n[0]}_lime.png", lime_mask)
cv2.imwrite(f"pair_{n[0]}_tan.png", tan_mask)
cv2.imwrite(f"pair_{n[0]}_lblue.png", lblue_mask)
cv2.imwrite(f"pair_{n[0]}_pink.png", pink_mask)
cv2.imwrite(f"pair_{n[0]}_thistle.png", thistle_mask)
start = 10000000
end = 0
blue, blue_full = ptlist(blue_mask,"blue")
tan, tan_full = ptlist(tan_mask,"tan")
lime, lime_full = ptlist(lime_mask,"lime")
lblue, lblue_full = ptlist(lblue_mask,"lblue")
green, green_full = ptlist(green_mask,"green")
black, black_full = ptlist(black_mask,"black")
pink, pink_full = ptlist(pink_mask,"pink")
white, white_full = ptlist(white_mask,"white")
red, red_full = ptlist(red_mask,"red")
thistle, thistle_full = ptlist(thistle_mask,"thistle")
combo, combo_full = ptlist(combo_mask,"combo")
#print(combo_full)
t = {}
t2 = {}
for lst in [blue,lime,lblue,green,black,pink,red,white,tan, thistle]:
for i in lst.keys():
if i not in t:
t[i] = {}
t[i][int(f"{list(lst[i])[0]}")] = lst[i][list(lst[i])[0]]
amin = ""
dir = ""
prev = []
lastx = ""
#print(t)
#tsort = [int(ii) for ii in list(t)]
tsort = []
sv = ""
rt = ""
pv = ""
cv = 0
ev = ""
cp = {}
for ii in list(t):
ii = int(ii)
for tcl in t[ii].values():
cp[tcl] = 1
tsort.append(ii)
if sv == "":
sv= ii
pv= ii
cv += 1
rt = ii
else:
if ii == pv + 1:
pv = ii
cv +=1
rt += ii
else:
#print(f"{rt} {cv}")
if cv != 0:
fv = int(math.floor(rt/cv))
cv = int(math.ceil(rt/cv))
#tsort.append(cv)
#tsort.append(fv)
#t2[cv] = cp.keys()
cv2.putText(img4, ".", (cv,800), cv2.FONT_HERSHEY_SIMPLEX, .5, (86,86,86.0),2, cv2.LINE_AA)
#cv2.putText(img4, ".", (fv,810), cv2.FONT_HERSHEY_SIMPLEX, .5, (86,86,86.0),2, cv2.LINE_AA)
sv = ii
pv = ii
cv = 0
rt = 0
cp = {}
t2[ii] = cp.keys()
#print(tsort)
bcool = False
bcool_spot = 0
scool = False
scool_spot = 0
tsort.sort()
action=False
#actionP=iw #- 160
actionP= tsort[-4]
byell = False
syell = False
pl = ""
temp = list(t2.keys())
temp.sort()
t2 = { i: t2[i] for i in temp}
print(t2)
for x in tsort:
print(f"x is {x} / {actionP} ")
if (x >= actionP):
action=True
#ts = []
#tsort2 = [int(ii) for ii in list(t[x])]
#tsort2.sort()
#tsort2.sort()
#for lst in tsort2:
# ts.append(t[x][lst])
#print(t[x].values())
#if "white" in t[x].values() or "red" in t[x].values():
# state["s"] = sell_reset.copy()
# state["b"] = buy_reset.copy()
for lst,s in [ ("blue","b"),("lblue","lb"),("green","g"),("black","br"),("white", "w") ]:
if lst in t2[x]:
state["b"][s] = 1
for lst,s in [ ("lime","l"),("pink","p"),("red","r"),("tan", "t"), ("thistle","th")]:
if lst in t2[x]:
state["s"][s] = 1
print(state)
if "white" in t2[x] and (bcool_spot == 0 or x > bcool_spot):
if dir != "up":
print("going up")
#lastx="lblue"
lastx="pink"
byell = True
syell = False
amin = "valley"
dir = "up"
scool_spot = x + 20
elif "red" in t2[x] and (scool_spot == 0 or x > scool_spot):
if dir != "dn":
print("going down")
lastx="lblue"
#lastx="pink"
syell = True
byell = False
amin = "peak"
dir = "dn"
scool_spot = x + 20
#print(f"{state}")
for sl in cl["buy"]:
if sl.items() <= state["b"].items():
if byell:
if pl == "b":
print("DOUBLE B")
pl = "b"
print(f"{action} {x} {instr} buy {sl}")
byell = False
syell = True
state["s"] = sell_reset.copy()
state["b"] = buy_reset.copy()
cv2.putText(img4, f"B", (x,400), cv2.FONT_HERSHEY_SIMPLEX, .5, (86,86,86.0),2, cv2.LINE_AA)
if action:
requests.get(f"http://oversite.home.neb:3000/order/{instr}/30000")
for sl in cl["sell"]:
if sl.items() <= state["s"].items():
if syell:
if pl == "s":
print("DOUBLE S")
pl = "s"
print(f"{action} {x} {instr} sell {sl}")
syell = False
byell = True
state["s"] = sell_reset.copy()
state["b"] = buy_reset.copy()
cv2.putText(img4, f"S", (x,200), cv2.FONT_HERSHEY_SIMPLEX, .5, (86,86,86),2, cv2.LINE_AA)
if action:
requests.get(f"http://oversite.home.neb:3000/order/{instr}/-30000")
cv2.imwrite(f"points_{n[0]}.png", img4)
exit()