Пример #1
0
        public void OpenPositionReceived(OpenPositionData openPositionData)
        {
            Task task = new Task(() =>
            {
                Log(LogEntryImportance.Info, "OpenPosition message received", true);
                if (openPositionData != null)
                {
                    //Cancel opening order
                    bool cancelOpeningOrderRes = CancelOpeningOrder();
                    if (!cancelOpeningOrderRes)
                    {
                        Log(LogEntryImportance.Error, "Unable to cancel current opening order. Cannot open new Position", true);
                    }
                    else
                    {
                        Log(LogEntryImportance.Info, "Opening Position...", true);
                        OpenPosition(openPositionData);
                    }
                }
                else
                {
                    Log(LogEntryImportance.Info, "OpenPositionData is null. Cannot open new Position...", true);
                }
            });

            task.Start();
        }
Пример #2
0
        private void OpenPosition(OpenPositionData openPositionData)
        {
            try
            {
                BrokerPosition           = new Position(this);
                BrokerPosition.Direction = openPositionData.Direction;

                //Create opening order
                Order openingOrder = _orderFactory.CreateOpeningOrder(openPositionData.Direction, KrakenOrderType.stop_loss, openPositionData.EnteringPrice, openPositionData.Volume, openPositionData.CandleStickId, openPositionData.ConfirmationId, validateOnly: openPositionData.ValidateOnly);
                UpdateOpeningOrder(openingOrder);
                //Place opening order and wait until closed or canceled
                Log(LogEntryImportance.Info, "Placing opening order...", true);
                PlaceOrderResult openingOrderResult = _client.PlaceOrder(openingOrder, true);
                openingOrder = openingOrderResult.Order;
                UpdateOpeningOrder(openingOrder);
                bool ok = false;

                #region Handle opening-order result

                switch (openingOrderResult.ResultType)
                {
                case PlaceOrderResultType.error:
                    _client.HandleErrors(openingOrderResult.Errors);
                    break;

                case PlaceOrderResultType.success:
                    Log(LogEntryImportance.Info, "Opening order filled", true);
                    UpdateEmergencyOrder(_orderFactory.CreateEmergencyExitOrder(openingOrder));
                    ok = true;
                    break;

                case PlaceOrderResultType.partial:
                    Log(LogEntryImportance.Info, string.Format("Opening order partially filled: {0}/{1}", openingOrder.VolumeExecuted, openingOrder.Volume), true);
                    ok = true;
                    break;

                case PlaceOrderResultType.txid_null:
                    Log(LogEntryImportance.Error, "An error occured while attempting to place an opening order: txid is null (unknown reason. Maybe the ValidateOnly argument was set to true)", true);
                    break;

                case PlaceOrderResultType.canceled_not_partial:
                    Log(LogEntryImportance.Info, string.Format("The opening order was canceled: {0}", openingOrder.Reason), true);
                    break;

                case PlaceOrderResultType.exception:
                    Log(LogEntryImportance.Error, string.Format("An error occured while attempting to place an opening order: {0}", openingOrderResult.Exception.Message), true);
                    break;

                default:
                    Log(LogEntryImportance.Error, string.Format("Unknown PlaceOrderResultType {0}", openingOrderResult.ResultType), true);
                    break;
                }

                #endregion


                if (!ok)
                {
                    return;
                }

                //if nothing went wrong, place exiting order
                Order closingOrder = _orderFactory.CreateStopLossOrder(openingOrder, openPositionData.ExitingPrice, openPositionData.ValidateOnly);
                UpdateClosingOrder(closingOrder);
                //Place closing order and wait until closed or canceled
                Log(LogEntryImportance.Info, "Placing closing order...", true);
                PlaceOrderResult closingOrderResult = _client.PlaceOrder(closingOrder, true);
                closingOrder = closingOrderResult.Order;
                UpdateClosingOrder(closingOrder);

                #region Handle closing-order result

                switch (closingOrderResult.ResultType)
                {
                case PlaceOrderResultType.error:
                    _client.HandleErrors(closingOrderResult.Errors);
                    break;

                case PlaceOrderResultType.success:
                    Log(LogEntryImportance.Info, "Closing order filled", true);
                    UpdateEmergencyOrder(null);
                    break;

                case PlaceOrderResultType.partial:
                    Log(LogEntryImportance.Info, string.Format("Closing order partially filled: {0}/{1}", closingOrder.VolumeExecuted, closingOrder.Volume), true);
                    break;

                case PlaceOrderResultType.txid_null:
                    Log(LogEntryImportance.Error, "An error occured while attempting to place a closing order: txid is null (unknown reason. Maybe the ValidateOnly argument was set to true)", true);
                    break;

                case PlaceOrderResultType.canceled_not_partial:
                    Log(LogEntryImportance.Info, string.Format("The closing order was canceled: {0}", closingOrder.Reason), true);
                    break;

                case PlaceOrderResultType.exception:
                    Log(LogEntryImportance.Error, string.Format("An error occured while attempting to place a closing order: {0}", closingOrderResult.Exception.Message), true);
                    break;

                default:
                    Log(LogEntryImportance.Error, string.Format("Unknown PlaceOrderResultType {0}", closingOrderResult.ResultType), true);
                    break;
                }

                #endregion
            }
            catch (Exception ex)
            {
                Log(LogEntryImportance.Error, string.Format("An exception occured in OpenPosition at line {0}. {1} {2}", ex.LineNumber(), ex.Message, ((ex.InnerException != null) ? ex.InnerException.Message : "")), true);
            }
        }