public async Task SaveSessions(ForexSessionsDTO session)
        {
            foreach (var sessionIn in session.sessions)
            {
                //var sessionIn = _mapper.Map<ForexSessionDTO>(session);
                var sessionModel = _mapper.Map <ForexSession>(sessionIn);
                var sessionMongo = _mapper.Map <ForexSessionMongo>(sessionModel);
                sessionMongo.idinfo       = sessionIn.Id;
                sessionMongo.ExperimentId = "NoExperiment";
                var findSession = await _context.ForexSessions.CountDocumentsAsync(x => x.Id == sessionMongo.Id);

                if (findSession == 0)
                {
                    Console.WriteLine($"Adding Session {sessionIn.Id}");
                    await _context.ForexSessions.InsertOneAsync(_mapper.Map <ForexSessionMongo>(sessionMongo));
                }
                else
                {
                    Console.WriteLine($"Updating Session {sessionIn.Id}");
                    var replace = await _context.ForexSessions.ReplaceOneAsync(sess => sess.Id == sessionMongo.Id, sessionMongo);
                }
            }
        }
Esempio n. 2
0
        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];
                    }
                }
            }
        }
Esempio n. 3
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);
                }
            }
        }
Esempio n. 4
0
        public async Task <ActionResult> Post([FromBody] ForexSessionsDTO sessions)
        {
            await _forexSessionMap.SaveSessions(sessions);

            return(Ok());
        }