找回密码
 立即注册
首页 业界区 业界 完全使用TRAE和AI 开发一款完整的应用----第一周 ...

完全使用TRAE和AI 开发一款完整的应用----第一周

袋岖荤 昨天 20:09
虽然也在工作使用使用ai 补全代码或者完善代码,但还是没有完全使用ai 做一款应用,不依赖手工编程、不依赖人查找资料
所以决定自己写一个应用玩玩,感受一下全完使用ai开发一款应用的乐趣, 跟上时代发展的步伐
不得不说以前写玩具项目挺枯燥,现在用ai写 还是挺有意思的
应用的需求:
使用ai 分析 k线,并进行点评,让天底下的韭菜没有看不懂的k线,让ai进行cosplay 扮演各路专家进行点评,这不是一款应用这是一款ai驱动的综艺节目
考虑我国复杂的金融监管环境,对股票分析限制非常多,所以对 美股 和港股还有web3 领域的token 进行分析
下面是项目和代码文件
主要是一个图表组件TradingChart.tsx和首页 page.tsx 
1.png

TradingChart.tsx
  1. 'use client'; // 确保只在客户端渲染
  2. import { useEffect, useRef } from 'react';
  3. import {
  4.   createChart,
  5.   CandlestickSeries,
  6.   HistogramSeries,
  7. } from 'lightweight-charts';
  8. export interface KLine {
  9.   time: string; // 'YYYY-MM-DD'
  10.   open: number;
  11.   high: number;
  12.   low: number;
  13.   close: number;
  14.   volume: number;
  15. }
  16. interface Props {
  17.   data: KLine[];
  18. }
  19. export default function TradingChart({ data }: Props) {
  20.   console.log(data);
  21.   const containerRef = useRef<HTMLDivElement>(null);
  22.   useEffect(() => {
  23.     if (!containerRef.current || !data.length) return;
  24.     const chart = createChart(containerRef.current, {
  25.       width: containerRef.current.clientWidth,
  26.       height: containerRef.current.clientHeight,
  27.       layout: { textColor: '#d1d4dc', background: { color: '#111' } },
  28.       grid:   { vertLines: { color: '#2a2e39' }, horzLines: { color: '#2a2e39' } },
  29.       rightPriceScale: { visible: true },
  30.       leftPriceScale:  { visible: true },
  31.       
  32.       crosshair: { mode: 1 },
  33.       timeScale: {
  34.         
  35.         fixLeftEdge: false,
  36.         fixRightEdge: false,
  37.         barSpacing: 10,
  38.         minBarSpacing: 5,
  39.       },
  40.     });
  41.    chart.applyOptions({
  42.     localization:{
  43.       dateFormat: 'yyyy-MM-dd',
  44.     }
  45.    })
  46.     /* ---- 蜡烛图 Pane(上方)---- */
  47.     const candlePane = chart.panes()[0];
  48.     const candleSeries = candlePane.addSeries(CandlestickSeries, {
  49.       upColor: '#26a69a',
  50.       downColor: '#ef5350',
  51.       borderVisible: false,
  52.       wickUpColor: '#26a69a',
  53.       wickDownColor: '#ef5350',
  54.       priceScaleId: 'right',
  55.     });
  56.     candleSeries.setData(
  57.       data.map(({ time, open, high, low, close }) => ({ time, open, high, low, close }))
  58.     );
  59.     /* ---- 成交量 Pane(下方 30%)---- */
  60.     const volumePane = chart.addPane();
  61.     volumePane.setHeight(80);
  62.     const volumeSeries = volumePane.addSeries(HistogramSeries, {
  63.       color: '#26a69a',
  64.       priceScaleId: 'right',
  65.       base: 0,
  66.     });
  67.    
  68.     // 格式化成交量Y轴标签 - 调整为20%高度
  69.     volumePane.priceScale('right').applyOptions({
  70.       //设置高度
  71.       
  72.       scaleMargins: {
  73.         top: 0.2,
  74.         bottom: 0,
  75.       },
  76.       borderVisible: false,
  77.     });
  78.    
  79.     // 自定义成交量格式化
  80.     volumeSeries.priceScale().applyOptions({
  81.       invertScale: false,
  82.       borderColor: '#2a2e39',
  83.     });
  84.    
  85.     // 使用priceFormatter来格式化成交量数值
  86.     volumeSeries.applyOptions({
  87.       priceFormat: {
  88.         type: 'custom',
  89.         minMove: 1,
  90.         formatter: (price: number) => {
  91.           if (price >= 1000000) {
  92.             return (price / 1000000).toFixed(1) + 'M';
  93.           } else if (price >= 1000) {
  94.             return (price / 1000).toFixed(0) + 'K';
  95.           } else {
  96.             return price.toFixed(0);
  97.           }
  98.         }
  99.       }
  100.     });
  101.     volumeSeries.setData(
  102.       data.map(d => ({
  103.         time: d.time,
  104.         value: d.volume,
  105.         color: d.close >= d.open ? '#26a69a' : '#ef5350',
  106.       }))
  107.     );
  108.     chart.timeScale().fitContent();
  109.     /* 自适应窗口大小 */
  110.     const ro = new ResizeObserver(() => chart.applyOptions({
  111.       width: containerRef.current!.clientWidth,
  112.       height: containerRef.current!.clientHeight,
  113.       
  114.     }));
  115.     ro.observe(containerRef.current);
  116.     return () => {
  117.       ro.disconnect();
  118.       chart.remove();
  119.     };
  120.   }, [data]);
  121.   return ;
  122. }
