Example #1
0
        //public override bool reqOrder_mt4(...
        public EFILLED_STATE reqOrder_mt4(string sSymbol, ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "",
                                          double dLots_exc = 0, double dPrice_exc = 0, DateTime dtTime_exc = default(DateTime))
        {
            if (CFATManager.m_nRunMode == ERUN_MODE.SIMULATION)
            {
                return(base.reqOrder(sSymbol, nCmd, ref dLots, ref dPrice, nOrderType, sLogicID, sComment, dLots, dPrice, DateTime.Now));
            }
            bool bRet = true;

            double dLots_req  = dLots;
            double dPrice_req = dPrice;

            //Send Request newOrder or Close or
            if (nCmd == ETRADER_OP.BUY)
            {
                bRet = m_mt4ApiDLL.mt4_reqNewOrder(sSymbol, "BUY", ref dLots, ref dPrice);
            }

            if (nCmd == ETRADER_OP.SELL)
            {
                bRet = m_mt4ApiDLL.mt4_reqNewOrder(sSymbol, "SELL", ref dLots, ref dPrice);
            }

            if (nCmd == ETRADER_OP.BUY_CLOSE || nCmd == ETRADER_OP.SELL_CLOSE)
            {
                bRet = closeOrder(sSymbol, nCmd, ref dLots, ref dPrice, nOrderType, sLogicID);
            }

            if (!bRet)
            {
                return(EFILLED_STATE.FAIL);
            }
            return(base.reqOrder(sSymbol, nCmd, ref dLots_req, ref dPrice_req, nOrderType, sLogicID, sComment, dLots, dPrice, DateTime.Now));
        }
Example #2
0
        private EFILLED_STATE register2Vt(string sSymbol, ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "",
                                          double dLots_exc = 0, double dPrice_exc = 0, DateTime dtTime_exc = default(DateTime))
        {
            if (dLots_exc == 0)
            {
                dLots_exc = dLots;
            }
            if (dPrice_exc == 0)
            {
                dPrice_exc = dPrice;
            }

            if (dtTime_exc == default(DateTime))
            {
                dtTime_exc = m_rates[sSymbol].m_dtTime;
            }

            if (nCmd == ETRADER_OP.BUY || nCmd == ETRADER_OP.SELL)
            {//For new order
                TPosItem posItem = new TPosItem();
                posItem.m_dtOpenTime_req = m_rates[sSymbol].m_dtTime;
                posItem.m_dtOpenTime_exc = dtTime_exc;

                posItem.m_dOpenPrice_req = dPrice;
                posItem.m_dOpenPrice_exc = dPrice_exc;

                posItem.m_dLots_req = dLots;
                posItem.m_dLots_exc = dLots_exc;

                posItem.m_nCmd                = nCmd;
                posItem.m_sSymbol             = sSymbol;
                posItem.m_dProfit_vt          = 0;
                posItem.m_sLogicID            = sLogicID;
                posItem.m_sComment            = sComment;
                posItem.m_dContractSize       = getContractSize(sSymbol);
                posItem.m_dCommission_percent = getCommissionPercent(sSymbol);

                if (sLogicID != "REQ_MERGE")
                {
                    m_lstPos_vt.Add(posItem);
                }
            }

            if (nCmd == ETRADER_OP.BUY_CLOSE || nCmd == ETRADER_OP.SELL_CLOSE)
            {//For close order
                closeOrder(sSymbol, nCmd, dLots, dPrice, nOrderType, sLogicID, sComment, dLots_exc, dPrice_exc, dtTime_exc);
            }

            EFILLED_STATE nRet = EFILLED_STATE.PARTIAL;

            if (Math.Abs(dLots - dLots_exc) < CFATCommon.ESP)
            {
                nRet = EFILLED_STATE.FULL;
            }
            if (Math.Abs(dLots_exc) < CFATCommon.ESP)
            {
                nRet = EFILLED_STATE.FAIL;
            }
            return(nRet);// updateRealPositions();
        }
Example #3
0
        private void calcSHTrailingStop()
        {
            //m_dTrailingStop_sh
            if (m_products[ID_SHANGHAI].getPosCount_vt() == 0)
            {
                return;
            }
            ETRADER_OP nCmd = m_products[ID_SHANGHAI].getPosCmd_vt(0);

            if (m_products[ID_SHANGHAI].getPosProfit_vt(0) < 500)
            {
                return;
            }

            double dSL = 0;

            if (nCmd == ETRADER_OP.BUY)
            {
                dSL = m_dBid_Shanghai - 0.5;
                if (m_dTrailingStop_sh < dSL)
                {
                    m_dTrailingStop_sh = dSL;
                }
            }
            if (nCmd == ETRADER_OP.SELL)
            {
                dSL = m_dAsk_Shanghai + 0.5;
                if (m_dTrailingStop_sh > dSL)
                {
                    m_dTrailingStop_sh = dSL;
                }
            }
        }
