//跟踪止损--修改止盈止损
void 跟踪止损(double sl)
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
if(PositionGetTicket(i)>0 || PositionGetSymbol(i)!="")//如果有订单
{
//如果多单。
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
MqlTradeRequest request={};
MqlTradeResult result={};
double 初始化止损 = PositionGetDouble(POSITION_SL);
request.action=TRADE_ACTION_SLTP;//用于修改订单
request.position=PositionGetInteger(POSITION_TICKET);
request.symbol=PositionGetString(POSITION_SYMBOL);
request.price=SymbolInfoDouble(m_symbol.Name(),SYMBOL_BID);
if(request.price>=初始化止损+sl)
{
request.sl= request.price - sl;
}
else
{
request.sl= 初始化止损;
}
//request.type=ORDER_TYPE_BUY;
bool X=OrderSend(request,result);
if(X==false)
{
Print("修改多单止损返回代码:" + IntegerToString(result.retcode));
}
}
//如果空单。
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
MqlTradeRequest request={};
MqlTradeResult result={};
double 初始化止损 = PositionGetDouble(POSITION_SL);
request.action=TRADE_ACTION_SLTP;//用于修改订单
request.position=PositionGetInteger(POSITION_TICKET);
request.symbol=PositionGetString(POSITION_SYMBOL);
request.price=SymbolInfoDouble(m_symbol.Name(),SYMBOL_BID);
if(初始化止损==0){
request.sl=PositionGetDouble(POSITION_PRICE_OPEN)+MathAbs(sl-PositionGetDouble(POSITION_PRICE_OPEN));
}
else if(request.price<=初始化止损-sl)
{
request.sl=request.price + sl;
}
else
{
request.sl=初始化止损;
}
//request.type=ORDER_TYPE_SELL;
bool X=OrderSend(request,result);
if(X==false)
{
Print("修改空单止损返回代码:" + IntegerToString(result.retcode));
}
}
}
}
}
购买
已有0人购买, 当前内容需向作者支付 2 H币 才能浏览
|