Closeing Problems, Please help - page 5

 

Hello Ais
Your response is pleaseing. To hear you would help me understand the complexity and then help reduce complexity, was more than I could ask. I humble myself before your generosity. Thank you.
I do have question concerning comment structure in your programs . Not that I want to get off the train of thought that has progressed, but a question of how the format looks is not familiar to me.

Example:

// < Structure 17 elements in 4 domains > //< >
// < 1. Data 8 elements in 2 domains /> //< >
// < 2. Code 9 elements in 2 domains /> //< >
// </Structure 17 elements in 4 domains > //< >
// //< >
// < 1. Data 8 = 5 i 3 d - s > //< >
// < 1.1. Input 7 = 4 i 3 d - s /> //< >
// < 1.2. Buffer 1 = 1 i - d - s /> //< >
// </1. Data 8 = 5 i 3 d - s > //< >
// //< >

The comments are structured so percise. With titles and sub comments, would be the best way for me to describe what I read. As if the comments are developed and formated or structured by a code machine?? It is impressive. Not that I understand all that is commented. Your ability in organization I find refreshing.

Thank you again. I will be studying the program and shall have more questions in the very near future.
Cheers

 

Hello Huckleberry

Glad to hear from you.


1. The structure

The structure is developed and formatted by me own.

//                                                                //<          >
// < Structure             16 elements  in      4 domains       > //<          >
// < 1. Data                7 elements  in      2 domains      /> //<          >
// < 2. Code                9 elements  in      2 domains      /> //<          >
// </Structure             16 elements  in      4 domains       > //<          >
//                                                                //<          >
     

The first intention is to strictly separate the global data and the code.
Computer program for me is model of real world.
Global data are the things of this world.
Code is the set of actions.
Separating them I am underlining, that I understand the difference between them.
Counting the number of elements helps me to check and control everything inside the program.


2. The formatting

Formatting is a simple HTML.
Following tag opens the part of program:

// <                                                            > //<          >
      


Following tag closes the part of program:

// </                                                           > //<          >
      


Following tag is used for parts of program in single line:

// <                                                           /> //<          >
      

Ending part of tag now is used for pages counting.
In future use of this part of tags may be extended.

                                                                  //<          >
      

Page structure helps to easily navigate through the program.
Using <Page Up> and <Page Down> I always see the same types of tags on the same places.


3. The elements

The element of data is a single global variable.
I see array as the single element too.
Local variables are just temporary entities of code elements.

The element of code is a single function.
Also I can use word "method".
I use word "interface" for united set of functions.

To reduce complexity I devide data and code in logical domains.
The number of domain depends on the program structure.
Usual domains for data: "input", "settings", "control", "buffer" ..
Usual domains for code: "special" for set of special functions, "interface" for set of other functions.
In case of large programs I can create "objects" with different "interfaces".

To control the integrity of data elements and code elements is used the counting.

//                                                                //<          >
// < 1. Data                7 =       4 i       3 d       - s   > //<          >
// < 1.1. Input             7 =       4 i       3 d       - s  /> //<          >
// < 1.2. Buffer            - =       - i       - d       - s  /> //<          >
// </1. Data                7 =       4 i       3 d       - s   > //<          >
//                                                                //<          >
// < 2. Code                9 /       - i      82 l       4 o   > //<          >
// < 2.1. Interface         6 /       - i      71 l       4 o  /> //<          >
// < 2.2. Special           3 /       - i      11 l       - o  /> //<          >
// </2. Code                9 /       - i      82 l       4 o   > //<          >
//                                                                //<          >
     

Opening and closing tags contain the same sums.
Usually I count the sum in the opening tag.
Then I just copy and insert the opening tag instead of the closing tag and then add closing slash "/".

To control the integrity of data the following notation is used.
The number of data elements of "integer" is noted as "i".
The number of data elements of "double" is noted as "d".
The number of data elements of "string" is noted as "s".
Left column contains sum of the line "i" + "d" + "s".

