Exemplo n.º 1
0
        public bool addInstruction2Wating(string code, CInstruction instruction)
        {
            decimal range = Limit.getInstance().getLimitRange(code);

            if (range != 0.10m)
            {
                string info = "";
                if (range == 0.05m)
                {
                    info = "ST stock";
                }
                else if (range == 0m)
                {
                    info = "unkown object";
                }

                Console.Write("Error-addInstruction2Wating: " + info + " (" + code + ")");

                return(false);
            }

            lock (lockInstructions)
            {
                registry2RDC(code);

                if (instruction.id == 0)
                {
                    instruction.id = --idNoneDB;
                }

                if (!this.dcInstrunctionWaiting.ContainsKey(code))
                {
                    this.dcInstrunctionWaiting.Add(code, new Dictionary <long, CInstruction>());
                }
                this.dcInstrunctionWaiting[code].Add(instruction.id, instruction);
            }
            return(true);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            /*
             * string szTime = DateTime.Now.ToString("HHmmss");
             * while (string.Compare(szTime, "091500") < 0
             || string.Compare(szTime, "150000") > 0)
             ||{
             || Console.WriteLine(szTime);
             || System.Threading.Thread.Sleep(1000);
             || szTime = DateTime.Now.ToString("HHmmss");
             ||}
             */

            StringBuilder sErrInfo = new StringBuilder(256);
            int           ret      = TradeXB.OpenTdx(sErrInfo);

            if (ret < 0)
            {
                Console.WriteLine("TradeXB.OpenTdx error: " + sErrInfo);
                Console.ReadLine();
                return;
            }

            RealtimeDataCenter.getInstance(5, "999999,399001,399006", RealtimeDataCenter.EDataSource.tradeX);
            Limit.getInstance();
            Reactor.getInstance();

            TN   tn   = new TN();
            MAID maid = new MAID();
            RI   ri   = new RI();
            RT   rt   = new RT();

            while (true)
            {
                Console.ReadLine();
            }
        }
