Advertisement
Digitally_Neon

StockHandler.cs - Robinhood Client

Jan 20th, 2021
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using robin.Classes;
  7. using Newtonsoft.Json;
  8. using robin.Classes.Entities;
  9. using System.IO;
  10. using robin_client.Classes.Entities;
  11. using robin_client.Classes;
  12. using System.Threading;
  13.  
  14. namespace robin_client.Commands
  15. {
  16.     class StockHandler
  17.     {
  18.         public static string toWrite;
  19.         [Command("Bot to auto-sell and auto-buy stocks.", "SHandler", "StockHandler", "SHandler", "SHd")]
  20.         public static async Task Do()
  21.         {
  22.             var allconfigs = DefaultSResultCache.Get();
  23.             List<SResult> l = new List<SResult>();
  24.             Console.WriteLine("Would you like to narrow down all configs by a specific stock? (type the symbol of the stock, or press enter if no)");
  25.             string inp = Console.ReadLine();
  26.             Console.Clear();
  27.             if (inp.Length > 0)
  28.                 for (int i = 0; i < allconfigs.Length; i++)
  29.                     if (allconfigs[i].symbol.ToLower() != inp.ToLower())
  30.                         allconfigs[i] = null;
  31.             if (allconfigs.Length > 0)
  32.                 for (int i = 0; i < allconfigs.Length; i++)
  33.                 {
  34.                     if(allconfigs[i] != null)
  35.                     {
  36.                         Console.WriteLine("\n");
  37.                         toWrite = $"[{i + 1}] {allconfigs[i].symbol}\n{{\n  {(allconfigs[i].confName != null ? allconfigs[i].confName : "[NO NAME]")}\n  Made on [{allconfigs[i].made}]\n}}";
  38.                         Console.WriteLine(toWrite);
  39.                     }
  40.                 }
  41.             Console.WriteLine("\n\nType the corresponding number of the config you'd like to use.");
  42.             inp = Console.ReadLine();
  43.             try
  44.             {
  45.                 int num = int.Parse(inp);
  46.                 SResult selConf = allconfigs[num - 1];
  47.                 var stock = await Program.Client.StockProcessor.GetStockFromSymbol(selConf.symbol);
  48.                 if(stock.Success)
  49.                 {
  50.                     var trstock = await Program.Client.StockProcessor.GetTradeableStock(stock.Result[0]);
  51.                     if(trstock.Success)
  52.                     {
  53.                         Console.WriteLine("How much would you like to invest?");
  54.                         float invamount = float.Parse(Console.ReadLine());
  55.                         Console.WriteLine("Is this a fake (paper) investment? (y/n)");
  56.                         inp = Console.ReadLine();
  57.                         bool paper = true;
  58.                         if (inp.Length > 0 & inp.ToLower().First() == 'n')
  59.                             paper = false;
  60.                         await StartBot(trstock.Result[0], selConf, invamount, paper);
  61.                     }
  62.                 }
  63.             }
  64.             catch(Exception E)
  65.             {
  66.                 Console.WriteLine("Invalid input.");
  67.                 var errorl = new StockLog()
  68.                 {
  69.                     Context = null,
  70.                     Information = E.ToString(),
  71.                     Title = "Error with StockHandler!",
  72.                     Type = LogSeverity.Error
  73.                 };
  74.                 Program.LogManager.SaveLog(errorl);
  75.             }
  76.         }
  77.         private static async Task StartBot(TradeableStock stock, SResult config, float investAmt, bool isPaper)
  78.         {
  79.             Console.Clear();
  80.             Console.WriteLine($"Started bot with {config.symbol}");
  81.             if(!isPaper)
  82.             {
  83.                 // order
  84.             }
  85.             float lastPercent = 0;
  86.             float profitPercent;
  87.             float maximumOverallProfit = int.MinValue;
  88.             if(!config.dayTrade)
  89.             {
  90.                 Console.WriteLine("Day-trading is not enabled for this configuration. Waiting 24 hours before starting bot.");
  91.                 await Task.Delay(0x5265C00);
  92.             }
  93.             while(true)
  94.             {
  95.                 var newstock_res = await Program.Client.StockProcessor.GetTradeableStock(stock.AssociatedStock);
  96.                 newstock_res.Result[0].Ask_Price = float.Parse(Console.ReadLine());
  97.                 if (newstock_res.Success)
  98.                 {
  99.                     var newstock = newstock_res.Result[0];
  100.                     profitPercent = (newstock.Ask_Price - stock.Ask_Price) / newstock.Ask_Price * 100;
  101.                     Console.WriteLine($"Profit is at {profitPercent}% [${investAmt * (profitPercent / 100) + investAmt}] from [${investAmt}]");
  102.                     if (profitPercent > maximumOverallProfit)
  103.                         maximumOverallProfit = profitPercent;
  104.                     if (profitPercent <= config.lowThreshold)
  105.                     {
  106.                         // sell
  107.                         Console.WriteLine("Selling stock.");
  108.                         break;
  109.                     }
  110.                     if(maximumOverallProfit >= config.profitThreshold & lastPercent > profitPercent)
  111.                     {
  112.                         // sell
  113.                         Console.WriteLine("Selling stock before loss of profit.");
  114.                         break;
  115.                     }
  116.                     if(profitPercent > config.maxProfitThreshold)
  117.                     {
  118.                         // sell
  119.                         Console.WriteLine("Selling stock due to maximum profit amount achieved.");
  120.                         break;
  121.                     }
  122.                     lastPercent = profitPercent;
  123.                 }
  124.                 else
  125.                     await Task.Delay(10000);
  126.                 await Task.Delay(2000);
  127.             }
  128.             Console.Clear();
  129.         }
  130.     }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement