掘金社区

基于C#的网格交易策略源码Pinned highlighted

龙玉小子 发表在策略研究 2021-11-17 09:57:18

策略研究
165
1
0

using System.Text;
using System.Runtime.InteropServices;
using GMSDK;
using TicTacTec.TA.Library; //金融指标库

namespace EmptyStrategy
{
public class MyStrategy : Strategy
{

    string   mSymName = "SHSE.588000";
    double   mPrice = 1.6; //箱体最高价,大于该价格全部卖出
    double   WagPre = 1.3; //网格幅度,百分比
    int      WagShuLiang = 15;
    int      WagIniLot = 2000;
    double   WagBeiShu = 1.3;
    double[] arrBuyPrice;
    int[]    arrBuyLot;
    bool[]   arrBuyKg;
    string   mFilePath;

    public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
    [DllImport("kernel32")]
    private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);

    //重写OnInit事件,进行策略开发
    public override void OnInit()
    {
        System.Console.WriteLine("OnInit事件执行!");

        mFilePath = System.Environment.CurrentDirectory + @"\config.ini";

        //如果配置文件存在,则读取配置文件中的配置数据
        if (System.IO.File.Exists(mFilePath))
        {
            mSymName = mIniRead("配置", "股票代码", "", mFilePath);
            mPrice = double.Parse(mIniRead("配置", "最高价格", "0", mFilePath));
            WagPre = double.Parse(mIniRead("配置", "网格幅度", "0", mFilePath));
            WagShuLiang = int.Parse(mIniRead("配置", "网格数量", "0", mFilePath));
            WagIniLot = int.Parse(mIniRead("配置", "起始股数", "0", mFilePath));
            WagBeiShu = double.Parse(mIniRead("配置", "加仓倍数", "0", mFilePath));

            arrBuyKg = new bool[WagShuLiang];
            arrBuyLot = new int[WagShuLiang];
            arrBuyPrice = new double[WagShuLiang];

            string bsbz = mIniRead("配置", "买卖标志", "0", mFilePath);
            string[] arrbz = bsbz.Split(',');

            if (arrbz.Length<WagShuLiang)
            {
                string ts = "";
                for(int i=0;i<WagShuLiang;i++)
                {
                    if (i == 0) ts = "0"; else ts = ts + ",0";
                }
                mIniWrite("配置", "买卖标志", ts, mFilePath);
                bsbz = mIniRead("配置", "买卖标志", "0", mFilePath);
                arrbz = bsbz.Split(',');
            }
            for (int i = 0; i < WagShuLiang; i++)
            {
                if (int.Parse(arrbz[i]) > 0)
                {
                    arrBuyKg[i] = true;
                }
                else arrBuyKg[i] = false;
            }

        }
        else
        {
            System.Environment.Exit(0);
        }
       
        //计算网格手数,及网格的价格
        for (int i = 0; i < WagShuLiang; i++)
        {
            if (i == 0)
            {
                arrBuyPrice[i] = mPrice * (1 - (WagPre / 100));
            }
            else
            {
                arrBuyPrice[i] = arrBuyPrice[i - 1] * (1 - (WagPre / 100));
            }

            int temlot = (int)(System.Math.Pow(WagBeiShu, i) * WagIniLot);
            int temzs = (int)(temlot / 100);
            arrBuyLot[i] = temzs * 100;
        }


        //订阅行情数据
        Subscribe(mSymName, "tick");
        return;
    }

    public override void OnTick(Tick tick)
    {
        double dq_price = tick.price; //获取tick的价格

        int t1 = int.Parse(tick.createdAt.ToString("HH")); //获取tick中的小时值
        int t2 = int.Parse(tick.createdAt.ToString("mm")); //获取tick中的分钟值

        //交易时间控制
        if (t1 < 9) return; 
        if (t1 >= 15) return;
        if (t1 <9 || (t1==9 && t2 < 30)) return;
        if (t1 == 14 && t2 >= 60) return;
        
        System.Console.Clear(); //清屏

        mShowInfo(dq_price, tick);//打印参数信息

        //获取持仓信息
        GMDataList<Position> mgp = GetPosition();
        if (mgp.status==0 && mgp.data.Count>0)
        {

            foreach (var v in mgp.data)
            {
                System.Console.WriteLine("当前持仓:{0} 持仓价:{1:f3} 持仓量:{2}", v.symbol, v.vwap,v.volume);
            }
         }

        //判断每个网格价格与实时价格是否达到买入条件
        for (int i = WagShuLiang - 1; i >= 0; i--)
        {
            if (!arrBuyKg[i] && dq_price < arrBuyPrice[i])
            {
                //买入
                GMData<Order> o = OrderVolume(mSymName, arrBuyLot[i], OrderSide.OrderSide_Buy, OrderType.OrderType_Market, PositionEffect.PositionEffect_Open, 0);
                arrBuyKg[i] = true;
                string kgbz = "";
                for (int m = 0; m < WagShuLiang; m++)
                {
                    if (m == 0)
                    {
                        if (arrBuyKg[m] == false) kgbz = "0"; else kgbz = "1";
                    }
                    else
                    {
                        if (arrBuyKg[m] == false) kgbz = kgbz + ",0"; else kgbz = kgbz + ",1";
                    }
                }
                mIniWrite("配置", "买卖标志", kgbz, mFilePath);
                System.Console.WriteLine("买入交易成功{0},{1:f3}", arrBuyLot[i], dq_price);
                return;
            }
        }

        //判断每个网格价格与实时价格是否达到卖出条件
        for (int i = 0; i < WagShuLiang - 1; i++)
        {
            if (arrBuyKg[i+1] && dq_price > arrBuyPrice[i])
            {
                //卖出
                GMData<Order> o = OrderVolume(mSymName, arrBuyLot[i + 1], OrderSide.OrderSide_Sell, OrderType.OrderType_Market, PositionEffect.PositionEffect_Close, 0);
                arrBuyKg[i + 1] = false;
                string kgbz = "";
                for (int m = 0; m < WagShuLiang; m++)
                {
                    if (m == 0)
                    {
                        if (arrBuyKg[m] == false) kgbz = "0"; else kgbz = "1";
                    }
                    else
                    {
                        if (arrBuyKg[m] == false) kgbz = kgbz + ",0"; else kgbz = kgbz + ",1";
                    }
                }
                mIniWrite("配置", "买卖标志", kgbz, mFilePath);
                System.Console.WriteLine("卖出交易成功{0},{1:f3}", arrBuyLot[i + 1], dq_price);
                return;
            }
        }

    }

    void mShowInfo(double jg,Tick t)
    {
        string strPL="\n";
        for(int i=0;i<arrBuyPrice.Length;i++)
        {
            string jc = "";
            if (arrBuyKg[i] == true) jc = "*"; else jc = "-";
            strPL=strPL+(i+1).ToString("00")+" " + arrBuyPrice[i].ToString("#.000") + "    " + arrBuyLot[i].ToString("00000")+"    "+jc+"\n";
        }
        System.Console.WriteLine("------------------------------------------------");
        System.Console.WriteLine("股票代码:{0}", mSymName);
        System.Console.WriteLine("最高价格:{0}", mPrice);
        System.Console.WriteLine("网格数量:{0}", WagShuLiang);
        System.Console.WriteLine("起始股数:{0}", WagIniLot);
        System.Console.WriteLine("加仓倍数:{0}", WagBeiShu);
        System.Console.WriteLine("------------------------------------------------{0}", strPL);
        System.Console.WriteLine("------------------------------------------------");
        System.Console.WriteLine("当前价格:{0:f3}   时间:{1:HH:mm:ss}", jg, t.createdAt);
        System.Console.WriteLine("------------------------------------------------");
    }

    public static string mIniRead(string section, string key, string def, string filePath)
    {
        StringBuilder sb = new StringBuilder(1024);
        GetPrivateProfileString(section, key, def, sb, 1024, filePath);
        return sb.ToString();
    }

    public static int mIniWrite(string section, string key, string value, string filePath)
    {
        return WritePrivateProfileString(section, key, value, filePath);
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyStrategy s = new MyStrategy("1dae1e3333c1b5cafdf8112bdc13e530408590b2","4d4484d0-3302-11ec-b2c5-38d547147393", StrategyMode.MODE_LIVE);
        s.Run();
        System.Console.WriteLine("回测完成!");
        System.Console.Read();
    }
}

}

评论: 1

Looks like your connection to 掘金量化社区 - 量化交易者的交流社区 was lost, please wait while we try to reconnect.