Esempio n. 1
0
        static async Task runTestDataDomain(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 session = new ForexSession()
            {
                Id          = sessionName,
                SessionType = "live",
                SessionUser = new SessionUser()
                {
                    Accounts = new Accounts()
                    {
                        Primary = new Account()
                        {
                            Id             = "primary",
                            Cash           = 191.41,
                            Trades         = new List <Trade>(),
                            ClosedTrades   = new List <Trade>(),
                            BalanceHistory = new List <BalanceHistory>()
                        }
                    }
                },
                Strategy = new Strategy()
                {
                    ruleName   = "RSI",
                    window     = 15,
                    position   = "short",
                    stopLoss   = 1.007,
                    takeProfit = 0.998,
                    units      = 100
                }
            };

            var realtimeprices = new Dictionary <string, ForexPricesDTO>();
            var trashName      = new List <ForexDailyPriceDTO>();

            foreach (var pair in pairs)
            {
                var urlGetDailyPricesRange = $"http://localhost:5002/api/forexdailyprices/{pair}/{startDate}/{endDate}";
                var dailypricesRange       = await GetAsync <List <ForexDailyPriceDTO> >(urlGetDailyPricesRange);

                trashName = dailypricesRange;
                await dailypricesRange.ParallelForEachAsync(async x =>
                {
                    var pricesResult = await GetRealPrices(x.Pair, x.Datetime.ToString("yyyyMMdd"));
                    realtimeprices.Add(pricesResult.Item1, pricesResult.Item2);
                }, maxDegreeOfParallelism : 8);


                /*int step = 8;
                 * int end = 0;
                 *
                 * for(int i=0;i<dailypricesRange.Count();i+=step)
                 * {
                 *  end = i + step;
                 *  if(end > dailypricesRange.Count()-1)
                 *      end =  dailypricesRange.Count()-1;
                 *
                 *  var batch = new List<ForexDailyPriceDTO>();
                 *  for(int j = i;j<end;j++)
                 *  {
                 *      batch.Add(dailypricesRange[j]);
                 *  }
                 *
                 *  var batchTasks = batch.Select(x => GetRealPrices(x.Pair,x.Datetime.ToString("yyyyMMdd")));
                 *  var batchRealPrices = await Task.WhenAll(batchTasks);
                 *
                 *  foreach(var pricelist in batchRealPrices)
                 *  {
                 *      realtimeprices.Add(pricelist.Item1,pricelist.Item2);
                 *  }
                 * }*/
            }

            foreach (var dailyPrice in trashName)
            {
                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 = realtimeprices[pair + currDayRealTime];//await GetAsync<ForexPricesDTO>(urlgetdailyrealprices);
                    Console.WriteLine($"{pair} {currDay}");
                    bool shouldTrade = await ShouldExecuteTrade(server, pair, session.Strategy.ruleName, currDay, session.Strategy.window);

                    //await executeTrade(server,session,dailyrealprices.prices[0],currDayRealTime);
                    var currPrice      = dailyrealprices.prices[0];
                    var openTradeUnits = session.SessionUser.Accounts.Primary.Trades.Select(x => x.Units);
                    if (shouldTrade)
                    {
                        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
                        };
                        session.ExecuteTrade(pair, trade.Price, trade.Units, trade.StopLoss, trade.TakeProfit, trade.Long, trade.Date);
                    }
                    var tradepairs = session.SessionUser.Accounts.Primary.Trades.Select(x => x.Pair);
                    if (tradepairs.Contains(pair))
                    {
                        foreach (var realPrice in dailyrealprices.prices.Take(100))
                        {
                            session.UpdateSession(pair, realPrice.Bid, realPrice.Ask, realPrice.Time.ToString());
                            //Console.WriteLine($" {realPrice.Time} {realPrice.Bid}");
                            //var responsePriceBody = await PatchAsync<ForexPriceDTO>(realPrice,urlpatchprice);
                        }
                        //sessionList = await GetAsync<ForexSessionsDTO>(urlget);
                        //session = sessionList.sessions[0];
                    }
                }
            }
        }
 public async Task UpdateSessionHelper(ForexSession sess)
 {
     var sessionMongo = _mapper.Map <ForexSessionMongo>(sess);
     var replace      = await _context.ForexSessions.ReplaceOneAsync(sess => sess.Id == sessionMongo.Id, sessionMongo);
 }