//This class now stores the information need for the stance and is called every turn to check it vs its conditions for success and failure
            public MarketStance(Stance s, int Quanity, int ClientID, string TargetStock, AlgoTrader owner, ThreadDataBaseHandler threadDataBaseHandler)
            {
                this.threadDataBaseHandler = threadDataBaseHandler;
                stance           = s;
                Owner            = owner;
                client           = ClientID;
                this.TargetStock = TargetStock;
                StartTime        = DateTime.Now;
                double CurrentPrice = threadDataBaseHandler.GetCountDouble("SELECT SUM(CurrentPrice) FROM Stock WHERE StockName = '" + this.TargetStock + "'");

                CurrentPrice = Math.Round(CurrentPrice, 2);
                switch (stance)
                {
                case Stance.ShortTermLong:
                    //For a long the trader believes the market will go up so success price if above current price
                    this.Quanity = Quanity;
                    SuccessPrice = CurrentPrice + 0.02f;
                    FailurePrice = CurrentPrice - 0.01f;
                    RequiredTime = 5;
                    ShortTermLong(Quanity, CurrentPrice);
                    break;

                case Stance.ShortTermShort:
                    //For a short the trader believes the market will go doen so success price if below current price
                    this.Quanity = Quanity;
                    SuccessPrice = CurrentPrice - 0.02f;
                    FailurePrice = CurrentPrice + 0.01f;
                    RequiredTime = 5;
                    ShortTermShort(Quanity, CurrentPrice);
                    break;
                }
                //Close DB thread for other parts of the program to be free to connect to DB
                threadDataBaseHandler.CloseCon();
            }
            public void RunTurn()
            {
                double   CurrentPrice = threadDataBaseHandler.GetCountDouble("SELECT SUM(CurrentPrice) FROM Stock WHERE StockName = '" + TargetStock + "'");
                TimeSpan TimeTaken    = DateTime.Now - StartTime;

                CurrentPrice = Math.Round(CurrentPrice, 2);
                switch (stance)
                {
                //Check stance against conditions then excute the completion of the stance
                case Stance.ShortTermLong:
                    if ((CurrentPrice >= SuccessPrice || CurrentPrice < FailurePrice) && !OfferPlaced && TimeTaken.TotalMinutes > RequiredTime)
                    {
                        threadDataBaseHandler.SetData(string.Format("INSERT INTO Pool (Type, Price, User, StockName, Quantity) VALUES ({0}, {1}, {2}, '{3}', {4})", (int)BidOffer.offer, CurrentPrice, client, TargetStock, Quanity));
                        OfferPlaced = true;
                        isCompleted = true;
                        Console.WriteLine("Finished long with " + Quanity);
                    }
                    break;

                case Stance.ShortTermShort:
                    if ((CurrentPrice <= SuccessPrice || CurrentPrice > FailurePrice) && !OfferPlaced && TimeTaken.TotalMinutes > RequiredTime)
                    {
                        threadDataBaseHandler.SetData(string.Format("INSERT INTO Pool (Type, Price, User, StockName, Quantity) VALUES ({0}, {1}, {2}, '{3}', {4})", (int)BidOffer.bid, CurrentPrice, client, TargetStock, Quanity));
                        OfferPlaced = true;
                        isCompleted = true;
                        Console.WriteLine("Finished short with " + Quanity);
                    }
                    break;
                }
            }
예제 #3
0
        private static void BasicTraders()
        {
            List <Trader> Traders = new List <Trader>();
            //Grab the basic traders from the DB
            MySqlDataReader reader = threadDataBaseHandler.GetData("SELECT * FROM UserAlgoTraders");

            //Load them into instance of the local class so that we can evaluate them
            while (reader.Read())
            {
                Trader trader = new Trader();
                trader.TraderID         = (int)reader["ID"];
                trader.OwnerId          = (int)reader["OwnerID"];
                trader.Trigger          = new Trigger();
                trader.Trigger.Target   = (string)reader["TTarget"];
                trader.Trigger.Operator = (MathOperator)((int)reader["TOperator"]);
                trader.Trigger.Value    = (double)reader["TValue"];
                trader.action           = new Action();
                trader.action.Target    = (string)reader["ATarget"];
                trader.action.BuyOrSell = (BuySell)((int)reader["ABuyOrSell"]);
                trader.action.Quantity  = (int)reader["AQuantity"];
                Traders.Add(trader);
            }
            List <Trader> TradersToDelete = new List <Trader>();

            //Now for all of the basic traders stored locally we now evaulute against it condition
            foreach (Trader trader in Traders)
            {
                string          TargetName      = "";
                MySqlDataReader mySqlDataReader = threadDataBaseHandler.GetData("SELECT StockName From stock WHERE FullName = '" + trader.action.Target + "'");
                while (mySqlDataReader.Read())
                {
                    TargetName = (string)mySqlDataReader["StockName"];
                }
                string TriggerName = "";
                mySqlDataReader = threadDataBaseHandler.GetData("SELECT StockName From stock WHERE FullName = '" + trader.Trigger.Target + "'");
                while (mySqlDataReader.Read())
                {
                    TriggerName = (string)mySqlDataReader["StockName"];
                }
                bool   TriggersSuccesful = true;
                double CurrentPrice      = threadDataBaseHandler.GetCountDouble("SELECT SUM(CurrentPrice) FROM Stock WHERE StockName = '" + TriggerName + "'");
                if (trader.Trigger.Operator == MathOperator.Greater)
                {
                    if (!(trader.Trigger.Value < CurrentPrice))
                    {
                        TriggersSuccesful = false;
                    }
                }
                else
                {
                    if (!(trader.Trigger.Value > CurrentPrice))
                    {
                        TriggersSuccesful = false;
                    }
                }
                //If the condition is met we now excute position set by the user
                if (TriggersSuccesful)
                {
                    CurrentPrice = threadDataBaseHandler.GetCountDouble("SELECT SUM(CurrentPrice) FROM Stock WHERE StockName = '" + TargetName + "'");
                    threadDataBaseHandler.SetData(string.Format("INSERT INTO Pool(Type, Price, User, StockName, Quantity) VALUES({0}, {1}, {2}, '{3}', {4})", (int)trader.action.BuyOrSell, CurrentPrice, trader.OwnerId, TargetName, trader.action.Quantity));
                    threadDataBaseHandler.SetData("DELETE FROM AlgoTrader WHERE ID = " + trader.TraderID);
                    //Add to traders to be delete as the basic trader is now complete
                    TradersToDelete.Add(trader);
                }
            }
            foreach (Trader t in TradersToDelete)
            {
                Traders.Remove(t);
            }
            TradersToDelete.Clear();
            //Close the DB thread to free it up for other parts of the program to use
            threadDataBaseHandler.CloseCon();
        }