1. 程序开仓大概率会报信号闪烁,明明已经在开仓指令里用了常量,是哪里出了问题呢?
2. 模拟的时候大部分时候不会运行平仓指令(一个平仓指令都不会下,但有时候又正常),在相应工作区打开K线图开平仓是正常的没有问题,平仓指令对应的条件用commentary在图上核对也都应该触发了,不知何故。
3. 偶尔在没有任何仓位的情况会出现平仓自动,这个是不是因为开始运行的时候读取了当前交易时间段之前的数据,而又触发了平仓条件呢?但是明明有marketposition限制,空仓为什么会下平仓指令呢?
Params
Numeric tp(0);
Numeric sl(0);
Numeric mintoout(0);
Numeric atrzhouqi(14);
Numeric atrbeishu(1);
Vars
Global Integer N;
Global Integer M;
series <Numeric> sl_price;
series <Numeric> tp_price;
Series <Integer> j_close;
Series <Integer> comp;
Global Bool jiu_uptodown(false);
Global Bool jiu_downtoup(false);
Series <Numeric> openshort_atr;
Series <Numeric> MP;
OnBarOpen(ArrayRef<Integer> indexs)
{
Commentary(onbaropen N = +text(N));
Commentary(onbaropen M = +text(M));
Commentary(仓位 = +text(MarketPosition));
Range[1:1]
{
Commentary(text(CurrentBar));
}
Range[2:2]
{
Commentary(CurrentBar = +text(CurrentBar));
Integer i(1);
Integer k(1);
j_close = Close;
If(j_close[4] <> 0)
{
if(J_close > j_close[4]) comp = 1;
Else if(j_close < j_close[4]) comp = -1;
Else if(j_close == j_close[4]) comp = 0;
}
While(CurrentBar - N > 8 and comp[i]==comp[i+1] And comp[1]==1) {i=i+1;}
If(i==8)
{
N = CurrentBar;
Integer j =1;
For j=1 to 8
PlotString(上升九转,text(j),H[9-j]+10*MinMove*PriceScale,Cyan,9-j);
PlayWavSound(D:\\\\notify.wav);
alert(symbolName+出现九转上升转下跌信号(第八根)↓↓↓+Text(CurrentBar));
jiu_uptodown = true;
}
If(CurrentBar == N+1 And comp[1] == 1)
{
PlotString(上升九转,text(9),H[1]+10*MinMove*PriceScale,Cyan,1);
}
While(CurrentBar - M > 8 and comp[k]==comp[k+1] And comp[1]==-1) {k=k+1;}
If(k==8)
{
M = CurrentBar;
Integer j =1;
For j=1 to 8
PlotString(下跌九转,text(j),L[9-j]-5*MinMove*PriceScale,Magenta,9-j);
PlayWavSound(D:\\\\notify.wav);
alert(symbolName+出现九转下跌转上升信号(第八根)↑↑↑+Text(CurrentBar));
jiu_downtoup = true;
}
If(CurrentBar == M+1 And comp[1] == -1)
{
PlotString(下跌九转,text(9),L[1]-5*MinMove*PriceScale,Magenta,1);
}
Commentary(N = +text(N));
Commentary(M = +text(M));
}
}
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
MP = MarketPosition;
Numeric ii;
Numeric result(1);
for ii = 0 to DataSourceSize - 1
{
result = result * data[ii].BarExistStatus;
}
if (result <> 1) Return;
If (data2.CurrentBar == N)
{
If (MP == 0 And jiu_uptodown == true)
{
SellShort(1,Open - 2 * MinMove * PriceScale);
jiu_uptodown = false;
if (sl == 0) sl_price = max(Data2.High[1],data2.High[2]) + intpart(atrbeishu * data2.AvgTrueRange(atrzhouqi));
Else sl_price = EntryPrice + sl;
If (tp == 0) tp_price = entryprice - intpart(atrbeishu * data2.AvgTrueRange(atrzhouqi));
Else tp_price = EntryPrice - tp;
Commentary(止损价格设置 = + text(sl_price));
Commentary(止赢价格设置 = + text(tp_price));
}
}
If(MP == -1)
{
Commentary(MP = + text(MP) + data2.CurrentBar = + text(data2.CurrentBar)+N = +Text(N));
if(data2.CurrentBar >N)
{
if(High>sl_price)
{
BuyToCover(1,High + 2 * MinMove * PriceScale);
Commentary(已止损);
}
If(Data1.High>data1.High[1] Or Low < tp_price)
{
BuyToCover(1,High + 2 * MinMove * PriceScale);
commentary(已止盈);
}
}
}
If (data2.CurrentBar == M)
{
If (MP == 0 And jiu_downtoup == true)
{
Buy(1,open + 2 * MinMove * PriceScale);
jiu_downtoup = false;
if (sl == 0) sl_price = Min(Data2.Low[1],data2.Low[2]) - intpart(atrbeishu * data2.AvgTrueRange(atrzhouqi));
Else sl_price = EntryPrice - sl;
If (tp == 0) tp_price = entryprice + intpart(atrbeishu * data2.AvgTrueRange(atrzhouqi));
Else tp_price = EntryPrice + tp;
Commentary(止损价格设置 = + text(sl_price));
Commentary(止赢价格设置 = + text(tp_price));
}
//If (data1.Close>data1.close[1])
}
If (MP == 1)
{
If(data2.CurrentBar>M)
{
Commentary(MP = + text(MP)+data2.CurrentBar = +text(data2.CurrentBar)+M = +Text(M));
if(Low<sl_price)
{
Sell(1,Low - 2 * MinMove * PriceScale);
Commentary(已止损);
}
If(Data1.Low<data1.low[1] || High>tp_price)
{
Sell(1,Low - 2 * MinMove * PriceScale);
commentary(已止盈);
}
}
}
}
自学的没法子,群也加不进去,看了几十个视频还没看完,自己先练练手嘛!谢谢老师的批评,我看海龟也是用的marketposition,下了些别人的策略也都用这个判定有没有仓,第一次听说这个是图表虚拟持仓啊,那该用啥判断仓位呢?我这个global bool变量的判断是放在5分图的onbaropen里,那会出什么问题呢,只运行一次呀。您的意思是应该用序列布尔变量嘛?
https://www.tbquant.net/TrainDetail?id=371
零基础第六课就应该已经介绍过信号虚拟账户的概念了
还有updown那个bool变量居然是global类型......
建议你开发模型前多看看软件里的代码示例或者零基础视频里的开发流程。
驾校没毕业就开车上路,槽点太多,无从下手
我用a函数重新写了,一开自动交易,就冒出来无数个平仓自动是干啥的呢?
你能写出来这么多行代码,却不知道marketposition代表的是图表虚拟信号持仓,不是真实持仓吗?
基础没打好,代码写得多反而更麻烦