Пример #1
0
        protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                                  Cbi.MarketPosition marketPosition, string orderId, DateTime time)
        {
            if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
            {
                if (execution.Order.Name == ENTER_LONG)
                {
                    profitTaker = execution.Order.AverageFillPrice + (this.ProfitTicks * TickSize);
                    stopLoss    = execution.Order.AverageFillPrice - (this.StopLossTicks * TickSize);
                }
                else if (execution.Order.Name == ENTER_SHORT)
                {
                    profitTaker = execution.Order.AverageFillPrice - (this.ProfitTicks * TickSize);
                    stopLoss    = execution.Order.AverageFillPrice + (this.StopLossTicks * TickSize);
                }

                //if (execution.Order.Name == ENTER_LONG)
                //{

                //}
                //else if (execution.Order.Name == ENTER_SHORT)
                //{
                //    ExitShortStopMarket(1, stopLoss, EXIT, ENTER_SHORT);
                //}
            }
        }
Пример #2
0
 public void UpdateCurPos(Cbi.Position position, double averagePrice,
                          int quantity, Cbi.MarketPosition marketPosition)
 {
     PosAvgPrice      = averagePrice;
     PosQuantity      = quantity;
     MktPosition      = marketPosition;
     PosUnrealizedPnL = position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, InstStrategy.Close[0]);
 }
Пример #3
0
 protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                           Cbi.MarketPosition marketPosition, string orderId, DateTime time)
 {
     if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
     {
         ExitLongLimit(3, true, 1, execution.Order.AverageFillPrice + (ProfitTicks * TickSize), "EXIT", "SMA");
         ExitLongStopMarket(3, true, 1, execution.Order.AverageFillPrice - (StopLossTicks * TickSize), "EXIT", "SMA");
     }
 }
Пример #4
0
 // WARNING!!!! Market position is not order position
 protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
                                          int quantity, Cbi.MarketPosition marketPosition)
 {
     if (position.MarketPosition == MarketPosition.Flat)
     {
     }
     if (position.MarketPosition == MarketPosition.Long)
     {
     }
     if (position.MarketPosition == MarketPosition.Short)
     {
     }
 }
Пример #5
0
        protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                                  Cbi.MarketPosition marketPosition, string orderId, DateTime time)
        {
            if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
            {
                if (execution.Order.Name == ENTER_LONG)
                {
                    enterTime = time;
                    ExitLongLimit(2, true, 1, execution.Order.AverageFillPrice + (this.ProfitTicks * TickSize), EXIT, ENTER_LONG);
                    ExitLongStopMarket(2, true, 1, execution.Order.AverageFillPrice - (this.StopLossTicks * TickSize), EXIT, ENTER_LONG);
                }
                else if (execution.Order.Name == ENTER_SHORT)
                {
                    enterTime = time;
                    ExitShortLimit(2, true, 1, execution.Order.AverageFillPrice - (this.ProfitTicks * TickSize), EXIT, ENTER_SHORT);
                    ExitShortStopMarket(2, true, 1, execution.Order.AverageFillPrice + (this.StopLossTicks * TickSize), EXIT, ENTER_SHORT);
                }
                else if (TradeSecondTrade)
                {
                    if (secondTrade)
                    {
                        secondTrade = false;
                    }
                    else if (execution.Order.Name == EXIT)
                    {
                        bool enterLong  = (execution.Order.IsLimit && execution.Order.FromEntrySignal == ENTER_LONG);
                        bool enterShort = (execution.Order.IsLimit && execution.Order.FromEntrySignal == ENTER_SHORT);

                        // || (execution.Order.IsStopMarket && execution.Order.FromEntrySignal == ENTER_SHORT)
                        // || (execution.Order.IsStopMarket && execution.Order.FromEntrySignal == ENTER_LONG)

                        if (enterLong)
                        {
                            //secondTrade = true;
                            currentStop = 0;
                            EnterLong(2, 1, ENTER_LONG);
                        }
                        else if (enterShort)
                        {
                            //secondTrade = true;
                            currentStop = 0;
                            EnterShort(2, 1, ENTER_SHORT);
                        }
                    }
                }
            }
        }
Пример #6
0
 protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                           Cbi.MarketPosition marketPosition, string orderId, DateTime time)
 {
     if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
     {
         if (execution.Order.Name == ENTER_LONG)
         {
             //ExitLongLimit(execution.Order.AverageFillPrice + (50 * TickSize), EXIT, ENTER_LONG);
             //ExitLongStopMarket(execution.Order.AverageFillPrice - (40 * TickSize), EXIT, ENTER_LONG);
         }
         else if (execution.Order.Name == ENTER_SHORT)
         {
             //ExitShortLimit(execution.Order.AverageFillPrice - (50 * TickSize), EXIT, ENTER_SHORT);
             //ExitShortStopMarket(execution.Order.AverageFillPrice + (40 * TickSize), EXIT, ENTER_LONG);
         }
     }
 }
Пример #7
0
        protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                                  Cbi.MarketPosition marketPosition, string orderId, DateTime time)
        {
            if (PrintDetails)
            {
                Print(string.Format("{0} | OEU | execution | {1} | {2}", Times[1][0], time, execution.ToString()));
            }

            if (execution.Order.OrderState != OrderState.Filled)
            {
                return;
            }

            // when the entry order fully fills, place the profit target and stop loss
            if (entryOrder != null && execution.Order == entryOrder)
            {
                ocoString = Guid.NewGuid().ToString();

                if (UseProfitTarget)
                {
                    if (PrintDetails)
                    {
                        Print(string.Format("{0} | OEU | placing profit target", execution.Time));
                    }

                    // calculate  a price for the profit target using the secondary series ticksize
                    currentPtPrice = execution.Order.AverageFillPrice + ProfitTargetDistance * tickSizeSecondary;
                    profitTarget   = placeHolderOrder;
                    SubmitOrderUnmanaged(1, OrderAction.Sell, OrderType.Limit, execution.Order.Filled, currentPtPrice, 0, ocoString, "profit target");
                }

                if (UseStopLoss)
                {
                    if (PrintDetails)
                    {
                        Print(string.Format("{0} | OEU | placing stop loss", execution.Time));
                    }

                    currentSlPrice = execution.Order.AverageFillPrice - StopLossDistance * tickSizeSecondary;
                    stopLoss       = placeHolderOrder;
                    SubmitOrderUnmanaged(1, OrderAction.Sell, OrderType.StopMarket, execution.Order.Filled, 0, currentSlPrice, ocoString, "stop loss");
                }
            }
        }
Пример #8
0
 protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                           Cbi.MarketPosition marketPosition, string orderId, DateTime time)
 {
     if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
     {
         if (execution.Order.Name == ENTER_LONG)
         {
             profitTaker = execution.Order.AverageFillPrice + (this.ProfitTicks * TickSize);
             double tempStopLoss = execution.Order.AverageFillPrice - (this.StopLossTicks * TickSize);
             stopLoss = Math.Max(stopLoss, tempStopLoss);
         }
         else if (execution.Order.Name == ENTER_SHORT)
         {
             profitTaker = execution.Order.AverageFillPrice - (this.ProfitTicks * TickSize);
             double tempStopLoss = execution.Order.AverageFillPrice + (this.StopLossTicks * TickSize);
             stopLoss = Math.Min(stopLoss, tempStopLoss);
         }
     }
 }
Пример #9
0
        protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                                  Cbi.MarketPosition marketPosition, string orderId, DateTime time)
        {
            if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
            {
                if (execution.Order.Name == ENTER_LONG)
                {
                    profitTaker = execution.Order.AverageFillPrice + (this.ProfitTicks * TickSize);
                    stopLoss    = execution.Order.AverageFillPrice - (this.StopLossTicks * TickSize);
                }
                else if (execution.Order.Name == ENTER_SHORT)
                {
                    profitTaker = execution.Order.AverageFillPrice - (this.ProfitTicks * TickSize);
                    stopLoss    = execution.Order.AverageFillPrice + (this.StopLossTicks * TickSize);
                }
                else if (TradeSecondTrade)
                {
                    if (secondTrade)
                    {
                        secondTrade = false;
                    }
                    else if (execution.Order.Name == PROFIT_TAKER)
                    {
                        bool enterLong  = execution.Order.FromEntrySignal == ENTER_LONG;
                        bool enterShort = execution.Order.FromEntrySignal == ENTER_SHORT;

                        // || (execution.Order.IsStopMarket && execution.Order.FromEntrySignal == ENTER_SHORT)
                        // || (execution.Order.IsStopMarket && execution.Order.FromEntrySignal == ENTER_LONG)

                        if (enterLong)
                        {
                            //secondTrade = true;
                            EnterLong(2, 1, ENTER_LONG);
                        }
                        else if (enterShort)
                        {
                            //secondTrade = true;
                            EnterShort(2, 1, ENTER_SHORT);
                        }
                    }
                }
            }
        }
Пример #10
0
        protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
                                                 int quantity, Cbi.MarketPosition marketPosition)
        {
            IndicatorProxy.Log2Disk = true;
            int bsx = BarsSinceExitExecution(0, "", 0);
            int bse = BarsSinceEntryExecution(0, "", 0);

            if (PrintOut > 1 && !IsInStrategyAnalyzer)
            {
                IndicatorProxy.PrintLog(true, IsLiveTrading(),
                                        CurrentBar + ":OnPositionUpdate, CurrentTrade not updated -- "
                                        + ";BarsSinceExit, BarsSinceEntry="
                                        + bsx + "," + bse
                                        + ";IsUnmanaged=" + IsUnmanaged
                                        + ";IsLiveTrading=" + IsLiveTrading()
                                        + ";GetMarketPosition=" + GetMarketPosition()
                                        + ";marketPosition=" + marketPosition
                                        + ";HasPosition=" + HasPosition()
                                        + ";CurrentTrade.PosQuantity=" + CurrentTrade.PosQuantity
                                        + ";CurrentTrade.MktPosition=" + CurrentTrade.MktPosition
                                        + ";quantity=" + quantity
                                        + ";GetAvgPrice=" + GetAvgPrice()
                                        + ";averagePrice=" + averagePrice);
            }
            //Print(position.ToString() + "--MarketPosition=" + position.MarketPosition);
            CurrentTrade.OnCurPositionUpdate(position, averagePrice, quantity, marketPosition);
            if (CurrentTrade.MktPosition != null && CurrentTrade.PosAvgPrice != null &&
                CurrentTrade.PosQuantity != null && CurrentTrade.PosUnrealizedPnL != null)
            {
                if (PrintOut > 1 && !IsInStrategyAnalyzer)
                {
                    IndicatorProxy.PrintLog(true, IsLiveTrading(),
                                            String.Format("{0}: OnPositionUpdate, CurrentTrade updated -- CurrentTrade.PosAvgPrice: {1}, CurrentTrade.PosQuantit={2}, CurrentTrade.MktPosition={3}, PnL={4}",
                                                          CurrentBar, CurrentTrade.PosAvgPrice, CurrentTrade.PosQuantity, CurrentTrade.MktPosition, CurrentTrade.PosUnrealizedPnL));
                }
            }
        }
 protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                           Cbi.MarketPosition marketPosition, string orderId, DateTime time)
 {
     RecordTradeProps(execution);
 }
Пример #12
0
        /// <summary>
        /// New position created: update position, setup SL/PT orders(the same bar);
        /// Old position closed: Start a new trade;
        /// Position is being held: check performane to adjust SL/PT orders;
        /// All flat: wait for Command/TradeSignal, or detect new position created;
        ///
        /// </summary>
        /// <param name="position"></param>
        /// <param name="averagePrice"></param>
        /// <param name="quantity">always>0</param>
        /// <param name="marketPosition">flat/long/short</param>
        public virtual void OnCurPositionUpdate(Cbi.Position position, double averagePrice,
                                                int quantity, Cbi.MarketPosition marketPosition)
        {
            try     {
                if (!this.InstStrategy.IsInStrategyAnalyzer && InstStrategy.PrintOut > 1)
                {
                    InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                                                         String.Format("{0}:OnCurPositionUpdate - AvgPrc: {1}, Quant={2}, MktPos={3}, marketPosition={4}, PnL={5}",
                                                                       InstStrategy.CurrentBar, PosAvgPrice, PosQuantity, MktPosition, marketPosition, PosUnrealizedPnL));
                }
                //Position pos = position.MemberwiseClone();
                PositionStatus ps = InstStrategy.GetPositionStatus(PosQuantity);
                switch (ps)
                {
                case PositionStatus.NewEstablished:                        //New position created, setup SL/PT(the same bar)
                    if (!this.InstStrategy.IsInStrategyAnalyzer && InstStrategy.PrintOut > 1)
                    {
                        InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                                                             String.Format("{0}:PositionStatus.NewEstablished, MktPos={1}, PosQuantity={2}, marketPosition={3}, quantity={4}",
                                                                           InstStrategy.CurrentBar, MktPosition, PosQuantity, marketPosition, quantity));
                    }
                    UpdateCurPos(position, averagePrice, quantity, marketPosition);

                    /*InstStrategy.CheckExitSignals();
                     * InstStrategy.SetExitTradeAction();
                     * InstStrategy.TakeTradeAction();*/
                    break;

                case PositionStatus.Liquidate:                        //Positions were closed, trade is done, init a new trade;
                    if (!this.InstStrategy.IsInStrategyAnalyzer && InstStrategy.PrintOut > 1)
                    {
                        InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                                                             String.Format("{0}:PositionStatus.Liquidate, MktPos={1}, PosQuantity={2}, marketPosition={3}, quantity={4}",
                                                                           InstStrategy.CurrentBar, MktPosition, PosQuantity, marketPosition, quantity));
                    }
                    InitNewTrade();
                    break;

                case PositionStatus.Hold:                        //Position is held, change SL/PT by rule/performance, scale in/out occured;
                    //=> this case is replaced by CheckPerformance() from OnBarUpdate, it won't happen here, only scale in/out will happen
                    if (!this.InstStrategy.IsInStrategyAnalyzer && InstStrategy.PrintOut > 1)
                    {
                        InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                                                             String.Format("{0}:Position is Held, MktPos={1}, marketPosition={2}, PosQuant={3}, Quant={4}",
                                                                           InstStrategy.CurrentBar, MktPosition, marketPosition, PosQuantity, quantity));
                    }
//						InstStrategy.CalProfitTargetAmt(PosAvgPrice, InstStrategy.MM_ProfitFactor);
//						InstStrategy.CalExitOcoPrice(PosAvgPrice, InstStrategy.MM_ProfitFactor);
//						InstStrategy.SetSimpleExitOCO(TradeAction.EntrySignal.SignalName);
                    break;

                case PositionStatus.Flat:
                    break;

                case PositionStatus.ScaledIn:
                    if (!this.InstStrategy.IsInStrategyAnalyzer && InstStrategy.PrintOut > 1)
                    {
                        InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                                                             String.Format("{0}:PositionStatus.ScaledIn, MktPos={1}, marketPosition={2}, PosQuant={3}, Quant={4}",
                                                                           InstStrategy.CurrentBar, MktPosition, marketPosition, PosQuantity, quantity));
                    }
                    break;

                case PositionStatus.ScaledOut:
                    if (!this.InstStrategy.IsInStrategyAnalyzer && InstStrategy.PrintOut > 1)
                    {
                        InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                                                             String.Format("{0}:PositionStatus.ScaledOut, MktPos={1}, marketPosition={2}, PosQuant={3}, Quant={4}",
                                                                           InstStrategy.CurrentBar, MktPosition, marketPosition, PosQuantity, quantity));
                    }
                    break;

                case PositionStatus.UnKnown:
                    break;
                }

                /*
                 * if (MktPosition == MarketPosition.Flat)
                 * {
                 *      if(marketPosition == MarketPosition.Flat) {
                 *              InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                 *              String.Format("{0}:NoChangeOnPosition, MktPos={1}, marketPosition={2}",
                 *                      InstStrategy.CurrentBar, MktPosition, marketPosition));
                 *      } else { //New position created, setup SL/PT
                 *              InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                 *              String.Format("{0}:NewOnPosition, MktPos={1}, marketPosition={2}",
                 *                      InstStrategy.CurrentBar, MktPosition, marketPosition));
                 *      }
                 *      //TradeAction.TrailingProfitTargetTics = InstStrategy.GetTicksByCurrency(TradeAction.profitTargetAmt);
                 *      //trailingSLTic = GetTicksByCurrency(CurrentTrade.stopLossAmt);
                 * }
                 * else if (marketPosition == MarketPosition.Flat) { //Positions were closed, trade is done, init a new trade;
                 *      InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(), "InitNewTrade called");
                 *      InitNewTrade();
                 * }
                 * else //Position is held, change SL/PT by rule/performance, scale in/out occured;
                 * {
                 *      InstStrategy.IndicatorProxy.PrintLog(true, InstStrategy.IsLiveTrading(),
                 *              String.Format("{0}:Position is Held, MktPos={1}, marketPosition={2}, PosQuant={3}, Quant={4}",
                 *              InstStrategy.CurrentBar, MktPosition, marketPosition, PosQuantity, quantity));
                 *      InstStrategy.CalProfitTargetAmt(PosAvgPrice, InstStrategy.MM_ProfitFactor);
                 *      InstStrategy.CalExitOcoPrice(PosAvgPrice, InstStrategy.MM_ProfitFactor);
                 *      InstStrategy.SetSimpleExitOCO(TradeAction.EntrySignal.SignalName);
                 * //				SetBracketOrder.OCOOrder.ProfitTargetOrder(OrderSignalName.EntryShort.ToString());
                 * //				SetBracketOrder.OCOOrder.StopLossOrder(OrderSignalName.EntryShort.ToString());
                 * }
                 */
            } catch (Exception ex) {
                InstStrategy.Print("Exception=" + ex.StackTrace);
            }
        }
Пример #13
0
 protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
                                          int quantity, Cbi.MarketPosition marketPosition)
 {
 }
Пример #14
0
        protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                                                  Cbi.MarketPosition marketPosition, string orderId, DateTime time)
        {
            if (PrintDetails)
            {
                Print(string.Format("{0} | OEU | execution | {1} | {2}", Times[1][0], time, execution.ToString()));
            }

            if (execution.Order.OrderState != OrderState.Filled)
            {
                return;
            }

            // when the entry order fills, place the profit target and stop loss
            if (entryOrder != null && execution.Order == entryOrder)
            {
                if (UseProfitTarget)
                {
                    if (PrintDetails)
                    {
                        Print(string.Format("{0} | OEU | placing profit target", execution.Time));
                    }

                    // setting initial profit target
                    if (isLongTrade)
                    {
                        // calculate  a price for the profit target using the secondary series ticksize
                        // currentPtPrice = execution.Order.AverageFillPrice + ProfitTargetDistance * tickSizeSecondary;
                        currentPtPrice = execution.Order.AverageFillPrice + profiltsTaking * tickSizeSecondary;
                        profitTarget   = placeHolderOrder;
                        ExitLongLimit(1, true, entryOrder.Quantity, currentPtPrice, "profit target", "entry");
                    }
                    else
                    {
                        // calculate  a price for the profit target using the secondary series ticksize
                        // currentPtPrice = execution.Order.AverageFillPrice - ProfitTargetDistance * tickSizeSecondary;
                        currentPtPrice = execution.Order.AverageFillPrice - profiltsTaking * tickSizeSecondary;
                        profitTarget   = placeHolderOrder;
                        ExitShortLimit(1, true, entryOrder.Quantity, currentPtPrice, "profit target", "entry");
                    }
                }

                if (UseStopLoss)
                {
                    if (PrintDetails)
                    {
                        Print(string.Format("{0} | OEU | placing stop loss", execution.Time));
                    }

                    //setting initial stop loss
                    if (isLongTrade)
                    {
                        // currentSlPrice = execution.Order.AverageFillPrice - StopLossDistance * tickSizeSecondary;
                        currentSlPrice = execution.Order.AverageFillPrice - stopLossVal * tickSizeSecondary;
                        stopLoss       = placeHolderOrder;
                        ExitLongStopMarket(1, true, entryOrder.Quantity, currentSlPrice, "stop loss", "entry");
                    }
                    else
                    {
                        // currentSlPrice = execution.Order.AverageFillPrice + StopLossDistance * tickSizeSecondary;
                        currentSlPrice = execution.Order.AverageFillPrice + stopLossVal * tickSizeSecondary;
                        stopLoss       = placeHolderOrder;
                        ExitShortStopMarket(1, true, entryOrder.Quantity, currentSlPrice, "stop loss", "entry");
                    }
                }
            }
        }
Пример #15
0
        protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
        {
            //Reset
            if (position.MarketPosition == MarketPosition.Flat)
            {
                SetStopLoss("StartOrder", CalculationMode.Pips, 90, true);
                CollectOrder = null;
                //SetStopLoss("StartOrder", CalculationMode.Pips, 90, true);
                _status = "";
            }
            //         if (MyAllOrders != null && !_status.IsNullOrEmpty() && CollectionMIT != null && _status.Equals("Collection") && CollectionMIT.OrderState == OrderState.Filled)
            //         {
            //             //position.BreakEven(MyAllOrders);
            //             Draw.Dot(this, @"MyCustomStrategy" + CurrentBars[0].ToString(), false, 0, Close[0], Brushes.Red);
            //         }


            //Recover
            if (_status.Equals("Recover"))
            {
                SetProfitTarget("Recover1", CalculationMode.Price, Position.AveragePrice + 1 * TickSize, true);
                SetProfitTarget("StartOrder", CalculationMode.Price, Position.AveragePrice + 1 * TickSize, true);
                SetProfitTarget("Recover2", CalculationMode.Price, Position.AveragePrice + 1 * TickSize, true);
                SetProfitTarget("Recover3", CalculationMode.Price, Position.AveragePrice + 1 * TickSize, true);
            }
        }
Пример #16
0
 protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, Cbi.MarketPosition marketPosition, string orderId, DateTime time)
 {
     //if (execution.Order.Name == "entryOrder" && execution.Order.OrderState == OrderState.Filled)
     //    entryOrder = execution.Order;
 }