Inheritance: DataObject
Exemple #1
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            // Add bar to bar series.
            Bars.Add(bar);

            Log(bar, barsGroup);

            if (fastSMA.Count > 0)
                Log(fastSMA.Last, fastSmaGroup);

            if (slowSMA.Count > 0)
                Log(slowSMA.Last, slowSmaGroup);

            // Calculate performance.
            Portfolio.Performance.Update();

            Log(Portfolio.Value, equityGroup);

            // Check strategy logic.
            if (fastSMA.Count > 0 && slowSMA.Count > 0)
            {
                Cross cross = fastSMA.Crosses(slowSMA, bar.DateTime);

                if (cross == Cross.Above)
                    buyOnNewBlock = true;

                if (cross == Cross.Below)
                    sellOnNewBlock = true;
            }

            positionInBlock = (positionInBlock++) % BarBlockSize;
        }
Exemple #2
0
        protected override void OnBarOpen(Instrument instrument, Bar bar)
        {
            double orderQty = Qty;

            // Set order qty if position already exist.
            if (HasPosition(Instrument))
                orderQty = Math.Abs(Position.Amount) + Qty;

            // Send trading orders if needed.
            if (positionInBlock == 0)
            {
                if (buyOnNewBlock)
                {
                    Order order = BuyOrder(Instrument, orderQty, "Reverse to Long");
                    Send(order);

                    buyOnNewBlock = false;
                }

                if (sellOnNewBlock)
                {
                    Order order = SellOrder(Instrument, orderQty, "Reverse to Short");
                    Send(order);

                    sellOnNewBlock = false;
                }
            }
        }
Exemple #3
0
        public override void OnBar(Bar bar)
        {
            if (Bars.Count < length)
                return;

            highestHigh = Bars.HighestHigh(length);
            lowestLow = Bars.LowestLow(length);
        }
Exemple #4
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            // Add bar to bar series.
            Bars.Add(bar);

            // Add bar to group.
            Log(bar, barsGroup);

            // Calculate performance.
            Portfolio.Performance.Update();

            // Add equity to group.
            Log(Portfolio.Value, equityGroup);

            // Check strategy logic.
            if (Bars.Count > 2)
            {
                if (!HasPosition(Instrument))
                {
                    if (Bars[Bars.Count - 1].Close > Bars[Bars.Count - 2].Close &&
                        Bars[Bars.Count - 2].Close > Bars[Bars.Count - 3].Close)
                    {
                        Order enterOrder = BuyOrder(Instrument, Qty, "Enter Long");
                        Send(enterOrder);
                    }
                    else if (Bars[Bars.Count - 1].Close < Bars[Bars.Count - 2].Close &&
                        Bars[Bars.Count - 2].Close < Bars[Bars.Count - 3].Close)
                    {
                        Order enterOrder = SellOrder(Instrument, Qty, "Enter Short");
                        Send(enterOrder);
                    }
                }
                else
                {
                    if (Position.Side == PositionSide.Short &&
                        Bars[Bars.Count - 1].Close > Bars[Bars.Count - 2].Close &&
                        Bars[Bars.Count - 2].Close > Bars[Bars.Count - 3].Close)
                    {
                        Order reverseOrder = BuyOrder(Instrument, Math.Abs(Position.Amount) + Qty, "Reverse to Long");
                        Send(reverseOrder);
                    }
                    else if (Position.Side == PositionSide.Long &&
                        Bars[Bars.Count - 1].Close < Bars[Bars.Count - 2].Close &&
                        Bars[Bars.Count - 2].Close < Bars[Bars.Count - 3].Close)
                    {
                        Order reverseOrder = SellOrder(Instrument, Math.Abs(Position.Amount) + Qty, "Reverse to Short");
                        Send(reverseOrder);
                    }
                }
            }
        }
Exemple #5
0
        internal bool OnBarOpen(Bar bar)
        {
            var item = this.items[(int)bar.Size] = this.items[(int)bar.Size] ?? new BarSliceItem();
            if (item.CloseDateTime == bar.OpenDateTime)
            {
                item.Bars.Add(bar);
                return false;
            }
            if (item.barCount == 0)
                item.CloseDateTime = bar.OpenDateTime.AddSeconds(bar.Size);

            item.barCount++;
            return true;
        }
