コード例 #1
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
        public void ExecuteSoftFlat(string symbol, int?timeToFlat = null)
        {
            var secondsToFlat = 5;

            if (timeToFlat != null)
            {
                secondsToFlat = (int)timeToFlat;
            }
            FormHelper.FormSetText(String.Format("Execute soft exit for {0}", symbol));
            var openPos = api.GetOpenPositionForSymbol(symbol);

            if (openPos.Volume != 0 && secondsToFlat == 0)
            {
                ExecuteFlatOrder(symbol);
            }
            else if (openPos.Volume != 0 && openPos.Side == "B")
            {
                var vol = Math.Abs(openPos.Volume);
                SoftSell(symbol, vol, secondsToFlat);
            }
            else if (openPos.Volume != 0 && openPos.Side == "S")
            {
                var vol = Math.Abs(openPos.Volume);
                SoftBuy(symbol, vol, secondsToFlat);
            }
            else
            {
                ExecuteFlatOrder(symbol);
            }
        }
コード例 #2
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
        public void ExecuteBuyBidInExitHalfOffer(string symbol, int shareSize, double halfTarget)
        {
            api.RegisterL1(symbol);
            var l1 = ExecuteGetL1(symbol);

            if (l1.Symbol == null)
            {
                log.AddFormLogMessage(String.Format("Cannot get Lv1 for {0}", symbol));
                return;
            }

            var openPos = api.GetOpenPositionForSymbol(symbol);

            if (openPos != null && openPos.Side != "B")
            {
                api.FlattenSymbol(symbol);
            }
            var remaining = GetRemainingShareFromOpenPos(openPos, shareSize);

            if (remaining > 0)
            {
                api.CancelBuyOrdersForSymbol(symbol);
                var buyPrice = l1.BidPrice;
                // only try once
                api.ExecuteOrder("Buy", symbol, buyPrice, shareSize);
                FormHelper.FormSetText(String.Format("Bid In @{0}", buyPrice));
                log.Updatelog(String.Format("Bid In @{0}", buyPrice));
            }
            const int oneMinute = 60000;

            Thread.Sleep(1 * oneMinute);
            api.CancelBuyOrdersForSymbol(symbol);

            //check if the thing gets filled
            openPos = api.GetOpenPositionForSymbol(symbol);
            if (openPos != null && openPos.Side == "B")
            {
                var halfShare = openPos.Volume / 2;
                halfShare  = Math.Abs(halfShare);
                halfTarget = Math.Round(halfTarget, 2);
                if (symbol.ToLowerInvariant().Contains("mhi"))
                {
                    halfTarget = Math.Round(halfTarget, 0);
                }

                if (halfShare > 0)
                {
                    api.ExecuteOrder("Sell", symbol, halfTarget, halfShare);
                    log.AddFormLogMessage(String.Format("Offer half @{0}", halfTarget));
                }
            }
        }
コード例 #3
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
        public void ExecuteExitHalf(string symbol)
        {
            FormHelper.FormSetText(String.Format("Execute exit half for {0}", symbol));

            api.RegisterL1(symbol);
            var l1 = ExecuteGetL1(symbol);

            if (l1.Symbol == null)
            {
                log.AddFormLogMessage(String.Format("Cannot get Lv1 for {0}", symbol));
                return;
            }

            var openPos = api.GetOpenPositionForSymbol(symbol);

            if (openPos.Volume == 0)
            {
                return;
            }

            var halfShare = openPos.Volume / 2;

            halfShare = Math.Abs(halfShare);

            //check if the thing gets filled
            openPos = api.GetOpenPositionForSymbol(symbol);
            if (openPos != null && openPos.Side == "B")
            {
                api.CancelBuyOrdersForSymbol(symbol);
                if (halfShare > 0)
                {
                    api.ExecuteOrder("Sell", symbol, l1.AskPrice, halfShare);
                    log.AddFormLogMessage(String.Format("Offer half @{0}", l1.AskPrice));
                }
            }
            else
            {
                api.CancelSellOrdersForSymbol(symbol);
                if (halfShare > 0)
                {
                    api.ExecuteOrder("Buy", symbol, l1.BidPrice, halfShare);
                    log.AddFormLogMessage(String.Format("Bid half @{0}", l1.BidPrice));
                }
            }

            const int oneMinute = 60000;

            Thread.Sleep(10 * oneMinute);

            api.CancelBuyOrdersForSymbol(symbol);
            api.CancelSellOrdersForSymbol(symbol);
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: louiegor/EsBacktest
        private void GetConnection()
        {
            var setting = new HttpWebDefaultSetting {
                ChromeId = Global.TraderId
            };
            var connection = Connection.SiteConnection(setting);

            if (!connection)
            {
                startButton.Visible = false;
                FormHelper.FormSetText("Cannot connect to source");
            }
        }
コード例 #5
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
        public int SafetyCheck(int tries)
        {
            tries--;

            //For Safety reason!!!
            if (tries == 0)
            {
                FormHelper.FormSetText("Something is wrong with the program!");
                FormHelper.FormSetText("Flat all!!!");
                log.Updatelog("Safety exit!!!");
                throw new ApplicationException("Something is wrong!!!");
            }

            return(tries);
        }
コード例 #6
0
ファイル: XmlHelper.cs プロジェクト: louiegor/EsBacktest
        public void CancelSellOrdersForSymbol(string symbol)
        {
            var xdoc = feed.CancelSellOrdersForSymbol(symbol);

            log.Updatelog("Execute Cancel Sell Order");
            try
            {
                xdoc.XPathSelectElements("//Content");
            }
            catch (Exception e)
            {
                log.Updatelog(e.Message);
                FormHelper.FormSetText(e.Message);
            }
        }
コード例 #7
0
ファイル: XmlHelper.cs プロジェクト: louiegor/EsBacktest
        public void FlattenSymbol(string symbol)
        {
            log.Updatelog(String.Format("Flattening symbol {0}", symbol));
            FormHelper.FormSetText("FlattenSymbol: " + symbol);
            var xdoc = feed.FlattenSymbol(symbol);

            try
            {
                xdoc.XPathSelectElements("//Content");
            }
            catch (Exception e)
            {
                log.Updatelog(e.Message);
                FormHelper.FormSetText(e.Message);
            }
        }
コード例 #8
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
 public void ExecuteCancelOpenOrders(string side, string symbol, int remaining)
 {
     if (remaining > 0)
     {
         FormHelper.FormSetText(String.Format("{0} shares for {1} is remained to be filled", remaining, symbol));
         log.Updatelog(String.Format("Cancel Open orders, {0} shares for {1} is remained to be filled", remaining, symbol));
         if (side == "Sell")
         {
             api.CancelSellOrdersForSymbol(symbol);
         }
         else if (side == "Buy")
         {
             api.CancelBuyOrdersForSymbol(symbol);
         }
         Thread.Sleep(1100);
     }
 }
コード例 #9
0
ファイル: XmlHelper.cs プロジェクト: louiegor/EsBacktest
        public bool RegisterAll()
        {
            var success = feed.RegisterAll();

            if (success)
            {
                FormHelper.FormSetText("Registered Sucessfully");
                log.Updatelog("Registered Sucessfully");
                return(true);
            }
            else
            {
                FormHelper.FormSetText("Register Failed");
                log.Updatelog("Register Failed");
                return(false);
            }
        }
コード例 #10
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
        public void ExecuteSellStop(string symbol, double inPrice)
        {
            Global.StopEntryTime     = 90000;
            Global.StopCheckInterval = 3000;
            api.RegisterL1(symbol);
            long elapsedMs = 0;
            var  watch     = Stopwatch.StartNew();

            FormHelper.FormSetText(String.Format("Execute SellStop {0} @ {1}", symbol, inPrice));

            while (elapsedMs < Global.StopEntryTime)
            {
                var l1 = ExecuteGetL1(symbol);
                if (l1.AskPrice < inPrice)
                {
                    var sellPrice = l1.BidPrice - 0.1;
                    // only try once
                    api.ExecuteOrder("Sell", symbol, sellPrice, Global.ShareSize);
                    log.AddFormLogMessage(String.Format("Price reached. Market In"));
                    break;
                }

                elapsedMs = watch.ElapsedMilliseconds;
                Thread.Sleep(Global.StopCheckInterval);
            }
            watch.Stop();

            var       position = api.GetOpenPositionForSymbol(symbol);
            const int stopLoss = 0;

            if (position.Volume != 0)
            {
                Global.UpdateSymbol(symbol, inPrice, stopLoss, position.Side, position.Volume);
                log.AddFormLogMessage(String.Format("Filled {0} for {1}", position.Symbol, position.Volume));
            }
            else
            {
                FormHelper.FormSetText(String.Format("Did not get fill"));
                log.Updatelog(String.Format("Did not get fill"));
            }

            api.CancelBuyOrdersForSymbol(symbol);
        }
コード例 #11
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
        public void CheckOrderStateAndCancelOrder(string execId, string side, string symbol)
        {
            var orderId = api.GetOrderNumber(execId);

            Thread.Sleep(1000);
            var state = api.GetOrderState(orderId);

            if (state.State != State.Cancelled && state.State != State.Filled)
            {
                FormHelper.FormSetText(String.Format("Did not get filled Cancel open orders"));

                if (side == "Buy")
                {
                    api.CancelBuyOrdersForSymbol(symbol);
                }
                else if (side == "Sell")
                {
                    api.CancelSellOrdersForSymbol(symbol);
                }
                Thread.Sleep(1000);
            }
        }
コード例 #12
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
 public void ExecuteFlatOrder(string symbol)
 {
     FormHelper.FormSetText(String.Format("Executing Flat for {0},", symbol));
     api.FlattenSymbol(symbol);
     Global.RemoveSymbol(symbol);
 }
コード例 #13
0
ファイル: MainModel.cs プロジェクト: louiegor/EsBacktest
        public void ExecuteExitHalfAndMarketAfterTime(string symbol, int second)
        {
            FormHelper.FormSetText(String.Format("Execute exit half for {0}", symbol));

            api.RegisterL1(symbol);
            var l1 = ExecuteGetL1(symbol);

            if (l1.Symbol == null)
            {
                log.AddFormLogMessage(String.Format("Cannot get Lv1 for {0}", symbol));
                return;
            }

            var openPos = api.GetOpenPositionForSymbol(symbol);

            if (openPos.Volume == 0)
            {
                return;
            }

            var initial = Math.Abs(openPos.Volume);

            var halfShare = openPos.Volume / 2;

            halfShare = Math.Abs(halfShare);

            //check if the thing gets filled
            openPos = api.GetOpenPositionForSymbol(symbol);
            if (openPos != null && openPos.Side == "B")
            {
                api.CancelBuyOrdersForSymbol(symbol);
                if (halfShare > 0)
                {
                    api.ExecuteOrder("Sell", symbol, l1.AskPrice, halfShare);
                    log.AddFormLogMessage(String.Format("Offer half @{0}", l1.AskPrice));
                }
            }
            else if (openPos != null && openPos.Side != "B")
            {
                api.CancelSellOrdersForSymbol(symbol);
                if (halfShare > 0)
                {
                    api.ExecuteOrder("Buy", symbol, l1.BidPrice, halfShare);
                    log.AddFormLogMessage(String.Format("Bid half @{0}", l1.BidPrice));
                }
            }

            Thread.Sleep(second * 1000);
            api.CancelBuyOrdersForSymbol(symbol);
            api.CancelSellOrdersForSymbol(symbol);

            var openPos1   = api.GetOpenPositionForSymbol(symbol);
            var remaining2 = GetRemainingShareForExit(openPos1, halfShare, initial);

            l1 = ExecuteGetL1(symbol);
            if (l1.Symbol == null)
            {
                log.AddFormLogMessage(String.Format("Cannot get Lv1 for {0}", symbol));
                return;
            }

            if (openPos1 != null && openPos1.Volume != halfShare && remaining2 > 0)
            {
                if (openPos1.Side == "B")
                {
                    api.ExecuteOrder("Sell", symbol, l1.BidPrice, remaining2);
                    log.AddFormLogMessage(String.Format("Market half @{0}", l1.AskPrice));
                }
                else
                {
                    api.ExecuteOrder("Buy", symbol, l1.AskPrice, remaining2);
                    log.AddFormLogMessage(String.Format("Market half @{0}", l1.BidPrice));
                }
            }
        }
コード例 #14
0
ファイル: FileWatcher.cs プロジェクト: louiegor/EsBacktest
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            //Workaround for firing twice: http://stackoverflow.com/a/3042963
            var lastWriteTime = File.GetLastWriteTime(e.FullPath);

            log.Updatelog(String.Format("Change Detected {0}", e.FullPath));

            DateTime lastExecTime;

            //Cannot execute the same stock in less than 20 seconds
            if (lastExecute.TryGetValue(e.FullPath, out lastExecTime))
            {
                log.Updatelog(String.Format("Cannot update in less than 20 second - {0} , now is {1}, last execute is {2}", e.FullPath, DateTime.Now, lastExecTime));
                var diff = DateTime.Now - lastExecTime;
                if (diff.TotalSeconds < 10)
                {
                    return;
                }
            }

            if (lastWriteTime != lastRead)
            {
                var sh      = new SettingHelper();
                var setting = sh.GetSetting();
                lastExecute[e.FullPath] = DateTime.Now;


                var fullPath = e.FullPath;
                var name     = e.Name.Replace(".txt", "");

                //FormHelper.FormSetText(String.Format("File Change detected for {0}...", name));


                //Non American Stocks
                if (name.Contains("-TSE") || name.Contains("-TC") || name.Contains(".JP"))
                {
                    name = name.Replace("-TSE", ".JP");
                    name = name.Replace("-TC", ".TO");
                }

                else if (name.Contains("total"))
                {
                    CsvParser.UpdateDictionary(e.FullPath);
                }

                //Futures
                else if (name.ToLowerInvariant() == "japanfuture")
                {
                    name = setting.JpFuture;
                }
                else if (name.ToLowerInvariant() == "hongkongfuture")
                {
                    name = setting.HkFuture;
                }
                else if (name.ToLowerInvariant() == "brazilfuture")
                {
                    name = setting.BraFuture;
                }
                else if (name.ToLowerInvariant() == "esfuture")
                {
                    name = setting.EsFuture;
                }
                else if (name.ToLowerInvariant() == "eurusd")
                {
                }
                //American Stocks - could be amex nasdaq or nyse
                else
                {
                    var amList = Global.AmExceptionList;
                    var nqList = Global.NqExceptionList;
                    if (amList.Contains(name))
                    {
                        name = name + ".AM";
                    }
                    else if (nqList.Contains(name))
                    {
                        name = name + ".NQ";
                    }
                    else
                    {
                        name = name + ".NY";
                    }
                }
                FormHelper.FormSetText(String.Format("File Change detected for {0}...", name));

                //
                // For debug
                //
                if (name.ToLowerInvariant().Contains("test") || name.ToLowerInvariant().Contains("debug"))
                {
                    return;
                }
                var line = FileReader(fullPath).Split(',');
                log.Updatelog(String.Format("executing side {0} - {1}", line[0], line[1]));
            }
            lastRead = lastWriteTime;
        }
コード例 #15
0
ファイル: Helper.cs プロジェクト: louiegor/EsBacktest
 public static void AddFormLogMessage(string text)
 {
     FormHelper.FormSetText(text);
     Updatelog(text);
 }
コード例 #16
0
ファイル: Helper.cs プロジェクト: louiegor/EsBacktest
 public static void AddFormLogMessage(this Log log, string text)
 {
     FormHelper.FormSetText(text);
     log.Updatelog(text);
 }