How to set order few pips above order initiation b

2019-04-03 00:14发布

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 ):

enter image description here

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;
       }
}

1条回答
戒情不戒烟
2楼-- · 2019-04-03 00:56

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 aMouseRightClick 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
查看更多
登录 后发表回答