本文介绍了如何在MQL4中的订单启动栏上方设置一些订单点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个止损订单,如果这是一个卖单,它将被放置在前一个订单的起始条的高点上方,或者如果是买入订单,则将其放置在前一个订单的起始条的低点下方./p>

下面是说明问题的图片(该示例描述了出售订单案例):

任何想法该怎么做?如果我使用固定的止损,则以下代码可以正常工作.如果我将止损替换为基于高或低订单的变量,则被触发.

这是我的代码:

//| 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;
       }
}
解决方案

主要问题

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

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 情况.

对于经纪人的T& C冲突(无需在回测中反映出来):

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

或在 MarketWatch MT4.Terminal 中检查符号-> STOPLEVEL距离的属性.


次要问题

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

// ---------------------------------------------vvvvv----------------------------
   bool  res2  = OrderClose( ticket, Lots, OrderClosePrice(), 10 ); # was db.POOL()-SELECT'd

I would like to create a stoploss order that will be placed above the high of the previous order's initiation bar in case this is a Sell order OR below the low of the previous order's initiation bar in case this is a Buy order.

Here is a picture to illustrate the issue ( the example depicts a sell order case ):

Any idea how to do that? The code below works fine if I use stoploss that is fixed. If I replace the stoploss with variables that are based on High or Low no orders are fired.

Here is my code:

//| 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;
       }
}
解决方案

Major issue

Once having assigned ( per each Market Event Quote Arrival )

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

Your instruction to OP_SELL shall be repaired:

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"
                    );

Symmetrically review and modify the OP_BUY case.

For Broker T&C collisions ( these need not get reflected in backtest ) review:

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

or inspect in the MT4.Terminal in the MarketWatch Symbols -> Properties for STOPLEVEL distance.


Minor Issue

Review also your code for OrderClose() -- this will fail due to having wrong Price:

// ---------------------------------------------vvvvv----------------------------
   bool  res2  = OrderClose( ticket, Lots, OrderClosePrice(), 10 ); # was db.POOL()-SELECT'd

这篇关于如何在MQL4中的订单启动栏上方设置一些订单点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:25