0

我想创建一个止损订单,如果这是一个卖出订单,它将放置在前一个订单启动柱的高点之上,或者如果这是一个买入订单,它会放置在前一个订单启动柱的低点之下。

这是一张图片来说明问题(该示例描述了一个单案例):

在此处输入图像描述

知道怎么做吗?如果我使用固定的止损,下面的代码可以正常工作。如果我用基于高或低的变量替换止损,则不会触发任何订单。

这是我的代码:

//| Expert initialization function                                   |
//+------------------------------------------------------------------+

/* -----------------------------------------------------------------------------
   KINDLY RESPECT THIS & DO NOT MODIFY THE EDITS AGAIN

   MQL4 FORMAT IS NOT INDENTATION SENSITIVE,
   HAS IDE-HIGHLIGHTING
   AND
   HAS NO OTHER RESTRICTIVE CONDITIONS ----------- THIS CODING-STYLE HELPS A LOT
                                                   FOR BOTH
                                                   EASY & FAST
                                                   TRACKING OF NON-SYNTACTIC ERRORS
                                                   AND
                                                   IMPROVES FAST ORIENTATION
                                                   IN ALGORITHM CONSTRUCTORS' MODs
                                                   DURING RAPID PROTOTYPING
   IF YOU CANNOT RESIST,
   SOLVE RATHER ANY OTHER PROBLEM,
   THAT MAY HELP SOMEONE ELSE's POST, THX
------------------------------------------- KINDLY RESPECT
                                            THE AIM OF StackOverflow
------------------------------------------- TO HELP OTHERS DEVELOP UNDERSTANDING,
                                                           THEIRS  UNDERSTANDING, OK?     */
extern int     StartHour      = 14; 
extern int     TakeProfit     = 70;
extern int     StopLoss       = 40;
extern double  Lots           =  0.01;
extern int     MA_period      = 20;
extern int     MA_period_1    = 45;
extern int     RSI_period14   = 14;
extern int     RSI_period12   = 12;

void OnTick() {
   static bool    IsFirstTick    = true;
   static int     ticket         = 0;
          double  R_MA           = iMA(  Symbol(), Period(), MA_period,    0, 0, 0, 1 );
          double  R_MA_Fast      = iMA(  Symbol(), Period(), MA_period_1,  0, 0, 0, 1 );
          double  R_RSI14        = iRSI( Symbol(), Period(), RSI_period14, 0, 0 );
          double  R_RSI12        = iRSI( Symbol(), Period(), RSI_period12, 0, 0 );
          double  HH             = High[1]; 
          double  LL             = Low[ 1];


   if (  Hour() == StartHour ) {
         if (  IsFirstTick == true ) {
               IsFirstTick  = false;

               bool  res1  = OrderSelect( ticket, SELECT_BY_TICKET );
               if (  res1 == true ) {
                     if (  OrderCloseTime() == 0 ) {
                           bool  res2  = OrderClose( ticket, Lots, OrderClosePrice(), 10 );
                           if (  res2 == false ) {
                                 Alert( "Error closing order # ", ticket );
                              }
                         }
                   }
               if (  High[1]    <  R_MA
                  && R_RSI12    >  R_RSI14
                  && R_MA_Fast  >= R_MA
                  ){
                     ticket = OrderSend( Symbol(),
                                         OP_BUY,
                                         Lots,
                                         Ask,
                                         10,
                                         Bid - LL         * Point * 10,
                                         Bid + TakeProfit * Point * 10,
                                         "Set by SimpleSystem"
                                         );
                   }
               if (  ticket < 0 ) {
                     Alert( "Error Sending Order!" );
                   }
               else {
                     if (  High[1]   >  R_MA
                        && R_RSI12   >  R_RSI14
                        && R_MA_Fast <= R_MA
                        ){
                           ticket = OrderSend( Symbol(),
                                               OP_SELL, 
                                               Lots,
                                               Bid,
                                               10,
                                               Ask + HH         * Point * 10,
                                               Ask - TakeProfit * Point * 10,
                                               "Set by SimpleSystem"
                                               );
                         }
                     if (  ticket < 0 ) {
                           Alert( "Error Sending Order!" );
                         }
                   }
             }
       }
   else {
         IsFirstTick = true;
       }
}
4

1 回答 1

2

主要问题

一旦分配(每个市场事件报价到达)

double HH = High[1],
       LL = Low[ 1];

您的指示OP_SELL将被修复:

ticket = OrderSend( Symbol(),
                    OP_SELL, 
                    Lots,
                    Bid,
                    10,
                 // ----------------------v--------------------------------------
                 // Ask              + HH * 10 * Point,
                 // intention was High[1] + 10 [PT]s ( if Broker allows ), right?
                    NormalizeDouble(   HH + 10 * Point,
                                       Digits      // ALWAYS NORMALIZE FOR .XTO-s
                                       ),
                 // vvv----------------------------------------------------------
                 // Ask - TakeProfit * Point * 10, // SAFER TO BASE ON BreakEvenPT
                    NormalizeDouble(   Ask
                                     - TakeProfit * Point * 10,
                                       Digits      // ALWAYS NORMALIZE FOR .XTO-s
                                       ),
                    "Set by SimpleSystem"
                    );

对称审查和修改OP_BUY案例。

对于 Broker T&C 冲突(这些不需要反映在回测中)审查:

MarketInfo( _Symbol, MODE_STOPLEVEL )
MarketInfo( _Symbol, MODE_FREEZELEVEL )

或在MT4.Terminal Symbols -> Properties 中检查 STOPLEVEL 距离。MarketWatch aMouseRightClick


小问题

还要检查您的代码OrderClose()- 由于价格错误,这将失败:

// ---------------------------------------------vvvvv----------------------------
   bool  res2  = OrderClose( ticket, Lots, OrderClosePrice(), 10 ); # was db.POOL()-SELECT'd
于 2014-10-14T15:16:31.403 回答