Closeing Problems, Please help - page 7

 
Ais wrote >>

Hello Cameofx,
Thanks for kind response.
This coding system is very simple and easy.
All done manually in MetaEditor.
In fact, the system is being designed to develop large programs easily and with high speed.
The system should be also flexible and reliable.
Best regards!

Hello Ais
This is an incredible piece of work..... How can the forum thank you enough? A thank you and a hip, hip, hooray.
I also acknowledge the patience you spent while explaining the many questions I posed. I thank you
Today I will be comparing the program that did not work, 1_20[1] with this program statement by statement. There is much to learn from the both.
You mentioned earlier in the developement of this program, that with the flexibility of this program, features can be added on. Features can be indicators, money management, stacking of positions, modifing pending orders, etc?? Can a feature be added that when a trade has been liquidated, no further trades will be executed upon the current bar? This might not be a problem with some folks on the forum, but I would like to backtest with an option where the system would not trade immediately on the current bar after a liquidation. Is it possible?
Thanks for being here.
Bye for now.
 

Hi Ais
There are some subtle changes in the comparison. I have been at it for a short time, I have not completed the work. But a strikeing difference is with the iNewBar (). By eliminateing iNewBar (), and replacing the iFirstRun variable with iTime_0, this forces the
( ( iNewBar () ==TRUE) || (iFirst == 1 ) ) out of the program equation. iSignalOpen is simplified with faster execution.
Great work.
Cheers
< edited>
The above proves the same for iSignalClose. Very easy to understand the control passing through the program.
Thank you.

 

Hello Ais
Found one more subtle difference in the iTryOpen and iTryClose. No apparent impact. But will read up on the 'else-if' operaters to be clear.
Thank again.
Be back tomorrow.
Cheers

 
////////////////////////////////////////////////////////////////////<         1>
// < Program : Property >                                         //<          >
//   #define   1    " "                                           //<          >
//   #define   2    " "                                           //<          >
// </Program : Property >                                         //<          >
//                                                                //<          >
// < Program : Content >                                          //<          >
//                                                                //<          >
// < Structure             18 elements  in      4 domains       > //<          >
// < 1. Data                9 elements  in      2 domains      /> //<          >
// < 2. Code                9 elements  in      2 domains      /> //<          >
// </Structure             18 elements  in      4 domains       > //<          >
//                                                                //<          >
// < 1. Data                9 =       4 i       3 d       - s   > //<          >
// < 1.1. Input             8 =       5 i       3 d       - s  /> //<          >
// < 1.2. Buffer            1 =       1 i       - d       - s  /> //<          >
// </1. Data                9 =       4 i       3 d       - s   > //<          >
//                                                                //<          >
// < 2. Code                9 /       - i      80 l       3 o   > //<          >
// < 2.1. Interface         6 /       - i      69 l       3 o  /> //<          >
// < 2.2. Special           3 /       - i      11 l       - o  /> //<          >
// </2. Code                9 /       - i      80 l       3 o   > //<          >
//                                                                //<          >
// </Program : Content >                                          //<          >

////////////////////////////////////////////////////////////////////<         2>
// < 1.1. Data : Input >                                          //<          >
//                                                                //<          >
// < 1.1. Input             8 =       5 i       3 d       - s   > //<          >
// <      1. Strategy       4 =       2 i       2 d       - s  /> //<          >
// <      2. Trading        4 =       3 i       1 d       - s  /> //<          >
// </1.1. Input             8 =       5 i       3 d       - s   > //<          >
//                                                                //<          >
// <      1.1.1. Strategy 4 >=====================================//<          >
                    int       iBaseLag          = 20            ; //<          >
                    int       iBaseBar          = 1             ; //<          >
                    double    dFactorTP         = 1.0           ; //<          >
                    double    dFactorSL         = 2.0           ; //<          >
// </     1.1.1. Strategy 4 >=====================================//<          >
//                                                                //<          >
// <      1.1.2. Trading 4 >======================================//<          >
                    int       iTradeBarOnce     = 1             ; //<          >
                    int       iSlippage         = 1             ; //<          >
                    int       iMagic            = 1             ; //<          >
                    double    dLots             = 0.1           ; //<          >
// </     1.1.2. Trading 4 >======================================//<          >
//                                                                //<          >
//                                                                //<          >
// </1.1. Data : Input >                                          //<          >
 
////////////////////////////////////////////////////////////////////<         3>
// < 1.2. Data : Buffer >                                         //<          >
//                                                                //<          >
// < 1.2. Buffer            1 =       1 i       - d       - s   > //<          >
// <      1. Flags          1 =       1 i       - d       - s  /> //<          >
// </1.2. Buffer            1 =       1 i       - d       - s   > //<          >
//                                                                //<          >
// <      1.2.1. Flags 1 >========================================//<          >
                    int       iTradeBarTime     = EMPTY         ; //<          >
// </     1.2.1. Flags 1 >========================================//<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
// </1.2. Data : Buffer >                                         //<          >

////////////////////////////////////////////////////////////////////<         4>
// < 2.1. Code : Interface >                                      //<          >
//                                                                //<          >
// < 2.1. Interface         6 /       - i      69 l       3 o   > //<          >
// <      1. iReserved_1              - i       - l       - o  /> //<          >
// <      2. iSignalOpen              - i      17 l       1 o  /> //<          >
// <      3. iSignalClose             - i      15 l       1 o  /> //<          >
// <      4. iGetTicket               - i       7 l       1 o  /> //<          >
// <      5. iTryOpen                 - i      15 l       - o  /> //<          >
// <      6. iTryClose                - i      15 l       - o  /> //<          >
// </2.1. Interface         6 /       - i      69 l       3 o   > //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
// </2.1. Code : Interface >                                      //<          >
 
////////////////////////////////////////////////////////////////////<         5>
// < 2.1.1. Code : Interface : iReserved_1 >                      //<          >
//int     iReserved_1 ()     //       - i       - l       - o     //<          >
//{                                                               //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//}                                                               //<          >
// </2.1.1. Code : Interface : iReserved_1 >                      //<          >

