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