示例#1
0
        public async void Step3SellTSLA(string AlgorithmName)
        {
            CultureInfo localculture = new CultureInfo("en-US");
            DateTime    dbdate       = Convert.ToDateTime(sqlitedb.ReadStateTable("fdq_days_after_quarter", AlgorithmName), localculture);

            Console.WriteLine("Reached a date while-loop at step 3. If nothing prints after this, then it's ok to close the script.");
            while ((DateTime.Now - dbdate).TotalDays <= 3)
            {
                Thread.Sleep(24 * 60 * 60 * 1000);
            }
            Console.WriteLine("Made it out of the date while-loop.");
            decimal original_sellprice;
            decimal ROI;
            string  orderid;

            // Sell Tesla for a profit when trade is Filled.
            using (conn.alpacaAccountApi = Environments.Paper.GetAlpacaTradingClient(new SecretKey(conn.API_KEY1, conn.API_SECRET1)))
                using (conn.alpacaDataApi = Environments.Paper.GetAlpacaDataClient(new SecretKey(conn.API_KEY1, conn.API_SECRET1)))
                {
                    Guid    previousStepTradeID          = new Guid(sqlitedb.ReadStateTable("previous_step_order_id", AlgorithmName));
                    decimal step2buyTSLAPrice            = (await conn.alpacaDataApi.GetLastQuoteAsync(unstableSymbol)).AskPrice;
                    string  CheckUnstableSymbolLastTrade = (await conn.alpacaAccountApi.GetOrderAsync(previousStepTradeID)).OrderStatus.ToString();
                    while (CheckUnstableSymbolLastTrade.ToLower() != "filled")
                    {
                        Thread.Sleep(60 * 1000);
                        CheckUnstableSymbolLastTrade = (await conn.alpacaAccountApi.GetOrderAsync(previousStepTradeID)).OrderStatus.ToString();
                    }
                    Console.WriteLine("Buying of " + unstableSymbol + " was completed.");
                    decimal percent_desired_profit          = 1.01M;
                    decimal profitLimit                     = Math.Round((step2buyTSLAPrice * percent_desired_profit), 2);
                    long    unstableSymbolQuantityInAccount = (await conn.alpacaAccountApi.GetPositionAsync(unstableSymbol)).Quantity;
                    var     order = await conn.alpacaAccountApi.PostOrderAsync(OrderSide.Sell.Limit(unstableSymbol, unstableSymbolQuantityInAccount, profitLimit));

                    orderid            = order.OrderId.ToString();
                    original_sellprice = Math.Round(Convert.ToDecimal(sqlitedb.ReadStateTable("initial_investment", AlgorithmName)) / Convert.ToDecimal(unstableSymbolQuantityInAccount), 2);
                    // Calculate ROI and record it
                    decimal initial_investment = Convert.ToDecimal(sqlitedb.ReadStateTable("initial_investment", AlgorithmName));
                    ROI = (original_sellprice / initial_investment) * 100M;
                    sqlitedb.InsertROI(ROI.ToString(), AlgorithmName);
                    // update state table output_step_state
                    sqlitedb.UpdateStateTable("previous_step_order_id", orderid, AlgorithmName);
                    sqlitedb.UpdateStateTable("output", "4", AlgorithmName);
                }
            FDQStepController();
        }