void InputHandler::processHoldAndRepeat() { HardwareKeyboard *kb = PageManager::GetHardwareKeyboard(); // touch and key repeat section struct timespec curTime; clock_gettime(CLOCK_MONOTONIC, &curTime); long seconds = curTime.tv_sec - touchStart.tv_sec; long nseconds = curTime.tv_nsec - touchStart.tv_nsec; long mtime = ((seconds) * 1000 + nseconds / 1000000.0) + 0.5; if (touch_status == TS_TOUCH_AND_HOLD && mtime > touch_hold_ms) { touch_status = TS_TOUCH_REPEAT; clock_gettime(CLOCK_MONOTONIC, &touchStart); LOGEVENT("TOUCH_HOLD: %d,%d", x, y); PageManager::NotifyTouch(TOUCH_HOLD, x, y); } else if (touch_status == TS_TOUCH_REPEAT && mtime > touch_repeat_ms) { LOGEVENT("TOUCH_REPEAT: %d,%d", x, y); clock_gettime(CLOCK_MONOTONIC, &touchStart); PageManager::NotifyTouch(TOUCH_REPEAT, x, y); } else if (key_status == KS_KEY_PRESSED && mtime > key_hold_ms) { LOGEVENT("KEY_HOLD: %d,%d", x, y); clock_gettime(CLOCK_MONOTONIC, &touchStart); key_status = KS_KEY_REPEAT; kb->KeyRepeat(); } else if (key_status == KS_KEY_REPEAT && mtime > key_repeat_ms) { LOGEVENT("KEY_REPEAT: %d,%d", x, y); clock_gettime(CLOCK_MONOTONIC, &touchStart); kb->KeyRepeat(); } }
void TradingSequenceRobot::init( HlpStruct::AlgBrokerDescriptor const& descriptor, BrkLib::BrokerBase* brokerlib, CppUtils::String const& runName, CppUtils::String const& comment, TradeParam const& tradeParam, CppUtils::StringMap const& symbolsFinam, CppUtils::StringMap const& symbolsSmartcom, CppUtils::StringMap const& symbolsSmartcomReverse, CppUtils::StringSet const& syntheticSmartcomList ) { symbolsFinamList_m = symbolsFinam; symbolsSmartcomList_m = symbolsSmartcom; symbolsSmartcomReverse_m = symbolsSmartcomReverse; syntheticSmartcomList_m = syntheticSmartcomList; // tradeParam_m = tradeParam; initStateMachine(); // connect to broker brokerDialogReal_m.initAndConnect( brokerlib, CppUtils::String(PAIR_TRADE) + "_real", runName, comment, base_m.getRtdataProvider_Smartcom() ); if (!brokerDialogReal_m.isConnected()) { LOGEVENT(HlpStruct::CommonLog::LL_ERROR, PAIR_TRADE, "onLibraryInitialized(...) session cannot be established: " << brokerDialogReal_m.getConnectString()); } // broker connect 2 brokerDialogDemo_m.initAndConnect( base_m.getAlgHolder().getBroker2(), CppUtils::String(PAIR_TRADE) + "_simul", runName, comment, base_m.getRtdataProvider_Smartcom() ); if (!brokerDialogDemo_m.isConnected()) { LOGEVENT(HlpStruct::CommonLog::LL_ERROR, PAIR_TRADE, "onLibraryInitialized(...) session with broker lib 2 cannot be established: " << brokerDialogDemo_m.getConnectString()); } if (tradeParam_m.isInvalid()) THROW(CppUtils::OperationFailed, "exc_TradeParamsInvalid", "ctx_onLibraryInitialized", tradeParam_m.toDescriptionString() ); calculatorLevel_m.initialized(); LOG_COMMON(base_m.getRunName(), base_m.getRtdataProvider_Smartcom(), BrkLib::AP_MEDIUM, "Trading parameters: " << tradeParam_m.toDescriptionString()); }
void GapCalculator::onLibraryFreed() { LOGEVENT(HlpStruct::CommonLog::LL_INFO, GAP_CALCULATOR, "Library freed"); }
void InputHandler::doTouchStart() { LOGEVENT("TOUCH_START: %d,%d", x, y); if (PageManager::NotifyTouch(TOUCH_START, x, y) > 0) { state = AS_NO_ACTION; } else { state = AS_IN_ACTION_AREA; } touch_status = TS_TOUCH_AND_HOLD; clock_gettime(CLOCK_MONOTONIC, &touchStart); }
void GapCalculator::onLibraryInitialized( HlpStruct::AlgBrokerDescriptor const& descriptor, BrkLib::BrokerBase* brokerlib, BrkLib::BrokerBase* brokerlib2, CppUtils::String const& runName, CppUtils::String const& comment ) { // list of symbols // symbol lits CppUtils::String const& trade_symbols_file = GET_ALG_TRADE_STRING(descriptor.initialAlgBrkParams_m, "TRADE_SYMBOLS_FILE"); if (CppUtils::fileExists(trade_symbols_file)) { if (!CppUtils::parseSymbolListFile(checkedSymbols_m, trade_symbols_file)) { LOGEVENT(HlpStruct::CommonLog::LL_ERROR, GAP_CALCULATOR, "Cannot read symbols from: " << trade_symbols_file); return; } }; provider_m = GET_ALG_TRADE_STRING(descriptor.initialAlgBrkParams_m, "TRADE_SYMBOL_PROVIDER"); beginTestingDate_m = CppUtils::parseFormattedDateTime("M/d/Y", GET_ALG_TRADE_STRING(descriptor.initialAlgBrkParams_m, "BEGIN_TESTING_DATE"), 0 ); endTestingDate_m = CppUtils::parseFormattedDateTime("M/d/Y", GET_ALG_TRADE_STRING(descriptor.initialAlgBrkParams_m, "END_TESTING_DATE"), 0 ); LOGEVENT(HlpStruct::CommonLog::LL_INFO, GAP_CALCULATOR, "Library initialized"); }
void AlgorithmDummy::onLibraryInitialized( HlpStruct::AlgBrokerDescriptor const& descriptor, BrkLib::BrokerBase* brokerlib, BrkLib::BrokerBase* brokerlib2, CppUtils::String const& runName, CppUtils::String const& comment ) { idx_m = 0; isLong_m = false; brokerConnect_m = new BrkLib::BrokerConnect(brokerlib, ALG_DUMMY, false, false, runName.c_str(), comment.c_str()); brokerConnect_m->connect(); if (!brokerConnect_m->getSession().isConnected()) { LOGEVENT(HlpStruct::CommonLog::LL_ERROR, ALG_DUMMY, "onLibraryInitialized(...) session cannot be established: " << brokerConnect_m->getSession().getConnection()); } }
void GapCalculator::returnProfitPerRun( bool const is_short, bool const singleton, double const& stolLossLevel, double const& volumeFactorFilter, double const& gapFilter, Inqueue::ContextAccessor& historyaccessor, CalcResult& result ) { result.stolLossLevel_m = stolLossLevel; result.volumeFactorFilter_m = volumeFactorFilter; result.gapFilter_m = gapFilter; result.beginTestingDate_m = beginTestingDate_m; result.endTestingDate_m = endTestingDate_m; result.isSingleton_m = singleton; result.fullPeriodCovered_m = true; // cached for(int l = 0; l < cachedIds_m.size(); l++) { int cache_id_i = cachedIds_m[l]; // now iterate Inqueue::DataPtr ptr = historyaccessor.dataFirst(cache_id_i); // 0, 1, ... -> HlpStruct::PriceData data_0; HlpStruct::PriceData const* pdata_1 = 0; while(true) { bool has_next = historyaccessor.dataNext(cache_id_i, ptr); if (has_next) pdata_1 = &historyaccessor.getCurrentPriceData(cache_id_i, ptr); else pdata_1 = &historyaccessor.getUnclosedCandle(cache_id_i); HlpStruct::PriceData const& data_1 = *pdata_1; if (!data_0.isValid()) { data_0 = data_1; if (data_1.time_m > beginTestingDate_m) { LOGEVENT(HlpStruct::CommonLog::LL_ERROR, GAP_CALCULATOR, "Begin testing date is " << CppUtils::getGmtTime(beginTestingDate_m) << " but found first data is " << CppUtils::getGmtTime(data_1.time_m) << " for symbol: " << cache_id_i); return; } continue; } // calculations result.checkedDaysCount_m++; // TODO!!! double gapValue=(Math::GetPrice2<Math::P_CloseBid>::getCandlePrice(data_0) - Math::GetPrice2<Math::P_OpenBid>::getCandlePrice(data_1))/Math::GetPrice2<Math::P_CloseBid>::getCandlePrice(data_0); // gap value always positive if (!is_short) gapValue = -gapValue; Math::MovAverMathEngine<Math::PriceSeriesProvider> maEngine(52); double ma = historyaccessor.callMathEngine<double, Math::P_Volume, Math::MovAverMathEngine<Math::PriceSeriesProvider> >(cache_id_i, maEngine); double factor1 = ma * Math::GetPrice2<Math::P_CloseBid>::getCandlePrice(data_0); if (gapValue > gapFilter) { result.gapFilterHitCount_m++; } if (factor1 > volumeFactorFilter) { result.volumeFactorHitCount_m++; } // gap filter if (gapValue > gapFilter) { // volume factor filter if (factor1 > volumeFactorFilter) { // total deals this day - we restrict the number of deals if (singleton && (result.dailyEntries_m[data_1.time_m].totalDeals_m>=1)) { // need to skip that test if (!has_next) break; // prev data_0 = data_1; continue; } // volume factor double profit; // open low only - all signals we have!!!!!!!!!!!!!!! result.pattTotalCount_m++; // otherwise GO ON result.dailyEntries_m[data_1.time_m].totalDeals_m++; double drawdown_abs; // this is our go to the negative side // muts be always positive if (is_short) drawdown_abs = Math::GetPrice2<Math::P_HighBid>::getCandlePrice(data_1) - Math::GetPrice2<Math::P_OpenBid>::getCandlePrice(data_1) ; else drawdown_abs = Math::GetPrice2<Math::P_OpenBid>::getCandlePrice(data_1) - Math::GetPrice2<Math::P_LowBid>::getCandlePrice(data_1) ; double drawdown = drawdown_abs / Math::GetPrice2<Math::P_OpenBid>::getCandlePrice(data_1); SymbolEntry& symbol_day_entry_i = result.dailyEntries_m[data_1.time_m].daySymbols_m[cache_id_i]; symbol_day_entry_i.prevDayClose_m = Math::GetPrice2<Math::P_CloseBid>::getCandlePrice(data_0); symbol_day_entry_i.symbol_m = historyaccessor.getSymbol(cache_id_i); symbol_day_entry_i.is_short_m = is_short; if ( drawdown > stolLossLevel ) { // stop loss executed profit = -drawdown_abs; result.pattStopLossCount_m++; // no profit symbol_day_entry_i.absoluteMaxIncomePriceGo_m = -1; symbol_day_entry_i.relativeMaxIncomePriceGo_m = -1; } else { if (is_short) profit = Math::GetPrice2<Math::P_OpenBid>::getCandlePrice(data_1) - Math::GetPrice2<Math::P_CloseBid>::getCandlePrice(data_1); else profit = Math::GetPrice2<Math::P_CloseBid>::getCandlePrice(data_1) - Math::GetPrice2<Math::P_OpenBid>::getCandlePrice(data_1); result.pattExecCount_m++; // profit if (is_short) symbol_day_entry_i.absoluteMaxIncomePriceGo_m = Math::GetPrice2<Math::P_OpenBid>::getCandlePrice(data_1) - Math::GetPrice2<Math::P_LowBid>::getCandlePrice(data_1); // low price else symbol_day_entry_i.absoluteMaxIncomePriceGo_m = Math::GetPrice2<Math::P_HighBid>::getCandlePrice(data_1) - Math::GetPrice2<Math::P_OpenBid>::getCandlePrice(data_1); // symbol_day_entry_i.relativeMaxIncomePriceGo_m = symbol_day_entry_i.absoluteMaxIncomePriceGo_m / symbol_day_entry_i.prevDayClose_m; } symbol_day_entry_i.absoluteProfit_m = profit; symbol_day_entry_i.relativeProfit_m = symbol_day_entry_i.absoluteProfit_m / symbol_day_entry_i.prevDayClose_m; //NOW calculate PL RATIO - the only parameter to optimize } // end of patter detect } // if (!has_next) break; // prev data_0 = data_1; }; // data_1 - the last unclosed candle if (pdata_1->time_m < this->endTestingDate_m) { LOGEVENT(HlpStruct::CommonLog::LL_ERROR, GAP_CALCULATOR, "End testing date is " << CppUtils::getGmtTime2(endTestingDate_m) << " but found last data is " << CppUtils::getGmtTime2(pdata_1->time_m) << " for symbol: " << cache_id_i); result.fullPeriodCovered_m = false; } } // end symbol loop }
void GapCalculator::onThreadStarted(Inqueue::ContextAccessor& contextaccessor, bool const firstLib, bool const lastLib) { /// cache symbols cachedIds_m.clear(); for(CppUtils::StringSet::const_iterator it= checkedSymbols_m.begin(); it != checkedSymbols_m.end(); it++) { CppUtils::String const& symbol_i = *it; int cache_id_i = contextaccessor.cacheSymbol(provider_m, symbol_i, HlpStruct::AP_Day, 1); if (cache_id_i <= 0) { LOGEVENT(HlpStruct::CommonLog::LL_ERROR, GAP_CALCULATOR, "Cannot cache symbol: " << symbol_i); continue; } if (contextaccessor.getSize(cache_id_i) < 2) { LOGEVENT(HlpStruct::CommonLog::LL_ERROR, GAP_CALCULATOR, "Cache size is less then 2: " << symbol_i); continue; } cachedIds_m.push_back(cache_id_i); } // ---------- double stopLossLevel, volumeFactorFilter, gapFilter; bool IS_SHORT = true; bool SINGLETON = true; double MIN_SL_LEVEL = 0.01; double MAX_SL_LEVEL = 0.06; double STEP_SL_LEVEl = 0.01; double MIN_VOLUME_FACTOR = 100000000.0; double MAX_VOLUME_FACTOR = 6.0*1000000000.0; double STEP_VOLUME_FACTOR = 250000000.0; double MIN_GAP_FILTER = 0.01; double MAX_GAP_FILTER = 0.15; double STEP_GAP_FILTER = 0.02; int totalcnt = 0; vector<CalcResult> resultlist; stopLossLevel = MIN_SL_LEVEL; while(stopLossLevel <=MAX_SL_LEVEL) { volumeFactorFilter = MIN_VOLUME_FACTOR; while(volumeFactorFilter <= MAX_VOLUME_FACTOR) { gapFilter = MIN_GAP_FILTER; while(gapFilter <= MAX_GAP_FILTER) { // //if (gapFilter== 0.03 && volumeFactorFilter == 1100000000 && stopLossLevel == 0.02) { CalcResult result(totalcnt); returnProfitPerRun(IS_SHORT, SINGLETON,stopLossLevel, volumeFactorFilter, gapFilter, contextaccessor, result); LOGEVENT(HlpStruct::CommonLog::LL_INFO, GAP_CALCULATOR, "Simulation result #" << totalcnt ); resultlist.push_back( result ); //} totalcnt++; gapFilter+= STEP_GAP_FILTER; } volumeFactorFilter += STEP_VOLUME_FACTOR; } stopLossLevel += STEP_SL_LEVEl; } createReportToTheFile("c:\\temp\\gap_calk.txt", resultlist ); LOGEVENT(HlpStruct::CommonLog::LL_INFO, GAP_CALCULATOR, "Simulation finished with total iterations: " << totalcnt ); }
void Retrace1::onEventArrived( Inqueue::ContextAccessor& contextaccessor, HlpStruct::CallingContext& callContext, bool& result, CppUtils::String &customOutputData ) { HlpStruct::XmlParameter out_result; out_result.getParameter("status_string").getAsString(true) = "OK"; out_result.getParameter("status").getAsString(true) = "0"; try { customOutputData = "FAILED"; CppUtils::String const& command = callContext.getCallCustomName(); LOG(MSG_INFO, ALG_RETRACE, "Command: " << command); CppUtils::String const& data_hex = callContext.getParameter<BrkLib::String_Proxy>("custom_data").get(); CppUtils::String data = CppUtils::unhexlify(data_hex); LOG(MSG_INFO, ALG_RETRACE, "Data before hex received: [" << data_hex << "]"); LOG(MSG_INFO, ALG_RETRACE, "Data received: [" << data << "]"); // get input parameter HlpStruct::XmlParameter in_param; HlpStruct::deserializeXmlParameter( in_param, data ); CppUtils::String user_id = in_param.getParameter("user_id").getAsString(); CppUtils::String session_id = in_param.getParameter("session_id").getAsString(); CppUtils::makeDir( resolveTmpDirectory(user_id)); CppUtils::makeDir( resolveDownloadDirectory(user_id)); CppUtils::makeDir( resolveCompletedDirectory(user_id)); CppUtils::Uid session_uid(session_id); if (!session_uid.isValid()) THROW(CppUtils::OperationFailed, "exc_InvalidSessionUid", "ctx_onEventArrived", session_id); // resolve directory if (command=="start_download_torrent") { // start download CppUtils::String torrent_url = in_param.getParameter("torrent_url").getAsString(); LOGEVENT(HlpStruct::CommonLog::LL_INFO, ALG_RETRACE, "Starting download - url: " << torrent_url << " user_id: " << user_id << " session _id: " << session_id ); CppUtils::sleep(20.0); } else if (command=="query_status") { // query status } else if (command=="cancel_download_torrent") { // cancel download } else THROW(CppUtils::OperationFailed, "exc_InvalidCommand", "ctx_onEventArrived", command); } catch(CppUtils::Exception& e) { out_result.getParameter("status_string").getAsString(true) = "Exception: " + e.message() + " - " + e.arg(); } catch(...) { out_result.getParameter("status_string").getAsString(true) = "Unknown exception"; } CppUtils::String out_result_str; HlpStruct::serializeXmlParameter(out_result, out_result_str ); customOutputData = CppUtils::hexlify(out_result_str); LOG(MSG_INFO, ALG_RETRACE, "Data output before: [" << out_result_str << "]"); LOG(MSG_INFO, ALG_RETRACE, "Data output: [" << customOutputData << "]"); // command to stop working thread // it's secure to do here // because thread cannot be stopped stopWorkingThread(contextaccessor); }