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

221 lines
9.3 KiB
Python

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
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":0, "lb": 1, "b":1, "g": 1, "br": 0}, { "w":1, "lb": 1, "b":1, "g": 1, "br": 0}, { "w":1, "lb": 1, "b":1, "g": 0, "br": 1}, { "w":0, "lb": 1, "b":1, "g": 1, "br": 1}, { "w":1, "lb": 1, "b":1, "g": 1, "br": 1}], "sell": [ { "r":1, "p": 1, "l": 1, "t": 0, "th":0}, { "r":0, "p": 1, "l": 1, "t": 1, "th":0}, { "r":1, "p": 1, "l": 1, "t": 1, "th":0}, { "r":0, "p": 1, "l": 1, "t": 0, "th":1}, { "r":0, "p": 1, "l": 1, "t": 1, "th":1}, { "r":1, "p": 1, "l": 1, "t": 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}
return(blue, pts_list)
THISTLE =[(216,191,216), (216,191,216)]
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)]
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():
print(f"\t{j}")
n = j.texts()[0].split(",")
j.set_focus()
j.maximize()
if n[0] not in hist:
hist[n[0]] = ""
state = { "buy" : { "w":0, "lb": 0, "b":0, "g": 0, "br": 0},"sell": { "r":0, "p": 0, "l": 0, "t": 0, "th":0} ,"cross": "", "last": "", "buy_map": ["white", "lblue", "blue", "green", "brown"], "sell_map" : [ "red", "pink", "lime", "tan", "thistle"] }
time.sleep(2)
print(n)
j.capture_as_image().save(f"pair_{n[0]}.png")
img = cv2.imread(f"pair_{n[0]}.png")
iw, ih, ia = img.shape
img4 = cv2.imread(f"pair_{n[0]}.png")
print("plines")
thistle_mask = cv2.inRange(img, THISTLE[0], THISTLE[1])
black_mask = cv2.inRange(img, BLACK[0], BLACK[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.
thistle_mask = cv2.dilate(thistle_mask, kernel, iterations=1)
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)
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 = {}
for lst in [blue,lime,lblue,green,black,pink,red,white,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]]
tsort = [int(ii) for ii in list(t)]
action=False
tsort.sort()
for x in tsort:
#@print(t[x])
#print(state)
#print(f"x is {x} / {ih} ")
if (ih - x <73):
action=True
bnum = False
snum = False
for cc in cl["sell"]:
if cc == state["sell"]:
snum = True
for cc in cl["buy"]:
if cc == state["buy"]:
bnum = True
#if 'pink' in t[x].values() and state["last"] != "sell":
if snum and state["last"] != "sell":
if state["cross"] != "sell":
state["buy"] = buy_reset
state["sell"] = sell_reset
state["cross"] = "sell"
print("WIPE")
cv2.putText(img4, f"W", (x,60 ), cv2.FONT_HERSHEY_SIMPLEX, .5, (86,86,86.0),2, cv2.LINE_AA)
#elif 'lblue' in t[x].values() and state["last"] != "buy":
elif bnum and state["last"] != "buy":
if state["cross"] != "buy":
state["buy"] = buy_reset
state["sell"] = sell_reset
state["cross"] = "buy"
print("WIPE")
cv2.putText(img4, f"W", (x,100 ), cv2.FONT_HERSHEY_SIMPLEX, .5, (86,86,86.0),2, cv2.LINE_AA)
for lst,s in [('white',"w"), ('lblue',"lb"), ('blue',"b"),('green',"g"),('black',"br")]:
if lst in t[x].values():
state["buy"][s] = 1
for lst,s in [('red',"r"),('pink',"p"), ('lime',"l"), ('tan',"t"),( 'thistle',"th")]:
if lst in t[x].values():
state["sell"][s] = 1
cro = state["cross"]
print(f"{x} {cro} b:{bnum} {bnum in cl.keys()} s:{snum} {snum in cl.keys()} {cl}")
instr = f"{n[0][0]}{n[0][1]}{n[0][2]}_{n[0][3]}{n[0][4]}{n[0][5]}"
if state["cross"] == "buy" and state["last"] != "buy" and bnum in cl["buy"]:
state["last"] = "buy"
#requests.get(f"http://oversite.home.neb:3000/order/{instr}/200000")
print("BUY")
cv2.putText(img4, f"B", (x,940 ), 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}/200000")
if state["cross"] == "sell" and state["last"] != "sell" and snum in cl["sell"]:
state["last"] = "sell"
print("SELL")
cv2.putText(img4, f"S", (x,900), 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}/-200000")
cv2.imshow("thoughts", img4)
cv2.setWindowProperty("thoughts", cv2.WND_PROP_TOPMOST, 1)
cv2.waitKey(20)
cv2.imwrite(f"points_{n[0]}.png", img4)
cv2.destroyWindow("thoughts")