////////////////////////////////////////////////////////////////////<         6>
// < 2.1.2. Code : Interface : iSignalOpen >                      //<          >
int       iSignalOpen ()     //       - i      17 l       1 o     //<          >
{                                                                 //<          >
if      ( iTradeBarTime    == iTime   ( 0 , 0 , 0 ) )             //<          >
if      ( 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 ,   //<          >
                              iBaseLag , iBaseBar           ) ] ; //<          >
          dLowest  = Low    [ iLowest  ( 0 , 0    , MODE_LOW  ,   //<          >
                              iBaseLag , 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 >                      //<          >
 
////////////////////////////////////////////////////////////////////<         7>
// < 2.1.3. Code : Interface : iSignalClose >                     //<          >
int       iSignalClose ()    //       - i      15 l       1 o     //<          >
{                                                                 //<          >
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   , iBaseLag , 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 * dFactorTP )  return    ( TRUE      ) ; //<          >
if      ( dLoss   > dATR * dFactorSL )  return    ( TRUE      ) ; //<          >
                                        return    ( EMPTY     ) ; //<          >
}                                                                 //<          >
// </2.1.3. Code : Interface : iSignalClose >                     //<          >

////////////////////////////////////////////////////////////////////<         8>
// < 2.1.4. Code : Interface : iGetTicket >                       //<          >
int       iGetTicket ()      //       - i       7 l       1 o     //<          >
{                                                                 //<          >
for     ( int i   = OrdersTotal () - 1 ; i >= 0 ; i -- )          //<          >
        {                                                         //<          >
          if      ( OrderSelect ( i , SELECT_BY_POS ) == TRUE   ) //<          >
          if      ( OrderMagicNumber ()               == iMagic ) //<          >
                                      return ( OrderTicket () ) ; //<          >
        } // for                                                  //<          >
                                      return ( EMPTY          ) ; //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
}                                                                 //<          >
// </2.1.4. Code : Interface : iGetTicket >                       //<          >
 
////////////////////////////////////////////////////////////////////<         9>
// < 2.1.5. Code : Interface : iTryOpen >                         //<          >
int       iTryOpen ()        //       - i      15 l       - o     //<          >
{                                                                 //<          >
int       iCommand          = iSignalOpen ()                    ; //<          >
if      ( iCommand         == EMPTY  )                return    ; //<          >
if      ( iCommand         == OP_BUY )                            //<          >
        { string    sType   = "Buy"  ;  int  iColor = Blue    ; } //<          >
else    {           sType   = "Sell" ;       iColor = Red     ; } //<          >
//                                                                //<          >
if      ( iCommand         == OP_BUY )  int  iMode  = MODE_ASK  ; //<          >
                                        else iMode  = MODE_BID  ; //<          >
double    dPrice  = MarketInfo ( Symbol () , iMode            ) ; //<          >
//                                                                //<          >
OrderSend  ( Symbol ()       , iCommand             , dLots   ,   //<          >
             NormalizeDouble ( dPrice , Digits )              ,   //<          >
             iSlippage  , 0  , 0 , "" , iMagic , 0  , iColor  ) ; //<          >
//                                                                //<          >
int       iTrap   = GetLastError ()                             ; //<          >
if      ( iTrap  == 0 )                                           //<          >
        { Alert   ( sType , " Was a Big Success"            ) ; } //<          >
else    { Alert   ( sType , " open exception "      , iTrap ) ; } //<          >
}                                                                 //<          >
// </2.1.5. Code : Interface : iTryOpen >                         //<          >

////////////////////////////////////////////////////////////////////<        10>
// < 2.1.6. Code : Interface : iTryClose >                        //<          >
int       iTryClose ()       //       - i      15 l       - o     //<          >
{                                                                 //<          >
int       iCommand          = iSignalClose ()                   ; //<          >
if      ( iCommand         == EMPTY  )                return    ; //<          >
if      ( OrderType ()     == OP_BUY )                            //<          >
        { string    sType   = "Buy"  ;  int  iColor = Red     ; } //<          >
else    {           sType   = "Sell" ;       iColor = Blue    ; } //<          >
//                                                                //<          >
if      ( OrderProfit ()    > 0      )  string sAct = "Take"    ; //<          >
                                        else   sAct = "Stop"    ; //<          >
double    dPrice  = OrderClosePrice ()                          ; //<          >
//                                                                //<          >
OrderClose ( OrderTicket ()  , OrderLots ()                   ,   //<          >
             NormalizeDouble ( dPrice , Digits )              ,   //<          >
             iSlippage                              , iColor  ) ; //<          >
//                                                                //<          >
int       iTrap   = GetLastError ()                             ; //<          >
if      ( iTrap  == 0 ) iTradeBarTime = iTime ( 0 , 0 , 0 ) ;   //<          >
          Alert   ( sType , " closed with Hard "    , sAct  ) ; } //<          >
else    { Alert   ( sType , " close exception "     , iTrap ) ; } //<          >
}                                                                 //<          >
// </2.1.6. Code : Interface : iTryClose >                        //<          >
 
////////////////////////////////////////////////////////////////////<        11>
// < 2.2. Code : Special >                                        //<          >
//                                                                //<          >
// < 2.2. Special           3 /       - i      11 l       - o   > //<          >
// <      1. init                     - i       1 l       - o  /> //<          >
// <      2. deinit                   - i       1 l       - o  /> //<          >
// <      3. start                    - i       9 l       - o  /> //<          >
// </2.2. Special           3 /       - i      11 l       - o   > //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
// </2.2. Code : Special >                                        //<          >