Example #4
0
        public void setData(string sData)
        {
            string[] sVals = sData.Split(',');

            m_dtOpenTime_req  = Convert.ToDateTime(sVals[0]);
            m_dtOpenTime_exc  = Convert.ToDateTime(sVals[1]);
            m_dtCloseTime_req = Convert.ToDateTime(sVals[2]);
            m_dtCloseTime_exc = Convert.ToDateTime(sVals[3]);

            m_sSymbol   = sVals[4];
            m_dLots_req = Convert.ToDouble(sVals[5]);
            m_dLots_exc = Convert.ToDouble(sVals[6]);

            m_nCmd = TRADER.string2Cmd(sVals[7]);

            m_dOpenPrice_req = Convert.ToDouble(sVals[8]);
            m_dOpenPrice_exc = Convert.ToDouble(sVals[9]);

            m_dClosePrice_req = Convert.ToDouble(sVals[10]);
            m_dClosePrice_exc = Convert.ToDouble(sVals[11]);

            m_dProfit_real = Convert.ToDouble(sVals[12]);
            m_sLogicID     = sVals[13];
            m_sComment     = sVals[14];
        }
Example #5
0
        public bool reqCloseAll(string sComment = "")
        {
            int        nPosCnt = m_site.getPosCount_vt(m_sSymbol, m_sLogicID);
            ETRADER_OP nCmd    = ETRADER_OP.NONE;
            double     dPrice  = 0;
            double     dLots   = 0;

            for (int i = 0; i < nPosCnt; i++)
            {
                nCmd = m_site.getPosCmd_vt(m_sSymbol, i, m_sLogicID);
                if (nCmd == ETRADER_OP.BUY)
                {
                    nCmd = ETRADER_OP.BUY_CLOSE;
                }
                if (nCmd == ETRADER_OP.SELL)
                {
                    nCmd = ETRADER_OP.SELL_CLOSE;
                }

                dPrice = m_site.getPosClosePrice_req(m_sSymbol, i, m_sLogicID);
                dLots  = m_site.getPosLots_exc(m_sSymbol, i, m_sLogicID);
                m_site.reqOrder(m_sSymbol, nCmd, ref dLots, ref dPrice, EORDER_TYPE.MARKET, m_sLogicID, sComment);
            }

            return(true);
        }
Example #6
0
        // ---

        public int reqOrder_async(ETRADER_OP nCmd, double dLots, double dPrice, EORDER_TYPE nOrderType, int nPriority, string sComment = "")
        {
            m_reqOrder_async.m_bProcessed = false;

            m_reqOrder_async.setProduct(m_sSymbol, m_site, m_sLogicID, m_dContractSize);
            m_reqOrder_async.setVal(nCmd, dLots, dPrice, nOrderType, nPriority, m_sLogicID, sComment);

            return(CSiteMng.registerOrder(m_reqOrder_async));
        }
Example #7
0
        private void checkForClose()
        {
            if (m_product_diff.getPosCount_vt() == 0)
            {
                return;
            }

            //CheckForClose by Signal line
            int nSignal = getSignal();
            int nSig_cc = m_indCC.getSignal(ex_dOpen_abs, ex_dClose_abs);

            ETRADER_OP nCmd = m_product_diff.getPosCmd_vt(0);

            if (nCmd == ETRADER_OP.BUY && TRADER.isContain(nSignal, (int)ETRADER_OP.BUY_CLOSE) &&
                TRADER.isContain(nSig_cc, (int)ETRADER_OP.BUY_CLOSE))
            {
                requestOrder(ETRADER_OP.BUY_CLOSE);
                return;
            }

            if (nCmd == ETRADER_OP.SELL && TRADER.isContain(nSignal, (int)ETRADER_OP.SELL_CLOSE) &&
                TRADER.isContain(nSig_cc, (int)ETRADER_OP.SELL_CLOSE))
            {
                requestOrder(ETRADER_OP.SELL_CLOSE);
                return;
            }


            //CheckForClose by Absolution profit
            if (m_products[0].getPosProfit_vt(0) + m_products[1].getPosProfit_vt(0) * m_products[2].m_dMid >= ex_dP_ABS)
            {
                if (nCmd == ETRADER_OP.BUY)
                {
                    requestOrder(ETRADER_OP.BUY_CLOSE);
                }
                if (nCmd == ETRADER_OP.SELL)
                {
                    requestOrder(ETRADER_OP.SELL_CLOSE);
                }
                return;
            }

            /*
             * //CheckForClose by MarketEndTime with minimum profit, 15:00~ 15:30
             * if ( CFATCommon.m_dtCurTime.Hour == 15 )
             * {
             *  if (m_products[0].getPosProfit_vt(0) + m_products[1].getPosProfit_vt(0) * m_products[2].m_dMid >= ex_dP_MIN)
             *  {
             *      if (nCmd == ETRADER_OP.BUY)
             *          requestOrder(ETRADER_OP.BUY_CLOSE);
             *      if (nCmd == ETRADER_OP.SELL)
             *          requestOrder(ETRADER_OP.SELL_CLOSE);
             *      return;
             *  }
             * }
             */
        }
Example #8
0
 public static ETRADER_OP getCloseCmd(ETRADER_OP nCmd)
 {
     if (nCmd == ETRADER_OP.BUY)
     {
         return(ETRADER_OP.BUY_CLOSE);
     }
     if (nCmd == ETRADER_OP.SELL)
     {
         return(ETRADER_OP.SELL_CLOSE);
     }
     return(nCmd);
 }
Example #9
0
        public void requestOrder(ETRADER_OP nCmd, double dLots_req, bool bReqAsync = false)
        {
            double dReqPrice_A = 0;
            double dReqPrice_B = 0;
            double dReqPrice_C = 0;
            double dLots       = dLots_req;

//HSM???
//             if (nCmd == ETRADER_OP.BUY_CLOSE || nCmd == ETRADER_OP.SELL_CLOSE)
//                 dLots = Math.Abs(m_product_A.getPosLots_Total_real()); //Some times there is different virtual & real lots

            if (nCmd == ETRADER_OP.BUY || nCmd == ETRADER_OP.SELL_CLOSE)
            {
                dReqPrice_A = m_product_A.m_dAsk;
                dReqPrice_B = m_product_B.m_dBid;
                if (m_nType_trade == EPRODUCT_TYPE_TRADE.A_BC)
                {
                    dReqPrice_C = m_product_C.m_dBid;
                }
            }

            if (nCmd == ETRADER_OP.SELL || nCmd == ETRADER_OP.BUY_CLOSE)
            {
                dReqPrice_A = m_product_A.m_dBid;
                dReqPrice_B = m_product_B.m_dAsk;
                if (m_nType_trade == EPRODUCT_TYPE_TRADE.A_BC)
                {
                    dReqPrice_C = m_product_C.m_dAsk;
                }
            }

            if (!bReqAsync)
            {
                m_product_A.reqOrder(nCmd, ref dLots, ref dReqPrice_A, EORDER_TYPE.MARKET);//First Order
                m_product_B.reqOrder(TRADER.cmdOpposite(nCmd), ref dLots, ref dReqPrice_B, EORDER_TYPE.MARKET);

                if (m_nType_trade == EPRODUCT_TYPE_TRADE.A_BC)
                {
                    m_product_C.reqOrder(TRADER.cmdOpposite(nCmd), ref dLots, ref dReqPrice_C, EORDER_TYPE.MARKET);
                }
            }
            else
            {
                m_product_A.reqOrder_async(nCmd, dLots, dReqPrice_A, EORDER_TYPE.PENDING_STOP, 0);//First Order
                m_product_B.reqOrder_async(TRADER.cmdOpposite(nCmd), dLots * 0.32, dReqPrice_B, EORDER_TYPE.MARKET, 1);

                if (m_nType_trade == EPRODUCT_TYPE_TRADE.A_BC)
                {
                    m_product_C.reqOrder_async(TRADER.cmdOpposite(nCmd), dLots * 0.46, dReqPrice_C, EORDER_TYPE.MARKET, 2);
                }
            }
        }
Example #10
0
        private bool isValidCloseCommand(ETRADER_OP posCmd, ETRADER_OP reqCmd)
        {
            if (reqCmd == ETRADER_OP.BUY_CLOSE && posCmd == ETRADER_OP.BUY)
            {
                return(true);
            }

            if (reqCmd == ETRADER_OP.SELL_CLOSE && posCmd == ETRADER_OP.SELL)
            {
                return(true);
            }
            return(false);
        }
Example #11
0
        private void checkForClose()
        {
            // added by cmh
            if (m_bIsFirstThreadLive || m_bIsSecondThreadLive)
            {
                return;
            }

            if (m_products[_FIRST].m_site.getStatus() == Site.CSite.SITE_STATUS.PROCESSING || m_products[_SECOND].m_site.getStatus() == Site.CSite.SITE_STATUS.PROCESSING)
            {
                return;
            }
            // ---
            if (m_products[_FIRST].getPosCount_vt() == 0)
            {
                return;
            }

            if ((DateTime.Now - m_dtLastFilledTime).TotalSeconds < ex_nPosHoldTimeSec)
            {
                return;
            }


            int        nSignal = getSignal();
            ETRADER_OP nCmd    = m_products[_FIRST].getPosCmd_vt(0);

            double dLots = ex_dLots;

            if (nCmd == ETRADER_OP.BUY && TRADER.isContain(nSignal, (int)ETRADER_OP.BUY_CLOSE))
            {
                if (CFATManager.isOnlineMode())
                {
                    CFATLogger.output_proc("close buy !");
                }
                requestOrder(ETRADER_OP.BUY_CLOSE);
                check3Times_loss();
                return;
            }

            if (nCmd == ETRADER_OP.SELL && TRADER.isContain(nSignal, (int)ETRADER_OP.SELL_CLOSE))
            {
                if (CFATManager.isOnlineMode())
                {
                    CFATLogger.output_proc("close sell !");
                }
                requestOrder(ETRADER_OP.SELL_CLOSE);
                check3Times_loss();
                return;
            }
        }
