private void loadData() { if (m_sName == "NO_NAME") { return; } if (!CFATManager.isOnlineMode()) { return; } string sFile = getStoreFileName(); if (!File.Exists(sFile)) { CFATLogger.output_proc("File don't exist: " + sFile); return; } var xs = new XmlSerializer(typeof(CCacheData)); using (var sr = new StreamReader(sFile)) { var tempObject = (CCacheData)xs.Deserialize(sr); m_tickData = tempObject.m_tickData; m_minData = tempObject.m_minData; m_renkoData = tempObject.m_renkoData; m_nCurPos_tick = tempObject.m_nCurPos_tick; m_nCurPos_min = tempObject.m_nCurPos_min; m_nCurPos_renko = tempObject.m_nCurPos_renko; } CFATLogger.output_proc("File loaded : " + sFile); }
private void publishToMQ() { if (!CFATManager.isOnlineMode()) { return; } if ((DateTime.Now - m_dtLastPublish).TotalSeconds < 2) //every 2 seconds publish message { return; } try { //parameters : publishParams(); //variables : logic_id@name1,val1@name2,val2@name3,val3..... // string sTxt = string.Format("{0}@isNeedCheckPosition,{1}@sh_ask,{2}@sh_bid,{3}@ld_ask,{4}@ld_bid,{5}@g_dOpen_London,{6}@m_dExpectTp,{7}@posCnt,{8}@real lots,{9}@ex_nIsNewOrder,{10}@lastPosType,{11}@lastPosTime,{12}@m_nOpen_Ticks,{13}", // m_sLogicID, // m_bIsNeedCheckPosition, // m_dAsk_Shanghai, m_dBid_Shanghai, // m_dAsk_London, m_dBid_London, // g_dOpen_London, m_dExpectTp, // m_products[ID_SHANGHAI].getPosCount(), m_products[ID_SHANGHAI].getPosLots_real(), // ex_nIsNewOrder, lastPosType, lastPosTime, m_nOpen_Ticks); // // CMQClient.publish_msg(sTxt, CFATCommon.MQ_TOPIC_VARS); m_dtLastPublish = DateTime.Now; } catch { CFATLogger.output_proc("CLogic_gold_latencyV2 : publishToMQ error!"); } }
public override bool OnInit() { CFATLogger.output_proc("Price_record init :"); loadParams(); CFATLogger.output_proc("Price_record param done! :"); return(base.OnInit()); }
private bool isPositionMatch() { if (CFATManager.m_nRunMode != ERUN_MODE.REALTIME) { return(true); } foreach (CProduct product in m_products) { if (!product.isPositionMatch()) { return(false); } } // If there is different virtual lots with first site and second site. //Here is problem when using multi position with ex_dAddLots !!!!!!!!!! if (Math.Abs(m_products[_FIRST].getPosLots_Total_vt() + m_products[_SECOND].getPosLots_Total_vt()) > ex_dAddLots + CFATCommon.ESP) { return(false); } // IF there is different real lots with first site and second site if (Math.Abs(m_products[_FIRST].getPosLots_Total_real() + m_products[_SECOND].getPosLots_Total_real()) > ex_dAddLots + CFATCommon.ESP) { return(false); } CFATLogger.output_proc(string.Format("Position matched! sym1={0},real lots = {1}, sym2={2}, real lots = {3}", m_products[_FIRST].getSymbol(), m_products[_FIRST].getPosLots_Total_real(), m_products[_SECOND].getSymbol(), m_products[_SECOND].getPosLots_Total_real())); return(true); }
private void check3Times_loss() { if (!CFATManager.isOnlineMode()) { return; } m_dLastProfit = m_tradeHistory.getProfit(0); if (ex_nIsNewOrder <= 0) { return; } if (m_tradeHistory.getTradeCount() < 3) { return; } double dProfit = 0; for (int i = 0; i < 3; i++) { dProfit = m_tradeHistory.getProfit(i); if (dProfit > 0) { return; } } CFATLogger.output_proc(m_sLogicID + " : 3 times loss, ex_nIsNewOrder will be 0."); setParam_newOrder(0);// continuously 3 times loss, stop trading. }
private void saveData() { if (m_sName == "NO_NAME") { return; } if (!CFATManager.isOnlineMode()) { return; } string sFile = getStoreFileName(); if (File.Exists(sFile)) { File.Delete(sFile); } var xs = new XmlSerializer(typeof(CCacheData)); using (TextWriter sw = new StreamWriter(sFile)) { xs.Serialize(sw, this); } CFATLogger.output_proc("File saved : " + sFile); m_dtLastSavedTime = DateTime.Now; }
// added by cmh public void requestFirstSiteOrder() { double dReqPrice_first = 0; double dLots = ex_dLots; if (ex_nUseRandomLots == 1) { dLots = getRandomLots(dLots); } dLots += ex_dAddLots; int nProfit_calc_01 = 0; EFILLED_STATE nRet; if ((m_nCmd4Thread == ETRADER_OP.BUY || m_nCmd4Thread == ETRADER_OP.SELL) && m_products[_FIRST].getPosCount_vt() == 0) { setParam_newOrder(ex_nIsNewOrder - 1); } if (m_nCmd4Thread == ETRADER_OP.BUY_CLOSE || m_nCmd4Thread == ETRADER_OP.SELL_CLOSE) { dLots = Math.Abs(m_products[_FIRST].getPosLots_vt(0)); } if (dLots < CFATCommon.ESP) { CFATLogger.output_proc("*** Invalid Lots !!!, pls check virtual position!!!!"); dLots = ex_dLots; } if (m_nCmd4Thread == ETRADER_OP.BUY || m_nCmd4Thread == ETRADER_OP.SELL_CLOSE) { dReqPrice_first = m_products[_FIRST].m_dAsk; nProfit_calc_01 = -1; } if (m_nCmd4Thread == ETRADER_OP.SELL || m_nCmd4Thread == ETRADER_OP.BUY_CLOSE) { dReqPrice_first = m_products[_FIRST].m_dBid; nProfit_calc_01 = 1; } nRet = m_products[_FIRST].reqOrder(m_nCmd4Thread, ref dLots, ref dReqPrice_first, EORDER_TYPE.MARKET);//First Order if (nRet == EFILLED_STATE.FULL) { m_tradeHistory.pushHistory(dReqPrice_first * nProfit_calc_01); dLots = getSecondLots(dLots); } else { CFATLogger.output_proc("first order failed!"); } // setState(ELOGIC_STATE.NEED_CHECK_POSITION_MATCH); m_dtLastFilledTime = DateTime.Now; m_bIsFirstThreadLive = false; m_products[_FIRST].m_site.setStatus(Site.CSite.SITE_STATUS.NONE); }
public virtual void OnDeInit() { CFATLogger.output_proc(string.Format("Site DeInit : {0} <------", m_sSiteName)); foreach (KeyValuePair <string, CCacheData> entry in m_rates_cache) { entry.Value.OnDeInit(); } }
private void matchPosReal2Virtual() { m_products[_FIRST].updateRealPositions(); m_products[_SECOND].updateRealPositions(); CFATLogger.output_proc(string.Format("matchPosReal2Virtual : {0}, {1}", m_products[_FIRST].getPosLots_Total_real(), m_products[_SECOND].getPosLots_Total_real())); if (Math.Abs(m_products[_FIRST].getPosLots_Total_real() + m_products[_SECOND].getPosLots_Total_real()) < CFATCommon.ESP + ex_dAddLots) { m_products[_FIRST].matchPosReal2Virtual(); m_products[_SECOND].matchPosReal2Virtual(); } }
public void setState(ELOGIC_STATE nState) { m_stState.m_nState = nState; m_stState.m_dtUpdatedTime = DateTime.Now; //HSM??? if (CFATManager.isOnlineMode()) { CFATLogger.output_proc(string.Format("{0},{1} : Change State -> {2}", CFATCommon.m_dtCurTime, m_sLogicID, m_stState.m_nState)); } }
private void process_event() { //Parameter changed if (CEvent_ParamChanged.g_bChanged) { CFATLogger.output_proc("Request : parameter change"); CEvent_ParamChanged.g_bChanged = false; changeLogicParams(); } //---------------- }
private void log_reqOrders() { string sLog = ""; foreach (TReqOrder reqOrderItem in m_lstReqOrders) { sLog = string.Format("{0},{1},cmd={2},reqLots={3}, excLots = {4}, processed = {5}, orderResult = {6}", reqOrderItem.m_sLogicID, reqOrderItem.m_product.m_sSymbol, reqOrderItem.m_nCmd, reqOrderItem.m_dLots_req, reqOrderItem.m_dLots_exc, reqOrderItem.m_bProcessed, reqOrderItem.m_nOrderResult); CFATLogger.output_proc(sLog); } }
public int getSignal_unitTest() { for (int i = 0; i < m_lstUnitTest_signal.Count; i++) { if (m_lstUnitTest_signal[i].m_dtTime == CFATCommon.m_dtCurTime) { CFATLogger.output_proc(string.Format("unitTest signal : {0},{1}", m_sLogicID, m_lstUnitTest_signal[i].m_sCmd)); return((int)TRADER.string2Cmd(m_lstUnitTest_signal[i].m_sCmd)); } } return((int)ETRADER_OP.NONE); }
public void matchPosReal2Virtual(string sSymbol, string sLogicID) { CFATLogger.output_proc(string.Format("matchPosReal2Virtual : {0}, real pos cnt = {1}", m_sSiteName, m_lstPos_real.Count)); foreach (TPosItem posItem in m_lstPos_real) { if (posItem.m_sSymbol == sSymbol)// && Math.Abs(getSymbolLots_real(sSymbol)) > CFATCommon.ESP) { CFATLogger.output_proc(string.Format("Real->Virtual position : posItemSymbol={0}, symbol = {1}, logicID = {2}", posItem.m_sSymbol, sSymbol, sLogicID)); posItem.m_sLogicID = sLogicID; m_lstPos_vt.Add(posItem); } } }
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; } }
public override bool OnInit() { CFATLogger.output_proc("connecting to pipe : " + m_sPipServerName); //MT4 init if (!m_mt4ApiDLL.connectToMT4(m_sPipServerName)) { CFATLogger.output_proc(string.Format("site = {0} : Cannot connect to PIP, pipe name = {1}, please check MT4 and parameters of EA!", m_sSiteName, m_sPipServerName)); return(false); } CFATLogger.output_proc("Connect to MT4 pipe OK! site = " + m_sSiteName); //FIX init string sConfigFile; sConfigFile = Path.Combine(Application.StartupPath, "Config\\Fix", m_sFixConfig_trade); CFATLogger.output_proc("loading : " + sConfigFile); m_fixApi_trade = new CFixAPI(sConfigFile); if (!m_fixApi_trade.fixLogin()) { CFATLogger.output_proc(string.Format("Connect to fix trade server is fail! : site name = {0}", m_sSiteName)); return(false); } sConfigFile = Path.Combine(Application.StartupPath, "Config\\Fix", m_sFixConfig_data); CFATLogger.output_proc("loading : " + sConfigFile); m_fixApi_data = new CFixAPI(sConfigFile); if (!m_fixApi_data.fixLogin()) { CFATLogger.output_proc(string.Format("Connect to fix data server is fail! : site name = {0}", m_sSiteName)); return(false); } CFATLogger.output_proc("Connect to Fix OK!"); //subscrib rates TRateRange rateRange; foreach (KeyValuePair <string, string> entry in m_sSymbols_fix) { rateRange = m_rateRange_fix[entry.Key]; m_fixApi_data.startSubscribe(entry.Value, entry.Key, rateRange.dMin, rateRange.dMax); Thread.Sleep(500); } return(base.OnInit()); }
public override void OnDeInit() { bool bRet = CSHGoldAPI.sg_logout(); if (bRet) { CFATLogger.output_proc("Shanghai gold Logout Success!"); } else { CFATLogger.output_proc("Shanghai gold Logout Fail!"); } base.OnDeInit(); }
public CFATManager(ERUN_MODE nMode = ERUN_MODE.BACKTEST, string sMode = "") { m_configGeneral = new TGeneralInfo(); CConfigMng.load_config_general(ref m_configGeneral); CMQClient.connectToMQ(m_configGeneral.sHost, m_configGeneral.sUser, m_configGeneral.sPwd); CFATLogger.output_proc(string.Format("###### Start Mode = {0} #######", sMode)); CFATManager.m_nRunMode = nMode; m_lstLogics = new List <CLogic>(); m_configLogics = new List <Dictionary <string, string> >(); m_configSites = new List <Dictionary <string, string> >(); m_configWorkTimes = new List <TWorkTimeInterval>(); }
public override bool OnInit() { bool bRet = CSHGoldAPI.sg_login(m_sID, m_sPwd); if (bRet) { CFATLogger.output_proc("Shanghai gold Login Success!"); } else { CFATLogger.output_proc("Shanghai gold Login Fail!"); } return(base.OnInit()); }
public void changeParams() { try { foreach (KeyValuePair <string, string> entry in CEvent_ParamChanged.g_params) { m_params.setVal(entry.Key, entry.Value); } } catch { CFATLogger.output_proc("Error : CLogic::changeParams "); } CFATLogger.output_proc(string.Format("Logic : {0}, parameter changed!", m_sLogicID)); }
public virtual bool OnInit() { CFATLogger.output_proc(string.Format("Logic Init : {0} ------>", m_sLogicName)); string sParams = m_params.getString(); CFATCommon.m_sOpt_param = sParams; setState(ELOGIC_STATE.NORMAL); CFATLogger.output_proc("Params : " + sParams); if (m_sMode == "unit_test") { load_unitTest(); } return(true); }
public bool OnInit() { if (!CSiteMng.OnInit()) { return(false); } CFATLogger.output_proc("Logic init start!"); foreach (CLogic objLogic in m_lstLogics) { if (!objLogic.OnInit()) { return(false); } } CFATLogger.output_proc("Logic init OK!"); return(true); }
public virtual bool OnInit() { CFATLogger.output_proc(string.Format("Site Init : {0} ------>", m_sSiteName)); m_accInfo.init(); m_report.init(); m_lstPos_vt.Clear(); m_lstPos_real.Clear(); m_lstPosHistory_vt.Clear(); m_status = SITE_STATUS.NONE; foreach (KeyValuePair <string, CCacheData> entry in m_rates_cache) { entry.Value.OnInit(); } CFATLogger.output_proc("updateRealPositions : "); return(updateRealPositions()); }
public override bool OnInit() { CFATLogger.output_proc("connecting to money-partners : " + m_sID); if (!m_mpApiDLL.connectToMP(m_sID, m_sPwd)) { CFATLogger.output_proc(string.Format("site = {0} : Cannot connect to money-partners, loginId = {1}, please check connection!", m_sSiteName, m_sID)); return(false); } CFATLogger.output_proc("Connect to MP pipe OK! site = " + m_sSiteName); for (int i = 0; i < m_sSymbols.Count; i++) { m_mpApiDLL.MP_registerSymbol(m_sSymbols[i], m_sID, m_sPwd); } m_mpApiDLL.MP_startSubscribe(); return(base.OnInit()); }
/// <summary> /// load backtest tick rates from file /// </summary> public override void loadRates_Tick() { string sFile = ""; foreach (string sSym in m_sSymbols) { sFile = Path.Combine(Application.StartupPath, "_log\\backtest_rates\\" + m_sSiteName, sSym + ".csv"); CFATLogger.output_proc("loadRates_Tick : ----->" + sFile); var objSR = new StreamReader(sFile); var objCSVR = new CsvReader(objSR); objCSVR.Configuration.HasHeaderRecord = false; List <TRatesTick> objRecords = new List <TRatesTick>(); objRecords = objCSVR.GetRecords <TRatesTick>().ToList(); m_ratesTick.Add(sSym, objRecords); CFATLogger.output_proc("loadRates_Tick : <-----" + sFile); } }
public override bool OnInit() { CFATLogger.output_proc(string.Format("IB Init {0}, {1}, {2}--->", m_sHost, m_nPort, m_nClientID)); if (!apiIB.connectToIB(m_sHost, m_nPort, m_nClientID)) { CFATLogger.output_proc(string.Format("site = {0} : Cannot connect to IB= {1}", m_sSiteName)); return(false); } Thread.Sleep(1000); foreach (string sSymbol in m_sSymbols) { CFATLogger.output_proc("IB subscribe : " + sSymbol); apiIB.subScribeMarketData(sSymbol); } Thread.Sleep(2000); return(base.OnInit()); }
public override bool OnInit() { CFATLogger.output_proc("connecting to pipe : " + m_sPipServerName); if (!m_mt4ApiDLL.connectToMT4(m_sPipServerName)) { CFATLogger.output_proc(string.Format("site = {0} : Cannot connect to PIP, pipe name = {1}, please check MT4 and parameters of EA!", m_sSiteName, m_sPipServerName)); return(false); } CFATLogger.output_proc("Connect to MT4 pipe OK! site = " + m_sSiteName); for (int i = 0; i < m_sSymbols.Count; i++) { m_mt4ApiDLL.mt4_registerSymbol(m_sSymbols[i]); } m_mt4ApiDLL.mt4_startSubscribe(); return(base.OnInit()); }
public override bool OnInit() { m_sBrokerID = "8000"; m_sMDAddr = "tcp://180.169.30.170:41213"; if (!apiCnFt.createMDSpi(m_sBrokerID, m_sMDAddr)) { CFATLogger.output_proc(string.Format("site = {0} : Cannot connect to China Future= {1}", m_sSiteName)); return(false); } foreach (string sSymbol in m_sSymbols) { apiCnFt.addSymbol(sSymbol); } return(base.OnInit()); }
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); }
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; }