To control the integrity of code the following notation is used.
The number of function inputs, that is number of function arguments, is noted as "i".
The number of open lines of code is noted as "l".
The number of function outputs is noted as "o".
Always "1" for single function that returns any value and "-" in all other cases.
Left column contains the number of functions in domain.


4. The domains

These are the headers of domains.

////////////////////////////////////////////////////////////////////<         2>
// < 1.1. Data : Input >                                          //<          >
//                                                                //<          >
// < 1.1. Input             7 =       4 i       3 d       - s   > //<          >
// <      1. Strategy       4 =       2 i       2 d       - s  /> //<          >
// <      2. Trading        3 =       2 i       1 d       - s  /> //<          >
// </1.1. Input             7 =       4 i       3 d       - s   > //<          >
//                                                                //<          >
     
////////////////////////////////////////////////////////////////////<         3>
// < 1.2. Data : Buffer >                                         //<          >
//                                                                //<          >
// < 1.2. Buffer            - =       - i       - d       - s   > //<          >
// </1.2. Buffer            - =       - i       - d       - s   > //<          >
//                                                                //<          >
     
////////////////////////////////////////////////////////////////////<         4>
// < 2.1. Code : Interface >                                      //<          >
//                                                                //<          >
// < 2.1. Interface         6 /       - i      71 l       4 o   > //<          >
// <      1. iNewBar                  - i       4 l       1 o  /> //<          >
// <      2. iSignalOpen              - i      15 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      71 l       4 o   > //<          >
//                                                                //<          >
     
////////////////////////////////////////////////////////////////////<        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   > //<          >
//                                                                //<          >
     


The rules and notations for counting are the same as for the header of program.
I just copy the opening tags of the headers of domains into the header of program.

Each page in domain has opening and closing tags.
For example:

////////////////////////////////////////////////////////////////////<        12>
// < 2.2.1. Code : Special : Init >                               //<          >
..
// </2.2.1. Code : Special : Init >                               //<          >
     

Leading numbers are used for the counting and navigation of the parts of structure.
Then follows notation for the part of program "Data" or "Code".
Then follows name of domain.
Then follows name of element of domain.


Hope this may help.

Bye for now and waiting for your reply

 

Hi Ais
Your sense of organization is ......how can one say it ..... practicle and sensible.
Intigrating the HTML makes for easy navigation, though it is beyond me at this time.
Very good.
At this time, I am disecting the program you sent me, one statement at a time. The concepts
you have on the variables, are understandable. Thank you again.
I'll be reply with questions I am sure.
Bye

 

Hello Ais,,,<this has been edited>
The program has not been absorbed as yet. Sorry it is taking so much of your time. There is an area that I must have missed,
because I have one error to contend with.
I must go for now, but will return tomorrow.
Bye for now

 

Hello Huckleberry
Some features was added for example and can easily be changed or removed.
Improvement of program is an everlasting process.
Bye for now
:)

 
Ais wrote >>

Hello Huckleberry
Some features was added for example and can easily be changed or removed.
Improvement of program is an everlasting process.
Bye for now
:)


Hello Ais
Thank you for all you have done.
Last night I finally found my mistake while I was copying your program.
At this time I am trying to understand what are some of the variables are for, and how they would work.
The iSignalClose is where I am inserting part of my other program to establish the OrderClosePrice. With
your suggestion:

dDelta = OrderOpenPrice () - OrderClosePrice

the program has more flexibility.
Be back with more questions. And comments.
Cheers
 

Hello Ais
The questions have occurred. Your program states in ..... iTryOpen ()

else if     ( iCommand       == OP_BUY )
   {          
string        sType           = "Buy"   ;   
int           iColor          =  Blue   ;               }
else         
   {          sType           = "Sell"  ;    
              iColor          =  Red    ;           

Out of my curiosity, should there be

else if     ( iCommand       == OP_BUY  )
   {          
string        sType           = "Buy"   ;   
int           iColor          =  Blue   ;               }
else if     ( iCommand       == OP_SELL )   // Was this ommitted     
   {          sType           = "Sell"  ;    
              iColor          =  Red    ;               }
Here is another question for better understanding
// < 2.1.1. Code : Interface : iNewBar >                          //<          >
int       iNewBar ()         //       - i       4 l       1 o     //<          >
{                                                                 //<          >
static    int       iTime_0 = 0                                 ; //<          >
//                                                                //<          >
if      ( iTime_0 < iTime   ( 0 , 0 , 0 ) )                       //<          >
        { iTime_0 = iTime   ( 0 , 0 , 0 ) ;  return ( TRUE  ) ; } //<          >
else    {                                    return ( EMPTY ) ; } //<          >
//                                                  
If iNewBar is TRUE, control is passed to iSignalOpen. If iNewBar is TRUE (equal to the current bar??), OR iFirstRun ==1, then iFirstBar = 0. This is confusing only because I do not understand the iNewBar () definition and/or iFirstRun definition. I can see where iFirstRun is decared and initialize, but what does it mean.
I am sorry to burden you for such simple questions. I can not calculate or see how the two work within the function. When you have time, could you clear this up please.
Thanks again for your efforts. : )
Cheers
 

Hello Huckleberry

Both questions are important!

Answer 1.

Both of the conditions are true, but if:
1.1. we know that

int       iCommand          = iSignalOpen ()                    ; //<          >
     

1.2. we know that "iSignalOpen ()" can return only 3 possible values: "EMPTY", "OP_BUY" and "OP_SELL";
1.3. first value already is checked

if      ( iCommand         == EMPTY  )                return    ; //<          >
     

1.4. second value is also checked

if      ( iCommand         == OP_BUY )                            //<          >
     

then is possible to omit last check "if" to slightly speed up computations.

else if     ( iCommand       == OP_SELL )                    //<          >
     


Answer 2.

2.1. We recognize that bar number 0 is changed by comparing opening time of bar number 0 with last stored opening time of bar number 0.
"iTime ( 0, 0, 0 )" returns opening time of current incomplete bar.
Index of current bar is always 0, it is third value in "iTime ( 0, 0, 0 )".
Declaring the variable "int iTime_0" as "static" means that value of this variable is saved between the function invocations.
Each time when opening time is changed, we:
2.1.1. assign "iTime_0 = iTime ( 0, 0, 0 ) ;" to store opening time of current bar;
2.1.2. return "TRUE" that means that current bar is a new bar, last bar number 0 became bar number 1.

////////////////////////////////////////////////////////////////////<         5>
// < 2.1.1. Code : Interface : iNewBar >                          //<          >
int       iNewBar ()         //       - i       4 l       1 o     //<          >
{                                                                 //<          >
static    int       iTime_0 = 0                                 ; //<          >
//                                                                //<          >
if      ( iTime_0 < iTime   ( 0 , 0 , 0 ) )                       //<          >
        { iTime_0 = iTime   ( 0 , 0 , 0 ) ;  return ( TRUE  ) ; } //<          >
else    {                                    return ( EMPTY ) ; } //<          >
}                                                                 //<          >
// </2.1.1. Code : Interface : iNewBar >                          //<          >
     

2.2. Our starting bar for computations of "dHigh" and "dLow" in "iSignalOpen ()", and "dATR" in "iSignalClose ()", is "iBaseBar=1".
While bar number 1 is the same, these values are the same.
It is a reason to compute these values only on new bars.
This speeds up the program.
The variable "static int iFirstRun" is a simple trigger.
Initial value "1" forces computation of "dHigh", "dLow" and "dATR" on first program run, regardless of any condition.
This improves reliability of the program.

<added later>
Exactly, on the first program run "iNewBar ()" in "iSignalOpen ()" returns "TRUE", and in "iSignalClose" returns "EMPTY"!
This is because on first invocation of "iNewBar ()", that is inside of first run of "iSignalOpen ()", value of "iTime_0 = 0", and on second invocation of "iNewBar ()", that is inside of first run of "iSignalClose ()", "iTime_0 = iTime ( 0, 0, 0 )"!
As result without first run control may be "dATR = 0".
Such is the first run.
</added later>

Immediately after that we assign "iFirstRun = 0" and the following condition will true only on new bars:

if    ( ( iNewBar () == TRUE )   ||   ( iFirstRun  == 1 ) )       //<          >
     

But!
Rests of code of "iSignalOpen ()" and "iSignalClose ()" executed on every tick!
Only computations of unchanged values are skipped.

////////////////////////////////////////////////////////////////////<         6>
// < 2.1.2. Code : Interface : iSignalOpen >                      //<          >
int       iSignalOpen ()     //       - i      15 l       1 o     //<          >
{                                                                 //<          >
static    int                           iFirstRun   = 1         ; //<          >
if    ( ( iNewBar () == TRUE )   ||   ( iFirstRun  == 1 ) )       //<          >
        {                               iFirstRun   = 0         ; //<          >
          int iIndexH = iHighest      ( 0 , 0    , MODE_HIGH  ,   //<          >
                                        iBaseLag , iBaseBar   ) ; //<          >
          int iIndexL = iLowest       ( 0 , 0    , MODE_LOW   ,   //<          >
                                        iBaseLag , iBaseBar   ) ; //<          >
          static double dHigh ; dHigh = High     [ iIndexH    ] ; //<          >
          static double dLow  ; dLow  = Low      [ iIndexL    ] ; //<          >
        } // if                                                   //<          >
//                                                                //<          >
double    dAsk    = MarketInfo        ( Symbol () , MODE_ASK  ) ; //<          >
if      ( dAsk    > dHigh )             return    ( OP_BUY    ) ; //<          >
//                                                                //<          >
double    dBid    = MarketInfo        ( Symbol () , MODE_BID  ) ; //<          >
if      ( dBid    < dLow  )             return    ( OP_SELL   ) ; //<          >
//                                                                //<          >
                                        return    ( EMPTY     ) ; //<          >
}                                                                 //<          >
// </2.1.2. Code : Interface : iSignalOpen >                      //<          >
     


Hope this may help.
Cheers!
:)

