量化交易也是一种交易。狭义上的量化交易指将交易条件转化为程序,并自动下单;广义的量化交易是系统交易方式,是一种综合交易系统,根据一系列交易条件,智能决策系统,将丰富的经验与交易条件相结合,管理交易过程中的风险控制。
import json
from web3 import Web3, HTTPProvider
from web3.contract import ConciseContract
获取合约的abi
def getAbi(filePath):
with open(filePath,'r') as abi_file:
mpc_abi = json.load(abi_file)
return mpc_abi
获取余额
def getBalance(contractObj,accountAddr):
return contractObj.functions.balanceOf(accountAddr).call()
获取合约对象
def getContractObj(web3Obj,contractAddr,abiPath):
con_abi = getAbi(abiPath)
return web3Obj.eth.contract(address=contractAddr,abi=con_abi)
发送ERC20
def sendErc20(web3obj,fromAddr,toAddr,value,contractAddr,abiPath):
contractAbi = getAbi(abiPath)
contractObj = web3obj.eth.contract(address=contractAddr,abi=contractAbi)
tx_hash = contractObj.functions.transfer(toAddr,value).transact({'from':fromAddr})
tx_receipt = web3obj.eth.wait_for_transaction_receipt(tx_hash)
if tx_receipt['status'] == 1:
return 'send Success'
else:
return 'send Failed'
def printBalance(web3obj,contractObj,fromAddr,toAddr,markIndex):
balanceA = getBalance(contractObj,fromAddr)
balanceB = getBalance(contractObj,toAddr)
fromMark = markIndex+" balanceA="
toMark = markIndex+" balanceB="
print(fromMark,web3obj.fromWei(balanceA,'ether'))
print(toMark, web3obj.fromWei(balanceB,'ether'))
发送ERC20
web3.py instance
w3 = Web3(HTTPProvider("http://localhost:8545"))
print('web3 connect:',w3.isConnected())
fromAddr = w3.eth.accounts[0]
toAddr = w3.eth.accounts[1]
print('fromAddr=',fromAddr)
print('toAddr =',toAddr)
value = w3.toWei(0.1,'ether')
abiPath = './myabi/DPC_abi.json'
contractAddr = '0xE250d901baeCb66F85D184D8aE9dA2bD4e705854' ##DPC合约地址
contractObj = getContractObj(w3,contractAddr,abiPath)
发送前
printBalance(w3,contractObj,fromAddr,toAddr,"#1")
bRet = sendErc20(w3,fromAddr,toAddr,value,contractAddr,abiPath)
print('result= ',bRet)
发送后
printBalance(w3,contractObj,fromAddr,toAddr,"#2")
import json
from web3 import Web3, HTTPProvider
from web3.contract import ConciseContract
获取合约的abi
def getAbi(fil