Closing Problem, Help Please

 

Hello to All,

Here I am with a code that works fine, but it is not closing properly. The closing strategy started with an idea for all positions to close if the price reached, 1*iATR, a signal for a profitable close.

And if the price moved agaisnt the trade by, 2*iATR, a signal for a StopLoss. As I said it works fine. But when the code is optimized, the closing is not proper.

//--- input parameters
int       iPrior=34;  // dATR = iATR(0, 0, iPrior, iBaseBar)
double    dTP=1.0;    // used as multiplier to dATR value, e.g. Profit = dTP*dATR
double    dSL=2.0;    // used as multiplier to dATR value, e.g. Loss = dSL*dATR


// <      1.1.1. Strategy 4 >=====================================//<          >
                    double MaximumRisk          = 0.1           ; //<          >
                    double Lots                 = 0.1           ; //<          >
                    int        iBaseBar          = 1            ; //<          >
                    int        iTradeBarOnce     = 1            ; //<          >
                    int        iSlippage         = 1            ; //<          >
                    int        iMagic            = 1            ; //<          >       
                    int        iTradeBarTime     = EMPTY        ; //<          >

// < 2.1.2. Code : Interface : iSignalOpen >                      //<          >
int       iSignalOpen ()                                          //<          >
{                                                                 //<          >
if    ( ( iTradeBarTime    == iTime   ( 0 , 0 , 0 ) )             //<          > 
      &&                                                          //<          >
      ( iTradeBarOnce    == 1 ))                                  //<          >
      return ( EMPTY);                                            //<          > 
                                                                  //<          >
static    int       iTime_0 = EMPTY                             ; //<          >  
if      (           iTime_0 < iTime   ( 0 , 0 , 0 ) )             //<          > 
        {           iTime_0 = iTime   ( 0 , 0 , 0 )             ; //<          >
          iTradeBarTime     = EMPTY                             ; //<          >
          static    double    dHighest , dLowest                ; //<          >
          dHighest = High   [ iHighest ( 0 , 0    , MODE_HIGH ,   //<          >
                              iPrior , iBaseBar           ) ] ; //<          >
          dLowest  = Low    [ iLowest  ( 0 , 0    , MODE_LOW  ,   //<          >
                              iPrior , iBaseBar           ) ] ; //<          >
        } // if                                                   //<          >
double    dAsk    = MarketInfo        ( Symbol () , MODE_ASK  ) ; //<          >
double    dBid    = MarketInfo        ( Symbol () , MODE_BID  ) ; //<          >
if      ( dAsk    > dHighest )          return    ( OP_BUY    ) ; //<          >
if      ( dBid    < dLowest  )          return    ( OP_SELL   ) ; //<          >
                                        return    ( EMPTY     ) ; //<          >
}                                                                 //<          >
// </2.1.2. Code : Interface : iSignalOpen >                      //<          >
////////////////////////////////////////////////////////////////////<        10>
// < 2.1.3. Code : Interface : iSignalClose >                     //<          >
int       iSignalClose ()                                         //<          >
{                                                                 //<          >
static    int       iTime_0 = EMPTY                             ; //<          >
if      (           iTime_0 < iTime   ( 0 , 0 , 0 ) )             //<          >
        {           iTime_0 = iTime   ( 0 , 0 , 0 )             ; //<          >
          static    double    dATR    , dProfit  , dLoss        ; //<          >
          dATR    = iATR    ( 0 , 0   , iPrior , iBaseBar )         ; //<          >
        } // if                                                   //<          >
//                                                                //<          >
double    dDelta  = OrderOpenPrice () - OrderClosePrice ()      ; //<          >
//                                                                //<          >
if      ( OrderType ()     == OP_BUY  )                           //<          >
        { dProfit = -dDelta ; dLoss  =  dDelta                ; } //<          >
else if ( OrderType ()     == OP_SELL )                           //<          >
        { dProfit =  dDelta ; dLoss  = -dDelta                ; } //<          >
else                                    return    ( EMPTY     ) ; //<          >
//                                                                //<          >
if      ( dProfit > dATR * dTP )  return    ( TRUE      ) ;       //<          >
if      ( dLoss   > dATR * dSL )  return    ( TRUE      ) ;       //<          >
                                        return    ( EMPTY     ) ; //<          >
}                                                                 //<          >
// </2.1.3. Code : Interface : iSignalClose >                     //<          >
 
  1. dDelta  = OrderOpenPrice () - OrderClosePrice ()   
    These functions can not be used until you orderSelect
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
    

  2. if      (           iTime_0 < iTime   ( 0 , 0 , 0 ) ) 
    You want != for several reasons.
    double    dAsk    = MarketInfo        ( Symbol () , MODE_ASK  ) ; //<          >
    double    dBid    = MarketInfo        ( Symbol () , MODE_BID  ) ;
    Why not use the simple functions Time[0], Ask, Bid
  3. Why don't you simply set a TP, and SL and be done. If you loose connection, the EA won't be able to close the trade and there goes your account.
  4. return    ( TRUE      ) ;       //<          >
                                            return    ( EMPTY     ) ; 
    EMPTY is not a bool, use FALSE
 

Hello WHRoeder,

Sorry for the incomplete message. I was interrupted. The complete code does work but it is not at my dispolsal while I am at my regular 9 to 5 job. The few items that you DID mention, I will look into and correct.

Thank you. Big time. : D

But the real question or problem that I had in mind, is when this code is optimized. So as I am understanding how I have setup this code, though it is not perfect, the dTP and dSL are 'double' not 'int'? Yes/No. I do have them listed as 'double'. Within the code, their purpose ( dTP and dSL) are used as multipliers with the value of dATR. When I check the results against the entry and the exit of any trade, the results come out OK. Profits (dTP) are the value of 1*dATR, and all losses (dSL) have the value of 2*dATR. All is good. The problem is when I check the results after the code has been optimized. E.g. iPrior = 34, dTP = 1.8, and dSL =1.1. ( I bring this example out from memory.) I will post the result and dATR values after work tonight. The problem with the results show the loss values are much more than a 1.1*dATR.

Again sorry for the incomplete message. Results will be posted along with the dATR values for both dTP=1, dSL=2. (The results that I agree should look like.) and I will post the results with the suspect values.

Thank you for your prompt reply and info.

Regards Huckleberry

 

Hello WHRoeder

The problem is gone. Thank you for your help.

And I understand your other suggestion and will take heed. The structure of the EA has a trailing stop built into it. And if the EA is run as such, I can see how this would delete an account when the system is turned off and restarted on a daily basis. A set TP and SL is best if run as a robot.

Onto another suggestion,------return ( FALSE vs EMPTY vs TRUE vs 0) ; ------This has been brought up before in constructive conversation. This code must be unorthodox to the max. When ever I insert ( FALSE, false, TRUE, true, 0, or is left blank), the code does not perform. I truely do not know how to explain it. The code works fine in this form, but everyone has had the opinion that using the EMPTY, will not work. I will send it to you (the code) and you can backtest it for yourself. Maybe you can make sense of it. I have puzzled over this for 8 or 9 months. Yes this is my idea for a code, but the structure was built by another programmer. This programmer can not be reached. I have great respect for him. He was always there. But for nearly three months, I have not heard from him.

Regards

Huckleberry

 
Is EMPTY a hangover from MQL2 ?
 
  1. RaptorUK:
    Is EMPTY a hangover from MQL2 ?
    nope SetIndexStyle() ObjectDeleteAll() others
  2. Huckleberry:
    ever I insert ( FALSE, false, TRUE, true, 0, or is left blank), the code does not perform.
    EMPTY = -1
    Boolean constants may have the value of true or false, numeric representation of them is 1 or 0 respectively. We can
    also use synonyms True and TRUE, False and FALSE.
    So when you return(EMPTY) you are returning TRUE. Figure out why when you return false the code doesn't work correctly.
 

That is interesting info. I will insert a -1 to see what happens. And get back. Thanks again. Really.

 
WHRoeder:
  1. RaptorUK:
    Is EMPTY a hangover from MQL2 ?
    nope SetIndexStyle() ObjectDeleteAll() others
Thanks for the info.
Reason: