def plot(self, ax, epochs): qued = self.eventqueue.dict for k in qued: ev = qued[k] st = ev.startep ed = ev.endep if st == ed: continue if ed < epochs[0] or st > epochs[-1]: continue sti = -1 edi = -1 if ev.side == env.SIDE_BUY: color = self.buycolor else: color = self.sellcolor for i in range(len(epochs)): if sti == -1 and st <= epochs[i]: sti = i if edi == -1 and ed < epochs[i]: edi = i - 1 if sti != -1: ax.scatter(sti, ev.start_price, c=color, marker=">", s=600, alpha=0.7) ax.annotate( " %d\n %.3f\n %s\n%s" % (ev.id, ev.start_price, lib.epoch2str(epochs[sti], "%m-%d\n%H:%M"), ev.desc), (sti, ev.start_price), size=30, color=color, horizontalalignment="left", verticalalignment="top") if edi != -1: if ev.profit > 0: marker = "o" else: marker = "x" ax.scatter(edi, ev.end_price, c=color, marker=marker, s=600, alpha=0.7) ax.annotate(" %d\n %.3f\n %s" % (ev.id, ev.end_price, lib.epoch2str(epochs[edi], "%m-%d\n%H:%M")), (edi, ev.end_price), size=30, color=color, horizontalalignment="right", verticalalignment="bottom")
def insertMonoPricedata(instrument, granularity, startprice, start, end, direction, pipdiff): db = MSSQLDB() pricetbl = lib.names.getPriceTable(instrument, granularity) priceunit = tradelib.pip2Price(1, instrument) unitsecs = tradelib.getUnitSecs(granularity) start -= (start % unitsecs) end -= (end % unitsecs) curr = start price = startprice cnt = 0 while curr <= end: i = cnt % 3 if i == 2: price -= pipdiff * priceunit * direction else: price += pipdiff * priceunit * direction if direction == 1: o = price c = price + 2 * pipdiff * priceunit else: o = price + 2 * pipdiff * priceunit c = price sql = "insert into %s ([ep],[dt],[o],[h],[l],[c],[v]) \ values (%d,'%s',%f,%f,%f,%f,%d)" % ( pricetbl, curr, lib.epoch2str(curr, "%Y-%m-%d %H:%M:%S"), o, price + 2 * pipdiff * priceunit, price, c, (i + 1) * 10) db.execute(sql) curr += unitsecs cnt += 1
def getPrice(self, startep, endep): errret = ([], [], [], [], [], []) price = self.priceToGet endep += 1 candleType = "" if price == "M": candleType = "mid" if price == "B": candleType = "bid" if price == "A": candleType = "ask" req = InstrumentsCandles(instrument="USD_JPY", params={ "granularity": self.granularity, "from": lib.epoch2str(startep, "%Y-%m-%dT%H:%M:%SZ"), "to": lib.epoch2str(endep, "%Y-%m-%dT%H:%M:%SZ"), "datetime_format": "UNIX" }) self.oandaw.request(req) if req.response == None: return errret cdls = req.response.get('candles') count = len(cdls) (t, o, h, l, c, v) = self.getIniPriceLists(0, count) for i in range(count): cdl = cdls[i] item = cdl[candleType] ti = cdl["time"].split(".")[0] t[i] = lib.str2epoch(ti, "%Y-%m-%dT%H:%M:%S") v[i] = cdl["volume"] o[i] = float(item["o"]) h[i] = float(item["h"]) l[i] = float(item["l"]) c[i] = float(item["c"]) return t, o, h, l, c, v
def plot(_id, i): price = self.peaks.get(_id) ax.scatter(i, price, c=self.color, marker="_", s=600) ax.annotate( " %.3f\n %s" % (price, lib.epoch2str(self.epochs[_id], "%m-%d\n%H:%M")), (i, price), size=30, color=self.color, horizontalalignment="center", verticalalignment=verticalalignment)
def plot(ie, i): if ie.price >= mdp: va = "bottom" else: va = "top" price = ie.price ax.scatter(i, price, c=ie.color, marker=ie.marker, s=600) ax.annotate(" %.3f\n %s\n %s" % (price, lib.epoch2str(ie.ep, "%m-%d\n%H:%M"), ie.info), (i, price), size=30, color=ie.color, horizontalalignment="center", verticalalignment=va)
def openOrder(self, tickEvent, orderEvent): _id = orderEvent.id if not _id in self.orders.keys(): orderEvent.epoch = tickEvent.time orderEvent.status = env.ESTATUS_ORDER_OPENED self.orders[_id] = orderEvent if orderEvent.validep == 0: validstr = "" else: validstr = "valid=%s" % lib.epoch2str(orderEvent.validep) lib.printMsg( orderEvent.epoch, "[%s] Order %d opened. %f side=%d tp=%f sl=%f %s" % (self.name, _id, orderEvent.price, orderEvent.side, orderEvent.takeprofit_price, orderEvent.stoploss_price, validstr)) else: raise Exception("Order %d already open." % _id)
''' Created on 2019/11/09 @author: kot ''' import env import lib from index.power import PowerIndex startep = lib.str2epoch("2019-10-17T20:00:00", env.DATE_FORMAT_NORMAL) endep = lib.str2epoch("2019-10-20T00:00:00", env.DATE_FORMAT_NORMAL) p = PowerIndex("USD_JPY", "M5", startep, endep, 12) (t, o, h, l, c, v) = p.subc.prices for i in range(len(p.epochs)): bu = p.bull_totals[i] be = p.bear_totals[i] print("%s price=%.3f bull_rate=%.3f bull_change=%.3f bear_change=%.3f" % (lib.epoch2str(p.epochs[i], env.DATE_FORMAT_NORMAL), c[i + p.sumspan], bu / (bu + be), p.bull[i] / bu, p.bear[i] / be))
def makeSmallPeriodData(instrument, large_period, small_period): reCreatePriceTable(instrument, small_period) lus = tradelib.getUnitSecs(large_period) sus = tradelib.getUnitSecs(small_period) if lus <= sus: raise Exception("large_period must be greater than small_period!") db = MSSQLDB() lpricetbl = lib.names.getPriceTable(instrument, large_period) spricetbl = lib.names.getPriceTable(instrument, small_period) diffrate = lus / sus cnt = db.countTable(lpricetbl) if cnt == 0: raise Exception("No data in %s" % lpricetbl) for (ep, o, h, l, c, v) in db.execute("select ep,o,h,l,c,v from %s;" % lpricetbl).fetchall(): bar_size = (h - l) / diffrate pdiff = (h - l - bar_size) / diffrate if c > o: direction = 1 price = l else: direction = -1 price = h vs = int(v / diffrate) for i in range(int(diffrate)): os = price if direction == 1: hs = price + bar_size ls = os cs = hs else: hs = price ls = price - bar_size cs = ls if i >= diffrate - 1: if direction == 1: hs = h cs = h else: ls = l cs = l sql = "insert into %s ([ep],[dt],[o],[h],[l],[c],[v]) \ values (%d,'%s',%f,%f,%f,%f,%d)" % ( spricetbl, ep, lib.epoch2str( ep, "%Y-%m-%d %H:%M:%S"), os, hs, ls, cs, vs) db.execute(sql) shuff = i % 4 if shuff == 2: price -= direction * pdiff elif shuff == 3: price += direction * pdiff * 2 else: price += direction * pdiff ep += sus