//Should yield the same results regardless of what chandles are used for input, but faster on closer candles private static List <PriceCandle> CreatePriceCandleList(IReadOnlyList <PriceCandle> price_candles, TimeScale time_scale) { if (price_candles.Count == 0) { return(new List <PriceCandle>()); } //Set times next candle DateTimeUTC open_time = PriceCandle.GetOpenTime(price_candles[0].OpenTime, time_scale); DateTimeUTC close_time = PriceCandle.GetCloseTime(open_time, time_scale); int current_candle_index = 0; //if we do not start on a candle open: if (price_candles[0].OpenTime != open_time) { //Wind forward to first candle in the next candle while ((current_candle_index < price_candles.Count) && (price_candles[current_candle_index].OpenTime != close_time)) { current_candle_index++; } //Set times next candle open_time = PriceCandle.GetOpenTime(close_time, time_scale); close_time = PriceCandle.GetCloseTime(open_time, time_scale); } if (price_candles.Count <= current_candle_index) { return(new List <PriceCandle>()); } //Work List <PriceCandle> new_price_candles = new List <PriceCandle>(); double open_bid = price_candles[current_candle_index].OpenBid; double high_bid = price_candles[current_candle_index].HighBid; double low_bid = price_candles[current_candle_index].LowBid; double close_bid = price_candles[current_candle_index].CloseBid; double open_ask = price_candles[current_candle_index].OpenAsk; double high_ask = price_candles[current_candle_index].HighAsk; double low_ask = price_candles[current_candle_index].LowAsk; double close_ask = price_candles[current_candle_index].CloseAsk; while (current_candle_index < price_candles.Count) { //If it is the end of our current price candle if (price_candles[current_candle_index].CloseTime == close_time) { // Update everything one last time //open_bid = high_bid = price_candles[current_candle_index].HighBid; low_bid = price_candles[current_candle_index].LowBid; close_bid = price_candles[current_candle_index].CloseBid; //open_ask = high_ask = price_candles[current_candle_index].HighAsk; low_ask = price_candles[current_candle_index].LowAsk; close_ask = price_candles[current_candle_index].CloseAsk; // Create candle new_price_candles.Add(new PriceCandle( open_time, time_scale, open_bid, high_bid, low_bid, close_bid, open_ask, high_ask, low_ask, close_ask, 0, 0)); //Set begin and end for our next candle open_time = PriceCandle.GetOpenTime(close_time, time_scale); close_time = PriceCandle.GetCloseTime(open_time, time_scale); //scroll forward current_candle_index++; //And if there is a next candle: if (current_candle_index < price_candles.Count) { // initialize open_bid = price_candles[current_candle_index].OpenBid; high_bid = price_candles[current_candle_index].HighBid; low_bid = price_candles[current_candle_index].LowBid; close_bid = price_candles[current_candle_index].CloseBid; open_ask = price_candles[current_candle_index].OpenAsk; high_ask = price_candles[current_candle_index].HighAsk; low_ask = price_candles[current_candle_index].LowAsk; close_ask = price_candles[current_candle_index].CloseAsk; } } else { //If we are not the end we must be some part so just update data //open_bid = high_bid = Math.Max(high_bid, price_candles[current_candle_index].HighBid); low_bid = Math.Min(low_bid, price_candles[current_candle_index].LowBid); close_bid = price_candles[current_candle_index].CloseBid; // open_ask = high_ask = Math.Max(high_ask, price_candles[current_candle_index].HighAsk); low_ask = Math.Min(low_ask, price_candles[current_candle_index].LowAsk); close_ask = price_candles[current_candle_index].CloseAsk; //scroll forward current_candle_index++; } } return(new_price_candles); }
private static List <PriceCandle> CreateSecond1(IReadOnlyList <Price> price_list_unclean) { //Build second 1 List <PriceCandle> second1 = new List <PriceCandle>(); DateTimeUTC current_time = price_list_unclean[0].Time; DateTimeUTC open_time = PriceCandle.GetOpenTime(current_time, TimeScale.Second1); DateTimeUTC close_time = PriceCandle.GetCloseTime(current_time, TimeScale.Second1); int price_index = 0; //if we do not start on a candle open: if (!current_time.Equals(open_time)) { //Wind forward to first candle in the next candle while (price_list_unclean[price_index].Time != close_time) { price_index++; } } double open_bid = price_list_unclean[price_index].Bid; double high_bid = price_list_unclean[price_index].Bid; double low_bid = price_list_unclean[price_index].Bid; double close_bid = price_list_unclean[price_index].Bid; double open_ask = price_list_unclean[price_index].Ask; double high_ask = price_list_unclean[price_index].Ask; double low_ask = price_list_unclean[price_index].Ask; double close_ask = price_list_unclean[price_index].Ask; while (price_index < price_list_unclean.Count) { //If it is our current price candle if (price_list_unclean[price_index].Time <= close_time) { //Update data //open_bid = close_bid; high_bid = Math.Max(high_bid, price_list_unclean[price_index].Bid); low_bid = Math.Min(low_bid, price_list_unclean[price_index].Bid); close_bid = price_list_unclean[price_index].Bid; //open_ask = close_bid; high_ask = Math.Max(high_ask, price_list_unclean[price_index].Ask); low_ask = Math.Min(low_ask, price_list_unclean[price_index].Ask); close_ask = price_list_unclean[price_index].Ask; price_index++; } else { //Other wise append a new candle second1.Add(new PriceCandle(open_time, TimeScale.Second1, open_bid, high_bid, low_bid, close_bid, open_ask, high_ask, low_ask, close_ask, 0, 0)); //And prepare the next one open_time = close_time; close_time = PriceCandle.GetCloseTime(open_time, TimeScale.Second1); open_bid = close_bid; high_bid = Math.Max(close_bid, price_list_unclean[price_index].Bid); low_bid = Math.Min(close_bid, price_list_unclean[price_index].Bid); close_bid = price_list_unclean[price_index].Bid; open_ask = close_ask; high_ask = Math.Max(close_ask, price_list_unclean[price_index].Ask); low_ask = Math.Min(close_ask, price_list_unclean[price_index].Ask); close_ask = price_list_unclean[price_index].Ask; //current.Clear(); //current.Add(new Price(close_time, second1.Last().CloseBid, second1.Last().CloseAsk)); } } //If last candle was complete if (price_list_unclean.Last().Time == close_time) { //Add last candle second1.Add(new PriceCandle(open_time, TimeScale.Second1, open_bid, high_bid, low_bid, close_bid, open_ask, high_ask, low_ask, close_ask, 0, 0)); } return(second1); }