复制代码
  page.tsx
  1. // app/page.tsx 或 pages/index.tsx
  2. "use client"
  3. import dynamic from 'next/dynamic';
  4. import { KLine } from '@/components/TradingChart';
  5. import TradingChart from '@/components/TradingChart';
  6. import { useState, useEffect } from 'react';
  7. interface Coin {
  8.   id: string;
  9.   symbol: string;
  10.   name: string;
  11. }
  12. export default function HomePage() {
  13.   const [kLineData, setKLineData] = useState<KLine[]>([]);
  14.   const [coins, setCoins] = useState<Coin[]>([]);
  15.   const [selectedCoin, setSelectedCoin] = useState<string>('bitcoin');
  16.   useEffect(() => {
  17.     const fetchCoins = async () => {
  18.       try {
  19.         const response = await fetch('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false');
  20.         const data = await response.json();
  21.         setCoins(data);
  22.       } catch (error) {
  23.         console.error('获取币种列表失败:', error);
  24.       }
  25.     };
  26.     fetchCoins();
  27.   }, []);
  28.   useEffect(() => {
  29.     const fetchData = async () => {
  30.       if (!selectedCoin) return;
  31.       
  32.       try {
  33.         // 180 天 和30天数据
  34.         // 并行获取OHLC和成交量数据
  35.         const [ohlcRes, volumeRes] = await Promise.all([
  36.           fetch(`https://api.coingecko.com/api/v3/coins/${selectedCoin}/ohlc?vs_currency=usd&days=180`),
  37.           fetch(`https://api.coingecko.com/api/v3/coins/${selectedCoin}/market_chart?vs_currency=usd&days=180&interval=daily`,{cache:'force-cache'})
  38.         ]);
  39.         const ohlcData = await ohlcRes.json();
  40.         const volumeData = await volumeRes.json();
  41.         // 合并数据并转换格式
  42.         const formattedData = ohlcData.map(([timestamp, open, high, low, close]: [number, number, number, number, number]) => {
  43.           // 匹配对应时间戳的成交量
  44.           const volumeEntry = volumeData.total_volumes?.find(([volTimestamp]: [number, number]) => volTimestamp === timestamp);
  45.           const volume = volumeEntry ? volumeEntry[1] : 0;
  46.           // 转换时间戳为YYYY-MM-DD格式
  47.           const date = new Date(timestamp);
  48.           // const time = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
  49.           return { time:timestamp/1000, open, high, low, close, volume };
  50.         });
  51.         setKLineData(formattedData);
  52.       } catch (error) {
  53.         console.error('数据获取失败:', error);
  54.       }
  55.     };
  56.     fetchData();
  57.   }, [selectedCoin]);
  58.   return (
  59.    
  60.         
  61.         
  62.            <select
  63.           value={selectedCoin}
  64.           onChange={(e) => setSelectedCoin(e.target.value)}
  65.           className="px-4 py-2 border border-gray-300 rounded-lg bg-white text-gray-700 shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 hover:border-gray-400 transition-colors duration-200"
  66.         >
  67.           {coins.map((coin) => (
  68.             <option key={coin.id} value={coin.id} className="py-1">
  69.               {coin.name} ({coin.symbol.toUpperCase()})
  70.             </option>
  71.           ))}
  72.         </select>
  73.         {/* 显示 下拉列表中选中的币种的current_price */}
  74.         
  75.           {coins.find((coin) => coin.id === selectedCoin)?.current_price} usdt
  76.         
  77.         
  78.         
  79.         
  80.         <TradingChart data={kLineData} />
  81.       
  82.       
  83.        对话框组件
  84.       
  85.    
  86.       
  87.     );
  88.   }
复制代码
  
 
 
下面是上周工作的成果 绘制了k线和显示了下拉列表还有价格
2.png

下面说一下我开发的步骤
1 首选是使用ai 进行需求分析
使用豆包进行的需求分析
https://www.doubao.com/thread/w0e5291f6269dbb19
2 使用ai 分析用什么工具才能绘制交易专用的图表
https://www.doubao.com/thread/wf2ef2d222fdba7eb
3 使用ai 寻找提供的数据源
使用kimi 寻找数据源
https://www.kimi.com/share/d23na3djqedfoaoeplr0
4 使用ai 开发图表组件
   怎么使用ai 开发专业的交易图表组件?现在多数ai 都支持html 在线预览 例如 豆包和kimi
  例如我需要绘制k线和成交量在一起的一个图表
  先使用ai 制作一个html页面,进行在线预览,如果对效果不满意就完善提示词
  我分别使用 豆包 deepseek kimi 若干次  最后在kimi 成功制作了交易组件的html页面
使用kimi制作html 页面
 https://www.kimi.com/share/d23na3djqedfoaoeplr0
在制作完成html 页面之后,在用ai将html的内容用react 或者vue 重写一遍
然后就是复制到ide 中 进行进一步的完善
 
 
  

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册