add.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. {% extends 'base.html' %}
  2. {% block title %}添加期货信息 - 期货数据管理系统{% endblock %}
  3. {% block content %}
  4. <div class="d-flex justify-content-between align-items-center mb-4">
  5. <h2>添加期货信息</h2>
  6. <a href="{{ url_for('future_info.index') }}" class="btn btn-secondary">返回列表</a>
  7. </div>
  8. <div class="card">
  9. <div class="card-body">
  10. <form id="future-info-form">
  11. <div class="row">
  12. <div class="col-md-6">
  13. <div class="form-group">
  14. <label for="contract_letter">合约代码</label>
  15. <input type="text" class="form-control" id="contract_letter" name="contract_letter" required>
  16. </div>
  17. </div>
  18. <div class="col-md-6">
  19. <div class="form-group">
  20. <label for="name">期货名称</label>
  21. <input type="text" class="form-control" id="name" name="name" required>
  22. </div>
  23. </div>
  24. </div>
  25. <div class="row">
  26. <div class="col-md-6">
  27. <div class="form-group">
  28. <label for="market">市场</label>
  29. <input type="text" class="form-control" id="market" name="market">
  30. </div>
  31. </div>
  32. <div class="col-md-6">
  33. <div class="form-group">
  34. <label for="exchange">交易所</label>
  35. <input type="text" class="form-control" id="exchange" name="exchange">
  36. </div>
  37. </div>
  38. </div>
  39. <div class="row">
  40. <div class="col-md-6">
  41. <div class="form-group">
  42. <label for="contract_multiplier">合约乘数</label>
  43. <input type="number" class="form-control" id="contract_multiplier" name="contract_multiplier">
  44. </div>
  45. </div>
  46. <div class="col-md-6">
  47. <div class="form-group">
  48. <label for="long_margin_rate">做多保证金率</label>
  49. <input type="number" step="0.01" class="form-control" id="long_margin_rate" name="long_margin_rate">
  50. </div>
  51. </div>
  52. </div>
  53. <div class="row">
  54. <div class="col-md-6">
  55. <div class="form-group">
  56. <label for="short_margin_rate">做空保证金率</label>
  57. <input type="number" step="0.01" class="form-control" id="short_margin_rate" name="short_margin_rate">
  58. </div>
  59. </div>
  60. <div class="col-md-6">
  61. <div class="form-group">
  62. <label for="open_fee">开仓费用(按手)</label>
  63. <input type="number" step="0.01" class="form-control" id="open_fee" name="open_fee">
  64. </div>
  65. </div>
  66. </div>
  67. <div class="row">
  68. <div class="col-md-6">
  69. <div class="form-group">
  70. <label for="close_fee">平仓费用(按手)</label>
  71. <input type="number" step="0.01" class="form-control" id="close_fee" name="close_fee">
  72. </div>
  73. </div>
  74. <div class="col-md-6">
  75. <div class="form-group">
  76. <label for="close_today_rate">平今费用(按金额)</label>
  77. <input type="number" step="0.01" class="form-control" id="close_today_rate" name="close_today_rate">
  78. </div>
  79. </div>
  80. </div>
  81. <div class="row">
  82. <div class="col-md-6">
  83. <div class="form-group">
  84. <label for="close_today_fee">平今费用(按手)</label>
  85. <input type="number" step="0.01" class="form-control" id="close_today_fee" name="close_today_fee">
  86. </div>
  87. </div>
  88. <div class="col-md-6">
  89. <div class="form-group">
  90. <label for="th_main_contract">主力合约</label>
  91. <input type="text" class="form-control" id="th_main_contract" name="th_main_contract">
  92. </div>
  93. </div>
  94. </div>
  95. <div class="row">
  96. <div class="col-md-6">
  97. <div class="form-group">
  98. <label for="current_main_contract">当前主力合约</label>
  99. <input type="text" class="form-control" id="current_main_contract" name="current_main_contract">
  100. </div>
  101. </div>
  102. <div class="col-md-6">
  103. <div class="form-group">
  104. <label for="th_order">排序</label>
  105. <input type="number" class="form-control" id="th_order" name="th_order">
  106. </div>
  107. </div>
  108. </div>
  109. <div class="row">
  110. <div class="col-md-6">
  111. <div class="form-group">
  112. <label for="long_margin_amount">做多1手保证金</label>
  113. <input type="number" step="0.01" class="form-control" id="long_margin_amount" name="long_margin_amount">
  114. </div>
  115. </div>
  116. <div class="col-md-6">
  117. <div class="form-group">
  118. <label for="short_margin_amount">做空1手保证金</label>
  119. <input type="number" step="0.01" class="form-control" id="short_margin_amount" name="short_margin_amount">
  120. </div>
  121. </div>
  122. </div>
  123. <div class="row">
  124. <div class="col-md-6">
  125. <div class="form-group">
  126. <label for="core_ratio">核心比率</label>
  127. <input type="number" step="0.01" min="0" max="1" class="form-control" id="core_ratio" name="core_ratio">
  128. </div>
  129. </div>
  130. <div class="col-md-6">
  131. <!-- 空列,保持布局平衡 -->
  132. </div>
  133. </div>
  134. <div class="row">
  135. <div class="col-md-12">
  136. <div class="form-group">
  137. <label for="long_term_trend" class="form-label">长期趋势特征</label>
  138. <!-- 隐藏的输入框,用于存储最终的数据 -->
  139. <input type="hidden" id="long_term_trend" name="long_term_trend">
  140. <!-- 趋势特征搜索和选择区域 -->
  141. <div class="trend-selection-area">
  142. <!-- 已选择的趋势特征标签 -->
  143. <div id="selected_trends" class="selected-trends mb-2">
  144. <!-- 已选择的趋势特征标签将在这里显示 -->
  145. </div>
  146. <!-- 趋势特征搜索输入框 -->
  147. <div class="position-relative">
  148. <input type="text" class="form-control" id="trend_search_input"
  149. placeholder="搜索并添加趋势特征..." autocomplete="off">
  150. <div id="trend_search_results" class="dropdown-menu w-100" style="display: none; max-height: 200px; overflow-y: auto;">
  151. <!-- 搜索结果将在这里显示 -->
  152. </div>
  153. </div>
  154. </div>
  155. <div id="trend_validation_feedback" class="invalid-feedback" style="display:none;"></div>
  156. <div class="form-text">搜索并选择趋势特征,支持添加多个</div>
  157. </div>
  158. </div>
  159. </div>
  160. <div class="form-group mt-4">
  161. <button type="submit" class="btn btn-primary">保存</button>
  162. <button type="reset" class="btn btn-secondary">重置</button>
  163. </div>
  164. </form>
  165. </div>
  166. </div>
  167. <script>
  168. document.addEventListener('DOMContentLoaded', function() {
  169. const form = document.getElementById('future-info-form');
  170. const longTermTrendInput = document.getElementById('long_term_trend');
  171. const trendSearchInput = document.getElementById('trend_search_input');
  172. const trendSearchResults = document.getElementById('trend_search_results');
  173. const selectedTrendsContainer = document.getElementById('selected_trends');
  174. let allTrends = []; // 存储所有趋势特征数据
  175. let selectedTrends = []; // 存储已选择的趋势特征
  176. let searchTimeout;
  177. // 加载所有趋势特征列表
  178. function loadTrendOptions() {
  179. fetch('/api/future_info/trends')
  180. .then(response => response.json())
  181. .then(data => {
  182. if (data.code === 0) {
  183. allTrends = data.data;
  184. console.log('加载趋势特征列表:', allTrends.length, '个');
  185. }
  186. })
  187. .catch(error => {
  188. console.error('获取趋势特征失败:', error);
  189. });
  190. }
  191. // 初始化页面时加载趋势特征列表
  192. loadTrendOptions();
  193. // 搜索趋势特征
  194. function searchTrends(query) {
  195. if (!query.trim()) {
  196. hideTrendSearchResults();
  197. return;
  198. }
  199. // 过滤已选择的趋势特征
  200. const selectedTrendNames = selectedTrends.map(trend => trend.name);
  201. const filteredTrends = allTrends.filter(trend =>
  202. trend.name.toLowerCase().includes(query.toLowerCase()) &&
  203. !selectedTrendNames.includes(trend.name)
  204. );
  205. showTrendSearchResults(filteredTrends);
  206. }
  207. // 显示搜索结果
  208. function showTrendSearchResults(results) {
  209. trendSearchResults.innerHTML = '';
  210. if (results.length === 0) {
  211. trendSearchResults.innerHTML = '<div class="dropdown-item-text text-muted">未找到匹配的趋势特征</div>';
  212. } else {
  213. results.slice(0, 10).forEach(trend => { // 限制显示前10个结果
  214. const item = document.createElement('div');
  215. item.className = 'dropdown-item cursor-pointer';
  216. item.style.cursor = 'pointer';
  217. item.textContent = trend.name;
  218. item.addEventListener('click', () => selectTrend(trend));
  219. trendSearchResults.appendChild(item);
  220. });
  221. }
  222. trendSearchResults.style.display = 'block';
  223. }
  224. // 隐藏搜索结果
  225. function hideTrendSearchResults() {
  226. trendSearchResults.style.display = 'none';
  227. }
  228. // 选择趋势特征
  229. function selectTrend(trend) {
  230. // 检查是否已经选择
  231. if (selectedTrends.find(t => t.name === trend.name)) {
  232. return;
  233. }
  234. selectedTrends.push(trend);
  235. updateSelectedTrendsDisplay();
  236. updateHiddenInput();
  237. // 清空搜索框
  238. trendSearchInput.value = '';
  239. hideTrendSearchResults();
  240. console.log('选择了趋势特征:', trend.name);
  241. }
  242. // 删除趋势特征
  243. function removeTrend(index) {
  244. selectedTrends.splice(index, 1);
  245. updateSelectedTrendsDisplay();
  246. updateHiddenInput();
  247. console.log('删除趋势特征,剩余:', selectedTrends.length, '个');
  248. }
  249. // 更新已选择趋势特征的显示
  250. function updateSelectedTrendsDisplay() {
  251. selectedTrendsContainer.innerHTML = '';
  252. selectedTrends.forEach((trend, index) => {
  253. const badge = document.createElement('span');
  254. badge.className = 'badge bg-primary me-2 mb-2 d-inline-flex align-items-center';
  255. badge.innerHTML = `
  256. ${trend.name}
  257. <button type="button" class="btn-close btn-close-white ms-2" style="font-size: 0.7em;" aria-label="Remove"></button>
  258. `;
  259. // 添加删除事件
  260. const closeBtn = badge.querySelector('.btn-close');
  261. closeBtn.addEventListener('click', () => removeTrend(index));
  262. selectedTrendsContainer.appendChild(badge);
  263. });
  264. }
  265. // 更新隐藏输入框的值
  266. function updateHiddenInput() {
  267. const trendNames = selectedTrends.map(trend => trend.name);
  268. longTermTrendInput.value = trendNames.join('+');
  269. console.log('更新隐藏输入框值:', longTermTrendInput.value);
  270. }
  271. // 监听趋势特征搜索输入
  272. trendSearchInput.addEventListener('input', function() {
  273. const query = this.value.trim();
  274. // 使用防抖进行搜索
  275. clearTimeout(searchTimeout);
  276. searchTimeout = setTimeout(() => {
  277. searchTrends(query);
  278. }, 300);
  279. });
  280. // 监听焦点离开事件,隐藏搜索结果
  281. trendSearchInput.addEventListener('blur', function() {
  282. // 延迟隐藏,允许用户点击搜索结果
  283. setTimeout(() => {
  284. hideTrendSearchResults();
  285. }, 200);
  286. });
  287. // 监听点击外部区域隐藏搜索结果
  288. document.addEventListener('click', function(event) {
  289. if (!event.target.closest('.trend-selection-area')) {
  290. hideTrendSearchResults();
  291. }
  292. });
  293. // 表单提交处理
  294. form.addEventListener('submit', function(event) {
  295. event.preventDefault();
  296. if (!form.checkValidity()) {
  297. event.stopPropagation();
  298. form.classList.add('was-validated');
  299. return;
  300. }
  301. // 收集表单数据
  302. const formData = {
  303. contract_letter: document.getElementById('contract_letter').value,
  304. name: document.getElementById('name').value,
  305. market: parseInt(document.getElementById('market').value),
  306. exchange: document.getElementById('exchange').value,
  307. contract_multiplier: document.getElementById('contract_multiplier').value ? Number(document.getElementById('contract_multiplier').value) : null,
  308. long_margin_rate: document.getElementById('long_margin_rate').value !== '' ? Number(document.getElementById('long_margin_rate').value) : null,
  309. short_margin_rate: document.getElementById('short_margin_rate').value !== '' ? Number(document.getElementById('short_margin_rate').value) : null,
  310. open_fee: document.getElementById('open_fee').value !== '' ? Number(document.getElementById('open_fee').value) : null,
  311. close_fee: document.getElementById('close_fee').value !== '' ? Number(document.getElementById('close_fee').value) : null,
  312. close_today_rate: document.getElementById('close_today_rate').value !== '' ? Number(document.getElementById('close_today_rate').value) : null,
  313. close_today_fee: document.getElementById('close_today_fee').value !== '' ? Number(document.getElementById('close_today_fee').value) : null,
  314. long_margin_amount: document.getElementById('long_margin_amount').value !== '' ? Number(document.getElementById('long_margin_amount').value) : null,
  315. short_margin_amount: document.getElementById('short_margin_amount').value !== '' ? Number(document.getElementById('short_margin_amount').value) : null,
  316. th_main_contract: document.getElementById('th_main_contract').value,
  317. current_main_contract: document.getElementById('current_main_contract').value,
  318. th_order: document.getElementById('th_order').value ? parseInt(document.getElementById('th_order').value) : null,
  319. long_term_trend: document.getElementById('long_term_trend').value,
  320. core_ratio: document.getElementById('core_ratio').value ? Number(document.getElementById('core_ratio').value) : null
  321. };
  322. console.log('准备提交的数据:', formData);
  323. // 发送添加请求
  324. fetch('/api/future_info/add', {
  325. method: 'POST',
  326. headers: {
  327. 'Content-Type': 'application/json'
  328. },
  329. body: JSON.stringify(formData)
  330. })
  331. .then(response => response.json())
  332. .then(data => {
  333. if (data.code === 0) {
  334. alert('添加成功');
  335. window.location.href = "{{ url_for('future_info.index') }}";
  336. } else {
  337. alert('添加失败:' + data.msg);
  338. }
  339. })
  340. .catch(error => {
  341. console.error('Error:', error);
  342. alert('添加失败');
  343. });
  344. });
  345. // 重置表单时重置趋势特征选择
  346. form.addEventListener('reset', function() {
  347. selectedTrends = [];
  348. updateSelectedTrendsDisplay();
  349. updateHiddenInput();
  350. trendSearchInput.value = '';
  351. hideTrendSearchResults();
  352. });
  353. });
  354. </script>
  355. {% endblock %}