3

-- dataParam 数据参数 --
-- 技术指标公式定义
dataParam.indicators =[[
MA18:MA(CLOSE,18);
LMA18:REF(MA18,1);
MA20:MA(CLOSE,20);
LMA20:REF(MA20,1);
MA30:MA(CLOSE,30);
LMA30:REF(MA30,1);
MA55:MA(CLOSE,55);
LMA55:REF(MA55,1);
MACD_DIFF:MACD_DIFF(10,22,CLOSE);
MACD_DEA:EMA(MACD_DIFF,8);
MACD:(MACD_DIFF-MACD_DEA)*2;
LMACD_DIFF:REF(MACD_DIFF,1);
LMACD_DEA:REF(MACD_DEA,1);
LMACD:REF(MACD,1);
CROSS:CROSS(MACD_DIFF,MACD_DEA);
CROSS1:REF(CROSS,1);
CROSS2:REF(CROSS,2);
condition:=CROSS(MACD_DIFF,MACD_DEA);
condition1:=REF(CROSS(MACD_DIFF,MACD_DEA),1);
BARSLAST:CUMC(1,0==condition1)-1;
]]

dataParam.formulaNum = 0 -- 智能公式数量（0-5，默认为 0，超出范围则取默认值）
-- 多个智能公式可以分别用于不同地方的决策
-- 如果用户此项设置为 0，则不进行训练，直接回测用户的策略
dataParam.columns = {'MA18','LMA18','MA30',"LMA30","MA20","LMA20","MA55","LMA55","MACD_DIFF","MACD_DEA","MACD","LMACD_DIFF","LMACD_DEA","LMACD","OPEN","HIGH","LOW","VOLUME","CROSS1"}
-- 通过遗传算法，将以以上因子为基础构建出新的智能公式技术指标

local commission = 0.0024  --总手续费
local drawdownrate = 0.08 --定义止损比率
local hightable = {} -- 定义最高价table
local lastformula1 = {} -- 定义公式1上一期table

return function ()
local pv = portfolio.portfolioValue()
print("pv:",pv,"cash:",portfolio.cash()) --
local eachTargetMoney = pv/#universe --
print("eachTargetMoney:",eachTargetMoney) -- 略输出日志
for _,target in pairs(universe) do -- for
if not isSuspend(target) then
local po = portfolio.position(target) --
local high = getBar(target,'HIGH') -- 最高价，用于止损计算
local low = getBar(target,'LOW') -- 最低价，用于止损计算
local MA18 = getBar(target,'MA18') -- 18日均线
local LMA18 = getBar(target,'LMA18') --昨天18日均线
local MA20 = getBar(target,'MA20') -- 20日均线
local LMA20 = getBar(target,'LMA20') --昨天20日均线
local MA30 = getBar(target,'MA30') -- 30日均线
local LMA30 = getBar(target,'LMA30') -- 昨天30日均线
local MA55 = getBar(target,'MA55') -- 55日均线
local LMA55 = getBar(target,'LMA55') -- 昨天55日均线
local MACD_DIFF = getBar(target,'MACD_DIFF') --DIF数据
local LMACD_DIFF = getBar(target,'LMACD_DIFF') --昨天的DIF数据
local MACD_DEA = getBar(target,'MACD_DEA')  --DEA数据
local LMACD_DEA = getBar(target,'LMACD_DEA')  ----昨天的DEA数据
local MACD = getBar(target,'MACD') --MACD数据
local LMACD = getBar(target,'LMACD')  --昨天的MACD数据
local price = getBar(target,'CLOSE')  -- 当前股价
local LTDPrice = getBar(target,'LTDPrice')  -- 昨日收盘股价
local CROSS = getBar(target,'CROSS')
local CROSS1 = getBar(target,'CROSS1')
local CROSS2 = getBar(target,'CROSS2')
local AA = getBar(target,'AA')
local BARSLAST = getBar(target,'BARSLAST')
--local condition1=REF(CROSS(MACD_DIFF,MACD_DEA),1)
--local BARSLAST = CUMC(1,0==condition1)-1;

-- print ("price", price )
-- print ("LTDPrice", LTDPrice )
-- print ("MA18 :", MA18)
-- print ("LMA18 :", LMA18)
-- print ("MA30 :", MA30)
-- print ("LMA30 :", LMA30)
-- print ("MA55 :", MA55)
-- print ("LMA55 :", LMA55)
-- print ("MACD_DIFF :", MACD_DIFF)
-- print ("LMACD_DIFF :", LMACD_DIFF)
-- print ("MACD_DEA :", MACD_DEA)
-- print ("LMACD_DEA :", LMACD_DEA)
-- print ("MACD :", MACD)
-- print ("LMACD :", LMACD)
print ("CROSS:",CROSS)
--print ("CROSS1:",CROSS1)
--print ("CROSS2:",CROSS2)
print("BARSLAST:",BARSLAST)
--print("AA:",AA)

-- 同步hightable，用于止损计算(止损代码，不需要修改)
if po.quantity > 0 then
if hightable[target] == nil or hightable[target]<high then
hightable[target] = high
end
else
hightable[target] = nil
end

-- 以下是策略逻辑

local want = (function() --不以函数形式出现，速率会有提升

if price ~= nil and price < MA20 then
return 0 --触发止损
end

if MA18 ~= nil and MA30 ~= nil and MA55 ~= nil and MACD_DIFF ~= nil and MACD_DEA ~= nil and MACD ~= nil
and LMACD_DIFF ~= nil and LMACD_DEA ~= nil and LMACD ~= nil and  MA18 > MA30 and MA30 > MA55
and  MACD_DIFF >= 0  and MACD_DEA >= 0 and CROSS
and REF(MACD_DIFF,BARSLAST+1) < 0

and po.quantity ==0 then
print ("OK",eachTargetMoney/price)
return eachTargetMoney/(1 + commission)/price --触发买入
end

if MA18 ~= nil and MA30 ~= nil and LMA18 ~= nil and LMA30 ~= nil and MA18 < MA30 and LMA18 > LMA30
and po.quantity > 0 then
return 0  --触发卖出
end

return po.quantity
end)()

if po.quantity ~= want then
ret = orderShares(target, want-po.quantity) -- 下单
--print("order :",ret,",",want,",",po)
end
end
end
end

<无标签>

bosimao006

lua 的编译器总会在可结束的 trunk 后加一个分号，所以对于级联的 and 表达式，

--- 共有 1 条评论 ---
bosimao006不是and的问题，我尝试在dataParam.indicators中加写 LMACD_DIFF_2:REF(MACD_DIFF,BARSLAST+1); 然后把 and REF(MACD_DIFF,BARSLAST+1) < 0 改写为 and LMACD_DIFF_2 < 0 错误变为了[分析公式脚本时:: 调用REF时发生错误:参数类型错误,需要立即数 参数:N(#1), 公式系统错误] 1年前

LMACD_DIFF_2:REF(MACD_DIFF,BARSLAST+1);

--- 共有 1 条评论 ---
bosimao006额。。可是总共只有3句话呀。。。这要怎么继续加括号呢？ condition1:=REF(CROSS(MACD_DIFF,MACD_DEA),1); BARSLAST:CUMC(1,0==condition1)-1; LMACD_DIFF_1:REF(MACD_DIFF,BARSLAST+1); 1年前