Ejemplo n.º 1
0
        public void Add(DateTime date_time, double bid, double ask)
        {
            PriceCandle candle = Second1.GetFuture(0);

            // Compute equity
            this.Equity = Cash;
            foreach (TradingOrder open_order in OpenOrders.Values)
            {
                Equity += open_order.ComputeValue(Prices.GetHistory(0));
            }


            Prices.Step();
            Second1.Step();
            if ((0 < Minute1.FutureCount) && (Prices.GetHistory(0).Time == Minute1.GetFuture(0).CloseTime))
            {
                Minute1.Step();
            }
            else
            {
                // if it is not the end of a minute the others also do not nee to step
                return;
            }

            //if ((0 < Minute15.FutureCount) && (Prices.GetHistory(0).Time == Minute15.GetFuture(0).CloseTime))
            //{
            //    Minute15.Step();
            //}

            //if ((0 < Minute30.FutureCount) && (Prices.GetHistory(0).Time == Minute30.GetFuture(0).CloseTime))
            //{
            //    Minute30.Step();
            //}

            //if ((0 < Hour1.FutureCount) && (Prices.GetHistory(0).Time == Hour1.GetFuture(0).CloseTime))
            //{
            //    Hour1.Step();
            //}

            //if ((0 < Hour4.FutureCount) && (Prices.GetHistory(0).Time == Hour4.GetFuture(0).CloseTime))
            //{
            //    Hour4.Step();
            //}

            //if ((0 < Day1.FutureCount) && (Prices.GetHistory(0).Time == Day1.GetFuture(0).CloseTime))
            //{
            //    Day1.Step();
            //}

            //if ((0 < Week1.FutureCount) && (Prices.GetHistory(0).Time == Week1.GetFuture(0).CloseTime))
            //{
            //    Week1.Step();
            //}

            //if ((0 < Month1.FutureCount) && (Prices.GetHistory(0).Time == Month1.GetFuture(0).CloseTime))
            //{
            //    Month1.Step();
            //}
        }
Ejemplo n.º 2
0
        public static PriceSet Read(BinaryReader reader)
        {
            TradingSymbol symbol = TradingSymbol.Read(reader);
            int           price_list_clean_count = reader.ReadInt32();
            List <Price>  price_list_clean       = new List <Price>();

            for (int price_index = 0; price_index < price_list_clean_count; price_index++)
            {
                price_list_clean.Add(Price.Read(reader));
            }

            int second1_count          = reader.ReadInt32();
            List <PriceCandle> second1 = new List <PriceCandle>();

            for (int price_index = 0; price_index < second1_count; price_index++)
            {
                second1.Add(PriceCandle.Read(reader));
            }
            return(new PriceSet(symbol, price_list_clean, second1));
        }
 public void AddStep(PriceCandle priceCandle)
 {
     throw new NotImplementedException("Not for fast mode");
 }
        public static List <TradingOrder> CheckOrderLimits(IDictionary <int, TradingOrder> open_orders, PriceCandle candle)
        {
            //TODO there is something nasty here if a candle hits the stoploss as well as the take profit.
            //Conservatively we go for the stop loss but this should be configurable.
            //UPDATE for second candles this should not matter
            List <TradingOrder> closing_orders = new List <TradingOrder>();

            //TODO speed this up, cos most of the time no orders would close
            foreach (TradingOrder trade_order in open_orders.Values)
            {
                //Check stop loss and take profits
                if (trade_order.UseLimits)
                {
                    switch (trade_order.OrderType)
                    {
                    case TradingOrderType.Long:
                        if (candle.LowBid < trade_order.StopLoss)
                        {
                            closing_orders.Add(trade_order.Close(candle.CloseTime, trade_order.StopLoss));
                        }
                        else if (trade_order.TakeProfit < candle.HighBid)
                        {
                            closing_orders.Add(trade_order.Close(candle.CloseTime, trade_order.TakeProfit));
                        }
                        break;

                    case TradingOrderType.Short:
                        if (trade_order.StopLoss < candle.HighAsk)
                        {
                            closing_orders.Add(trade_order.Close(candle.CloseTime, trade_order.StopLoss));
                        }
                        else if (candle.LowAsk < trade_order.TakeProfit)
                        {
                            closing_orders.Add(trade_order.Close(candle.CloseTime, trade_order.TakeProfit));
                        }
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
            return(closing_orders);
        }
        public void StepSecond()
        {
            if (Second1.FutureCount == 0)
            {
                throw new Exception("No future to step to");
            }
            PriceCandle candle = Second1.GetFuture(0);

            // Close orders
            List <TradingOrder> ClosingOrders = CheckOrderLimits(OpenOrders, candle);

            foreach (TradingOrder closed_order in ClosingOrders)
            {
                this.OpenOrders.Remove(closed_order.OrderTicket);
                this.ClosedOrders.Add(closed_order);
                this.Cash += closed_order.Profit;
            }

            // Compute equity
            this.Equity = Cash;
            foreach (TradingOrder open_order in OpenOrders.Values)
            {
                Equity += open_order.ComputeValue(Prices.GetHistory(0));
            }


            Prices.Step();
            Second1.Step();
            if ((0 < Minute1.FutureCount) && (Prices.GetHistory(0).Time == Minute1.GetFuture(0).CloseTime))
            {
                Minute1.Step();
            }
            else
            {
                // if it is not the end of a minute the others also do not nee to step
                return;
            }

            if ((0 < Minute15.FutureCount) && (Prices.GetHistory(0).Time == Minute15.GetFuture(0).CloseTime))
            {
                Minute15.Step();
            }

            if ((0 < Minute30.FutureCount) && (Prices.GetHistory(0).Time == Minute30.GetFuture(0).CloseTime))
            {
                Minute30.Step();
            }

            if ((0 < Hour1.FutureCount) && (Prices.GetHistory(0).Time == Hour1.GetFuture(0).CloseTime))
            {
                Hour1.Step();
            }

            if ((0 < Hour4.FutureCount) && (Prices.GetHistory(0).Time == Hour4.GetFuture(0).CloseTime))
            {
                Hour4.Step();
            }

            if ((0 < Day1.FutureCount) && (Prices.GetHistory(0).Time == Day1.GetFuture(0).CloseTime))
            {
                Day1.Step();
            }

            if ((0 < Week1.FutureCount) && (Prices.GetHistory(0).Time == Week1.GetFuture(0).CloseTime))
            {
                Week1.Step();
            }

            if ((0 < Month1.FutureCount) && (Prices.GetHistory(0).Time == Month1.GetFuture(0).CloseTime))
            {
                Month1.Step();
            }
        }
Ejemplo n.º 6
0
        //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);
        }
Ejemplo n.º 7
0
        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);
        }