TAMath transform
FLOOR - 向下取整
函数说明
将数组中每个元素向下取整到最接近的整数。
语法
python
result = TA.FLOOR(records)参数
| 参数名 | 类型 | 说明 |
|---|---|---|
| records | array | 数值数组 |
返回值
返回向下取整后的数组
计算方法
对输入数组的每个元素x,返回不大于x的最大整数
使用场景
- 网格交易买入价(最常用)
- 订单数量保守计算
- 资金分配安全处理
- 价格档位向下对齐
基础示例
python
import math
def main():
# 向下取整示例
prices = [1.1, 1.5, 1.9, 2.0]
floored = [math.floor(p) for p in prices]
# 结果: [1, 1, 1, 2]
# 负数向下取整
values = [-1.1, -1.5, -1.9]
floored_neg = [math.floor(v) for v in values]
# 结果: [-2, -2, -2]高级应用
1. 网格交易完整系统
python
def grid_trading_system():
ticker = exchange.GetTicker()
current_price = ticker['Last']
grid_spacing = 100 # 网格间距100元
num_grids = 10 # 上下各10个网格
# 基准网格(向下取整)
base_grid = math.floor(current_price / grid_spacing) * grid_spacing
# 生成网格
buy_grids = [] # 买入网格(下方)
sell_grids = [] # 卖出网格(上方)
for i in range(1, num_grids + 1):
buy_price = base_grid - i * grid_spacing
sell_price = base_grid + i * grid_spacing
buy_grids.append(buy_price)
sell_grids.append(sell_price)
Log(f"当前价格: {current_price}")
Log(f"基准网格: {base_grid}")
Log(f"买入网格: {buy_grids[:3]}...") # 显示前3个
Log(f"卖出网格: {sell_grids[:3]}...")
return buy_grids, sell_grids2. 安全的资金分配
python
def safe_capital_allocation():
account = exchange.GetAccount()
total_balance = account['Balance']
# 分配给5个资产
num_assets = 5
# 向下取整确保不会超额分配
safe_allocation = math.floor(total_balance / num_assets)
# 总分配资金
total_allocated = safe_allocation * num_assets
# 剩余资金(由于向下取整产生)
余额 = total_balance - total_allocated
Log(f"总资金: {total_balance}")
Log(f"每资产: {safe_allocation}")
Log(f"总分配: {total_allocated}")
Log(f"余额: {余额}")
return safe_allocation3. 订单数量保守计算
python
def conservative_order_size():
account = exchange.GetAccount()
ticker = exchange.GetTicker()
# 最大可买数量
max_quantity = account['Balance'] / ticker['Last']
# 最小交易单位
min_lot = 0.001
# 向下取整到最小单位(保守)
safe_quantity = math.floor(max_quantity / min_lot) * min_lot
Log(f"理论最大: {max_quantity:.6f}")
Log(f"安全数量: {safe_quantity:.6f}")
return safe_quantity4. 价格通道下轨
python
def price_channel_floor():
records = exchange.GetRecords()
# 计算20周期最高最低价
highs = [r['High'] for r in records[-20:]]
lows = [r['Low'] for r in records[-20:]]
highest = max(highs)
lowest = min(lows)
# 通道宽度
channel_width = highest - lowest
# 下轨向下取整到整数(更保守的支撑位)
support_level = math.floor(lowest)
# 上轨向上取整
resistance_level = math.ceil(highest)
Log(f"支撑位: {support_level}")
Log(f"阻力位: {resistance_level}")
Log(f"通道宽度: {channel_width:.2f}")
return support_level, resistance_level5. 分批止盈价格
python
def take_profit_levels():
position = exchange.GetPosition()
if not position:
return
entry_price = position[0]['Price']
# 分3批止盈:5%, 10%, 15%
profit_ratios = [0.05, 0.10, 0.15]
take_profit_prices = []
for ratio in profit_ratios:
tp_price = entry_price * (1 + ratio)
# 向下取整到整数价位(确保达到才触发)
tp_price_floor = math.floor(tp_price)
take_profit_prices.append(tp_price_floor)
Log(f"入场价: {entry_price}")
Log(f"止盈价格:")
for i, price in enumerate(take_profit_prices, 1):
Log(f" 第{i}批: {price}")
return take_profit_prices6. 时间周期整数化
python
def time_period_floor():
# 根据K线数量计算周期
records = exchange.GetRecords()
num_records = len(records)
# 使用25%的数据作为周期
ideal_period = num_records * 0.25
# 向下取整(使用更短周期,更敏感)
period = math.floor(ideal_period)
# 确保最小周期
period = max(period, 5)
Log(f"K线数量: {num_records}")
Log(f"理想周期: {ideal_period:.1f}")
Log(f"实际周期: {period}")
return period注意事项
- floor(1.1) = 1, floor(1.9) = 1, floor(2.0) = 2
- floor(-1.1) = -2(向下是远离0方向)
- 金融计算中floor比ceil更保守
- 避免超额分配资金时使用floor
- Python中可使用
math.floor()替代