Example #12
0
        public void setVal(ETRADER_OP nCmd, double dLots_req, double dPrice_req, EORDER_TYPE nOrderType,
                           int nPriority, string sLogicID, string sComment)
        {
            m_nCmd       = nCmd;
            m_dLots_req  = dLots_req;
            m_dPrice_req = dPrice_req;
            m_nOrderType = nOrderType;
            m_nPriority  = nPriority;
            m_sLogicID   = sLogicID;
            m_sComment   = sComment;

            m_dLots_exc    = 0;
            m_nOrderResult = EFILLED_STATE.FAIL;
            m_dPrice_exc   = 0;
        }
Example #13
0
        public override EFILLED_STATE reqOrder(string sSymbol, ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "",
                                               double dLots_exc = 0, double dPrice_exc = 0, DateTime dtTime_exc = default(DateTime))
        {
            EFILLED_STATE nRet      = EFILLED_STATE.FULL;
            ETRADER_OP    nCmd_req  = nCmd;
            double        dLots_req = dLots;

            available_lots_cmd(sSymbol, ref nCmd_req, ref dLots_req);

            if (Math.Abs(dLots_req - dLots) > CFATCommon.ESP)
            {
                nRet  = EFILLED_STATE.PARTIAL;
                dLots = dLots_req;
            }

            base.reqOrder(sSymbol, nCmd_req, ref dLots_req, ref dPrice, nOrderType, sLogicID, sComment, dLots_req, dPrice, CFATCommon.m_dtCurTime);
            return(nRet);
        }
Example #14
0
        public override EFILLED_STATE reqOrder(string sSymbol, ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "",
                                               double dLots_exc = 0, double dPrice_exc = 0, DateTime dtTime_exc = default(DateTime))
        {
            if (CFATManager.m_nRunMode == ERUN_MODE.SIMULATION)
            {
                return(base.reqOrder(sSymbol, nCmd, ref dLots, ref dPrice, nOrderType, sLogicID, sComment, dLots, dPrice, DateTime.Now));
            }
            //Send Request newOrder or Close or
            //MessageBox.Show("CSiteFixMt4::reqOrder !");
            string sOrderID   = "FAT" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
            double dAmount    = 0;
            string sFixSymbol = sSymbol;

            if (sFixSymbol.Contains(".agg"))//HSM_TestCode!!!
            {
                sFixSymbol = sSymbol.Insert(3, "/");
                sFixSymbol = sFixSymbol.Replace(".agg", "");
            }
            string sCmd = "";

            if (nCmd == ETRADER_OP.BUY || nCmd == ETRADER_OP.SELL_CLOSE)
            {
                sCmd = "BUY";
            }
            if (nCmd == ETRADER_OP.SELL || nCmd == ETRADER_OP.BUY_CLOSE)
            {
                sCmd = "SELL";
            }

            dAmount = dLots * getContractSize(sSymbol);

            //MessageBox.Show("Before QueryEnterOrder!");
            CFATLogger.output_proc(string.Format("Fix req : sym={0},price={1}, amount={2}, order id= {3}, acc = {4}, cmd = {5}", sFixSymbol, dPrice, dAmount, sOrderID, m_sFixAcc, sCmd));
            double dPrice_req = dPrice;
            double dLots_req  = dLots;
            int    nRet       = m_fixApi_trade.QueryEnterOrder(sFixSymbol, sCmd, ref dPrice, ref dAmount, sOrderID, m_sFixAcc);

            dLots = dAmount / getContractSize(sSymbol);
            CFATLogger.output_proc(string.Format("Fix response : result = {0}, sym={1},price={2}, amount={3}, ", nRet, sFixSymbol, dPrice, dAmount));
            //if ( nRet == TORDER_RESULT.FILLED) //HSM_???
            return(base.reqOrder(sSymbol, nCmd, ref dLots_req, ref dPrice_req, nOrderType, sLogicID, sComment, dLots, dPrice, DateTime.Now));
            //return false;
        }
