最后由 ken138888 于 2023-10-24 14:59 编辑
需求:当价格到达设定的区间时,开始修改止和止损价格。
问题:用MT5的“1分钟OHLC”模式回测时,一切正常,但用“每个点基于实时点”回测时,日志就显示4756错误代码,如下图显示:
TP2
TP1
SL2
SL1
代码如下: void yidong(int tppoint_1,int tppoint_2,string symbol,ENUM_POSITION_TYPE type,int magic,string com) { // tppoint_1=400 // tppoint_2=500
int t=PositionsTotal(); for(int i=t-1;i>=0;i--) { if(PositionGetTicket(i)>0) { if(PositionGetString(POSITION_SYMBOL)==symbol) { double bid=SymbolInfoDouble(symbol,SYMBOL_BID); double ask=SymbolInfoDouble(symbol,SYMBOL_ASK); long dig=SymbolInfoInteger(symbol,SYMBOL_DIGITS); double pot=SymbolInfoDouble(symbol,SYMBOL_POINT); double op=PositionGetDouble(POSITION_PRICE_OPEN); double sl= PositionGetDouble(POSITION_SL); double tp=PositionGetDouble(POSITION_TP);
if(type==POSITION_TYPE_BUY && magic == PositionGetInteger(POSITION_MAGIC) && com == PositionGetString(POSITION_COMMENT) ) {
if(bid-op>=pot*tppoint_1 && bid-op<pot*tppoint_2 && sl<op+tppoint_1*pot )
{ MqlTradeRequest request={}; MqlTradeResult result={}; request.action=TRADE_ACTION_SLTP; request.position=PositionGetTicket(i); request.symbol=symbol; request.sl=op+tppoint_1*pot; request.tp=op+tppoint_2*pot; request.comment=com; request.magic=magic; if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); }
}
if(type==POSITION_TYPE_SELL && magic == PositionGetInteger(POSITION_MAGIC) && com == PositionGetString(POSITION_COMMENT) ) {
if( op-ask>=pot*tppoint_1 && op-ask<tppoint_2*pot && op-tppoint_1*pot<sl )
{
MqlTradeRequest request={}; MqlTradeResult result={}; request.action=TRADE_ACTION_SLTP; request.position=PositionGetTicket(i); request.symbol=symbol; request.sl=op-tppoint_1*pot ; request.tp=op-tppoint_2*pot ; request.comment=com; request.magic=magic; if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); }
} } } } } |