Пример #1
0
 public virtual void Run(Tick tick)
 {
 }
Пример #2
0
 public bool MakeCandle(Tick tick)
 {
     return(MakeCandle(tick, setup.TimeFrame));
 }
Пример #3
0
        private async Task <List <Tick> > GetDataCSV(DateTime lasttime)
        {
            var ticks = new List <Tick>();
            var files = Directory.GetFiles(DataDir, this.setup.Symbol + "*_1min.csv");

            foreach (var file in files)
            {
                var info = new FileInfo(file);
                if (!info.Exists)
                {
                    OnStatusChanged(file + " não existe.");
                    continue;
                }
                var buffer = new byte[info.Length];
                if (buffer.Length < 10000)
                {
                    continue;
                }

                using (var asciifile = File.OpenRead(file))
                {
                    await asciifile.ReadAsync(buffer, 0, buffer.Length);

                    var lines = System.Text.Encoding.UTF8.GetString(buffer).Split('\n').ToList();
                    lines.RemoveAll(l => l.Length == 0);
                    var last = lines.Last();
                    var eod  = last.Contains(":") ? 3 : 2;

                    foreach (var line in lines)
                    {
                        var columns = line.Split(';');

                        try
                        {
                            var time = DateTime.Parse(columns[1]);
                            if (eod == 3)
                            {
                                time += TimeSpan.Parse(columns[2]);
                            }

                            if (time < lasttime)
                            {
                                continue;
                            }

                            var originals = new decimal[4];
                            for (var i = 0; i < 4; i++)
                            {
                                originals[i] = decimal.Parse(columns[eod + i]);
                            }
                            if (originals[3] > originals[0])
                            {
                                var tmp = originals[1];
                                originals[1] = originals[2];
                                originals[2] = tmp;
                            }

                            var values = originals.Distinct().ToList();
                            if (originals[3] == originals[0])
                            {
                                values.Add(originals[3]);
                            }

                            var add = new double[values.Count];
                            add[0] = eod > 2 ? 0 : 10;
                            add[add.Length - 1] = eod > 2 ? 59 : 16 + 5 / 6;
                            if (add.Length > 2)
                            {
                                add[1] = eod > 2 ? 30 : 13;

                                if (add.Length > 3)
                                {
                                    add[1] = eod > 2 ? 20 : 12;
                                    add[2] = eod > 2 ? 40 : 14;
                                }
                            }

                            var lineticks = new List <Tick>();

                            for (var i = 0; i < values.Count; i++)
                            {
                                var tick = new Tick {
                                    Symbol = columns[0].Trim(), Value = values[i], Qnty = 0
                                };
                                tick.Time = eod == 2 ? time.AddHours(add[i]) : time.AddSeconds(add[i]);
                                lineticks.Add(tick);
                            }

                            ticks.AddRange(lineticks);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        };
                    }
                }
            }
            return(ticks.OrderBy(t => t.Time).ToList());
        }
Пример #4
0
        public override void Run(Tick tick)
        {
            currentTime = tick.Time;
            var eod = currentTime >= eodTime;

            if (Filter(tick))
            {
                return;
            }

            #region While trading:
            if (type != 0)
            {
                trade.ExitTime  = tick.Time;
                trade.ExitValue = tick.Value;

                // Try StopLoss Exit
                if (type != 0 && type * tick.Value < trade.StopLoss * type)
                {
                    trade.ExitValue = trade.StopLoss;
                    type            = 0;
                }

                // Try StopGain Exit
                if (type != 0 && type * tick.Value >= trade.StopGain * type)
                {
                    trade.ExitValue = trade.StopGain;
                    type            = 0;
                }

                // Is EOD?
                if (type != 0 && tick.Time >= eodTime)
                {
                    type = 0;
                }

                if (type == 0)
                {
                    cantrade   = false;
                    trade.Cost = 12;
                    trades.Add(trade);
                }
            }
            #endregion
            //
            StartOrReset();
            Indicadors(MakeCandle(tick));

            if (type == 0 && eodTime > tick.Time && cantrade)
            {
                if (_candles.Count == 0 || _candles[0].OpenTime.AddMinutes(setup.TimeFrame) > tick.Time)
                {
                    return;
                }
                type = int.Parse(_candles[0].Indicators.Split(';')[0]);

                trade = new Trade
                {
                    Qnty       = 1,
                    Type       = type,
                    Symbol     = setup.Symbol,
                    EntryTime  = tick.Time,
                    EntryValue = tick.Value + slippage * type,
                    StopLoss   = tick.Value - (decimal)setup.StaticLoss * type,
                    StopGain   = tick.Value + (decimal)setup.StaticGain * type,
                };
            }
        }