def gWaitForMessage(self, sMsgType, sChartId, sMark, *lArgs): """ Raises a Mt4Timeout error if there is no answer in oOptions['OTCmd2']['iRetvalTimeout'] seconds. Raising an error lets us return None as a return value. The protocol talking to Mt4 has the void return type, which gRetvalToPython returns as None. """ from OTMql427.SimpleFormat import gRetvalToPython, lUnFormatMessage self.eSendMessage(sMsgType, sChartId, sMark, *lArgs) i = 0 iTimeout = self.oConfig['OTCmd2']['iRetvalTimeout'] self.vDebug("Waiting for: " +sMsgType +" " +sMark) while i < int(iTimeout): if sMsgType == 'exec': s = self.oMixin.sRecvReply() if s != "": gRetval = gRetvalToPython(lUnFormatMessage(s)) return self.G(gRetval) # drop through elif sMsgType == 'cmd': if sMark in self.oListenerThread.dRetvals.keys(): gRetval = self.oListenerThread.dRetvals[sMark] del self.oListenerThread.dRetvals[sMark] return self.G(gRetval) else: self.vError("Unrecognized sMsgType: " +sMsgType) return None i += 5 self.vDebug("Waiting: " +repr(i)) time.sleep(5.0) self._G = None raise Mt4Timeout("No retval returned in " +str(iTimeout) +" seconds")
def vCallbackOnListener(self, sBody): sys.stdout.write("DEBUG: sBody: " + repr(sBody) + '\n') lArgs = lUnFormatMessage(sBody) sMsgType = lArgs[0] sChart = lArgs[1] sIgnore = lArgs[2] # should be a hash on the payload sMark = lArgs[3] sPayloadType = lArgs[4] gPayload = lArgs[4:] # overwritten below try: # sys.stdout.write("INFO: sChart: " +repr(sChart) +'\n') if sChart not in self.lCharts: # keep a list of charts that we have seen for "chart list" self.lCharts.append(sChart) #? FixMe: what does null come back as? if sMsgType == 'retval': # gRetvalToPython requires try: gPayload = gRetvalToPython(lArgs) except Exception as e: sys.stdout.write( "WARN: vCallbackOnListener error decoding to Python: %s\n%r" % ( str(e), sBody, )) return self.jLastRetval = gPayload self.dRetvals[sMark] = gPayload sys.stdout.write( "DEBUG: vCallbackOnListener set dRetvals: %s\n" % (sMark, )) elif sMsgType == 'bar': assert sPayloadType == 'json', \ sMsgType +" sPayloadType expected 'json' not " \ +sPayloadType +"\n" +sBody # can use this to find the current bid and ask gPayload = json.loads(lArgs[5]) self.jLastBar = gPayload elif sMsgType == 'tick': assert sPayloadType == 'json', \ sMsgType +" sPayloadType expected 'json' not " \ +sPayloadType +"\n" +sBody # can use this to find the current bid and ask gPayload = json.loads(lArgs[5]) self.jLastTick = gPayload elif sMsgType == 'timer': assert sPayloadType == 'json', \ sMsgType +" sPayloadType expected 'json'" +"\n" +sBody # can use this to find if we are currently connected gPayload = json.loads(lArgs[5]) self.gLastTimer = gPayload elif sMsgType in ['cmd', 'exec']: #? why do we see outgoing messages? return else: sys.stdout.write( "WARN: vCallbackOnListener unrecognized sMsgType: %r\n" % (sBody, )) return # may need to print more info here - chart and sMark if sMsgType == 'retval': # FixMe: not sure where these None are coming from if gPayload is not None: # you really need sMark for retval self.vPprint(sMsgType, gPayload, "INFO: [" + sMark + "] ") else: self.vPprint(sMsgType, gPayload) except Exception as e: sys.stderr.write(traceback.format_exc(10) + "\n")
def vCallbackOnListener(self, sBody): sys.stdout.write("DEBUG: sBody: " +repr(sBody) +'\n') lArgs = lUnFormatMessage(sBody) sMsgType = lArgs[0] sChart = lArgs[1] sIgnore = lArgs[2] # should be a hash on the payload sMark = lArgs[3] sPayloadType = lArgs[4] gPayload = lArgs[4:] # overwritten below try: # sys.stdout.write("INFO: sChart: " +repr(sChart) +'\n') if sChart not in self.lCharts: # keep a list of charts that we have seen for "chart list" self.lCharts.append(sChart) #? FixMe: what does null come back as? if sMsgType == 'retval': # gRetvalToPython requires try: gPayload = gRetvalToPython(lArgs) except Exception as e: sys.stdout.write("WARN: vCallbackOnListener error decoding to Python: %s\n%r" % (str(e), sBody, )) return self.jLastRetval = gPayload self.dRetvals[sMark] = gPayload sys.stdout.write("DEBUG: vCallbackOnListener set dRetvals: %s\n" % (sMark, )) elif sMsgType == 'bar': assert sPayloadType == 'json', \ sMsgType +" sPayloadType expected 'json' not " \ +sPayloadType +"\n" +sBody # can use this to find the current bid and ask gPayload = json.loads(lArgs[5]) self.jLastBar = gPayload elif sMsgType == 'tick': assert sPayloadType == 'json', \ sMsgType +" sPayloadType expected 'json' not " \ +sPayloadType +"\n" +sBody # can use this to find the current bid and ask gPayload =json.loads(lArgs[5]) self.jLastTick = gPayload elif sMsgType == 'timer': assert sPayloadType == 'json', \ sMsgType +" sPayloadType expected 'json'" +"\n" +sBody # can use this to find if we are currently connected gPayload = json.loads(lArgs[5]) self.gLastTimer = gPayload elif sMsgType in ['cmd', 'exec']: #? why do we see outgoing messages? return else: sys.stdout.write("WARN: vCallbackOnListener unrecognized sMsgType: %r\n" % (sBody, )) return # may need to print more info here - chart and sMark if sMsgType == 'retval': # FixMe: not sure where these None are coming from if gPayload is not None: # you really need sMark for retval self.vPprint(sMsgType, gPayload, "INFO: [" +sMark +"] ") else: self.vPprint(sMsgType, gPayload) except Exception as e: sys.stderr.write(traceback.format_exc(10) +"\n")