Exemple #6
0
        internal void OnBar(Bar bar)
        {
            var item = this.items[(int)bar.Size];
            if (item == null)
                return;

            if (--item.barCount == 0)
            {
                this.framework.EventServer.OnEvent(new BarSlice(bar));
                item.CloseDateTime = DateTime.MinValue;
                foreach (var b in item.Bars)
                    this.framework.EventServer.OnEvent(b);
                item.Bars.Clear();
            }
        }
Exemple #7
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            Log(bar, barChartGroup);

            Bars.Add(bar);

            if (Bars.Count % 20 == 0)
            {
                if (!HasPosition(instrument))
                    Buy(instrument, 5, "Buy");
                else
                    Sell(instrument, 5, "Sell");
            }

            Portfolio.Performance.Update();
        }
Exemple #8
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            // Add bar to bar series.
            Bars.Add(bar);

            // Add bar to group.
            Log(bar, barsGroup);

            // Add upper bollinger band value to group.
            if (bbu.Count > 0)
                Log(bbu.Last, bbuGroup);

            // Add lower bollinger band value to group.
            if (bbl.Count > 0)
                Log(bbl.Last, bblGroup);

            // Add simple moving average value bands to group.
            if (sma.Count > 0)
                Log(sma.Last, smaGroup);

            // Calculate performance.
            Portfolio.Performance.Update();

            // Add equity to group.
            Log(Portfolio.Value, equityGroup);

            // Check strategy logic.
            if (!HasPosition(instrument))
            {
                if (bbu.Count > 0 && bar.Close >= bbu.Last)
                {
                    Order enterOrder = SellOrder(Instrument, Qty, "Enter");
                    Send(enterOrder);
                }
                else if (bbl.Count > 0 && bar.Close <= bbl.Last)
                {
                    Order enterOrder = BuyOrder(Instrument, Qty, "Enter");
                    Send(enterOrder);
                }
            }
            else
                UpdateExitLimit();
        }
Exemple #9
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            // Add bar to bar series.
            Bars.Add(bar);

            Log(bar, barsGroup);

            // Calculate performance.
            Portfolio.Performance.Update();

            Log(Portfolio.Value, equityGroup);

            if (Bars.Count % 10 == 0)
            {
                if (!HasPosition(instrument))
                    Buy(instrument, Qty, "Buy spread");
                else
                    Sell(instrument, Qty, "Sell spread");
            }
        }
Exemple #10
0
 internal void OnBarOpen(Bar bar)
 {
     if (TraceOnBar && TraceOnBarOpen && (FilterBarSize < 0 || (FilterBarSize == bar.Size && FilterBarType == BarType.Time)))
     {
         this.fillPrice = this.currPrice = GetPrice(bar.Open);
         if (TrailOnOpen)
             this.trailPrice = GetPrice(bar.Open);
         this.method_1();
     }
 }
        public override object Read(BinaryReader reader, byte version)
        {
            var bar = new Bar();
            bar.DateTime = new DateTime(reader.ReadInt64());
            bar.OpenDateTime = new DateTime(reader.ReadInt64());
            bar.InstrumentId = reader.ReadInt32();
            bar.Size = reader.ReadInt64();
            bar.High = reader.ReadDouble();
            bar.Low = reader.ReadDouble();
            bar.Open = reader.ReadDouble();
            bar.Close = reader.ReadDouble();
            bar.Volume = reader.ReadInt64();
            bar.OpenInt = reader.ReadInt64();
            bar.Status = (BarStatus)reader.ReadByte();
            if (version >= 1)
                bar.Type = (BarType)reader.ReadByte();
            if (version >= 2)
            {
                bar.ProviderId = reader.ReadInt32();
            }
            if (version <= 2)
            {
                int num = reader.ReadInt32();
                if (num != 0)
                {
                    //bar.Fields = new ObjectTable();
                    for (var i = 0; i < num; i++)
                    {
                        bar.Fields[i] = reader.ReadDouble();
                    }
                }
            }
            if (version >= 3 && reader.ReadBoolean())
            {
                var fields = (ObjectTable)this.streamerManager.Deserialize(reader);
                for (int i = 0; i < fields.Size; i++)
                {
                    bar.Fields[i] = fields[i];
                }
                //    bar.Fields = (ObjectTable)this.streamerManager.Deserialize(reader);

            }
            return bar;
        }