Exemplo n.º 3
0
        protected override void recv(RealtimeDataCenter.RealtimeData rd)
        {
            if (DateTime.Now.Date != rd.dt.Date)
            {
                return;
            }           //expired data

            //Console.Write(1);
            CInstruction[] inss = getInstructionFromWaitingByCode(rd.code);
            if (inss != null)
            {
                foreach (var instruction in inss)
                {
                    //:open or close instruction preprocess
                    string szTime = DateTime.Now.ToString("HHmmss");
                    if (string.Compare(szTime, CInstruction.szExecOpenTimeBegin) < 0 &&
                        instruction.priceType == (short)EPriceType.OPEN)
                    {
                        if (!this.dcCodeLastRdForOpen.ContainsKey(rd.code))
                        {
                            this.dcCodeLastRdForOpen.Add(rd.code, rd);
                        }
                        else
                        {
                            this.dcCodeLastRdForOpen[rd.code] = rd;
                        }

                        setTimer(10);
                        continue;
                    }
                    if (string.Compare(szTime, CInstruction.szExecCloseTimeBegin) < 0 &&
                        instruction.priceType == (short)EPriceType.CLOSE)
                    {
                        if (!this.dcCodeLastRdForClose.ContainsKey(rd.code))
                        {
                            this.dcCodeLastRdForClose.Add(rd.code, rd);
                        }
                        else
                        {
                            this.dcCodeLastRdForClose[rd.code] = rd;
                        }

                        setTimer(10);
                        continue;
                    }
                    ///:~

                    /*
                     * if (rd.C == 0) //call auction in open
                     * {
                     * }
                     */

                    if (instruction.bingo(rd))
                    {
                        decimal      range = 0.01m;
                        EEntryOrExit ee    = (EEntryOrExit)instruction.entryOrExit;
                        if (ee == EEntryOrExit.entry)
                        {
                            range = 0.01m;
                        }                  //谨慎entry
                        else if (ee == EEntryOrExit.exit)
                        {
                            range = 0.03m;
                        }                  //确保exit

                        //:send
                        ECategory category = (ECategory)instruction.category;
                        switch (category)
                        {
                        case ECategory.BUY:
                        case ECategory.RZ_BUY:
                        case ECategory.RQ_RETURN:
                            if (instruction.price == null)
                            {
                                instruction.price = rd.sell5c[0];
                            }
                            if (instruction.price == 0)     //limit-up, sell none
                            {
                                instruction.price = rd.buy5c[0];
                            }
                            instruction.price *= (1m + range);

                            decimal limitUp = Limit.getInstance().getLimitUp(rd.code, rd.lastC);
                            if (instruction.price > limitUp)
                            {
                                instruction.price = limitUp;
                            }

                            if (instruction.priceDesc != null && instruction.priceDesc != 0)
                            {
                                if (instruction.price > instruction.priceDesc)
                                {
                                    instruction.price = instruction.priceDesc;
                                }
                            }
                            break;

                        case ECategory.SELL:
                        case ECategory.RQ_SELL:
                        case ECategory.RZ_RETURN:
                            if (instruction.price == null)
                            {
                                instruction.price = rd.buy5c[0];
                            }
                            if (instruction.price == 0)     //limit-down, buy none
                            {
                                instruction.price = rd.sell5c[0];
                            }
                            instruction.price *= (1m - range);

                            decimal limitDown = Limit.getInstance().getLimitDown(rd.code, rd.lastC);
                            if (instruction.price < limitDown)
                            {
                                instruction.price = limitDown;
                            }

                            if (instruction.priceAsc != null && instruction.priceAsc != 0)
                            {
                                if (instruction.price < instruction.priceAsc)
                                {
                                    instruction.price = instruction.priceAsc;
                                }
                            }
                            break;

                        case ECategory.RQ_Q2Q:
                            break;

                        default:
                            break;
                        }

                        instruction.state    = (short)EInstructionState.SENT;
                        instruction.sendTime = DateTime.Now;
                        instruction.price    = Math.Floor((decimal)instruction.price * 100m) / 100m;

                        //exec
                        //...SendOrder
                        //update DB
                        long maybeOldId = instruction.id;
                        if (instruction.id > 0)
                        {
                            var vs = (from n in db.tsInstructions
                                      where n.id == instruction.id
                                      select n).Single();
                            instruction.modifyDBObj(ref vs);
                            db.SubmitChanges();
                        }
                        else
                        {
                            if (instruction.sso != null)
                            {
                                string sqlSSO         = instruction.sso.getInsertSql();
                                string sqlInstruction = instruction.getInsertSql();
                                long?  idSSO          = instruction.sso.id;
                                long?  idInstruction  = instruction.id;

                                try
                                {
                                    db.spAddSSOAndInstruction(sqlSSO, sqlInstruction, ref idSSO, ref idInstruction);
                                    instruction.id     = (long)idInstruction;
                                    instruction.sso.id = (long)idSSO;
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e);
                                    Console.WriteLine(sqlSSO);
                                    Console.WriteLine(sqlInstruction);

                                    WriteLog.getInstance().pushData("error2DB: " + sqlSSO + "; " + sqlInstruction);
                                }
                            }
                        }

                        Console.Write(2);
                        string ifs = "";
                        if (instruction.sso != null)
                        {
                            ifs = instruction.sso.idForStrategies.ToString();
                        }
                        Console.WriteLine("(" + rd.code + "," + ifs + "," + instruction.desc + "," + instruction.sendTime + "," + instruction.price + ")");

                        //write log
                        WriteLog.getInstance().pushData(instruction.getLog());

                        addInstruction2Sent(rd.code, instruction);
                        removeInstructionFromWating(rd.code, maybeOldId);
                        ///:~
                    }
                }
            }

            inss = getInstructionFromSentByCode(rd.code);
            if (inss != null)
            {
                foreach (var instruction in inss)
                {
                    //...QueryDatas(Category=2)
                    bool      bSuccesful = false;
                    ECategory category   = (ECategory)instruction.category;
                    if (category == ECategory.BUY)
                    {
                        if (instruction.price > rd.C ||
                            (instruction.price == rd.C && DateTime.Now.Subtract((DateTime)instruction.sendTime).TotalSeconds > 5))
                        {
                            bSuccesful = true;
                        }
                    }
                    else if (category == ECategory.SELL)
                    {
                        if (instruction.price < rd.C ||
                            (instruction.price == rd.C && DateTime.Now.Subtract((DateTime)instruction.sendTime).TotalSeconds > 5))
                        {
                            bSuccesful = true;
                        }
                    }

                    if (bSuccesful)
                    {
                        instruction.price      = rd.C; //...QueryDatas(Category=2)' price
                        instruction.state      = (short)EInstructionState.SUCCESSFUL;
                        instruction.resultTime = DateTime.Now;

                        //exec
                        //update DB
                        if (instruction.id > 0)
                        {
                            var vs = (from n in db.tsInstructions
                                      where n.id == instruction.id
                                      select n).Single();
                            instruction.modifyDBObj(ref vs);
                            db.SubmitChanges();
                        }

                        Console.Write(3);
                        Console.WriteLine("(" + rd.code + "," + instruction.resultTime + "," + instruction.price + ")");

                        //write log
                        WriteLog.getInstance().pushData(instruction.getLog());

                        removeInstructionFromSent(rd.code, instruction.id);
                    }
                }
            }
        }
