marketposition问题

Params //此处添加参数 Numeric k1(0.3); //不对称区间参数 Numeric k2(0.2); //不对称区间参数 Numeric ExitOnCloseMine(14.58); //清仓时间 Numeric N(3); Numeric lots(1); Numeric danger(-1750); Numeric profit(2150); Numeric thred_short(500); Numeric thred_long(500); //Numeric ni_target(500); //Numeric shun_target(500); Numeric up_count(1); //Numeric down_count(2); Numeric color_aver(86450); Numeric color_nolong(3000); Numeric color_short(4000); Numeric today_stop(-2000); Vars //此处添加变量 Series<Numeric> upband; //通道上轨变量 Series<Numeric> dnband; //通道下轨变量 Global Numeric HH; Global Numeric HC; Global Numeric LC; Global Numeric LL; Global Numeric range1; Global Numeric longPosition(0); Global Numeric shortPosition(0); Series<Numeric> LongMA; //长周期均线变量 Global Numeric sumVolumeWeightedPrice (0); Global Numeric sumVolume(0); Global Numeric sign_ni(0); Global Numeric p_stop(0); Global Numeric total_loss(0); Global Numeric high_profit(0); Global Numeric close_position(0); Global Numeric need_plus(0); Global Numeric short_build(0); Global Numeric long_build(0); Global Numeric count_close(0); Global Numeric Open_bar(0); Global Numeric market_count(0); Global Numeric kui_count(0); DefsEvents //此处实现事件函数 //初始化事件函数,策略运行期间,首先运行且只有一次,应用在订阅数据等操作 OnInit() { } OnBarClose(ArrayRef<Integer> indexs) { // 累加加权价格和成交量 sumVolumeWeightedPrice = sumVolumeWeightedPrice + close * vol; sumVolume = sumVolume + vol; // 计算 VWMA if (sumVolume > 0) LongMA = sumVolumeWeightedPrice / sumVolume; // 计算移动平均线 PlotNumeric(MA, LongMA); If( Time >= ExitOnCloseMine / 100) { Sell(0,Close); BuyToCover(0,Close); longPosition = 0; shortPosition = 0; } If( data1.Close < LongMA) { count_close = count_close + 1; } Else If(data1.Close > LongMA) { count_close = 0; } //止损 If(MarketPosition == -1) { If((short_build - Close[1])*lots < danger) { total_loss = total_loss + (short_build - Close[1])*lots; BuyToCover(shortposition , Close); shortposition = 0; kui_count = kui_count + 1; If(sign_ni == 0) { sign_ni = 1; } } } Else If(MarketPosition == 1) { If((Close[1] - long_build)*lots < danger) { total_loss = total_loss + (Close[1] - long_build)*lots; Sell(longposition , Close); longposition = 0; kui_count = kui_count + 1; If(sign_ni == 0) { sign_ni = 1; } } } //有色网均价比较 //若持多仓,并且建仓价格在有色网均价之上,并且当前在亏损,则等到价格降到有色网均价之下时止损 If(Time < ExitOnCloseMine / 100 && time > 0.09 ){ If( MarketPosition == 1 ) { If((Close < color_aver) && (Close[1] - long_build) < 0 && long_build > color_aver && market_count == 1) { Sell(longPosition,Close); longPosition = 0; total_loss = total_loss + (Close[1] - long_build) * lots; kui_count = kui_count + 1; If(sign_ni == 0) { sign_ni = 1; } } } //若持空仓,并且建仓价格在有色网均价之下,并且当前在亏损,则等到价格涨到有色网均价之上时止损 If( MarketPosition == -1) { If(short_build < color_aver && (short_build - Close[1]) < 0 && Close > color_aver && market_count == 1) { BuyToCover(shortPosition,Close); shortPosition = 0; total_loss = total_loss + (short_build - Close[1]) * lots; kui_count = kui_count + 1; If(sign_ni == 0) { sign_ni = 1; } } } } //若当天亏损到达2000,则当天停止交易 If(kui_count == 2) { p_stop = 1; } //止盈 If(MarketPosition == -1 && (short_build - Close[1])*lots >= high_profit) { high_profit = (short_build - Close[1])*lots; close_position = high_profit * 0.6; Commentary(Text(close_position)); } Else If(MarketPosition == 1 && (Close[1] - long_build)*lots >= high_profit) { high_profit = (Close[1] - long_build)*lots; close_position = high_profit * 0.6; Commentary(Text(close_position)); } /* If(PositionProfit >= high_profit) { high_profit = PositionProfit; close_position = high_profit * 0.6; } */ //Commentary(long_build: + Text((short_build - Close[1])*lots)); Commentary(close_position: + Text(close_position)); If(MarketPosition == -1) { If((short_build - Close[1])*lots < close_position && close_position > 0 && (short_build - Close[1]) > 0 ) { If((short_build - Close[1])*lots < 2400) { //Commentary(进来前(short_build - Close[1])*lots: + Text((short_build - Close[1])*lots)); //Commentary(进来前close_position: + Text(close_position)); BuyToCover(shortposition , Close); shortposition = 0; high_profit = profit; If(sign_ni == 1) { need_plus = 1; } Else If(Open_bar > 6000 && sign_ni == 0) { sign_ni = 1; } } Else If((short_build - Close[1])*lots >= 2400) { //Commentary(修改(short_build - Close[1])*lots: + Text((short_build - Close[1])*lots)); //Commentary(修改close_position: + Text(close_position)); close_position = high_profit * 0.4; Commentary(Text(close_position)); } //Commentary(出来后(short_build - Close[1])*lots: + Text((short_build - Close[1])*lots)); //Commentary(出来后close_position: + Text(close_position)); } } Else If(MarketPosition == 1) { If((Close[1] - long_build)*lots < close_position && close_position > 0 && (Close[1] - long_build) > 0) { If((Close[1] - long_build)*lots < 2400) { //Commentary(进入前(Close[1] - long_build)*lots: + Text((Close[1] - long_build)*lots)); //Commentary(进入前close_position: + Text(close_position)); Sell(longposition , Close); longposition = 0; high_profit = profit; If(sign_ni == 1) { need_plus = 1; } } Else If((Close[1] - long_build)*lots >= 2400) { //Commentary(修改(Close[1] - long_build)*lots: + Text((Close[1] - long_build)*lots)); //Commentary(修改close_position: + Text(close_position)); close_position = high_profit * 0.4; Commentary(Text(close_position)); } //Commentary(出去后(Close[1] - long_build)*lots: + Text((Close[1] - long_build)*lots)); // Commentary(出去后close_position: + Text(close_position)); } } Commentary(Close MarketPosition:+ Text(MarketPosition)); } OnBarOpen(ArrayRef<Integer> indexs) { Commentary(open MarketPosition:+ Text(MarketPosition)); If(p_stop == 0) { //逆势 If(Time < ExitOnCloseMine / 100 && time > 0.0905 && sign_ni == 0 ) { //开空仓 if ((Close[1] > (LongMA + thred_short) && shortPosition == 0 && longPosition == 0) Or (Close[1] > (color_aver + color_short) && shortPosition == 0 && longPosition == 0) Or (count_close >= 6000 && shortPosition == 0 && longPosition == 0)) { SellShort(lots, Open); short_build = Open; shortPosition = shortPosition + lots; Open_bar = count_close; close_position = 0; market_count = market_count + 1; } //开多仓 else if (Close[1] < (LongMA - thred_long) && longPosition == 0 && shortPosition == 0 && Close[1] < (color_aver + color_nolong) && count_close <5000) { Buy(lots, Open); long_build = Open; longPosition = longPosition + lots; close_position = 0; market_count = market_count + 1; } } //顺势 Else If(Time < ExitOnCloseMine / 100 && Time > 0.09 && sign_ni == 1 && MarketPosition == 0) { If(need_plus == 0) { If(Close[1] > LongMA && Close[1] > upband ) { Commentary(MarketPosition:+ Text(MarketPosition)); Buy(lots , Open); //Commentary(buy MarketPosition:+ Text(MarketPosition)); //Commentary(顺势开多单); long_build = Open; longPosition = longPosition + lots; market_count = market_count + 1; } Else If(Close[1] < LongMA && Close[1] < dnband ) { SellShort(lots , Open); Commentary(顺势开空单); short_build = Open; shortPosition = shortPosition + lots; market_count = market_count + 1; } } Else If(need_plus == 1) { If( Close[1] > (color_aver + color_nolong)) { sign_ni = 0; } If(Close[1] < (color_aver + color_short)) { sign_ni = 0; } } } } } OnBar(ArrayRef<Integer> indexs) { if (TrueDate(0) <> TrueDate(1)) { // 重置累加器 sumVolumeWeightedPrice = 0.0; sumVolume = 0.0; sign_ni = 0; p_stop = 0; total_loss = 0; high_profit = profit; close_position =0; need_plus = 0; count_close = 0; market_count = 0; kui_count = 0; //short_build = 0; //long_build = 0; } HH=data1.HighD(N); //N日前的高点 LL=data1.LowD(N); //N日前的低点 HC = data1.CloseD(N); LC = data1.OpenD(N); range1 = Max(HH - LC, HC - LL ); upband = data1.OpenD(0) + range1 * k1; dnband = data1.OpenD(0) - range1 * k2; data0.PlotNumeric(upband:, upband); data0.PlotNumeric(dnband:, dnband); Commentary(当前盈利: + Text(PositionProfit)); Commentary(总亏损: + Text(total_loss)); Commentary(sign_ni: + Text(sign_ni)); Commentary(need_plus: + Text(need_plus)); Commentary( bar MarketPosition: + Text(MarketPosition)); //Commentary(低于均线的bar数: + Text(count_close)); //Commentary(long_build: + Text(long_build)); //Commentary(short_build: + Text(short_build)); //Commentary(多亏损: + Text((Close[1] - long_build)*lots)); //Commentary(空亏损: + Text((short_build - Close[1])*lots)); } //当前策略退出时触发 OnExit() { }我在tick数据中用marketposition作为条件判断是否入场,上图在平仓后,满足入场条件,立刻开多单,但是实盘中没有报单,我查看了onbaropen,onbarclose和onbar中的marketposition的值,同一根tick数据上的不一样,是否是这个问题,问题主要出在标红字段。

您好,代码很长,逻辑要搞清楚很费时间,对解决问题可能又没有什么直接帮助,但不看懂逻辑,直接看您说的标为顺势的代码部分,也很难看出问题,这是个矛盾。我只能针对您说的问题回复下,OnBarOpen 、OnBar、OnBarClose不管实际顺序怎么写,用到的MarketPosition及其它变量,实际的顺序就是OnBarOpen->OnBar->OnBarClose。我做了个简化的例子测试,用tick数据运行策略,发单一切正常,MarketPosition在OnBarOpen、OnBar、OnBarClose中的值也完全正常。至于您自己的代码为什么不正常可能还得您自己再去耐心调试了

问题主要出在标注为顺势的代码部分,请老师看看问题出在哪