예제 #1
0
        public TradingItem(TradingStrategy tsItem, string itemCode, string itemName, long buyingPrice, int buyingQnt, bool completeBuying = false, bool sold = false, string buyOrderType = "", string sellOrderType = "")
        {
            this.ts               = tsItem;
            this.itemCode         = itemCode;
            this.itemName         = itemName;
            this.buyingPrice      = buyingPrice;
            this.buyingQnt        = buyingQnt;
            this.outStandingQnt   = buyingQnt;
            this.isCompleteBuying = false;
            this.conditionUid     = ts.buyCondition.Uid;
            this.isBuyCancel      = false;
            this.isCompleteSold   = false;

            this.buyOrderNum  = string.Empty;
            this.sellOrderNum = string.Empty;

            this.buyOrderType = buyOrderType;
            //this.sellOrderType = sellOrderType;
            this.usingDivideSellLoss         = ts.useDivideSellLoss;
            this.usingDivideSellProfit       = ts.useDivideSellProfit;
            this.usingDivideSellLossLoop     = ts.useDivideSellLossLoop;
            this.usingDivideSellProfitLoop   = ts.useDivideSellProfitLoop;
            this.usingStopLossAfterBuyMore   = ts.usingStopLossAfterBuyMore;
            this.usingTakeProfitAfterBuyMore = ts.usingTakeProfitAfterBuyMore;
            this.divideSellCount             = ts.divideSellCount;
            this.divideSellCountProfit       = ts.divideSellCountProfit;
            this.Uid = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
            curState = TRADING_ITEM_STATE.AUTO_TRADING_STATE_BUY_BEFORE_ORDER;
        }
예제 #2
0
        public TradingStrategyForSave(TradingStrategy strategy)
        {
            BindingFlags flags = BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;

            FieldInfo[] fieldArray = strategy.GetType().GetFields(flags);

            BindingFlags flagsStrategySave = BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;

            FieldInfo[] StrategySaveFieldArray = this.GetType().GetFields(flagsStrategySave);


            try
            {
                foreach (FieldInfo field in fieldArray)
                {
                    foreach (FieldInfo SaveField in StrategySaveFieldArray)
                    {
                        if (field.Name == SaveField.Name)
                        {
                            SaveField.SetValue(this, field.GetValue(strategy));
                        }
                    }
                }

                foreach (var item in strategy.tradingItemList)
                {
                    tradingSaveItemList.Add(new TradingItemForSave(item, this));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
예제 #3
0
        public void Stop()
        {
            martinGailStack.Clear();

            tradingStrategy = null;
            Item            = null;
            step            = 0;
            maxStep         = 0;
            startMoney      = 0;
        }
예제 #4
0
        public bool IsMartinStrategy(TradingStrategy strategy)
        {
            if (tradingStrategy == null)
            {
                return(false);
            }

            if (tradingStrategy == strategy)
            {
                return(true);
            }
            return(false);
        }
예제 #5
0
        public void SetMartinStrategy(TradingStrategy strategy, int _maxStep)
        {
            step                         = 0;
            maxStep                      = _maxStep;
            startMoney                   = strategy.itemInvestment;
            tradingStrategy              = strategy;
            strategy.OnReceiveCondition += OnReceiveConditionResult;

            strategy.OnReceiveBuyOrder  += OnReceiveBuyOrderTryResult;
            strategy.OnReceiveBuyChejan += OnReceiveChejanResult;

            strategy.OnReceiveSellOrder  += OnReceiveSellOrderTryResult;
            strategy.OnReceiveSellChejan += OnReceiveSellChejanResult;
        }
예제 #6
0
        public TrailingItem(string itemcode, int _firstPrice, TradingStrategy inputStrategy)
        {
            itemCode         = itemcode;
            strategy         = inputStrategy;
            firstPrice       = _firstPrice;
            settingTickCount = strategy.trailTickValue;
            tickBongInfoMgr  = new TickBongInfoMgr(settingTickCount);

            buyOrderOption = inputStrategy.buyOrderOption;

            //sellOrderOption = inputStrategy.sellOrderOption;

            itemInvestment = inputStrategy.itemInvestment;
            if (inputStrategy.usingPercentageBuy)
            {
                percentageCheckPrice = (int)((float)firstPrice * (100.0f - inputStrategy.percentageBuyValue) * 0.01f);
                isPercentageCheckBuy = true;
            }
            if (inputStrategy.usingGapTrailBuy)
            {
                isGapTrailBuy              = true;
                gapTrailBuyCheckPrice      = firstPrice; //등장 시점의가격
                gapTrailBuyCheckDateTime   = DateTime.Now;
                gapTrailBuyCheckTimeSecond = inputStrategy.gapTrailBuyTimeValue;
            }
            isVwmaCheck       = inputStrategy.usingVwma;
            isCheckStockIndex = inputStrategy.usingCheckIndex;

            if (inputStrategy.usingEnvelope5)
            {
                EnvelopeValueList.Add(0.05);
            }
            if (inputStrategy.usingEnvelope7)
            {
                EnvelopeValueList.Add(0.07);
            }
            if (inputStrategy.usingEnvelope10)
            {
                EnvelopeValueList.Add(0.1);
            }
            if (inputStrategy.usingEnvelope15)
            {
                EnvelopeValueList.Add(0.15);
            }
        }
예제 #7
0
        public void AddStrategyToDataGridView(TradingStrategy tradingStrategy)
        {
            if (tradingStrategy != null)
            {
                int rowIndex = tsDataGridView.Rows.Add();
                tsDataGridView["매매전략_계좌번호", rowIndex].Value   = tradingStrategy.account;
                tsDataGridView["매매전략_재실행", rowIndex].Value    = tradingStrategy.usingRestart;
                tsDataGridView["매매전략_매수조건식", rowIndex].Value  = tradingStrategy.buyCondition.Name;
                tsDataGridView["매매전략_시간제한사용", rowIndex].Value = tradingStrategy.usingTimeLimit;

                if (tradingStrategy.usingTimeLimit)
                {
                    tsDataGridView["매매전략_시작시간", rowIndex].Value = tradingStrategy.startDate.ToString("HH:mm");
                    tsDataGridView["매매전략_종료시간", rowIndex].Value = tradingStrategy.endDate.ToString("HH:mm");
                }
                if (tradingStrategy.usingTickBuy)
                {
                    tsDataGridView["매매전략_호가적용", rowIndex].Value = (int)tradingStrategy.tickBuyValue * -1;
                }
                if (tradingStrategy.usingTickBuy)
                {
                    tsDataGridView["매매전략_호가적용", rowIndex].Value = (int)tradingStrategy.tickBuyValue * -1;
                }
                if (tradingStrategy.usingTrailing)
                {
                    tsDataGridView["매매전략_트레일링", rowIndex].Value = (int)tradingStrategy.trailTickValue;
                }

                tsDataGridView["매매전략_총투자금", rowIndex].Value   = tradingStrategy.totalInvestment;
                tsDataGridView["매매전략_매수종목수", rowIndex].Value  = tradingStrategy.buyItemCount;
                tsDataGridView["매매전략_종목별투자금", rowIndex].Value = tradingStrategy.itemInvestment;
                if (tradingStrategy.GetTradingStrategyItem(StrategyItemName.TAKE_PROFIT_SELL) != null)
                {
                    tsDataGridView["매매전략_익절", rowIndex].Value  = tradingStrategy.GetActiveTradingStrategyItem(StrategyItemName.TAKE_PROFIT_SELL);
                    tsDataGridView["매매전략_익절률", rowIndex].Value = ((TradingStrategyItemWithUpDownValue)tradingStrategy.GetTradingStrategyItem(StrategyItemName.TAKE_PROFIT_SELL)).checkConditionValue;
                }
                if (tradingStrategy.GetTradingStrategyItem(StrategyItemName.STOPLOSS_SELL) != null)
                {
                    tsDataGridView["매매전략_손절", rowIndex].Value  = tradingStrategy.GetActiveTradingStrategyItem(StrategyItemName.STOPLOSS_SELL);
                    tsDataGridView["매매전략_손절률", rowIndex].Value = ((TradingStrategyItemWithUpDownValue)tradingStrategy.GetTradingStrategyItem(StrategyItemName.STOPLOSS_SELL)).checkConditionValue;
                }
            }
        }
예제 #8
0
        public int CheckMartinValid(TradingStrategy strategy)
        {
            if (tradingStrategy != null)
            {
                return(Martin_ErrorCode.ALREADY_STRATEGY);
            }
            if (strategy.takeProfitRate == 0 || strategy.takeProfitRate != strategy.stoplossRate * -1)
            {
                return(Martin_ErrorCode.NOT_VALID_PROFIT);
            }
            if (strategy.usingRestart != false)
            {
                return(Martin_ErrorCode.RESTART_ON);
            }
            if (strategy.buyItemCount != 1)
            {
                return(Martin_ErrorCode.BUY_ITEM_COUNT);
            }

            return(Martin_ErrorCode.ERR_NONE);
        }
예제 #9
0
        public TrailingPercentageItemForSave(TrailingItem itemTrail, TradingStrategy ts)
        {
            strategySave = new TradingStrategyForSave(ts);

            BindingFlags flags = BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;

            FieldInfo[] fieldArray = itemTrail.GetType().GetFields(flags);

            BindingFlags flagsStrategySave = BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;

            FieldInfo[] StrategySaveFieldArray = this.GetType().GetFields(flagsStrategySave);

            foreach (FieldInfo field in fieldArray)
            {
                foreach (FieldInfo SaveField in StrategySaveFieldArray)
                {
                    if (field.Name == SaveField.Name)
                    {
                        SaveField.SetValue(this, field.GetValue(itemTrail));
                    }
                }
            }
        }
예제 #10
0
        private void AddStratgy(TradingStrategyForSave saved)
        {
            List <TradingStrategyADDItem> tradingStrategyItemList = new List <TradingStrategyADDItem>();

            TradingStrategy ts = new TradingStrategy(
                saved.account,
                saved.buyCondition,
                saved.buyOrderOption,
                saved.totalInvestment,
                saved.buyItemCount,
                saved.sellProfitOrderOption,
                saved.sellStopLossOrderOption,
                saved.sellDivideStopLossOrderOption,
                false,
                saved.usingRestart
                );

            //추가전략 적용
            ts.remainItemCount = saved.remainItemCount;

            //trading item load
            foreach (var item in saved.tradingSaveItemList)
            {
                TradingItem itemAdd = item.ReloadTradingItem();
                itemAdd.ts = ts;
                ts.tradingItemList.Add(itemAdd);

                string fidList = "9001;302;10;11;25;12;13"; //9001:종목코드,302:종목명
                axKHOpenAPI1.SetRealReg("9001", itemAdd.itemCode, fidList, "1");

                //매매진행 데이터 그리드뷰 표시
                form.SetTradingItemUI(itemAdd);
            }

            if (saved.usingTimeLimit)
            {
                TradingStrategyItemBuyTimeCheck timeBuyCheck =
                    new TradingStrategyItemBuyTimeCheck(
                        StrategyItemName.BUY_TIME_LIMIT,
                        CHECK_TIMING.BUY_TIME,
                        new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, saved.startDate.Hour, saved.startDate.Minute, 0),
                        new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, saved.endDate.Hour, saved.endDate.Minute, 0));
                ts.AddTradingStrategyItemList(timeBuyCheck);
                ts.usingTimeLimit = true;
                ts.startDate      = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, saved.startDate.Hour, saved.startDate.Minute, 0);
                ts.endDate        = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, saved.endDate.Hour, saved.endDate.Minute, 0);
            }

            ts.usingTickBuy = saved.usingTickBuy;
            ts.tickBuyValue = saved.tickBuyValue;

            ts.usingTrailing   = saved.usingTrailing;
            ts.trailTickValue  = saved.trailTickValue;
            ts.trailMinusValue = saved.trailMinusValue;

            ts.usingPercentageBuy = saved.usingPercentageBuy;
            ts.percentageBuyValue = saved.percentageBuyValue;

            ts.usingVwma       = saved.usingVwma;
            ts.usingEnvelope5  = saved.usingEnvelope5;
            ts.usingEnvelope7  = saved.usingEnvelope7;
            ts.usingEnvelope10 = saved.usingEnvelope10;
            ts.usingEnvelope15 = saved.usingEnvelope15;
            ts.usingCheckIndex = saved.usingCheckIndex;
            ts.trailTickValue  = saved.trailTickValue;

            if (saved.usingGapTrailBuy)
            {
                ts.usingGapTrailBuy            = true;
                ts.gapTrailCostPercentageValue = saved.gapTrailCostPercentageValue;
                ts.gapTrailBuyTimeValue        = saved.gapTrailBuyTimeValue;

                TradingStrategyItemWithUpDownPercentValue trailGapBuy =
                    new TradingStrategyItemWithUpDownPercentValue(
                        StrategyItemName.BUY_GAP_CHECK,
                        CHECK_TIMING.BUY_TIME,
                        string.Empty,
                        ts.gapTrailCostPercentageValue);

                trailGapBuy.OnReceivedTrData += form.OnReceiveTrDataCheckGapTrailBuy;
                ts.AddTradingStrategyItemList(trailGapBuy);
            }

            if (saved.takeProfitRate > 0)
            {
                double takeProfitRate = 0;
                TradingStrategyItemWithUpDownValue takeProfitStrategy = null;
                takeProfitRate     = saved.takeProfitRate;
                takeProfitStrategy =
                    new TradingStrategyItemWithUpDownValue(
                        StrategyItemName.TAKE_PROFIT_SELL,
                        CHECK_TIMING.SELL_TIME,
                        IS_TRUE_OR_FALE_TYPE.UPPER_OR_SAME,
                        takeProfitRate);
                takeProfitStrategy.OnReceivedTrData += form.OnReceiveTrDataCheckProfitSell;
                ts.AddTradingStrategyItemList(takeProfitStrategy);
                ts.takeProfitRate = takeProfitRate;
            }

            if (saved.stoplossRate < 0)
            {
                double stopLossRate = 0;
                stopLossRate = saved.stoplossRate;
                TradingStrategyItemWithUpDownValue stopLossStrategy = null;
                stopLossStrategy =
                    new TradingStrategyItemWithUpDownValue(
                        StrategyItemName.STOPLOSS_SELL,
                        CHECK_TIMING.SELL_TIME,
                        IS_TRUE_OR_FALE_TYPE.DOWN,
                        stopLossRate);

                stopLossStrategy.OnReceivedTrData += form.OnReceiveTrDataCheckStopLoss;
                ts.AddTradingStrategyItemList(stopLossStrategy);
                ts.stoplossRate = stopLossRate;
            }

            if (saved.usingBuyMore)
            {
                ts.usingBuyMore = true;
                //물타기
                TradingStrategyItemBuyingDivide buyMoreStrategy =
                    new TradingStrategyItemBuyingDivide(
                        StrategyItemName.BUY_MORE_LOSS,
                        CHECK_TIMING.SELL_TIME,
                        IS_TRUE_OR_FALE_TYPE.DOWN,
                        saved.buyMoreRateLoss,
                        saved.buyMoreMoney);

                buyMoreStrategy.OnReceivedTrData += form.OnReceiveTrDataBuyMore;
                ts.AddTradingStrategyItemList(buyMoreStrategy);
                //불타기
                TradingStrategyItemBuyingDivide buyMoreStrategyProfit =
                    new TradingStrategyItemBuyingDivide(
                        StrategyItemName.BUY_MORE_PROFIT,
                        CHECK_TIMING.SELL_TIME,
                        IS_TRUE_OR_FALE_TYPE.UPPER_OR_SAME,
                        saved.buyMoreRateProfit,
                        saved.buyMoreMoney);

                buyMoreStrategyProfit.OnReceivedTrData += form.OnReceiveTrDataBuyMore;
                ts.AddTradingStrategyItemList(buyMoreStrategyProfit);
            }

            if (saved.usingBuyCancelByTime)
            {
                ts.usingBuyCancelByTime = true;

                TradingStrategyItemCancelByTime buyCancelStrategy =
                    new TradingStrategyItemCancelByTime(
                        StrategyItemName.BUY_CANCEL_BY_TIME,
                        CHECK_TIMING.BUY_ORDER_BEFORE_CONCLUSION,
                        DateTime.Now.Ticks
                        );

                buyCancelStrategy.OnReceivedTrData += form.OnReceiveTrDataBuyCancelByTime;
                ts.AddTradingStrategyItemList(buyCancelStrategy);
            }

            if (saved.useDivideSellLoss)
            {
                ts.useDivideSellLoss     = true;
                ts.useDivideSellLossLoop = saved.useDivideSellLossLoop;
                TradingStrategyItemWithUpDownValue divideStopLossStrategy = null;
                divideStopLossStrategy =
                    new TradingStrategyItemWithUpDownValue(
                        StrategyItemName.STOPLOSS_DIVIDE_SELL,
                        CHECK_TIMING.SELL_TIME,
                        IS_TRUE_OR_FALE_TYPE.DOWN,
                        saved.divideStoplossRate);

                divideStopLossStrategy.OnReceivedTrData += form.OnReceiveTrDataCheckStopLossDivide;

                ts.AddTradingStrategyItemList(divideStopLossStrategy);
                ts.divideStoplossRate       = saved.divideStoplossRate;
                ts.divideSellLossPercentage = (saved.divideSellLossPercentage);
                ts.divideSellCount          = saved.divideSellCount;
            }

            if (saved.useDivideSellProfit)
            {
                ts.useDivideSellProfit     = true;
                ts.useDivideSellProfitLoop = saved.useDivideSellProfitLoop;
                TradingStrategyItemWithUpDownValue divideProfitStrategy = null;
                divideProfitStrategy =
                    new TradingStrategyItemWithUpDownValue(
                        StrategyItemName.TAKE_PROFIT_DIVIDE_SELL,
                        CHECK_TIMING.SELL_TIME,
                        IS_TRUE_OR_FALE_TYPE.UPPER_OR_SAME,
                        saved.divideTakeProfitRate);

                divideProfitStrategy.OnReceivedTrData += form.OnReceiveTrDataCheckProfitDivide;

                ts.AddTradingStrategyItemList(divideProfitStrategy);
                ts.divideTakeProfitRate       = saved.divideTakeProfitRate;
                ts.divideSellProfitPercentage = (saved.divideSellProfitPercentage);
                ts.divideSellCountProfit      = saved.divideSellCountProfit;
            }

            if (form.tradingStrategyList.FindAll(o => (o.buyCondition.Name == ts.buyCondition.Name)).Count > 0)
            {
                return;
            }

            form.tradingStrategyList.Add(ts);
            form.AddStrategyToDataGridView(ts);

            if (ts.buyCondition.Name.Contains("dummy") == false)
            {
                form.StartMonitoring(ts.buyCondition);
            }

            if (saved.usingDoubleCheck)
            {
                form.StartMonitoring(ts.doubleCheckCondition);
                form.doubleCheckHashTable.Add(ts.doubleCheckCondition.Name, ts);
            }
        }