Beispiel #1
0
        protected void PositionsOnClosed(PositionClosedEventArgs args)
        {
            // Reset Global Variables:
            ReachedMinTradePercent = false;

            //Validation...

            if (!ReversalTradeEnabled)
            {
                Print("Reversal Trade not valid on this day.");
                return;
            }


            if (args.Position.Label == "BarBreakReversal" || args.Position.GrossProfit >= 0)
            {
                Print("Reversal trade not valid.");
                return;
            }


            // Check the Day of Week for Trading....
            bool OkToTradeToday = false;

            foreach (string DOW in ValidDays.Split(','))
            {
                if (MarketSeries.OpenTime.LastValue.DayOfWeek.ToString().ToLower().Contains(DOW.ToLower()))
                {
                    OkToTradeToday = true;
                }
            }

            if (!OkToTradeToday)
            {
                return;
            }



            // Create Market Oder:
            double    BarLength = Math.Abs((double)args.Position.EntryPrice - (double)args.Position.StopLoss);
            TradeType Trade;
            double    Entry;
            double    SL = BarLength * (1 / Symbol.TickSize);
            double    TP = BarLength * (1 / Symbol.TickSize);

            SL = SL - ReversalBarBreakPointSize;
            TP = TP - ReversalBarBreakPointSize;

            long Contracts = (GetContractSize() * ReversalContractMultiplier);

            if (args.Position.TradeType == TradeType.Buy)
            {
                //Sell at double stakes...
                Trade = TradeType.Sell;
                Entry = (double)args.Position.StopLoss - ReversalBarBreakPointSize;
            }
            else
            {
                //Buy at double stakes..
                Trade = TradeType.Buy;
                Entry = (double)args.Position.StopLoss + ReversalBarBreakPointSize;
            }


            var Result = PlaceStopOrder(Trade, Symbol, Contracts, Entry, "BarBreakReversal", SL, TP, MarketSeries.OpenTime.LastValue.AddMinutes(ReversalValidMinutes));

            if (Result.IsSuccessful)
            {
                string OrderPlacedSummary = "Order Placed! BarLength: " + BarLength.ToString() + ", " + Trade.ToString() + ": " + Symbol.Code.ToString() + ", Entry: " + Entry.ToString() + ", Stop: " + SL.ToString() + ", TakeProfit: " + TP.ToString();
                Print(OrderPlacedSummary);
                Notifications.SendEmail("*****@*****.**", "*****@*****.**", "New Position Opened", OrderPlacedSummary);
            }
            else
            {
                Print("Error: " + Result.Error);
            }
        }
Beispiel #2
0
        protected override void OnBar()
        {
            if (!TradingEnabled)
            {
                return;
            }

            // Check the Day of Week for Trading....
            bool OkToTradeToday = false;

            foreach (string DOW in ValidDays.Split(','))
            {
                if (MarketSeries.OpenTime.LastValue.DayOfWeek.ToString().ToLower().Contains(DOW.ToLower()))
                {
                    OkToTradeToday = true;
                }
            }

            if (!OkToTradeToday)
            {
                return;
            }



            // Check that the last Bar was the Open.
            if (MarketSeries.OpenTime.Last(1).TimeOfDay == new TimeSpan(SignalBarHour, SignalBarMinute, 0))
            {
                double BarLength = GetBarLength(1);

                MarketSeries     MarketSeriesDaily = MarketData.GetSeries(TimeFrame.Daily);
                AverageTrueRange ATR = Indicators.AverageTrueRange(MarketSeriesDaily, 5, MovingAverageType.Simple);

                if (ATR.Result.LastValue >= MinATR)
                {
                    if (GetBarDirection(1) != "NEUTRAL")
                    {
                        TradeType Trade;
                        double    Entry;
                        double    SL;
                        double    TP        = BarLength * (1 / Symbol.TickSize);
                        long      Contracts = GetContractSize();

                        if (GetBarDirection(1) == "DOWN")
                        {
                            Trade = TradeType.Sell;
                            Entry = MarketSeries.Low.Last(1) - BarBreakPointSize;
                            SL    = (MarketSeries.High.Last(1) + BarBreakPointSize) - Entry;

                            //Increasing the SL a little more
                            SL = SL + 8;
                        }
                        else
                        {
                            Trade = TradeType.Buy;
                            Entry = MarketSeries.High.Last(1) + BarBreakPointSize;
                            SL    = Entry - (MarketSeries.Low.Last(1) - BarBreakPointSize);

                            //Increasing the SL a little more
                            SL = SL + 8;
                        }

                        SL = SL * (1 / Symbol.TickSize);

                        var Result = PlaceStopOrder(Trade, Symbol, Contracts, Entry, "BarBreak", SL, TP, MarketSeries.OpenTime.LastValue.AddMinutes(MaxMinuteValid));

                        if (Result.IsSuccessful)
                        {
                            string OrderPlacedSummary = "Order Placed! BarLength: " + BarLength.ToString() + ", " + Trade.ToString() + ": " + Symbol.Code.ToString() + ", Entry: " + Entry.ToString() + ", Stop: " + SL.ToString() + ", TakeProfit: " + TP.ToString();
                            Print(OrderPlacedSummary);
                            Notifications.SendEmail("*****@*****.**", "*****@*****.**", "New Position Opened", OrderPlacedSummary);
                        }
                        else
                        {
                            Print("Error: " + Result.Error);
                        }
                    }
                }
                else
                {
                    Print("5 Day ATR (" + ATR.Result.LastValue.ToString() + ") was outside acceptable boundaries.");
                }
            }
        }