Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:35:52+00:00 2026-06-13T22:35:52+00:00

I would like to achieve same results from code sample below by using syntax

  • 0

I would like to achieve same results from code sample below by using syntax LINQ query to object.
Please, help me guys.

public static List<OHLC> OHLC(List<FileRaw> fileRawsList, TimeSpan timeSpan, DateTime beginDate, DateTime endOfLoopDate)
    {

        List<OHLC> ohlcList = new List<OHLC>();

        for (DateTime i = beginDate; i < endOfLoopDate; )
        {
            DateTime iLow = i;
            DateTime iMax = i.Add(timeSpan);
            OHLC ohlcRaw = new OHLC()
                {
                    Open = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Bid).DefaultIfEmpty().First(),
                    High = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Bid).DefaultIfEmpty().Max(),
                    Low = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Bid).DefaultIfEmpty().Min(),
                    Close = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Bid).DefaultIfEmpty().Last(),
                    Time = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Time).DefaultIfEmpty(iLow).First()
                };

            i = i.Add(timeSpan);
            ohlcList.Add(ohlcRaw);
        }
        return ohlcList;
    }

it produce

Open: 1.229570, High: 1.233580, Low: 1.221730, Close: 1.224630, Date: 01/08/2012 00:00:02
Open: 1.224620, High: 1.240460, Low: 1.213320, Close: 1.217490, Date: 02/08/2012 00:00:03
Open: 1.217530, High: 1.239160, Low: 1.217230, Close: 1.238650, Date: 03/08/2012 00:00:02
Open: 0,        High: 0,        Low: 0,        Close: 0,        Date: 04/08/2012 00:00:02
Open: 1.239720, High: 1.239830, Low: 1.239180, Close: 1.239460, Date: 05/08/2012 17:00:03
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:35:54+00:00Added an answer on June 13, 2026 at 10:35 pm

    This should give the raw groups:

     return fileRawList.Where(file => file.Time >= beginDate)
                        .Where(file => file.Time < endofLoopDate)
                        .OrderBy(file => file.Time)
                        .GroupBy(file => file.Time.Ticks / timeSpan.Ticks,
                                 (k, g) => new OHLC()
                                      {
                                        Open = g.Select(p => p.Bid).DefaultIfEmpty().First(),
                                        High = g.Select(p => p.Bid).DefaultIfEmpty().Max(),
                                        Low = g.Select(p => p.Bid).DefaultIfEmpty().Min(),
                                        Close = g.Select(p => p.Bid).DefaultIfEmpty().Last(),
                                        Time = g.Select(p => p.Time).DefaultIfEmpty().First()
                                              })
                         .ToList();
    

    While this should give a regular stream of OHLC:

    var rawGroups = fileRawList.Where(file => file.Time >= beginDate)
                        .Where(file => file.Time < endofLoopDate)
                        .OrderBy(file => file.Time)
                        .ToLookup(file => (file.Time.Ticks - beginDate.Ticks) / timeSpan.Ticks,
                                  file => new {Bid = file.Bid, Time = file.Time})
                        .ToDictionary( g => g.Key, g => new OHLC()
                                      {
                                        Open = g.Select(p => p.Bid).DefaultIfEmpty(0).First(),
                                        High = g.Select(p => p.Bid).DefaultIfEmpty(0).Max(),
                                        Low = g.Select(p => p.Bid).DefaultIfEmpty(0).Min(),
                                        Close = g.Select(p => p.Bid).DefaultIfEmpty(0).Last(),
                                        Time = g.Select(p => p.Time).First()
                                              });
    
    return Enumerable.Range(0,(Int32)((endofLoopDate.Ticks - beginDate.Ticks)/timeSpan.Ticks))
                     .Select(i => rawGroups.Keys.Contains(i) ? 
                                  rawGroups[i] :
                                  new OHLC()
                                  {
                                   Open = 0,
                                   High = 0,
                                   Low = 0,
                                   Close = 0,
                                   Time = new DateTime(beginDate.Ticks + k*timeSpan.Ticks)
                                  }).ToList();
    

    Alternative:

    var result = Enumerable.Range(0,(Int32)((endofLoopDate.Ticks - beginDate.Ticks)/timeSpan.Ticks))
                           .GroupJoin(fileRawList.Where(file => file.Time >= beginDate)
                                                 .Where(file => file.Time < endofLoopDate)
                                                 .OrderBy(file => file.Time),
                                      i => i,
                                      file => (file.Time.Ticks - beginDate.Ticks) / timeSpan.Ticks,
                                      (k,g) => new OHLC()
                                          {
                                           Open = g.Select(p => p.Bid).DefaultIfEmpty(0).First(),
                                           High = g.Select(p => p.Bid).DefaultIfEmpty(0).Max(),
                                           Low = g.Select(p => p.Bid).DefaultIfEmpty(0).Min(),
                                           Close = g.Select(p => p.Bid).DefaultIfEmpty(0).Last(),
                                           Time = g.Any() ? 
                                             g.Select(p => p.Time).First() :
                                             new DateTime(beginDate.Ticks + i*timeSpan.Ticks)
                                          })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to achieve something very similar to Microsoft Access query designer -
I would like to be able to build a jQuery object from a string
I would like to achive the same functinoanlity in the UITextField control as Google
I would like to achieve the following: I want a free Application Lifecycle Management
I would like to achieve the following: On homepage load, display modal box Within
I would like to achieve something similar to how scala defines Map as both
I would like to achieve this functionality. <p:column> <p:commandLink value=prihlasit oncomplete=dlg.show(); action=#{signForProjectBean.setProjectForDetail(item)} /> </p:column>
I would like to achieve the following, I need to generate report for multiple
What I would like to achieve here is a user selects an operator, e.g.
Here is what I would like to achieve: I have a web service A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.