首页 > 编程语言 >VNPY-网络交易(算法交易)

VNPY-网络交易(算法交易)

时间:2023-08-22 15:25:53浏览次数:46  
标签:VNPY bid self price 算法 base grid tick 交易

from vnpy.trader.constant import Direction
from vnpy.trader.object import TradeData, OrderData, TickData
from vnpy.trader.engine import BaseEngine
from vnpy.trader.constant import OrderType, Offset, Direction
from ..template import AlgoTemplate
import math


class MyGridAlgo(AlgoTemplate):
    """"""

    display_name = "My Grid Tradding  网格交易 "

    default_setting = {
        "step_price": 1.0,
        "step_volume": 1,
        "interval": 1,
        "grid_up":100,
        "grid_down":50
    }

    variables = [
        "pos",
        "long_pos",
        "short_pos",
        "timer_count",
        "vt_orderid",
        "grid_bid_base",
        "grid_ask_base"
    ]

    def __init__(
        self,
        algo_engine: BaseEngine,
        algo_name: str,
        vt_symbol: str,
        direction: str,
        offset: str,
        price: float,
        volume: float,
        setting: dict
    ):
        """"""
        super().__init__(algo_engine, algo_name,vt_symbol, direction, offset, price, volume, setting)

        # Parameters
        self.step_price = setting["step_price"]
        self.step_volume = setting["step_volume"]
        self.interval = setting["interval"]
        self.grid_up=setting['grid_up']
        self.grid_down=setting['grid_down']
        self.grid_bid_base=0
        self.grid_ask_base=0

        # Variables
        self.timer_count = 0
        self.vt_orderid = ""
        self.pos = 0
        self.long_pos=0
        self.short_pos=0
        self.last_tick = None
        self.put_event()

    def on_tick(self, tick: TickData):
        """"""
        self.last_tick = tick

    def on_timer(self):
        """"""
        if not self.last_tick:
            return

        self.timer_count += 1
        if self.timer_count < self.interval:
            self.put_event
            return
        self.timer_count = 0

        if self.vt_orderid:
            self.cancel_all()





        bid_diff = self.last_tick.bid_price_1 - self.grid_bid_base;##平多或者开空的价差(买入价价差)
        ask_diff = self.last_tick.ask_price_1 - self.grid_ask_base;##开多或者平空的价差(卖出价价差)


        # 市场下跌时买多
        if ask_diff <= -1* self.step_price:
            self.grid_ask_base = self.last_tick.ask_price_1
            self.grid_bid_base= self.last_tick.bid_price_1
            if self.grid_ask_base >=self.grid_down and self.grid_ask_base<=self.grid_up:
                self.write_log("下跌开多")
                ##下跌时加多仓(加多仓位)
                self.vt_orderid= self.buy(
                    self.last_tick.ask_price_1,
                    self.step_volume,
                    OrderType.LIMIT,
                    Offset.OPEN
                )
                ##下跌时平空仓(平空获利)
                self.buy(
                    self.last_tick.ask_price_1,
                    self.step_volume,
                    OrderType.LIMIT,
                    Offset.CLOSE
                )
        # 市场上涨时平多
        elif bid_diff >= self.step_price:
            self.write_log("上涨平多")
            self.grid_bid_base = self.last_tick.bid_price_1
            self.grid_ask_base=self.last_tick.ask_price_1
            if self.grid_bid_base >=self.grid_down and self.grid_bid_base<=self.grid_up:
                ##上涨时,平多仓

                self.sell(
                    self.last_tick.bid_price_1,
                    self.step_volume,
                    OrderType.LIMIT,
                    Offset.CLOSE 
                    )
                ##上涨时开空仓
                self.sell(
                    self.last_tick.bid_price_1,
                    self.step_volume,
                    OrderType.LIMIT,
                    Offset.OPEN
                    )
        # Update UI
        self.put_event()

    def on_order(self, order: OrderData):
        """"""
        if not order.is_active():
            self.vt_orderid = ""
            self.put_event

    def on_trade(self, trade: TradeData):
        """"""
        if trade.direction == Direction.LONG:
            if trade.offset==Offset.OPEN:
                self.long_pos +=  trade.volume
                self.pos += trade.volume
            else:
                self.short_pos -=  trade.volume
                self.pos -= trade.volume

        else:
            if trade.offset==Offset.OPEN:
                self.short_pos +=  trade.volume
                self.pos -= trade.volume
            else:
                self.long_pos -= trade.volume
                self.pos -=trade.volume

        self.put_event

 

标签:VNPY,bid,self,price,算法,base,grid,tick,交易
From: https://www.cnblogs.com/ip99/p/17648583.html

相关文章

  • VNPY-网格交易(策略交易)
    ##grid_trade_strategy.pyfromvnpy_ctastrategyimport(CtaTemplate,StopOrder,TickData,BarData,TradeData,OrderData,BarGenerator,ArrayManager,)fromvnpy.trader.constantimportOrderType,Offset,DirectionclassGr......
  • vnpy_tora华鑫奇点gateway改动版
    fromtypingimportDict,Tuple,Any,Listimportpytzfromdatetimeimportdatetimefromvnpy.eventimportEventEnginefromvnpy.trader.constantimport(Direction,Exchange,OrderType,Product,Status,Offset,)fromvnpy.trader.......
  • [代码随想录]Day24-回溯算法part04
    题目:93.复原IP地址思路:函数参数:参数就一个stirng,path先收集ip地址的四个部分,最后存入res中时拼接成一个string,因此path和res都是[]string类型终止条件:当path有了ip的四个部分就终止;当然只有完全的把字符串遍历了才会存入res单层逻辑:先取1-3位,判断是不是0-255内的数并且没......
  • Redis系列19:LRU内存淘汰算法分析
    Redis系列1:深刻理解高性能Redis的本质Redis系列2:数据持久化提高可用性Redis系列3:高可用之主从架构Redis系列4:高可用之Sentinel(哨兵模式)Redis系列5:深入分析Cluster集群模式追求性能极致:Redis6.0的多线程模型追求性能极致:客户端缓存带来的革命Redis系列8:Bitmap实现亿万级......
  • 【算法】分治初步
    目录定义示例快速排序实现第k小三分法归并排序实现定义分治,字面上的解释是“分而治之”,就是把一个问题分成多个的相同或相似的子问题,直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合并。示例快速排序把原数组分成左右两段,保证左\(≤\)右,再对左右分别排序。实......
  • knn 算法的实现原理是怎样的
    K最近邻(K-NearestNeighbors,简称KNN)算法是一种用于分类和回归的基本机器学习算法。其原理是基于样本之间的距离度量,通过找出离待预测样本最近的K个训练样本,利用这K个样本的标签信息进行分类或回归预测。主要思想就是物以类聚人以群分的思想,关键就是KNN中K近邻中K的确定,和距离的定义......
  • java笔试手写算法面试题大全含答案
    1.统计一篇英文文章单词个数。publicclassWordCounting{publicstaticvoidmain(String[]args){try(FileReaderfr=newFileReader("a.txt")){intcounter=0;booleanstate=false;intcurrentChar;while((currentChar=fr.read())!=-1){if(currentChar=='......
  • java笔试手写算法面试题大全含答案
    1.统计一篇英文文章单词个数。publicclassWordCounting{publicstaticvoidmain(String[]args){try(FileReaderfr=newFileReader("a.txt")){intcounter=0;booleanstate=false;intcurrentChar;while((currentChar=fr.read())!=-1){if(currentChar==�......
  • 用 Dijkstra 算法解决最短路问题
    话不多说,先看图1.1朴素版的Dijkstra算法一般用到这个情况稠密图,也就是节点的个数比边的个数少。(稠密图用邻接矩阵存储)#include<cstring>#include<iostream>#include<algorithm>usingnamespacestd;constintN=510;intn,m;intg[N][N];//稠密图用邻接矩阵,g......
  • 【校招VIP】java语言考点之垃圾回收算法
    考点介绍:垃圾回收算法是必考题。GC中的垃圾指的是存在于内存中的、不会再被使用的对象。而垃圾回收就是把那些不再被使用的对象进行清除,收回占用的内存空间......一、考点题目1、java中如何判断对象是否是垃圾?解答:引用计数:在对象中添加一个引用计数器,如果被引用计数器加1,引用......