private void publish_rates_indicators() { if (CFATManager.m_nRunMode == ERUN_MODE.OPTIMIZE) { return; } if (!ex_bPublishRates) { return; } if (CFATManager.m_nRunMode == ERUN_MODE.BACKTEST) { m_product_diff.publish_min(); m_indCC.publish_min(); m_indBand.publish_min(); return; } if (CFATManager.isOnlineMode()) { m_product_diff.publish_tick(); m_indCC.publish_tick(); return; } }
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!"); } }
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); }
/// <summary> /// Called with every tick /// </summary> public override int OnTick() { if (CFATManager.isOnlineMode()) { m_benchMarking.push_Ontick_start(DateTime.Now); //For bench marking } m_nTradeRet = ETRADE_RET.NONE; updateState(); getRates(); if (m_stState.m_nState == ELOGIC_STATE.NORMAL) { checkForClose(); checkForOpen(); } if (m_bIsOrderHappened && !m_bIsFirstThreadLive && !m_bIsSecondThreadLive) { setState(ELOGIC_STATE.NEED_CHECK_POSITION_MATCH); m_bIsOrderHappened = false; } publishToMQ();//For Manager if (CFATManager.isOnlineMode()) { m_benchMarking.push_Ontick_end(DateTime.Now); //For bench marking } base.OnTick(); return((int)m_nTradeRet); }
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; }
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. }
public override int OnTick() { if (CFATManager.isOnlineMode()) { m_benchMarking.push_Ontick_start(DateTime.Now); //For bench marking } updateState_v2(40, 20, 60); //wait check pos : 40s, wait force close : 20s , restart logic after stop by error : 60s //Here is update price and indicators pattern //First update price, after update indicators m_product_diff.updateRates(); updateIndicators(); //---------------------------------------------- if (m_stState.m_nState == ELOGIC_STATE.NORMAL) { checkForClose(); checkForOpen(); } publishToMQ();//For Manager if (CFATManager.isOnlineMode()) { m_benchMarking.push_Ontick_end(DateTime.Now); //For bench marking } return(base.OnTick()); }
public double getPosLots_Total_real() { if (!CFATManager.isOnlineMode()) { return(m_site.getPosLots_Total_vt(m_sSymbol)); } return(m_site.getPosLots_Total_real(m_sSymbol)); }
public double getPosLots_real(int nIndex) { if (!CFATManager.isOnlineMode()) { return(getPosLots_vt(nIndex)); } return(m_site.getPosLots_real(m_sSymbol, nIndex)); }
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 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 void publishParams() { string sMsg = m_sLogicID; string sName = ""; for (int i = 0; i < m_params.getCount(); i++) { sName = m_params.getName(i); sMsg += "@"; sMsg += sName; sMsg += ","; sMsg += m_params.getVal_string(sName); } //Add System name sMsg += "@"; sMsg += "system_name,"; sMsg += CFATManager.getSystemName(); CMQClient.publish_msg(sMsg, CFATCommon.MQ_TOPIC_PARAM_C2V); }
public virtual EERROR OnTick() { if (!isValidRates()) { if (CFATManager.isOnlineMode()) { CFATLogger.output_proc(string.Format("{0}: Price is 0, Error!", m_sSiteName)); } return(EERROR.RATE_INVALID); } // modified by cmh if (getStatus() != SITE_STATUS.PROCESSING) { updatePosList();//Update close price and calc profit } // --- pushTick_cache(); // push cache data return(EERROR.NONE); }
private void checkUpdateTick() { if (!CFATManager.isOnlineMode()) { return; } bool bIsTickUpdated = isTickUpdated(); if (m_bIsTickUpdated != bIsTickUpdated) { if (!bIsTickUpdated) { CFATLogger.output_proc(string.Format("--------->don't update ticks :logic = {0}, site ={1}", m_sLogicID, m_sErrSite)); } else { CFATLogger.output_proc(string.Format("<---------update ticks :logic= {0}, site = {1}", m_sLogicID, m_sErrSite)); } } m_bIsTickUpdated = bIsTickUpdated; }
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); }
private void publishToMQ() { return;//HSM??? publish_rates_indicators(); if (!CFATManager.isOnlineMode()) { return; } if ((DateTime.Now - m_dtLastPublish).TotalSeconds < 2) //every 2 seconds publish message { return; } try { //parameters : publishParams(); //history publishTradeHistory(); //Variables publishVariables(); if (m_stState.m_nState == ELOGIC_STATE.LOGIC_STOP_BY_ERROR) { CFATLogger.output_proc(string.Format(" **** {0} Logic Stoped!!!", m_sLogicID)); } m_dtLastPublish = DateTime.Now; } catch { CFATLogger.output_proc("CLogic_Arb_org : publishToMQ error!"); } }
public static void config_logicParam_down(EasyNetQ.FluentConfiguration.ISubscriptionConfiguration subConfig) { subConfig.WithTopic(CFATCommon.MQ_TOPIC_PARAM_V2C + ":" + CFATManager.getSystemName()); subConfig.WithExpires(1000 * 60 * 10); subConfig.WithDurable(false); }
private void requestOrder(ETRADER_OP nCmd, string sComment = "") { double dLots = ex_dLots; double dPrice = 0; double dCurPrice = 0; if (nCmd == ETRADER_OP.BUY || nCmd == ETRADER_OP.SELL_CLOSE) {//Buy or SellClose dCurPrice = m_dAsk_Shanghai; dPrice = m_dAsk_Shanghai + ex_dSlippage; if (nCmd == ETRADER_OP.BUY) { m_dOpen_ld_midPrice = m_dMid_London; m_dMaxProfit = -100; m_dMinProfit = 100; } m_dOpen10TickSeconds = get10TickSecond(); } else//Sell or BuyClose { dCurPrice = m_dBid_Shanghai; dPrice = m_dBid_Shanghai - ex_dSlippage; if (nCmd == ETRADER_OP.SELL) { m_dOpen_ld_midPrice = m_dMid_London; m_dMaxProfit = -100; m_dMinProfit = 100; } m_dOpen10TickSeconds = get10TickSecond(); } //For BackTest if (!CFATManager.isOnlineMode()) { m_products[ID_SHANGHAI].reqOrder(nCmd, ref dLots, ref dPrice, EORDER_TYPE.MARKET, sComment); double dLDPrice = m_dMid_London; m_products[ID_LONDON].reqOrder(nCmd, ref dLots, ref dLDPrice, EORDER_TYPE.MARKET, sComment); return; } //For RealTime if (nCmd == ETRADER_OP.BUY_CLOSE || nCmd == ETRADER_OP.SELL_CLOSE) { if (ex_nIsNewOrder <= 0) { ex_nIsNewOrder = 1; } } if (ex_nIsNewOrder > 0) { CFATLogger.output_proc(string.Format("Request order, cmd = {0}, current price ={1}, request price = {2}, request lost = {3}", TRADER.cmd2String(nCmd), dCurPrice, dPrice, dLots)); m_products[ID_SHANGHAI].reqOrder(nCmd, ref dLots, ref dPrice, EORDER_TYPE.MARKET, sComment); CFATLogger.output_proc(string.Format("Response order, response price = {0}, response lost = {1}", dPrice, dLots)); ex_nIsNewOrder--; m_bIsNeedCheckPosition = true; } else { CFATLogger.output_proc(string.Format("new Signal = {0}, ex_nIsNewOrder = {1}", TRADER.cmd2String(nCmd), ex_nIsNewOrder)); } }
// --- private void checkForOpen() { // 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() >= ex_nMaxPosCnt) { return; } if ((DateTime.Now - m_dtLastFilledTime).TotalSeconds < ex_nNewPosTimeSec) { return; } if (m_nTradeRet == ETRADE_RET.NEWORDER)// If position closed at checkForClose with same tick, { return; } int nSignal = getSignal(); if (TRADER.isContain(nSignal, (int)ETRADER_OP.BUY)) { if (m_products[_FIRST].getPosCount_vt() > 0) { if (m_products[_FIRST].getPosCmd_vt(0) != ETRADER_OP.BUY) { return; } } if (ex_nIsNewOrder > 0) { if (CFATManager.isOnlineMode()) { CFATLogger.output_proc(string.Format("open buy ! bid = {0}, ask = {1}, bid = {2}, ask = {3}", m_products[_FIRST].m_dBid, m_products[_FIRST].m_dAsk, m_products[_SECOND].m_dBid, m_products[_SECOND].m_dAsk)); } requestOrder(ETRADER_OP.BUY); } // else // CFATLogger.output_proc(string.Format("{0} : Signal Buy! diff = {1}, bid = {2}, ask = {3}, bid = {4}, ask = {5}",m_sLogicID, m_products[_SECOND].m_dBid - m_products[_FIRST].m_dAsk, // m_products[_FIRST].m_dBid, m_products[_FIRST].m_dAsk, m_products[_SECOND].m_dBid, m_products[_SECOND].m_dAsk)); } if (TRADER.isContain(nSignal, (int)ETRADER_OP.SELL)) { if (m_products[_FIRST].getPosCount_vt() > 0) { if (m_products[_FIRST].getPosCmd_vt(0) != ETRADER_OP.SELL) { return; } } if (ex_nIsNewOrder > 0) { if (CFATManager.isOnlineMode()) { CFATLogger.output_proc(string.Format("open sell ! bid = {0}, ask = {1}, bid = {2}, ask = {3}", m_products[_FIRST].m_dBid, m_products[_FIRST].m_dAsk, m_products[_SECOND].m_dBid, m_products[_SECOND].m_dAsk)); } requestOrder(ETRADER_OP.SELL); } // else // CFATLogger.output_proc(string.Format("{0} : Signal Sell! diff = {1}, bid = {2}, ask = {3}, bid = {4}, ask = {5}",m_sLogicID, m_products[_FIRST].m_dBid - m_products[_SECOND].m_dAsk, // m_products[_FIRST].m_dBid, m_products[_FIRST].m_dAsk, m_products[_SECOND].m_dBid, m_products[_SECOND].m_dAsk)); } }
public static CSite newSite(string sSiteName, string sPipeSerName = "", string sPipeSerOrderName = "", string sPipe_rate = "", string sPipe_order = "") { CSite site = null; if (!CFATManager.isOnlineMode()) { return(CSiteMng.newSite_backtest(sSiteName)); } // modified by cmh //if (sSiteName == "TRADE_VIEW" || sSiteName == "FXCM" || sSiteName == "GP_MT4") // { // site = new CSiteMT4(); // site.setName(sSiteName); // site.setPipeServerName(sPipeSerName); // g_allSites.Add(sSiteName, site); // } if (sSiteName == "TRADE_VIEW_MT4" || sSiteName == "GP_MT4" || sSiteName == "AAFX_MT4" || sSiteName == "EAGLEFX_MT4" || sSiteName == "IC_MT4" || sSiteName == "XTREAM_MT4") { site = new CSiteMT5(); site.setName(sSiteName); site.setPipeServerName_MT5(sPipeSerName, sPipeSerOrderName); g_allSites.Add(sSiteName, site); } if (sSiteName == "TRADE_VIEW_MT5" || sSiteName == "GP_MT5" || sSiteName == "AAFX_MT5" || sSiteName == "EAGLEFX_MT5" || sSiteName == "IC_MT5" || sSiteName == "XTREAM_MT5") { site = new CSiteMT5(); site.setName(sSiteName); site.setPipeServerName_MT5(sPipe_rate, sPipe_order); g_allSites.Add(sSiteName, site); } // --- if (sSiteName == "TRADE_VIEW") { site = new CSiteFixMt4V2(); site.setName(sSiteName); site.setPipeServerName(sPipeSerName); g_allSites.Add(sSiteName, site); } if (sSiteName == "GP") { site = new CSiteFixMt4(); //site = new CSiteMT4(); site.setName(sSiteName); site.setPipeServerName(sPipeSerName); g_allSites.Add(sSiteName, site); } if (sSiteName == "SHGOLD") { site = new CSiteSHGold(); site.setName(sSiteName); site.setPipeServerName(sPipeSerName); g_allSites.Add(sSiteName, site); } if (sSiteName == "IB") { site = new CSiteIB(); site.setName(sSiteName); g_allSites.Add(sSiteName, site); } if (sSiteName == "BITMEX") { site = new CSiteBitMex(); site.setName(sSiteName); g_allSites.Add(sSiteName, site); } if (sSiteName == "OKEX") { site = new CSiteOkex(); site.setName(sSiteName); g_allSites.Add(sSiteName, site); } if (sSiteName == "DERIBIT") { site = new CSiteDeribit(); site.setName(sSiteName); g_allSites.Add(sSiteName, site); } if (sSiteName == "BITFLY") { site = new CSiteBitFlyer(); site.setName(sSiteName); g_allSites.Add(sSiteName, site); } if (sSiteName == "CNFUTURE") { site = new CSiteCNFT(); site.setName(sSiteName); g_allSites.Add(sSiteName, site); } return(site); }