Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using robin.Classes;
- using Newtonsoft.Json;
- using robin.Classes.Entities;
- using System.IO;
- using robin_client.Classes.Entities;
- using robin_client.Classes;
- using System.Threading;
- namespace robin_client.Commands
- {
- class StockHandler
- {
- public static string toWrite;
- [Command("Bot to auto-sell and auto-buy stocks.", "SHandler", "StockHandler", "SHandler", "SHd")]
- public static async Task Do()
- {
- var allconfigs = DefaultSResultCache.Get();
- List<SResult> l = new List<SResult>();
- 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)");
- string inp = Console.ReadLine();
- Console.Clear();
- if (inp.Length > 0)
- for (int i = 0; i < allconfigs.Length; i++)
- if (allconfigs[i].symbol.ToLower() != inp.ToLower())
- allconfigs[i] = null;
- if (allconfigs.Length > 0)
- for (int i = 0; i < allconfigs.Length; i++)
- {
- if(allconfigs[i] != null)
- {
- Console.WriteLine("\n");
- toWrite = $"[{i + 1}] {allconfigs[i].symbol}\n{{\n {(allconfigs[i].confName != null ? allconfigs[i].confName : "[NO NAME]")}\n Made on [{allconfigs[i].made}]\n}}";
- Console.WriteLine(toWrite);
- }
- }
- Console.WriteLine("\n\nType the corresponding number of the config you'd like to use.");
- inp = Console.ReadLine();
- try
- {
- int num = int.Parse(inp);
- SResult selConf = allconfigs[num - 1];
- var stock = await Program.Client.StockProcessor.GetStockFromSymbol(selConf.symbol);
- if(stock.Success)
- {
- var trstock = await Program.Client.StockProcessor.GetTradeableStock(stock.Result[0]);
- if(trstock.Success)
- {
- Console.WriteLine("How much would you like to invest?");
- float invamount = float.Parse(Console.ReadLine());
- Console.WriteLine("Is this a fake (paper) investment? (y/n)");
- inp = Console.ReadLine();
- bool paper = true;
- if (inp.Length > 0 & inp.ToLower().First() == 'n')
- paper = false;
- await StartBot(trstock.Result[0], selConf, invamount, paper);
- }
- }
- }
- catch(Exception E)
- {
- Console.WriteLine("Invalid input.");
- var errorl = new StockLog()
- {
- Context = null,
- Information = E.ToString(),
- Title = "Error with StockHandler!",
- Type = LogSeverity.Error
- };
- Program.LogManager.SaveLog(errorl);
- }
- }
- private static async Task StartBot(TradeableStock stock, SResult config, float investAmt, bool isPaper)
- {
- Console.Clear();
- Console.WriteLine($"Started bot with {config.symbol}");
- if(!isPaper)
- {
- // order
- }
- float lastPercent = 0;
- float profitPercent;
- float maximumOverallProfit = int.MinValue;
- if(!config.dayTrade)
- {
- Console.WriteLine("Day-trading is not enabled for this configuration. Waiting 24 hours before starting bot.");
- await Task.Delay(0x5265C00);
- }
- while(true)
- {
- var newstock_res = await Program.Client.StockProcessor.GetTradeableStock(stock.AssociatedStock);
- newstock_res.Result[0].Ask_Price = float.Parse(Console.ReadLine());
- if (newstock_res.Success)
- {
- var newstock = newstock_res.Result[0];
- profitPercent = (newstock.Ask_Price - stock.Ask_Price) / newstock.Ask_Price * 100;
- Console.WriteLine($"Profit is at {profitPercent}% [${investAmt * (profitPercent / 100) + investAmt}] from [${investAmt}]");
- if (profitPercent > maximumOverallProfit)
- maximumOverallProfit = profitPercent;
- if (profitPercent <= config.lowThreshold)
- {
- // sell
- Console.WriteLine("Selling stock.");
- break;
- }
- if(maximumOverallProfit >= config.profitThreshold & lastPercent > profitPercent)
- {
- // sell
- Console.WriteLine("Selling stock before loss of profit.");
- break;
- }
- if(profitPercent > config.maxProfitThreshold)
- {
- // sell
- Console.WriteLine("Selling stock due to maximum profit amount achieved.");
- break;
- }
- lastPercent = profitPercent;
- }
- else
- await Task.Delay(10000);
- await Task.Delay(2000);
- }
- Console.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement