RMBUSD 发表于 2006-3-15 21:00

移动止损

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&&current_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&&current_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:order_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;

//+-------------------------------------------------------

蓝胡子 发表于 2006-3-16 00:05

厉害,的确是个好东西,可惜不会用。:(:(

hottest 发表于 2006-3-16 04:47

RMBUSD, 您的程序内为何有笑面公仔?
是不是出了什么错?

谢谢您无私的分享!

RMBUSD 发表于 2006-3-16 09:37

论坛上表情符号在作怪,竖大母指应有“)”,张大嘴:o应为“:o”,郁闷表情:(为":("

打开MetaEditor新建一个Expert Advisor取名为XXX,删去编辑中的所有内容,贴上去,保存-编译-打开MT4-导航栏-智能交易-双击XXX-允许自动交易-OK-MT4工具栏上的“智能交易“按钮要点下。价格每变动一下可以在MT4终端的智能交易标签下看到止损的变化
注意:不要忘记修改你开的头寸的编号与你要设的止损点。
      该止损设置只在客户机上运行,不上传服务器,因此关机无效!
两个程序分别在MT4 forex ltd和fxdd LLC 3.86上调试通过

[ 本帖最后由 RMBUSD 于 2006-3-16 09:48 编辑 ]

hottest 发表于 2006-3-16 11:37

原帖由 RMBUSD 于 2006-3-16 09:37 发表
论坛上表情符号在作怪,竖大母指应有“)”,张大嘴:o应为“:o”,郁闷表情:(为":("

打开MetaEditor新建一个Expert Advisor取名为XXX,删去编辑中的所有内容,贴上去,保存-编译-打开MT4-导 ...

;) 是 ;)
在发帖时, 您可选取 "禁用 Smilies" (编辑您的帖便可)
看看可否消灭作怪的Smilies!

[ 本帖最后由 hottest 于 2006-3-16 11:38 编辑 ]

happy88888 发表于 2006-3-16 18:49

谢谢您无私

RMBUSD 发表于 2006-3-16 21:58

呵呵,有财大家发。
人算不如"鸡"算,超短线还是机算好
虽不懂什么波浪什么浑沌,利用自已写的一些小程序还总能获些蝇头小利。
希望多向汇市前辈学习各种理论,放长线钓大鱼。

偶然帅 发表于 2006-7-17 11:15

MT4可以移动止损的,只是不能在下单时设置。下单以后,在订单上按鼠标右键,选择“追踪止损”,就可以设置移动止损的点数了。

genho 发表于 2006-7-18 15:28

谢谢!!!

heitaoj 发表于 2006-8-24 21:27

一直怀疑MT4的移动止损问题,没想到楼主也有同感,不知道其他平台是否也也样的问题,比如GTS....
页: [1] 2
查看完整版本: 移动止损