@Alex,请帮忙看看下面代码是不是有问题? [MC]
-
MC用户求助:
采用tick周期
目的是价格比13.10低0.04就买入,比13.10高0.05就卖出
以买入为例:
如果买入成功后,13.10变成13.06,比13.06高0.05卖出,比13.06低0.04买入,以此类推;
vars:mpab(0),order_status(0),order_no(1),cen(13.10),up(0.05),down(0.04);{mpab记录实盘证券帐号的总股数,order_status记录成交状态,cen代表当前参考价格,up,down分别为上下波动}
if MarketPosition_at_Broker > 0 then
mpab = MarketPosition_at_Broker;
if mpab[2] > 0 then {试验发现,无论给mpab什么值,发生了mpab = MarketPosition_at_Broker后,总是mpab[1]=0,所以参考bar设为3,mpab[2] >0 保证解决了初始0值的问题,如果初始是0,则condition1一开始都是成立的}
begin
condition1 = mpab > mpab[1];
condition2 = mpab < mpab[1];
condition3 = mpab = mpab[1];
condition4 = close - cen >= up;
condition5 = cen - close >= down;
if condition3 and order_status = 0 then
begin
if condition4 then
begin
sell 200 shares from entry(text(order_no)) next bar at market;
print("----sell----");
order_no = order_no -1;
order_status = 1;
end;
if condition5 then
begin
order_no = order_no + 1;
buy (text(order_no)) 200 shares next bar at market;
print("++++buy++++");
order_status = 1;
end;
end;
if condition1 then
begin
cen = cen + up;
order_status = 0;
end;
if condition2 then
begin
cen = cen - down;
order_status = 0;
end;
end;
print(cen," ",mpab," ",mpab[1]," ",order_status ," ", order_no);
问题出在实盘模拟的时候,打印了++++buy++++,但是图上面没有标定买入信号,mpab的值没有变化,电脑右下也没有弹出发出委托的气泡,请问这是哪里除了问题?
打印结果如下:
13.10 101400.00 101400.00 0.00 1.00
13.10 101400.00 101400.00 0.00 1.00
++++buy++++
13.10 101400.00 101400.00 1.00 2.00
13.10 101400.00 101400.00 1.00 2.00
13.10 101400.00 101400.00 1.00 2.00(来自旧论坛客户,thgink)
-
MC回复讨论一:
您的代码中有一个bug,您应该在开启自动交易时手动输入了初始仓位,这个也在您的初始参数order_no初始值为1中看出来了,我将在下面点出您的问题,假设都已经开启自动交易了,并且手动输入了101400股:
第一、初始输入会在图表上标记"Initial Entry",所以按照您代码中的方式,您一直卖不出去,因为您是from entry(text(order_no)),而不是from entry("Initial Entry")。
第二、如果价格在开启自动交易之后下降然后执行市价买入命令,我猜测您因为使用默认允许同一方向一笔进场,如果是这样的话,因为图表已经有初始多头进场了,后续即使满足进场条件也不会再多头进场。
第三、基于上面两点,您肯定不能卖出股票也不能买入股票;另外,由于代码“if MarketPosition_at_Broker > 0 then
mpab = MarketPosition_at_Broker;”的存在,所以一旦mpab不为0,那么导致后续mpab一直不为0
第四、另外,问一下,您使用是SA模式还是AA模式,使用的是真回报还是假回报?
-
MC回复讨论二:
SA,真回报
设置的是同一方向允许6500笔进场
这里的逻辑是昨日收盘,受伤已经有很多股票才会执行程序,如果没有股票则什么也不做
那个bug我也直到,所以真是的使用的是这个
if condition4 then
begin
if order_no > 1 then
begin
sell 200 shares from entry(text(order_no)) this bar on close;
order_no = order_no -1;
end
else
sell 200 shares from entry("Initial Entry") this bar on close;
print("----sell----");
order_status = 1;
end;
这两个似乎都不是问题的关键,问题的关键是MarketPosition_at_Broker不行,换成marketposition*currentshares初步看来就可以了,我本来的目标是想通过实时的获取券商处的股票数,以判定实际上是否发生成交,可是加载策略的时候会把历史数据计算一次,这会导致一些问题,但是也不是关键,不是历史数据导致的问题就是发不出单
我想请教:具体得知成交了用那种方式最靠谱?我指的相对靠谱是说确实成交了,比如挂单200股卖出,我确实卖了200股
其实,只有一个方式才是稳妥的,就是能否根据时刻获取持股数,比如在t时刻触发条件,做出了买或卖,这时候将t时刻的持股数记录下来,在对比t之后每个时刻的持股数,如果有变动,变动在预期值,这说明按要求发单且成交了。我这个方案的问题在于逻辑上要求得到瞬时变化,这要求我有点高了,可能也与multicharts的每个bar运行一次的逻辑有冲突。还像请教您一下,能否获得比如一小时前的持仓数?虽然现在并不需要这么做了。
-
MC回复讨论三:
后来改成这样了:
[SameExitFromOneEntryOnce = false];
vars:cen(13.40), tims(0),cur(0), up(0.03),down(0.03),cur_r(0),size(200),order_status(0),order_no(0),var1(0);
var1 = 0;
if 0 >= marketposition_at_broker then buy ("Initial Entry") 10000 shares this bar on close;
cur = currentshares;
if marketposition_at_broker> 0 then
begin
if order_status = 1 and var1 = 0 then
begin
var1 = 1;
print("order_status = 1 and var1 = 0 ",cur_r," ",cur," ",time_s);
if cur_r - cur >= size then
begin
print("condition3:",cur_r," ",cur," ",time_s);
order_status = 0;
cen = cen + up;
end;
if cur - cur_r >= size then
begin
print("condition4:",cur_r," ",cur," ",time_s);
order_status = 0;
cen = cen - down;
end;
end;
if close - cen >= up and order_status = 0 and var1 = 0 then
begin
print("condition1 and order_status = 0 and var1 = 0"," ",time_s);
tims = time_s;
if order_no >= 1 then
begin
sell size shares from entry(text(order_no)) this bar on close;
order_no = order_no - 1;
end
else
begin
sell size shares from entry("Initial Entry") this bar on close;
order_no = 0;
end;
print("----"," ",time_s);
order_status = 1;
var1 = 1;
end;
if cen - close >= down and order_status = 0 and var1 = 0 then
begin
print("condition2 and order_status = 0 and var1 = 0"," ",time_s);
tims = time_s;
order_no = order_no + 1;
buy (text(order_no)) size shares this bar on close;
print("++++"," ",time_s);
order_status = 1;
var1 =1;
end;
if time_s = tims then
begin
print("time_s = tims "," ",time_s);
cur_r= currentshares;
end;
end;
print(currentbar," ",var1, " ",tims," ",order_status," ",order_no," ",close," ",cen," ",cur," ",cur_r," ",time_s);
应该没问题了。
多谢指导!
-
MC回复讨论四:
后来改成这样了:
[SameExitFromOneEntryOnce = false];
vars:cen(13.40), tims(0),cur(0), up(0.03),down(0.03),cur_r(0),size(200),order_status(0),order_no(0),var1(0);
var1 = 0;
if 0 >= marketposition_at_broker then buy ("Initial Entry") 10000 shares this bar on close;
cur = currentshares;
if marketposition_at_broker> 0 then
begin
if order_status = 1 and var1 = 0 then
begin
var1 = 1;
print("order_status = 1 and var1 = 0 ",cur_r," ",cur," ",time_s);
if cur_r - cur >= size then
begin
print("condition3:",cur_r," ",cur," ",time_s);
order_status = 0;
cen = cen + up;
end;
if cur - cur_r >= size then
begin
print("condition4:",cur_r," ",cur," ",time_s);
order_status = 0;
cen = cen - down;
end;
end;
if close - cen >= up and order_status = 0 and var1 = 0 then
begin
print("condition1 and order_status = 0 and var1 = 0"," ",time_s);
tims = time_s;
if order_no >= 1 then
begin
sell size shares from entry(text(order_no)) this bar on close;
order_no = order_no - 1;
end
else
begin
sell size shares from entry("Initial Entry") this bar on close;
order_no = 0;
end;
print("----"," ",time_s);
order_status = 1;
var1 = 1;
end;
if cen - close >= down and order_status = 0 and var1 = 0 then
begin
print("condition2 and order_status = 0 and var1 = 0"," ",time_s);
tims = time_s;
order_no = order_no + 1;
buy (text(order_no)) size shares this bar on close;
print("++++"," ",time_s);
order_status = 1;
var1 =1;
end;
if time_s = tims then
begin
print("time_s = tims "," ",time_s);
cur_r= currentshares;
end;
end;
print(currentbar," ",var1, " ",tims," ",order_status," ",order_no," ",close," ",cen," ",cur," ",cur_r," ",time_s);
应该没问题了。
多谢指导!
有思路,想编写各种指标公式,程序化交易模型,选股公式,预警公式的朋友
可联系技术人员 QQ: 511411198 进行 有偿 编写!(不贵!点击查看价格!)
相关文章
-
没有相关内容