Example #15
0
        private bool closeOrder(string sSymbol, ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "")
        {
            CFATLogger.output_proc(string.Format("close order : site = {0}, sym= {1}, cmd = {2}, lots = {3}", m_sSiteName, sSymbol, nCmd, dLots));
            double   dRemainLots = dLots;
            TPosItem posItem;
            bool     bRet = true;

            for (int i = 0; i < m_lstPos_real.Count; i++)
            {
                posItem = m_lstPos_real[i];

                if (posItem.m_sSymbol != sSymbol)
                {
                    continue;
                }
                //if (posItem.m_nLogicID != nLogicID) continue;

                if (!isValidCloseCommand(posItem.m_nCmd, nCmd))
                {
                    continue;
                }

                if (dRemainLots >= posItem.m_dLots_exc)
                {
                    CFATLogger.output_proc(string.Format("close item : ticket = {0}, lots = {1}", posItem.m_nTicket, posItem.m_dLots_exc));
                    bRet         = m_mt4ApiDLL.mt4_reqCloseOrder(posItem.m_nTicket, ref posItem.m_dLots_exc, ref dPrice);
                    dRemainLots -= posItem.m_dLots_exc;
                }
                else
                {
                    CFATLogger.output_proc(string.Format("close item : ticket = {0}, lots = {1}", posItem.m_nTicket, dRemainLots));
                    bRet        = m_mt4ApiDLL.mt4_reqCloseOrder(posItem.m_nTicket, ref dRemainLots, ref dPrice);
                    dRemainLots = 0;
                }

                if (Math.Abs(dRemainLots) < CFATCommon.ESP)
                {
                    break;
                }
            }

            return(true);
        }
Example #16
0
        private void checkForClose()
        {
            if (m_product_diff.getPosCount_vt() == 0)
            {
                return;
            }

            int        nSignal = getSignal();
            ETRADER_OP nCmd    = m_product_diff.getPosCmd_vt(0);

            if (nCmd == ETRADER_OP.BUY && TRADER.isContain(nSignal, (int)ETRADER_OP.BUY_CLOSE))
            {
                requestOrder(ETRADER_OP.BUY_CLOSE);
            }

            if (nCmd == ETRADER_OP.SELL && TRADER.isContain(nSignal, (int)ETRADER_OP.SELL_CLOSE))
            {
                requestOrder(ETRADER_OP.SELL_CLOSE);
            }
        }
Example #17
0
 public static string cmd2String(ETRADER_OP nCmd)
 {
     if (nCmd == ETRADER_OP.BUY)
     {
         return("BUY");
     }
     if (nCmd == ETRADER_OP.SELL)
     {
         return("SELL");
     }
     if (nCmd == ETRADER_OP.BUY_CLOSE)
     {
         return("BUYCLOSE");
     }
     if (nCmd == ETRADER_OP.SELL_CLOSE)
     {
         return("SELLCLOSE");
     }
     return("NONE");
 }
Example #18
0
        private bool isHedgePosition(string sSymbol, double dLots, ETRADER_OP nCmd)
        {
            foreach (TPosItem posItem in m_lstPos_real)
            {
                if (posItem.m_sSymbol != sSymbol)
                {
                    continue;
                }
                if (Math.Abs(posItem.m_dLots_exc - dLots) > CFATCommon.ESP)
                {
                    continue;
                }

                if (posItem.m_nCmd != nCmd)
                {//Hedge position
                    m_lstPos_real.Remove(posItem);
                    return(true);
                }
            }
            return(false);
        }
Example #19
0
        public override EFILLED_STATE reqOrder(string sSymbol, ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "",
                                               double dLots_exc = 0, double dPrice_exc = 0, DateTime dtTime_exc = default(DateTime))
        {
            if (CFATManager.m_nRunMode == ERUN_MODE.SIMULATION)
            {
                return(base.reqOrder(sSymbol, nCmd, ref dLots, ref dPrice, nOrderType, sLogicID, sComment, dLots, dPrice, DateTime.Now));
            }

            string sCmd       = TRADER.cmd2String(nCmd);
            double dLots_req  = dLots;
            double dPrice_req = dPrice;

            bool bRet = CSHGoldAPI.sg_newOrder(sSymbol, sCmd, ref dPrice, ref dLots);

            if (!bRet)
            {
                return(EFILLED_STATE.FAIL);
            }

            return(base.reqOrder(sSymbol, nCmd, ref dLots_req, ref dPrice_req, nOrderType, sLogicID, sComment, dLots, dPrice, DateTime.Now));
        }
Example #20
0
        public static ETRADER_OP cmdOpposite(ETRADER_OP nCmd)
        {
            if (nCmd == ETRADER_OP.BUY)
            {
                return(ETRADER_OP.SELL);
            }
            if (nCmd == ETRADER_OP.SELL)
            {
                return(ETRADER_OP.BUY);
            }
            if (nCmd == ETRADER_OP.BUY_CLOSE)
            {
                return(ETRADER_OP.SELL_CLOSE);
            }
            if (nCmd == ETRADER_OP.SELL_CLOSE)
            {
                return(ETRADER_OP.BUY_CLOSE);
            }

            return(ETRADER_OP.NONE);
        }
Example #21
0
        private void calcATRVal()
        {
            if (m_products[ID_SHANGHAI].getPosCount_vt() == 0)
            {
                return;
            }
            ETRADER_OP nCmd = m_products[ID_SHANGHAI].getPosCmd_vt(0);


            for (int i = 0; i < ex_nATRPeriod; i++)
            {
                m_dATR += Math.Abs(m_products[ID_LONDON].getTick(i).dBid - m_products[ID_LONDON].getTick(i + 1).dBid);
            }
            m_dATR = m_dATR / ex_nATRPeriod;

            //m_dATR = m_products[ID_LONDON].getStd_tick(ex_nATRPeriod);
            if (m_dATR < 0.2)
            {
                m_dATR = 0.2;
            }
            double dSL = 0;

            if (nCmd == ETRADER_OP.BUY)
            {
                dSL = m_dMid_London - m_dATR * ex_dATRMultiple;
                if (m_dTrailingStop_ld < dSL)
                {
                    m_dTrailingStop_ld = (m_dTrailingStop_ld + dSL) / 2;
                }
            }

            if (nCmd == ETRADER_OP.SELL)
            {
                dSL = m_dMid_London + m_dATR * ex_dATRMultiple;
                if (m_dTrailingStop_ld > dSL)
                {
                    m_dTrailingStop_ld = (m_dTrailingStop_ld + dSL) / 2;
                }
            }
        }
Example #22
0
        public void clearPositions()
        {
            //Clear Virtual positions
            m_site.clearVirtualPositions(m_sSymbol);

            //Clear Real positions
            double dLots = m_site.getPosLots_Total_real(m_sSymbol);

            if (Math.Abs(dLots) < CFATCommon.ESP)
            {
                return;
            }

            ETRADER_OP nCmd = ETRADER_OP.NONE;

            if (dLots > 0) // Buy
            {
                nCmd = ETRADER_OP.BUY_CLOSE;
            }
            else // Sell
            {
                nCmd = ETRADER_OP.SELL_CLOSE;
            }

            double dReqLots  = Math.Abs(dLots);
            double dReqPrice = 0;

            if (nCmd == ETRADER_OP.BUY_CLOSE) //Sell
            {
                dReqPrice = getTick(0).dBid;
            }

            if (nCmd == ETRADER_OP.SELL_CLOSE) //Buy
            {
                dReqPrice = getTick(0).dAsk;
            }

            reqOrder(nCmd, ref dReqLots, ref dReqPrice, EORDER_TYPE.MARKET);
        }
Example #23
0
        public override EFILLED_STATE reqOrder(string sSymbol, ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "",
                                               double dLots_exc = 0, double dPrice_exc = 0, DateTime dtTime_exc = default(DateTime))
        {
            if (CFATManager.m_nRunMode == ERUN_MODE.SIMULATION)
            {
                return(base.reqOrder(sSymbol, nCmd, ref dLots, ref dPrice, nOrderType, sLogicID, sComment, dLots, dPrice, DateTime.Now));
            }

            double dAmount = 0;

            string sCmd = "";

            if (nCmd == ETRADER_OP.BUY || nCmd == ETRADER_OP.SELL_CLOSE)
            {
                sCmd = "BUY";
            }
            if (nCmd == ETRADER_OP.SELL || nCmd == ETRADER_OP.BUY_CLOSE)
            {
                sCmd = "SELL";
            }

            dAmount = dLots * getContractSize(sSymbol);

            CFATLogger.output_proc(string.Format("IB req : sym={0},price={1}, amount={2}, cmd = {3}", sSymbol, dPrice, dAmount, sCmd));
            double dPrice_req = dPrice;
            double dLots_req  = dLots;
            bool   bRet       = apiIB.reqOrder(sSymbol, sCmd, ref dAmount, ref dPrice);

            dLots = dAmount / getContractSize(sSymbol);
            CFATLogger.output_proc(string.Format("IB response : result = {0}, sym={1},price={2}, amount={3}, ", bRet, sSymbol, dPrice, dAmount));
            //if ( nRet == TORDER_RESULT.FILLED) //HSM_???
            if (!bRet)
            {
                return(EFILLED_STATE.FAIL);
            }
            return(base.reqOrder(sSymbol, nCmd, ref dLots_req, ref dPrice_req, nOrderType, sLogicID, sComment, dLots, dPrice, DateTime.Now));
            //return false;
        }
Example #24
0
        private void checkForClose()
        {
            if (m_products[ID_SHANGHAI].getPosCount_vt() == 0)
            {
                return;
            }

            ETRADER_OP nCmd      = m_products[ID_SHANGHAI].getPosCmd_vt(0);
            ETRADER_OP nCloseCmd = TRADER.getCloseCmd(nCmd);

            double dProfit = m_products[ID_SHANGHAI].getPosProfit_vt(0);

            setMinMax_forLog();

            if (isMarketCloseTime_forClose())
            {
                //m_products[ID_SHANGHAI].reqCloseAll("Market Time Close");
                requestOrder(nCloseCmd, getMinMaxStr_forLog());
                return;
            }


            int nSignal = getSignal();

            if (nCmd == ETRADER_OP.BUY && TRADER.isContain(nSignal, (int)ETRADER_OP.BUY_CLOSE))
            {
                //m_products[ID_SHANGHAI].reqCloseAll();
                requestOrder(ETRADER_OP.BUY_CLOSE, getMinMaxStr_forLog());
                return;
            }

            if (nCmd == ETRADER_OP.SELL && TRADER.isContain(nSignal, (int)ETRADER_OP.SELL_CLOSE))
            {
                //m_products[ID_SHANGHAI].reqCloseAll();
                requestOrder(ETRADER_OP.SELL_CLOSE, getMinMaxStr_forLog());
                return;
            }
        }
Example #25
0
        public void requestOrder(ETRADER_OP nCmd)
        {
            if (nCmd == ETRADER_OP.BUY || nCmd == ETRADER_OP.SELL)
            {
                setParam_newOrder(ex_nIsNewOrder - 1);
            }

            if (CFATManager.isOnlineMode())
            {
                CFATLogger.output_proc(string.Format("Order : {0}, diff = {1}", nCmd.ToString(), m_product_diff.m_dMid));
            }

            m_product_diff.requestOrder(nCmd, ex_dLots, true);
            if (CFATManager.isOnlineMode())
            {
                setState(ELOGIC_STATE.WAITING_ORDER_RESPONSE);
            }
            else
            {
                setState(ELOGIC_STATE.NORMAL);
            }
            //setState(ELOGIC_STATE.NEED_CHECK_POSITION_MATCH);
        }
Example #26
0
        private void calcSARVal()
        {
            if (m_products[ID_SHANGHAI].getPosCount_vt() == 0)
            {
                return;
            }
            ETRADER_OP nCmd = m_products[ID_SHANGHAI].getPosCmd_vt(0);

            if (nCmd == ETRADER_OP.BUY)
            {
                if (m_dMaxVal + ex_dLDStepForSAR < m_dMid_London)
                {
                    m_dMaxVal = m_dMid_London;
                    m_dAlpha += ex_dSARStep;
                    if (m_dAlpha > ex_dSARMax)
                    {
                        m_dAlpha = ex_dSARMax;
                    }
                    m_dTrailingStop_ld = m_dTrailingStop_ld + m_dAlpha * (m_dMaxVal - m_dTrailingStop_ld);
                }
            }

            if (nCmd == ETRADER_OP.SELL)
            {
                if (m_dMaxVal - ex_dLDStepForSAR > m_dMid_London)
                {
                    m_dMaxVal = m_dMid_London;
                    m_dAlpha += ex_dSARStep;
                    if (m_dAlpha > ex_dSARMax)
                    {
                        m_dAlpha = ex_dSARMax;
                    }
                    m_dTrailingStop_ld = m_dTrailingStop_ld + m_dAlpha * (m_dMaxVal - m_dTrailingStop_ld);
                }
            }
            m_dTrailingStop_ld = m_dTrailingStop_ld + (ex_dAlphaSAR) * (m_dMaxVal - m_dTrailingStop_ld);
        }
Example #27
0
        private void closeOrder(string sSymbol, ETRADER_OP nCmd, double dLots, double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "",
                                double dLots_exc = 0, double dPrice_exc = 0, DateTime dtTime_exc = default(DateTime))
        {
            if (sLogicID == "REQ_MERGE")
            {
                return;
            }
            double   dRemainLots = dLots_exc;
            TPosItem posItem;

            for (int i = 0; i < m_lstPos_vt.Count; i++)
            {
                posItem = m_lstPos_vt[i];

                if (posItem.m_sSymbol != sSymbol)
                {
                    continue;
                }
                if (posItem.m_sLogicID != sLogicID)
                {
                    continue;
                }

                if (!isValidCloseCommand(posItem.m_nCmd, nCmd))
                {
                    continue;
                }

                if (dRemainLots >= posItem.m_dLots_exc - CFATCommon.ESP)
                {
                    m_lstPos_vt.Remove(posItem);
                    i--;
                    posItem.m_sComment       += "@";
                    posItem.m_sComment       += sComment;
                    posItem.m_dClosePrice_exc = dPrice_exc;
                    posItem.m_dtCloseTime_req = m_rates[sSymbol].m_dtTime;
                    posItem.m_dtCloseTime_exc = dtTime_exc;
                    posItem.calcProfit();
                    addToHistoryItem(posItem);
                    dRemainLots -= posItem.m_dLots_exc;
                }
                else
                {
                    posItem.m_dLots_exc -= dRemainLots;
                    posItem.calcProfit();
                    m_lstPos_vt[i] = posItem;

                    posItem.m_dLots_exc       = dRemainLots;
                    posItem.m_sComment       += "@";
                    posItem.m_sComment       += sComment;
                    posItem.m_dClosePrice_exc = dPrice_exc;
                    posItem.m_dtCloseTime_req = m_rates[sSymbol].m_dtTime;
                    posItem.m_dtCloseTime_exc = dtTime_exc;
                    posItem.calcProfit();
                    addToHistoryItem(posItem);
                    dRemainLots = 0;
                }

                if (Math.Abs(dRemainLots) < CFATCommon.ESP)
                {
                    break;
                }
            }
        }
Example #28
0
 // modified by cmh
 public EFILLED_STATE reqOrder_withoutEXC(ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "")
 {
     return(m_site.reqOrder_withoutEXC(m_sSymbol, nCmd, ref dLots, ref dPrice, nOrderType, sLogicID, sComment));
 }
Example #29
0
        public void available_lots_cmd(string sSymbol, ref ETRADER_OP nCmd, ref double dLots)
        {
            double dLots_real = 0;

            if (CFATManager.m_nRunMode == ERUN_MODE.REALTIME)
            {
                dLots_real = getPosLots_Total_real(sSymbol);
            }
            else
            {
                dLots_real = getPosLots_Total_vt(sSymbol);
            }

            //Case 1 :
            if (Math.Abs(dLots_real) < CFATCommon.ESP)
            {
                if (nCmd == ETRADER_OP.BUY_CLOSE)
                {
                    nCmd = ETRADER_OP.SELL;
                }
                if (nCmd == ETRADER_OP.SELL_CLOSE)
                {
                    nCmd = ETRADER_OP.BUY;
                }
                return;
            }

            ETRADER_OP nCmd_real = ETRADER_OP.NONE;

            dLots_real = Math.Abs(dLots_real);

            if (dLots_real > CFATCommon.ESP)
            {
                nCmd_real = ETRADER_OP.BUY;
            }
            else
            {
                nCmd_real = ETRADER_OP.SELL;
            }

            //Case 2 :
            if (nCmd_real == nCmd)
            {
                return;
            }

            //Case 3 :
            if (nCmd_real == ETRADER_OP.BUY)
            {
                if (nCmd == ETRADER_OP.SELL)
                {//Case 3-1 :
                    nCmd = ETRADER_OP.BUY_CLOSE;
                    if (dLots_real < dLots - CFATCommon.ESP)
                    {
                        dLots = dLots_real;
                    }
                    return;
                }

                if (nCmd == ETRADER_OP.BUY_CLOSE)
                {//Case 3-2 :
                    if (dLots_real < dLots - CFATCommon.ESP)
                    {
                        dLots = dLots_real;
                    }
                    return;
                }

                if (nCmd == ETRADER_OP.SELL_CLOSE)
                {//Case 3-3 :
                    return;
                }
            }

            //Case 4 :
            if (nCmd_real == ETRADER_OP.SELL)
            {
                if (nCmd == ETRADER_OP.BUY)
                {//Case 4-1 :
                    nCmd = ETRADER_OP.SELL_CLOSE;
                    if (dLots_real < dLots - CFATCommon.ESP)
                    {
                        dLots = dLots_real;
                    }
                    return;
                }

                if (nCmd == ETRADER_OP.SELL_CLOSE)
                {//Case 4-2 :
                    if (dLots_real < dLots - CFATCommon.ESP)
                    {
                        dLots = dLots_real;
                    }
                    return;
                }

                if (nCmd == ETRADER_OP.BUY_CLOSE)
                {//Case 4-3 :
                    return;
                }
            }
        }
Example #30
0
 public virtual EFILLED_STATE reqOrder(string sSymbol, ETRADER_OP nCmd, ref double dLots, ref double dPrice, EORDER_TYPE nOrderType, string sLogicID, string sComment = "",
                                       double dLots_exc = 0, double dPrice_exc = 0, DateTime dtTime_exc = default(DateTime))
 {
     return(register2Vt(sSymbol, nCmd, ref dLots, ref dPrice, nOrderType, sLogicID, sComment, dLots_exc, dPrice_exc, dtTime_exc));
 }