Example #1
0
 public bool SellAll(int accountId, BrokerEmulator broker)
 {
     int amount = broker.GetHoldingAmountForSymbol(accountId, symbol);
     decimal price = broker.GetPrice(symbol);
     DateTime time = broker.GetTime(symbol);
     Console.WriteLine("At time " + time + " sell " + amount + " shares of " + symbol + " at $" + price + " a share. +$" + amount*price);
     return broker.SellMarketPrice(accountId, symbol, amount);
 }
Example #2
0
 public bool BuyAll(int accountId, BrokerEmulator broker)
 {
     decimal balance = broker.GetBalance(accountId);
     decimal price = broker.GetPrice(symbol);
     DateTime time = broker.GetTime(symbol);
     int amount = (int) (balance / price);
     Console.WriteLine("At time " + time + " buy " + amount + " shares of " + symbol + " at $" + price + " a share. -$" + amount*price);
     return broker.BuyMarketPrice(accountId, symbol, amount);
 }
Example #3
0
        public bool takeAction(int accountId, BrokerEmulator broker)
        {
            decimal currentPrice = broker.GetPrice(symbol);
            if (lastCheckPoint == null)
            {
                lastCheckPoint = currentPrice;
                return true;
            }

            bool res = true;

            decimal lastPrice = (decimal)lastCheckPoint;

            //Dont hold any shares overnight
            if (broker.GetTime(symbol).TimeOfDay >= new TimeSpan(15, 58, 0))
            {
                SellAll(accountId, broker);
            }
            if (currentPrice > lastPrice * (1 + upwardDelta)) {
                lastCheckPoint = currentPrice;
                res = BuyAll(accountId, broker);
            }
            else if (currentPrice < lastPrice * (1 - downwardDelta))
            {
                lastCheckPoint = currentPrice;
                res = SellAll(accountId, broker);
            }

            currentTrend = currentPrice > lastPrice;
            if (lastTrend != currentTrend)
            {
                lastCheckPoint = currentPrice;
            }

            lastTrend = currentTrend;

            return res;
        }
Example #4
0
 public bool takeAction(int accountId, BrokerEmulator broker, string symbol)
 {
     throw new NotImplementedException();
 }
Example #5
0
        public bool TakeAction(int accountId, BrokerEmulator broker)
        {
            decimal currentPrice = broker.GetPrice(symbol);
            DateTime currentTime = broker.GetTime(symbol);
            longSma.Take(currentTime, currentPrice);
            shortSma.Take(currentTime, currentPrice);

            //Console.WriteLine(currentTime + " " + shortSma.GetAverage());

            //Sell all at the end of day
            if (currentTime.Hour == 15 && currentTime.Minute >= 58)
            {
                return SellAll(accountId, broker);
            }

            bool res = true;
            var predictor = forseeConstant * currentPrice;
            if (shortSma.GetAverage() - predictor > longSma.GetAverage())
            {
                if (firstCross == DateTime.MaxValue) firstCross = currentTime;
                if (firstCross.Add(safety) <= currentTime) res = BuyAll(accountId, broker);
            }
            else if (shortSma.GetAverage() - predictor < longSma.GetAverage())
            {
                res= SellAll(accountId, broker);
                firstCross = DateTime.MaxValue;
            }

            return res;
        }
Example #6
0
 internal Trader(IStrategy strategyToUse, decimal balance = (decimal) 1e5)
 {
     strategy = strategyToUse;
     broker = new BrokerEmulator();
     accountId = broker.CreateAccount(balance);
 }