Exemple #12
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            // Add bar to bar series.
            Bars.Add(bar);

            // Add bar to group.
            Log(bar, barsGroup);

            // Add highest value to group.
            if (highest != 0)
                Log(highest, highestGroup);

            // Add lowest value to group.
            if (lowest != 0)
                Log(lowest, lowestGroup);

            // Calculate performance.
            Portfolio.Performance.Update();

            // Add equity to group.
            Log(Portfolio.Value, equityGroup);

            // Check strategy logic.
            if (highest != 0 && lowest != 0)
            {
                if (!HasPosition(instrument))
                {
                    // Enter long/short.
                    if (bar.Close > highest)
                    {
                        Order enterOrder = BuyOrder(Instrument, Qty, "Enter Long");
                        Send(enterOrder);
                    }
                    else if (bar.Close < lowest)
                    {
                        Order enterOrder = SellOrder(Instrument, Qty, "Enter Short");
                        Send(enterOrder);
                    }
                }
                else
                {
                    // Reverse to long/short.
                    if (Position.Side == PositionSide.Long && bar.Close < lowest)
                    {
                        Order reverseOrder = SellOrder(Instrument, Math.Abs(Position.Amount) + Qty, "Reverse to Short");
                        Send(reverseOrder);
                    }
                    else if (Position.Side == PositionSide.Short && bar.Close > highest)
                    {
                        Order reverseOrder = BuyOrder(Instrument, Math.Abs(Position.Amount) + Qty, "Reverse to Long");
                        Send(reverseOrder);
                    }
                }
            }

            // Calculate channel's highest/lowest values.
            if (Bars.Count > Length)
            {
                highest = Bars.HighestHigh(Length);
                lowest = Bars.LowestLow(Length);
            }
        }
Exemple #13
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            Group[] instrumentGroups = null;

            // Add bar to bar group (index 0).
            if (groups.TryGetValue(instrument, out instrumentGroups))
                Log(bar, instrumentGroups[0]);
        }
Exemple #14
0
 public override void OnBar(Bar bar)
 {
     Bars.Add(bar);
 }
Exemple #15
0
 protected virtual void OnData(DataObject obj)
 {
     var tick = obj as Tick;
     if (this.bar == null)
     {
         // new bar begins!
         this.bar = new Bar
         {
             InstrumentId = tick.InstrumentId,
             Type = this.barType,
             Size = this.barSize,
             OpenDateTime = GetBarOpenDateTime(obj),
             DateTime = this.GetDataObjectDateTime(obj, ClockType.Local),
             Open = tick.Price,
             High = tick.Price,
             Low = tick.Price,
             Close = tick.Price,
             Volume = tick.Size,
             Status = BarStatus.Open
         };
         this.factory.Framework.EventServer.OnEvent(this.bar);
     }
     else
     {
         // update it!
         if (tick.Price > this.bar.High)
             this.bar.High = tick.Price;
         if (tick.Price < this.bar.Low)
             this.bar.Low = tick.Price;
         this.bar.Close = tick.Price;
         this.bar.Volume += tick.Size;
         this.bar.DateTime = GetDataObjectDateTime(obj, ClockType.Local);
     }
     ++this.bar.N;
 }
Exemple #16
0
 protected virtual void OnBar(Bar bar)
 {
     // noop
 }
Exemple #17
0
 internal void OnBar(Bar bar)
 {
     if (Strategy?.Status == StrategyStatus.Running)
         Strategy.EmitBar(bar);
 }
Exemple #18
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            // Add bar to bar series.
            Bars.Add(bar);

            Log(bar, barsGroup);

            if (fastSMA.Count > 0)
                Log(fastSMA.Last, fastSmaGroup);

            if (slowSMA.Count > 0)
                Log(slowSMA.Last, slowSmaGroup);

            // Calculate performance.
            Portfolio.Performance.Update();

            // Add equity to group.
            Log(Portfolio.Value, equityGroup);

            // Check strategy logic.
            if (fastSMA.Count > 0 && slowSMA.Count > 0)
            {
                Cross cross = fastSMA.Crosses(slowSMA, bar.DateTime);

                // Enter long/short.
                if (!HasPosition(instrument))
                {
                    if (cross == Cross.Above)
                    {
                        enterOrder = BuyOrder(Instrument, Qty, "Enter Long");
                        Send(enterOrder);
                    }
                    else if (cross == Cross.Below)
                    {
                        enterOrder = SellOrder(Instrument, Qty, "Enter Short");
                        Send(enterOrder);
                    }
                }
            }
        }
 public void Save(Bar bar, SaveMode option = SaveMode.Add) => Save(bar.InstrumentId, bar, option);
Exemple #20
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            Bars.Add(bar);

            // Log open close and position info to Strategy Monitor.
            Log(bar.DateTime, bar.Close, closeMonitorGroup);
            Log(bar.DateTime, bar.Open, openMonitorGroup);

            if (HasPosition(instrument))
                Log(bar.DateTime, Position.Side.ToString(), positionMonitorGroup);
            else
                Log(bar.DateTime, "None", positionMonitorGroup);

            // Log bars.
            Log(bar, barsGroup);

            if (sma1.Count == 0 || sma2.Count == 0)
                return;

            // Log sma.
            Log(bar.DateTime, sma1.Last, sma1Group);
            Log(bar.DateTime, sma2.Last, sma2Group);

            // Update performance.
            Portfolio.Performance.Update();

            // Log equity.
            Log(bar.DateTime, Portfolio.Value, equityGroup);

            // Does the fast average cross over the slow average? If so, time to buy long.
            Cross cross = sma1.Crosses(sma2, bar.DateTime);

            // We only allow one active position at a time.
            if (entryEnabled)
            {
                // If price trend is moving upward, open a long position using a market order, and send it in.
                if (cross == Cross.Above)
                {
                    marketOrder = BuyOrder(instrument, Qty, "Entry");
                    Send(marketOrder);

                    // If one cancels all exit method is desired, we
                    // also issue a limit (profit target) order, and
                    // a stop loss order in case the breakout fails.
                    // The OCA exit method uses a real stop loss order.
                    // The Stop exit method uses a stop indicator.
                    // Use either the OCA or Stop method, not both at once.
                    if (OCAExitEnabled)
                    {
                        // Create and send a profit limit order.
                        double profitTarget = LimitOCALevel * bar.Close;
                        limitOrder = SellLimitOrder(instrument, Qty, profitTarget, "Limit OCA " + OCACount);
                        limitOrder.OCA = "OCA " + Instrument.Symbol + " " + OCACount;
                        Send(limitOrder);

                        // Create and send a stop loss order.
                        double lossTarget = StopOCALevel * bar.Close;
                        stopOrder = SellStopOrder(instrument, Qty, lossTarget, "Stop OCA " + OCACount);
                        stopOrder.OCA = "OCA " + Instrument.Symbol + " " + OCACount;
                        Send(stopOrder);

                        // Bump the OCA count to make OCA group strings unique.
                        OCACount++;
                    }

                    entryEnabled = false;
                }
            }
            // Else if entry is disabled on this bar, we have an open position.
            else
            {
                // If we are using the crossover exit, and if the fast
                // average just crossed below the slow average, issue a
                // market order to close the existing position.
                if (CrossoverExitEnabled)
                {
                    if (cross == Cross.Below)
                    {
                        marketOrder = SellOrder(instrument, Qty, "Crossover Exit");
                        Send(marketOrder);
                    }
                }
            }
        }
Exemple #21
0
 protected override void OnBar(Instrument instrument, Bar bar)
 {
     Log(bar, barChartGroup);
 }
Exemple #22
0
 protected override void OnBar(Instrument instrument, Bar bar)
 {
     // Add bar to bar group.
     Log(bar, barsGroups[instrument]);
 }
Exemple #23
0
        public override void OnBar(Bar bar)
        {
            if (bar.DateTime.TimeOfDay > new TimeSpan(15, 45, 00))
            {
                inSession = false;

                if (HasLongPosition(1))
                    Sell(1, "StopSession");

                if (HasShortPosition(1))
                    Buy(1, "StopSession");
            }
            else
                inSession = true;

            if (hold)
                if (++holdCount == 5)
                {
                    hold = false;
                    holdCount = 0;
                    canEntry = true;
                }
        }