Exemplo n.º 4
0
        protected override void recv(RealtimeDataCenter.RealtimeData rd)
        {
            if (DateTime.Now.Date != rd.dt.Date)
            {
                return;
            } //expired data

            if (this.dcWindow.ContainsKey(rd.code))
            {
                Tuple <decimal, decimal> wind = this.dcWindow[rd.code];
                if (rd.L <= wind.Item2) //当日将不再会形成跳空式的关闭窗口(即RI)
                {
                    removeObj(rd.code);
                }
                else
                {
                    if (rd.C >= wind.Item1)
                    {
                        bool bCreate          = false;
                        Reactor.EPriceType pt = Reactor.EPriceType.REALTIME;

                        if ((rd.C / rd.lastC - 1) * 100 > 9.0m) //推测其将limit-up,先买入(此点需继续验证)
                        {
                            bCreate = true;
                            pt      = Reactor.EPriceType.REALTIME;
                        }
                        else if (string.Compare(rd.dt.ToString("HHmmss"), "145940") > 0)
                        {
                            bCreate = true;
                            pt      = Reactor.EPriceType.CLOSE;
                        }

                        if (bCreate)
                        {
                            Reactor.CInstruction instruction = new Reactor.CInstruction();
                            instruction.id            = 0;
                            instruction.priceAsc      = wind.Item2 + 0.01m;
                            instruction.priceDesc     = Limit.getInstance().getLimitUp(rd.code, rd.lastC) - 0.01m; //-0.01m, don't buy when limit-up
                            instruction.generateTime  = DateTime.Now;
                            instruction.priceType     = (short)pt;
                            instruction.category      = (short)Reactor.ECategory.BUY;
                            instruction.state         = (short)Reactor.EInstructionState.WATING;
                            instruction.desc          = "entry";
                            instruction.quantity      = (int)(Math.Round(CASH_SIZE / (decimal)instruction.priceAsc / 100m) * 100m);
                            instruction.codeReference = rd.code;

                            if (instruction.quantity > 0)
                            {
                                instruction.sso = new Reactor.CSimplifiedStrategiesObject()
                                {
                                    id = 0,
                                    idForStrategies = (long)Reactor.EIFS.RI,
                                    code            = rd.code,
                                    state           = (short)Reactor.ESSOState.WAIT_CREATE,
                                    planQuantity    = instruction.quantity,
                                    lastStopPrice   = (decimal)instruction.priceAsc,
                                    createTime      = instruction.generateTime,
                                    direction       = (short)Reactor.EDirection.LONG,
                                };
                                instruction.analyseEntryOrExit((Reactor.EDirection)(instruction.sso.direction));

                                removeObj(rd.code);
                                Reactor.getInstance().addInstruction2Wating(rd.code, instruction);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
Arquivo: RT.cs Projeto: 296083197/ts
        protected override void recv(RealtimeDataCenter.RealtimeData rd)
        {
            if (DateTime.Now.Date != rd.dt.Date)
            {
                return;
            } //expired data

            if (this.dcTendency.ContainsKey(rd.code))
            {
                decimal[] dcps = this.dcTendency[rd.code].ToArray();
                foreach (var dcp in dcps)
                {
                    if (rd.C >= dcp)
                    {
                        bool bCreate          = false;
                        Reactor.EPriceType pt = Reactor.EPriceType.REALTIME;

                        if ((rd.C / rd.lastC - 1) * 100 < 3.0m)
                        {
                            bCreate = true;
                            pt      = Reactor.EPriceType.REALTIME;
                        }

                        if (bCreate)
                        {
                            Reactor.CInstruction instruction = new Reactor.CInstruction();
                            instruction.id            = 0;
                            instruction.priceAsc      = dcp + 0.01m;
                            instruction.priceDesc     = Limit.getInstance().getLimitUp(rd.code, rd.lastC) - 0.01m; //-0.01m, don't buy when limit-up
                            instruction.generateTime  = DateTime.Now;
                            instruction.priceType     = (short)pt;
                            instruction.category      = (short)Reactor.ECategory.BUY;
                            instruction.state         = (short)Reactor.EInstructionState.WATING;
                            instruction.desc          = "entry";
                            instruction.quantity      = (int)(Math.Round(CASH_SIZE / (decimal)instruction.priceAsc / 100m) * 100m);
                            instruction.codeReference = rd.code;

                            if (instruction.quantity > 0)
                            {
                                instruction.sso = new Reactor.CSimplifiedStrategiesObject()
                                {
                                    id = 0,
                                    idForStrategies = (long)Reactor.EIFS.RT,
                                    code            = rd.code,
                                    state           = (short)Reactor.ESSOState.WAIT_CREATE,
                                    planQuantity    = instruction.quantity,
                                    lastStopPrice   = (decimal)instruction.priceAsc,
                                    createTime      = instruction.generateTime,
                                    direction       = (short)Reactor.EDirection.LONG,
                                };
                                instruction.analyseEntryOrExit((Reactor.EDirection)(instruction.sso.direction));

                                //直接removeObj,以及下面的break (foreach),而不使用下面的注释部分,原因在于:Reactor.EIFS.RT中,同一对象有多个tendency line时,当日只进入一次
                                removeObj(rd.code);

                                /*
                                 * this.dcTendency[rd.code].Remove(dcp);
                                 * if (this.dcTendency[rd.code].Count == 0)
                                 * { removeObj(rd.code); }
                                 */
                                Reactor.getInstance().addInstruction2Wating(rd.code, instruction);

                                break;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        private void worker()
        {
            bool bExecuted = false;

            while (!bExecuted)
            {
                DateTime dt = DateTime.Now;
                if (string.Compare(dt.ToString("HHmmss"), "144000") > 0 &&
                    string.Compare(dt.ToString("HHmmss"), "155900") < 0)
                {
                    bExecuted = true;

                    List <KeyValuePair <string, List <coRDC.Bar1M> > > lt1M = new List <KeyValuePair <string, List <coRDC.Bar1M> > >();
                    coRDC crdc = new coRDC();
                    crdc.getAll1MBars(ref lt1M);
                    foreach (var obj in lt1M)
                    {
                        string code  = obj.Key;
                        int    count = obj.Value.Count;
                        if (count < 200)
                        {
                            continue;
                        }

                        int       moreAvgInDayCount = 0;
                        decimal   sumV = 0, sum_vm = 0, maxH = 0;
                        decimal[] avg = new decimal[count];
                        int       idx = 0;
                        foreach (var b1m in obj.Value)
                        {
                            if (b1m.H > maxH)
                            {
                                maxH = b1m.H;
                            }

                            sumV   += b1m.V;
                            sum_vm += b1m.vm;
                            if (sumV > 0)
                            {
                                avg[idx] = sum_vm / sumV;
                                if (b1m.C > avg[idx])
                                {
                                    moreAvgInDayCount++;
                                }
                            }

                            idx++;
                        }

                        decimal percent = (decimal)moreAvgInDayCount / count;
                        if (percent >= 0.50m)
                        {
                            decimal lastC = Limit.getInstance().getLastC(code);
                            if (lastC > 0 &&
                                maxH != Limit.getInstance().getLimitUp(code, lastC))      //exclude that limit up to drawdown
                            {
                                decimal priceAsc  = Math.Max(lastC * 1.085m, maxH * 0.999m);
                                decimal priceDesc = Limit.getInstance().getLimitUp(code, lastC) - 0.01m;
                                if (priceDesc >= priceAsc)
                                {
                                    Reactor.CInstruction instruction = new Reactor.CInstruction();
                                    instruction.id            = 0;
                                    instruction.priceAsc      = priceAsc;
                                    instruction.priceDesc     = priceDesc;
                                    instruction.generateTime  = DateTime.Now;
                                    instruction.priceType     = (short)Reactor.EPriceType.REALTIME;
                                    instruction.category      = (short)Reactor.ECategory.BUY;
                                    instruction.state         = (short)Reactor.EInstructionState.WATING;
                                    instruction.desc          = "entry";
                                    instruction.quantity      = (int)(Math.Round(CASH_SIZE / (decimal)instruction.priceAsc / 100m) * 100m);
                                    instruction.codeReference = code;

                                    if (instruction.quantity > 0)
                                    {
                                        instruction.sso = new Reactor.CSimplifiedStrategiesObject()
                                        {
                                            id = 0,
                                            idForStrategies = (long)Reactor.EIFS.MAID,
                                            code            = code,
                                            state           = (short)Reactor.ESSOState.WAIT_CREATE,
                                            planQuantity    = instruction.quantity,
                                            lastStopPrice   = (decimal)instruction.priceAsc,
                                            createTime      = instruction.generateTime,
                                            direction       = (short)Reactor.EDirection.LONG,
                                        };
                                        instruction.analyseEntryOrExit((Reactor.EDirection)(instruction.sso.direction));

                                        Reactor.getInstance().addInstruction2Wating(code, instruction);
                                    }
                                }
                            }
                        }
                    }
                }

                if (!bExecuted)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }