Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom notify #104

Merged
merged 5 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)