 | |  | | MT3.8和MT4系统止损设定后不随价格变化而变动除非手工再次修改,当市场发生反转时,获利往往难以兑现。如以1.2000做多E/U,止损为20点即1.1980止损,当上涨到1.2050时市场发生反转,如果不设止盈,服务器将在1.1980平仓,亏20点。如果设止盈,这个点又不太好确定。
我写了个小程序,希望对大家有用。如以1.2000做多E/U,止损为20点(可以修改)即1.1980止损,当价格涨到1.2001是,止损设定也改变为1.1981,当价格跃到1.1999是,止损设定仍为1.1980。当为1.2050时市场发生反转,止损设定值为1.1930,平仓获利30点。总之,止损值只向盈利方向移动。
//MT4下程序
#property copyright "RMBUSD"
#property link "http://www.metaquotes.net"
//---- input parameters
int DEFPOINT;
double last_price;
double stop_loss;
double current_price,open_price;
int order_type,ticket;
bool ok;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
ok=false;
ticket=1166615;//在这里修改Order ID
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{
DEFPOINT=20+Ask-Bid;//在这里修改止损点数
order_type=OrderType();
open_price=OrderOpenPrice();
current_price=MarketInfo(OrderSymbol(),9+order_type);
last_price=current_price;
switch(order_type)
{
case OP_BUY:stop_loss=open_price-DEFPOINT*Point;break;
case OP_SELL:stop_loss=open_price+DEFPOINT*Point;break;
default:return(0);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if(ok==true) return(0);
if(OrderSelect(ticket,SELECT_BY_TICKET)==true)
{
switch(order_type)
{
case OP_BUY:buy();break;
case OP_SELL:sell();break;
default:return(0);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
int buy()
{
current_price=MarketInfo(OrderSymbol(),MODE_ASK);
Print("目前的止损在: ",stop_loss-Ask+Bid);
Print("last_price is ",last_price-Ask+Bid);
Print("current_price is ",current_price-Ask+Bid);
Print("===================================" ;
if(current_price<=stop_loss)
{
OrderClose(ticket,OrderLots(),Bid,1);
ok=true;
Alert("Order is Closed !!! " ;
//Exit;
}
else if(current_price-last_price>0&¤t_price-DEFPOINT*Point>stop_loss)
{
stop_loss=current_price-DEFPOINT*Point;
}
last_price=current_price;
return(0);
}
int sell()
{
current_price=MarketInfo(OrderSymbol(),MODE_BID);
Print("目前的止损在: ",stop_loss);
Print("last_price is ",last_price);
Print("current_price is ",current_price);
Print("=======================================" ;
if(current_price>=stop_loss)
{
OrderClose(ticket,OrderLots(),Ask,1);
ok=true;
Alert("Order is Closed !!! " ;
}
else if(current_price-last_price<0&¤t_price+DEFPOINT*Point<stop_loss)
{
stop_loss=current_price+DEFPOINT*Point;
}
last_price=current_price;
return(0);
}
++++++++++++++++++++++++++++++++
//MT3.8程序
Variable:current_price(0);
Variable:ticket(2753606);
Variable rder_total(0);
order_total=TotalTrades;
Variable:i(0);
Define: DEFPOINT(20);
Variable:stop_loss(0),sl(0);
for i=1 to order_total
Begin
if OrderValue(i,VAL_TICKET)=ticket then
Begin
if OrderValue(i,VAL_TYPE)=OP_SELL then
Begin
current_price=PriceAsk;
sl=current_price+DEFPOINT*Point;
End;
if OrderValue(i,VAL_TYPE)=OP_BUY then
Begin
current_price=PriceBid;
sl=current_price-DEFPOINT*Point;
End;
end;
end;
Variable:last_price(0);
for i=1 to order_total
Begin
if stop_loss==0 then stop_loss=sl;
if last_price==0 then last_price=current_price;
if OrderValue(i,VAL_TICKET)=ticket then
Begin
if OrderValue(i,VAL_TYPE)=OP_SELL then
Begin
if current_price>=stop_loss then
Begin
CloseOrder(ticket,lots,ask,2,red);
Alert("The Order is Closed!!!! " ;
Break;
end;
if current_price-last_price<0 and current_price+DEFPOINT*Point<stop_loss then
Begin
stop_loss=current_price+DEFPOINT*Point;
end;
end;
if OrderValue(i,VAL_TYPE)=OP_BUY then
Begin
if current_price<=stop_loss then
Begin
CloseOrder(ticket,lots,bid,2,red);
Alert("The Order is Closed !!!! " ;
break;
end;
if current_price-last_price>0 and current_price-DEFPOINT*Point>stop_loss then
Begin
stop_loss=current_price-DEFPOINT*Point;
end;
end;
print("current price is: ",current_price," last price is ",last_price);
print("目前的止损为: ",stop_loss);
print("===========================" ;
last_price=current_price;
end;
end;
//+------------------------------------------------------- |  |  |  |  |
|