static async Task executeTrade(string server, ForexSessionDTO session, ForexPriceDTO currPrice, string currDay) { //string currDay = currPrice.UTCTime.ToString("yyyy-MM-dd"); string urlpatchtrade = $"http://{server}/api/forexsession/executetrade/{session.Id}"; string urlrealtrade = $"http://{server}/api/forextrade"; var openTradesDTO = await GetAsync <ForexOpenTradesDTO>(urlrealtrade); var openTradeUnits = openTradesDTO.Trades.Select(x => x.Units);//session.SessionUser.Accounts.Primary.Trades.Select(x => x.Units); var trade = new ForexTradeDTO() { Pair = currPrice.Instrument, Price = currPrice.Bid, Units = (int)getFiFo(openTradeUnits, session.Strategy.units), StopLoss = currPrice.Bid * session.Strategy.stopLoss, TakeProfit = currPrice.Bid * session.Strategy.takeProfit, Date = currDay }; var realtrade = new ForexTradeDTO() { Pair = pairToInstrument[currPrice.Instrument], Price = currPrice.Bid, Units = (int)getFiFo(openTradeUnits, session.Strategy.units), StopLoss = currPrice.Bid * session.Strategy.stopLoss, TakeProfit = currPrice.Bid * session.Strategy.takeProfit, Date = currDay }; var responseTradeBody = await PatchAsync <ForexTradeDTO>(trade, urlpatchtrade); var responseRealTradeBody = await PostAsync <ForexTradeDTO>(realtrade, urlrealtrade); }
static async Task runTestData(string server) { string sessionName = "liveSession2"; string urlget = $"http://localhost:5002/api/forexsession/{sessionName}"; string urlpost = $"http://localhost:5002/api/forexsession"; string urlpatchprice = $"http://localhost:5002/api/forexsession/updatesession/{sessionName}"; var startDate = "20190324"; var endDate = "20200522"; var sessionList = await GetAsync <ForexSessionsDTO>(urlget); if (sessionList.sessions.Length > 0) { await client.DeleteAsync(urlget); } var sessionIn = new ForexSessionDTO() { Id = sessionName, SessionType = "live", SessionUser = new SessionUserDTO() { Accounts = new AccountsDTO() { Primary = new AccountDTO() { Id = "primary", Cash = 191.41, } } }, Strategy = new StrategyDTO() { ruleName = "RSI", window = 15, position = "short", stopLoss = 1.007, takeProfit = 0.998, units = 100 } }; //var sessionsList = new ForexSessionDTO[]{sessionIn}; var sessions = new ForexSessionsDTO() { sessions = new ForexSessionDTO[] { sessionIn } }; var responsePostBody = await PostAsync <ForexSessionsDTO>(sessions, urlpost); var sessionsDTO = await GetAsync <ForexSessionsDTO>(urlget); var session = sessionsDTO.sessions[0]; var urlGetDailyPricesRange = $"http://localhost:5002/api/forexdailyprices/AUDUSD/{startDate}/{endDate}"; var dailypricesRange = await GetAsync <List <ForexDailyPriceDTO> >(urlGetDailyPricesRange); foreach (var dailyPrice in dailypricesRange) { foreach (var pair in pairs) { var currDay = dailyPrice.Datetime.ToString("yyyy-MM-dd"); var currDayRealTime = dailyPrice.Datetime.ToString("yyyyMMdd"); var urlgetdailyrealprices = $"http://localhost:5002/api/forexdailyrealprices/{pair}/{currDayRealTime}"; var dailyrealprices = await GetAsync <ForexPricesDTO>(urlgetdailyrealprices); Console.WriteLine($"{pair} {currDay}"); bool shouldTrade = await ShouldExecuteTrade(server, pair, session.Strategy.ruleName, currDay, session.Strategy.window); if (shouldTrade) { await executeTrade(server, session, dailyrealprices.prices[0], currDayRealTime); sessionList = await GetAsync <ForexSessionsDTO>(urlget); session = sessionList.sessions[0]; } var tradepairs = session.SessionUser.Accounts.Primary.Trades.Select(x => x.Pair); if (tradepairs.Contains(pair)) { foreach (var realPrice in dailyrealprices.prices.Take(100)) { //Console.WriteLine($" {realPrice.Time} {realPrice.Bid}"); var responsePriceBody = await PatchAsync <ForexPriceDTO>(realPrice, urlpatchprice); } sessionList = await GetAsync <ForexSessionsDTO>(urlget); session = sessionList.sessions[0]; } } } }
static async Task runDailyTrader(string server) { string sessionName = "liveSessionRSICSharp"; string urlget = $"http://{server}/api/forexsession/{sessionName}"; string urlpost = $"http://{server}/api/forexsession"; string urlpatchprice = $"http://{server}/api/forexsession/updatesession/{sessionName}"; string urlgetdailyrealprices = $"http://{server}/api/forexprices"; var sessionList = await GetAsync <ForexSessionsDTO>(urlget); if (sessionList.sessions.Length == 0) { var sessionIn = new ForexSessionDTO() { Id = sessionName, StartDate = DateTime.Now.ToString("yyyy-MM-dd"), SessionType = "live", SessionUser = new SessionUserDTO() { Accounts = new AccountsDTO() { Primary = new AccountDTO() { Id = "primary", Cash = 191.41, } } }, Strategy = new StrategyDTO() { ruleName = "RSI", window = 15, position = "short", stopLoss = 1.007, takeProfit = 0.998, units = 100 } }; var sessions = new ForexSessionsDTO() { sessions = new ForexSessionDTO[] { sessionIn } }; var responsePostBody = await PostAsync <ForexSessionsDTO>(sessions, urlpost); } while (true) { try { var sessionsDTO = await GetAsync <ForexSessionsDTO>(urlget); var session = sessionsDTO.sessions[0]; var currDay = DateTime.Now.ToString("yyyy-MM-dd"); var currDayTrade = DateTime.Now.ToString("yyyy-MM-dd"); var prices = await GetAsync <ForexPricesDTO>(urlgetdailyrealprices); foreach (var pair in pairs) { var days = session.SessionUser .Accounts .Primary .Trades .Where(x => x.Pair == pair) .Select(x => x.OpenDate).ToList(); days.InsertRange (0, session .SessionUser .Accounts .Primary .ClosedTrades .Where(x => x.Pair == pair) .Select(x => x.OpenDate).ToList() ); var price = prices.prices.FirstOrDefault(x => x.Instrument == pair); if (!days.Contains(currDayTrade)) { bool shouldTrade = await ShouldExecuteTrade(server, pair, "RSI", currDay, 14); Console.WriteLine($"{pair} {price.Bid} {shouldTrade}"); if (shouldTrade) { await executeTrade(server, session, price, currDayTrade); sessionList = await GetAsync <ForexSessionsDTO>(urlget); session = sessionList.sessions[0]; } } var responsePriceBody = await PatchAsync <ForexPriceDTO>(price, urlpatchprice); } Console.WriteLine("Updating"); await Task.Delay(1000 *30 *1); } catch (Exception e) { Console.WriteLine(e.Message); } } }