////////////////////////////////////////////////////////////////////<        12>
// < 2.2.1. Code : Special : Init >                               //<          >
int       init    ()         //       - i       1 l       - o     //<          >
{                                                                 //<          >
Alert   ( ""               , "Start " , UninitializeReason () ) ; //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
}                                                                 //<          >
// </2.2.1. Code : Special : Init >                               //<          >
 
////////////////////////////////////////////////////////////////////<        13>
// < 2.2.2. Code : Special : Deinit >                             //<          >
int       deinit  ()         //       - i       1 l       - o     //<          >
{                                                                 //<          >
Alert   ( ""               , "Stop "  , UninitializeReason () ) ; //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
//                                                                //<          >
}                                                                 //<          >
// </2.2.2. Code : Special : Deinit >                             //<          >

////////////////////////////////////////////////////////////////////<        14>
// < 2.2.3. Code : Special : Start >                              //<          >
int       start   ()         //       - i       9 l       - o     //<          >
{                                                                 //<          >
// < 2.2.3.1. History data inspection 4 >`````````````````````````//<          >
static    int       iTrigger   = 0       ; if ( iTrigger == 0 ) { //<          >
  if  ( ( iTime ( 0 , 0 , 0 ) == 0                          )     //<          >
  ||    ( iBars ( 0 , 0     )  < iBaseLag     + iBaseBar    ) )   //<          >
          return                         ; else iTrigger  = 1 ; } //<          >
// </2.2.3.1. History data inspection 4 >`````````````````````````//<          >
//                                                                //<          >
// < 2.2.3.2. Main routine 3 >````````````````````````````````````//<          >
int       iTicket           = iGetTicket ()                     ; //<          >
//                                                                //<          >
if      ( iTicket < 0 )       iTryOpen   ()                     ; //<          >
else                          iTryClose  ()                     ; //<          >
// </2.2.3.2. Main routine 3 >````````````````````````````````````//<          >
//                                                                //<          >
// < 2.2.3.3. Exception handler 2 >```````````````````````````````//<          >
int       iTrap   =           GetLastError ()                   ; //<          >
if      ( iTrap   > 0 )       Alert  ( "Exception " , iTrap   ) ; //<          >
// </2.2.3.3. Exception handler 2 >```````````````````````````````//<          >
}                                                                 //<          >
// </2.2.3. Code : Special : Start >                              //<          >
////////////////////////////////////////////////////////////////////<         0>


Complete list of changes:

1. Global variable "int iTradeBarOnce = 1 ;" has been added into domain "Data : Input".
2. Domain "Data : Buffer" has been created.
3. Global variable "int iTradeBarTime" has been added in domain "Data : Buffer".
4. Empty function "iReserved_1 ()" has been added.
5. Function "iSignalOpen ()" has been changed.
6. Function "iTryClose ()" has been changed.


Notes:

1. Global variable "int iTradeBarOnce" allows / forbids repeating trade on the same bar.
2. Value of this variable "0" allows and value "1" forbids repeating trade on the same bar.
3. Global variable "int iTradeBarTime" transmits opening time of traded bar from function "iTryClose ()".
4. Function "iSignalOpen ()" recieves this value and uses it to control repeating trade.
5. I like to use "1" and "0" values instead of "TRUE" and "FALSE".
6. Following statements are equivalent, but first works faster:

if      ( iTradeBarTime    == iTime   ( 0 , 0 , 0 ) )             //<          >
if      ( iTradeBarOnce    == 1 )       return    ( EMPTY     ) ; //<          >

if    ( ( iTradeBarTime    == iTime   ( 0 , 0 , 0 ) )             //<          >
     && ( iTradeBarOnce    == 1 ) )     return    ( EMPTY     ) ; //<          >

7. Empty function "iReserved_1" has been added for future use.


Last edit: 2010.03.18 22:54
State: Ready



Hello Huckleberry and hi!
A little later I am going to slightly extend the program to make it more readable.
Cheers!

Files:
1_29.mq4  27 kb
Reason: