Example #1
0
        /// <summary>
        /// Returns time of next specified value Usage: AudioSource.PlayScheduled(AtNext(Beat.TickValue.Quarter))
        /// </summary>
        /// <param name="beatTickValue">TickValue for which to get time of next</param>
        /// <returns>Time of next specified value</returns>
        public double AtNext(TickValue beatTickValue)
        {
            switch (beatTickValue)
            {
            case TickValue.ThirtySecond:
                return(AtNextThirtySecond());

            case TickValue.SixteenthTriplet:
                return(AtNextSixteenthTriplet());

            case TickValue.Sixteenth:
                return(AtNextSixteenth());

            case TickValue.EighthTriplet:
                return(AtNextEighthTriplet());

            case TickValue.Eighth:
                return(AtNextEighth());

            case TickValue.QuarterTriplet:
                return(AtNextQuarterTriplet());

            case TickValue.Quarter:
                return(AtNextQuarter());

            case TickValue.Half:
                return(AtNextHalf());

            case TickValue.Measure:
                return(AtNextMeasure());

            default:
                return(AtNextBeat());
            }
        }
Example #2
0
 /// <summary>
 /// Print a TicketValue
 /// </summary>
 /// <param name="value">The value</param>
 public static void Print(this TickValue value)
 {
     Console.WriteLine(
         string.Format("{0,-25}    {1,-25}    {2,-30}    {3,-25}", "Cash", "Food", "Resources", "Science Points"));
     Console.WriteLine(
         string.Format(
             "{0,-15}  {1,10:G4}  {2,-15}  {3,10:G4}  {4,-20}  {5,10:G4}  {6,-15}  {7,10:G4}",
             "Income",
             value.ProducedCash,
             "Food Produced",
             value.ProducedFood,
             "Iron Mined",
             value.ProducedIron,
             "Military",
             0));
     Console.WriteLine(
         string.Format(
             "{0,-15}  {1,10:G4}  {2,-15}  {3,10:G4}  {4,-20}  {5,10:G4}  {6,-15}  {7,10:G4}",
             "Units",
             -value.Units,
             "Food Consumed",
             value.ProducedFood,
             "Iron Decayed",
             value.DecayedIron,
             "Welfare",
             0));
     Console.WriteLine(
         string.Format(
             "{0,-15}  {1,10:G4}  {2,-15}  {3,10:G4}  {4,-20}  {5,10:G4}  {6,-15}  {7,10:G4}",
             "Buildings",
             -value.Buildings,
             "Food Decayed",
             value.DecayedFood,
             "Plutonium Mined",
             value.ProducedEnergy,
             "Economy",
             0));
     Console.WriteLine(
         string.Format(
             "{0,-15}  {1,10:G4}  {2,-15}  {3,10:G4}  {4,-20}  {5,10:G4}  {6,-15}  {7,10:G4}",
             "Bureaucracy",
             -value.DecayedCash,
             "Net Flow",
             value.ProducedFood,
             "Plutonium Decayed",
             value.DecayedEnergy,
             "Construction",
             0));
     Console.WriteLine(
         string.Format(
             "{0,-15}  {1,10}  {2,-15}  {3,10}  {4,-20}  {5,10:G4}  {6,-15}  {7,10:G4}",
             string.Empty,
             string.Empty,
             string.Empty,
             string.Empty,
             "Mana Mined",
             0,
             "Resources",
             0));
 }
Example #3
0
 public Args(TickValue beatVal, double beatTime, double nextBeatTime, TickMask <bool> beatTickMask)
 {
     BeatVal      = beatVal;
     BeatTime     = beatTime;
     NextBeatTime = nextBeatTime;
     TickMask     = beatTickMask;
 }
Example #4
0
        /// <summary>
        /// Helper fucntions for timing things like tweens & animations to the beat clock
        /// </summary>
        /// <param name="beatTickValue"></param>
        /// <returns></returns>
        public float LengthOf(TickValue beatTickValue)
        {
            switch (beatTickValue)
            {
            case TickValue.ThirtySecond:
                return(ThirtySecondLength());

            case TickValue.SixteenthTriplet:
                return(SixteenthTripletLength());

            case TickValue.Sixteenth:
                return(SixteenthLength());

            case TickValue.EighthTriplet:
                return(EighthTripletLength());

            case TickValue.Eighth:
                return(EighthLength());

            case TickValue.QuarterTriplet:
                return(QuarterTripletLength());

            case TickValue.Quarter:
                return(QuarterLength());

            case TickValue.Half:
                return(HalfLength());

            case TickValue.Measure:
                return(MeasureLength());

            default:
                return(BeatLength());
            }
        }
Example #5
0
        /// <summary>
        /// Imports quotes to storage.
        /// </summary>
        /// <param name="ticks">Ticks, which should be imported.</param>
        /// <param name="level2">Ignore depth of level2, if false.</param>
        /// <param name="overwriteTickChainsWithEqualTime"></param>
        /// <param name="rebuild">The method rebuilds all bars, if true.</param>
        public void Import(IEnumerable <Quote> ticks, bool level2, bool overwriteTickChainsWithEqualTime, bool rebuild)
        {
            var groupdeBySymbols = ticks.GroupBy(t => t.Symbol);

            foreach (var symbolsTicks in groupdeBySymbols)
            {
                var groupedTicksValues = symbolsTicks.Select(qt =>
                {
                    var tl2Bids = qt.Bids.Select(qa => new FeedLevel2Record
                    {
                        Price  = (decimal)qa.Price,
                        Type   = FxPriceType.Bid,
                        Volume = qa.Volume
                    });
                    var tl2Asks = qt.Asks.Select(qa => new FeedLevel2Record
                    {
                        Price  = (decimal)qa.Price,
                        Type   = FxPriceType.Ask,
                        Volume = qa.Volume
                    });

                    var ret = new TickValue(qt.CreatingTime, tl2Asks.Concat(tl2Bids));
                    return(ret);
                });

                var provider = this.GetOrCreateHistoryManager(symbolsTicks.Key);
                provider.ImportTicks(groupedTicksValues, symbolsTicks.Key, level2, true, overwriteTickChainsWithEqualTime ? TicksImportRules.Replace
                                                                                                                          : TicksImportRules.Append, null);
                if (rebuild)
                {
                }
            }
        }
Example #6
0
 public T this[TickValue i] {
     get {
         return(_beatMaskArr[(int)i]);
     }
     set {
         _beatMaskArr[(int)i] = value;
     }
 }
Example #7
0
 public static double ExitPrice(this TickValue tick, BuyStyle style)
 {
     if (style == BuyStyle.Long)
     {
         return(tick.BidPrice);
     }
     else
     {
         return(tick.AskPrice);
     }
 }
Example #8
0
 public static double EntryPrice(BuyStyle style, TickValue tick)
 {
     if (style == BuyStyle.Long)
     {
         return(tick.AskPrice);
     }
     else
     {
         return(tick.BidPrice);
     }
 }
Example #9
0
 public static int CalculateQuantity(BuyStyle style, TickValue from, double budget)
 {
     if (style == BuyStyle.Long)
     {
         return(CalculateQuantity(BuyStyle.Long, from.AskPrice, budget));
     }
     else
     {
         //short
         return(CalculateQuantity(BuyStyle.Short, from.BidPrice, budget));
     }
 }
Example #10
0
 public static Trade CalculateTrade(BuyStyle style, long fromTime, TickValue from, long toTime, TickValue to, double budget)
 {
     if (style == BuyStyle.Long)
     {
         return(CalculateTrade(BuyStyle.Long, from.AskPrice, to.BidPrice, fromTime, toTime, budget));
     }
     else
     {
         //short
         return(CalculateTrade(BuyStyle.Short, from.BidPrice, to.AskPrice, fromTime, toTime, budget));
     }
 }
Example #11
0
 public static (double Profit, int Quantity) CalculateProfit(BuyStyle style, TickValue from, double to, double budget)
 {
     if (style == BuyStyle.Long)
     {
         return(CalculateProfit(BuyStyle.Long, from.AskPrice, to, budget));
     }
     else
     {
         //short
         return(CalculateProfit(BuyStyle.Short, from.BidPrice, to, budget));
     }
 }
Example #12
0
 public static (float Profit, int Quantity) CalculateProfit(BuyStyle style, TickValue from, float to, int quantity)
 {
     if (style == BuyStyle.Long)
     {
         return(CalculateProfit(BuyStyle.Long, (float)from.AskPrice, to, quantity));
     }
     else
     {
         //short
         return(CalculateProfit(BuyStyle.Short, (float)from.BidPrice, to, quantity));
     }
 }
Example #13
0
        /// <param name="style">The trade style</param>
        /// <param name="price">Price of the stock</param>
        /// <param name="quantity">Absolute quantity</param>
        /// <param name="profitDollar">Profit in dollars, can be negative if loss planned</param>
        /// <returns></returns>
        public static double LimitAtProfit(BuyStyle style, TickValue tick, int quantity, double profitDollar)
        {
            if (quantity == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(quantity));
            }
            quantity = Math.Abs(quantity);

            //161*(1+0.05/100)
            var mod           = style == BuyStyle.Long ? 1 : -1;
            var balance       = (style == BuyStyle.Long ? tick.AskPrice : tick.BidPrice) * quantity;
            var targetbalance = balance + mod * profitDollar;
            var targetPrice   = targetbalance / quantity;

            return(Math.Round(targetPrice, 2));
        }
Example #14
0
        public Quote ToQuote(string symbol, TickValue tick, int depth)
        {
            var creatingTime = tick.Time;

            var bids = tick.Level2
                       .Where(o => o.Type == FxPriceType.Bid)
                       .Select(o => new QuoteEntry((double)o.Price, (double)o.Volume))
                       .ToArray();

            var asks = tick.Level2
                       .Where(o => o.Type == FxPriceType.Ask)
                       .Select(o => new QuoteEntry((double)o.Price, (double)o.Volume))
                       .ToArray();

            Array.Sort(bids, BidComparator);
            Array.Sort(asks, AskComparator);

            return(new Quote(symbol, creatingTime, bids.Take(depth).ToArray(), asks.Take(depth).ToArray()));
        }
Example #15
0
        /// <summary>
        /// Sync the execution of a particular callback to a specified beat a specified number of times
        /// </summary>
        /// <param name="callback">Function to call</param>
        /// <param name="beatTickValue">Beat value at which function will be called</param>
        /// <param name="repetitions">Number of times function will be called</param>
        /// <returns></returns>
        IEnumerator YieldForSyncRepeating(System.Action callback, TickValue beatTickValue, int repetitions)
        {
            Debug.Log("Yield for sync TickMask: " + TickMask[beatTickValue]);
            int  startCount    = _tickCounter + _measureCount * _ticksPerMeasure;
            bool isStartNote   = true;
            bool waiting       = true;
            int  timesRun      = 0;
            int  tickOfLastRun = 0;

            while (waiting)
            {
                isStartNote = isStartNote && startCount == _tickCounter + _measureCount * _ticksPerMeasure;
                if (isStartNote)
                {
                    yield return(false);
                }

                isStartNote = false;

                if ((beatTickValue == TickValue.ThirtySecond || TickMask[beatTickValue]) && (tickOfLastRun != _tickCounter + _measureCount * _ticksPerMeasure))
                {
                    if (repetitions <= timesRun)
                    {
                        waiting = false;
                    }
                    else
                    {
                        callback();
                        tickOfLastRun = _tickCounter + _measureCount * _ticksPerMeasure;
                        timesRun++;
                        yield return(false);
                    }
                }
                else
                {
                    yield return(false);
                }
            }
        }
Example #16
0
 /// <summary>
 /// Sync the execution of a particular callback to a specified beat a specified number of times
 /// </summary>
 /// <param name="callback">Function to call at next beatTickValue</param>
 /// <param name="beatTickValue">Beat value at which function will be called</param>
 /// <param name="repetitions">Number of times function will be called</param>
 public void SyncFunction(System.Action callback, TickValue beatTickValue, int repetitions)
 {
     StartCoroutine(YieldForSyncRepeating(callback, beatTickValue, repetitions));
 }
Example #17
0
 /// <summary>
 /// Sync the execution of a function to the next specified beat, optionally add a number repetitions
 /// </summary>
 /// <param name="callback">Function to call at next beatTickValue</param>
 /// <param name="beatTickValue">Beat value at which function will be called (Default is measure / downbeat)</param>
 IEnumerator YieldForSync(System.Action callback, TickValue beatTickValue)
 {
     return(YieldForSyncRepeating(callback, beatTickValue, 1));
 }
Example #18
0
        //Note - these sync functions do not work properly

        /// <summary>
        /// Sync the execution of a function to the next specified beat, optionally add a number repetitions
        /// </summary>
        /// <param name="callback">Function to call at next beatTickValue</param>
        /// <param name="beatTickValue">Beat value at which function will be called (Default is measure / downbeat)</param>
        public void SyncFunction(System.Action callback, TickValue beatTickValue = TickValue.Measure)
        {
            StartCoroutine(YieldForSync(callback, beatTickValue));
        }
Example #19
0
 public static (double Profit, int Quantity) CalculateProfit(BuyStyle style, double from, TickValue to, int quantity)
 {
     if (style == BuyStyle.Long)
     {
         return(CalculateProfit(BuyStyle.Long, from, to.BidPrice, quantity));
     }
     else
     {
         //short
         return(CalculateProfit(BuyStyle.Short, from, to.AskPrice, quantity));
     }
 }
Example #20
0
        /// <summary>
        /// Imports quotes to storage.
        /// </summary>
        /// <param name="ticks">Ticks, which should be imported.</param>
        /// <param name="level2">Ignore depth of level2, if false.</param>
        /// <param name="overwriteTickChainsWithEqualTime"></param>
        /// <param name="rebuild">The method rebuilds all bars, if true.</param>
        public void Import(IEnumerable<Quote> ticks, bool level2, bool overwriteTickChainsWithEqualTime, bool rebuild)
        {
            var groupdeBySymbols = ticks.GroupBy(t => t.Symbol);
            foreach (var symbolsTicks in groupdeBySymbols)
            {
                var groupedTicksValues = symbolsTicks.Select(qt =>
                {
                    var tl2Bids = qt.Bids.Select(qa => new FeedLevel2Record
                    {
                        Price = (decimal)qa.Price,
                        Type = FxPriceType.Bid,
                        Volume = qa.Volume
                    });
                    var tl2Asks = qt.Asks.Select(qa => new FeedLevel2Record
                    {
                        Price = (decimal)qa.Price,
                        Type = FxPriceType.Ask,
                        Volume = qa.Volume
                    });

                    var ret = new TickValue(qt.CreatingTime, tl2Asks.Concat(tl2Bids));
                    return ret;
                });

                var provider = this.GetOrCreateHistoryManager(symbolsTicks.Key);
                provider.ImportTicks(groupedTicksValues, symbolsTicks.Key, level2, true, overwriteTickChainsWithEqualTime ? TicksImportRules.Replace
                                                                                                                          : TicksImportRules.Append, null);
                if (rebuild)
                {
                }
            }
        }
Example #21
0
 public static (float Profit, int Quantity) CalculateProfit(BuyStyle style, float from, TickValue to, float budget)
 {
     if (style == BuyStyle.Long)
     {
         return(CalculateProfit(BuyStyle.Long, from, (float)to.BidPrice, budget));
     }
     else
     {
         //short
         return(CalculateProfit(BuyStyle.Short, from, (float)to.AskPrice, budget));
     }
 }
Example #22
0
        public Quote ToQuote(string symbol, TickValue tick, int depth)
		{
			var creatingTime = tick.Time;

            var bids = tick.Level2
                           .Where(o => o.Type == FxPriceType.Bid)
                           .Select(o => new QuoteEntry((double)o.Price, (double)o.Volume))
                           .ToArray();

            var asks = tick.Level2
                           .Where(o => o.Type == FxPriceType.Ask)
                           .Select(o => new QuoteEntry((double)o.Price, (double)o.Volume))
                           .ToArray();

			Array.Sort(bids, BidComparator);
			Array.Sort(asks, AskComparator);

			return new Quote(symbol, creatingTime, bids.Take(depth).ToArray(), asks.Take(depth).ToArray());
		}
Example #23
0
 public override string ToString()
 {
     return(TickValue.ToString() + "," + RandomValue.ToString());
 }