Exemple #24
0
 internal void OnBar(Bar bar)
 {
     if (TraceOnBar && (FilterBarSize < 0 || (FilterBarSize == bar.Size && FilterBarType == BarType.Time)))
     {
         this.trailPrice = GetPrice(bar.Close);
         switch (Side)
         {
             case PositionSide.Long:
                 this.fillPrice = this.currPrice = GetPrice(bar.Low);
                 if (this.trailOnHighLow)
                     this.trailPrice = GetPrice(bar.High);
                 break;
             case PositionSide.Short:
                 this.fillPrice = this.currPrice = GetPrice(bar.High);
                 if (this.trailOnHighLow)
                     this.trailPrice = GetPrice(bar.Low);
                 break;
         }
         switch (FillMode)
         {
             case StopFillMode.Close:
                 this.fillPrice = GetPrice(bar.Close);
                 break;
             case StopFillMode.Stop:
                 this.fillPrice = this.stopPrice;
                 break;
         }
         this.method_1();
     }
 }
 public void OnBar(Bar bar)
 {
     throw new NotImplementedException();
 }
Exemple #26
0
        protected override void OnBar(Instrument instrument, Bar bar)
        {
            // Add bar to bar series.
            Bars.Add(bar);

            Log(bar, barsGroup);

            // Log RSI, BuyLevel and SellLevel values.
            if (rsi.Count > 0)
            {
                Log(BuyLevel, buyLevelGroup);
                Log(SellLevel, sellLevelGroup);
                Log(rsi.Last, rsiGroup);
            }

            // Calculate performance.
            Portfolio.Performance.Update();

            Log(Portfolio.Value, equityGroup);

            // Check strategy logic.
            if (rsi.Count > 1)
            {
                if (!HasPosition(Instrument))
                {
                    if (rsi[rsi.Count - 1] < BuyLevel && rsi[rsi.Count - 2] > BuyLevel)
                    {
                        Order enterOrder = BuyOrder(Instrument, Qty, "Enter Long");
                        Send(enterOrder);
                    }
                    else if (rsi[rsi.Count - 1] > SellLevel && rsi[rsi.Count - 2] < SellLevel)
                    {
                        Order enterOrder = SellOrder(Instrument, Qty, "Enter Short");
                        Send(enterOrder);
                    }
                }
                else
                {
                    if (Position.Side == PositionSide.Long)
                    {
                        if (rsi[rsi.Count - 1] < BuyLevel && rsi[rsi.Count - 2] > BuyLevel)
                        {
                            Order enterOrder = BuyOrder(Instrument, Qty, "Add to Long");
                            Send(enterOrder);
                        }
                        else if (rsi[rsi.Count - 1] > SellLevel && rsi[rsi.Count - 2] < SellLevel)
                        {
                            Order reverseOrder = SellOrder(Instrument, Math.Abs(Position.Amount) + Qty, "Reverse to Short");
                            Send(reverseOrder);
                        }
                    }
                    else if (Position.Side == PositionSide.Short)
                    {
                        if (rsi[rsi.Count - 1] > SellLevel && rsi[rsi.Count - 2] < SellLevel)
                        {
                            Order enterOrder = SellOrder(Instrument, Qty, "Add to Short");
                            Send(enterOrder);
                        }
                        else if (rsi[rsi.Count - 1] < BuyLevel && rsi[rsi.Count - 2] > BuyLevel)
                        {
                            Order reverseOrder = BuyOrder(Instrument, Math.Abs(Position.Amount) + Qty, "Reverse to Long");
                            Send(reverseOrder);
                        }
                    }
                }
            }
        }
Exemple #27
0
 protected void EmitBar()
 {
     this.bar.Status = BarStatus.Close;
     this.factory.Framework.EventServer.OnEvent(this.bar);
     this.bar = null;
 }
Exemple #28
0
 public Bar(Bar bar) : base(bar)
 {
     InstrumentId = bar.InstrumentId;
     Type = bar.Type;
     Size = bar.Size;
     OpenDateTime = bar.OpenDateTime;
     Open = bar.Open;
     High = bar.High;
     Low = bar.Low;
     Close = bar.Close;
     Volume = bar.Volume;
     OpenInt = bar.OpenInt;
 }
 internal void OnBar(Bar bar)
 {
     this.latestBar[bar.InstrumentId] = bar;
 }
Exemple #30
0
        public override void OnBar(Bar bar)
        {
            // log bars

            Log(bar, barsChartGroup);

            // log equity

            Log(Portfolio.Value, equityChartGroup);

            // update performance

            Portfolio.Performance.Update();
        }