P.S. Some remarks added later.
And, looking at these remarks, is obviously that "iNewBar ()" has to be corrected.
This is because this function invoked twice to check single event.
Now, for the same event on first invocation "iNewBar ()" returns correct value, and on second returns incorrect value.
Will be corrected.



 

Hi Ais
Thank you for clarification. I will follow up with more reading. I don't have a complete handle on the code as yet.
But you mentioned that there was something that had to be corrected as far as the iNewBar?? I am not sure the iNewBar is at fault for the problem what the backtest revealed. Possible two problems??
Here is what I saw when I opened the chart after a backtest. We know that there is a Buy entry when the Ask is above the highest high of the last twenty bars. The Sell is just the opposite. Yet the entries are not consistent. Is this the area you saw?
Found one thing more. The Hard Stop is calculated Atr*2. This is correc, yet I do not see where this is calculated. But the real problem is that the TakeProfit has the same Atr*2 calculation. If I knew where the Hard Stop is calculated, I could possibly correct the TakeProfit. TakeProfit in this case should only be Atr*1.
Take care and I'll be back tomorrow.
Cheers

 

Hi Huckleberry

TakeProfit condition was invented by me own for test purposes.
Now it is changed.
Instead "Atr*2" and "Atr*1" I use "dATR * dFactorTP" ans "dATR * dFactorSL".
Initial values "dFactorTP = 1.0" and "dFactorSL = 2.0" are in the page 2 "Data : Input".
Such solution allows to easily optimize these values.

I have changed functions "iSignalOpen ()" and "iSignalClose ()".
Check of new bar now is implemented in these functions.
Conditions for "Buy" and "Sell" in "iSignalOpen ()" seems like probably correct.

Complete program is in attached file in the next page.

Cheers

Reason: