Skip to content

Commit

Permalink
Merge pull request #104 from xingetouzi/CustomNotify
Browse files Browse the repository at this point in the history
Custom notify
  • Loading branch information
cheatm authored May 24, 2019
2 parents 9b9787b + ab6e0de commit f5b382c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
34 changes: 34 additions & 0 deletions vnpy/trader/utils/templates/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from xml.etree import ElementTree
from collections import defaultdict
import json
import requests
import re

ROOT_TAG = "ding"


def dingFormatter():
e = ElementTree.Element(ROOT_TAG)
e.text = ".*?"
return ElementTree.tostring(e, "unicode")


DingCompiler = re.compile(dingFormatter())


def makeNotify(message, titles, channels):
root = ElementTree.Element(ROOT_TAG)

for title in titles:
etitle = ElementTree.SubElement(root, "title")
etitle.text = title

for channel in channels:
echannel = ElementTree.SubElement(root, "channel")
echannel.text = channel

emessage = ElementTree.SubElement(root, "message")
emessage.text = message

return ElementTree.tostring(root, "unicode")

22 changes: 21 additions & 1 deletion vnpy/trader/utils/templates/orderTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from vnpy.trader.vtConstant import *
from vnpy.trader.language import constant
from vnpy.trader.app.ctaStrategy import ctaBase
from vnpy.trader.utils.templates.notify import makeNotify
from datetime import datetime, timedelta, timezone
from collections import Iterable
import numpy as np
Expand Down Expand Up @@ -1545,7 +1546,6 @@ def cancelOrder(self, vtOrderID):
self.onOrder(op.order)
return

print('send cancel', vtOrderID)
return super().cancelOrder(vtOrderID)

def isCancel(self, op):
Expand All @@ -1560,3 +1560,23 @@ def isOrderVolumeValid(self, vtSymbol, orderType, volume, price=None):

maximum = self.maximumOrderVolume(vtSymbol, orderType, price)
return maximum >= volume

def notify(self, title, message, *channels):
text = makeNotify(
message,
(title,),
channels if channels else (self.author,)
)
self.writeLog(text)

def simpleNotify(self, message):
self.notify(
self.name,
message,
self.author
)

def notifyPosition(self, key, value, *channels):
message = "%-24s %10s" % (key, value)
self.writeLog(message)
self.notify("Position: %s" % self.name, message, *channels)

0 comments on commit f5b382c

Please sign in to comment.