Download MetaTrader 5

HELP with programming

To add comments, please log in or register
Write articles, help other developers and make money
tippy
70
tippy 2016.10.26 14:42 

Hi all.  I'm new to this forum and am hoping someone can help with some programming of an EA.

 I am trying to adapt the Sidus EA  (thanks Yuriy Tokman).  I want to change the closing of a trade to be...

 1) If the commodity channel is above or below a certain value, close trade, else:

2) Close the trade at the next arrow.

 

Currently the indicator only closes a trade at each subsequent arrow.

 

Can anyone help please?

Thanks,

John 

Marco vd Heijden
Moderator
4704
Marco vd Heijden 2016.10.26 14:43  
Maybe you can try freelance
Mladen Rakic
54598
Mladen Rakic 2016.10.26 15:16  
tippy:

Hi all.  I'm new to this forum and am hoping someone can help with some programming of an EA.

 I am trying to adapt the Sidus indicator (thanks Yuriy Tokman).  I want to change the closing of a trade to be...

 1) If the commodity channel is above or below a certain value, close trade, else:

2) Close the trade at the next arrow.

 

Currently the indicator only closes a trade at each subsequent arrow.

 

Can anyone help please?

Thanks,

John 

Almost all versions of "sidus" indicators repaint and those that don't are having signals that are anything but what you would like to see.

You will find it very difficult (and without results that you expect) when you try to use repainting indicator(s) in an EA. Probably you should consider not using that indicator for your EA

tippy
70
tippy 2016.10.26 16:00  
Mladen Rakic:

Almost all versions of "sidus" indicators repaint and those that don't are having signals that are anything but what you would like to see.

You will find it very difficult (and without results that you expect) when you try to use repainting indicator(s) in an EA. Probably you should consider not using that indicator for your EA

Thanks for the reply.  I know what you mean by repaint, but can you explain what this means specifically for this indicator?
Mladen Rakic
54598
Mladen Rakic 2016.10.26 16:10  
tippy:
Thanks for the reply.  I know what you mean by repaint, but can you explain what this means specifically for this indicator?

But I don't know what do you mean.

If you mean that it will constantly change the past signals and make them look better then they really are, yes, it will do so. It is calculating from future bars to the past bars and it keeps future signals as if the have already happened. It would not be a problem if it was not using global scope variables to "remember" "previous" states (which makes the whole thing a simple coding error). The attached is a version that uses the same logic as the repainting version(s), but it does not repaint. Compare it to the one you have and see the difference

Files:
tippy
70
tippy 2016.10.26 16:19  
Mladen Rakic:

But I don't know what do you mean.

If you mean that it will constantly change the signals, yes, it will do so. It is calculating from future bars to the past bars and it keeps future signals as if the have already happened. It would not be a problem if it was not using global scope variable to "remember" previous states (which makes the whole thing a simple coding error). The attached is a version that uses the same logic as the repainting version(s), but it does not repaint. Compare it to the one you have and see the difference

This is the one I am currently using in demo mode. 
Files:
Mladen Rakic
54598
Mladen Rakic 2016.10.26 16:23  
tippy:
This is the one I am currently using in demo mode. 
That is an EA, but never mind - the signals in that EA are coded correctly and you do not need to change anything
tippy
70
tippy 2016.10.26 16:25  
Mladen Rakic:
That is an EA, but never mind - the signals in that EA are coded correctly and you do not need to change anything
Thanks you sop much for replying.  Any thoughts on my initial question regarding implementing the CC as an exit strategy?  I am trying to learn how to code by looking at other codes and I think I am getting the hang of it.  Although at the moment I am trying to sort out an error message of having an "else" but no "if", even though I seem to have the if!!
Mladen Rakic
54598
Mladen Rakic 2016.10.26 16:42  
tippy:
Thanks you sop much for replying.  Any thoughts on my initial question regarding implementing the CC as an exit strategy?  I am trying to learn how to code by looking at other codes and I think I am getting the hang of it.  Although at the moment I am trying to sort out an error message of having an "else" but no "if", even though I seem to have the if!!

You already have a closing loop. Why don't you add the conditions that you described in the same loop?

For example - line 53:

            if(GetSignal()==-1 || iBarShift(NULL,0,OrderOpenTime())!=0)
which would make it close the order on the next bar after the order was opened. Similar thing you can do for CCI condition(s)
tippy
70
tippy 2016.10.26 16:57  
Mladen Rakic:

You already have a closing loop. Why don't you add the conditions that you described in the same loop?

For example - line 53:

            if(GetSignal()==-1 || iBarShift(NULL,0,OrderOpenTime())!=0)
which would make it close the order on the next bar after the order was opened. Similar thing you can do for CCI condition(s)
OK I'll give that a try.  Thanks again for your help!
Ian Smith
159
Ian Smith 2016.10.26 18:04  

The version of the code you posted didn't have a problem with the if and else statements. Indenting is a very good way to keep track of your statements. Keep in mind that there are several valid forms of syntax, but only one is recommended.

        if(OrderMagicNumber() != OurMagic) continue;

This is valid, because there is a single statement after the if statement.

if(OrderType()==OP_BUY) 

closeorder(OrderTicket()); 

 This is also valid, but dangerous.

if(OrderType()==OP_BUY) 

logentry("Closing %s", OrderTicket());

closeorder(OrderTicket()); 

 This demonstrates the danger of not using curly braces. What actually happens now is that all orders are closed, but only the OP_BUY is logged.

if(OrderType()==OP_BUY) 

logentry("Closing %s", OrderTicket());

closeorder(OrderTicket()); 

  }

Now it behaves as expected. From your posted (correct) code, I prefer the following spacing:

if(OrderType()==OP_BUY)

{

if(GetSignal()==-1)

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position

return(0);

}

}  else  {

if(GetSignal()==1)

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position

 return(0);

}

}

 Usually when I'm helping people learn programming, I start off by fixing the spacing and then the bugs in if loops become obvious. The last programmer I helped a lot had about 30 if statements scattered around, and they had a lot of different styles to the statements. I found 5 major bugs in the if logic and they were all solved by having standardized spacing, indenting and use of { and }.

I personally write like this:

  if( OrderType()==OP_BUY ) { //Explain logic

//do stuff 

} else if ( OrderType()==OP_SELL ) { //Explain catch, especially in case we have something besides OP_BUY and OP_SELL

//do other stuff 

} //End of OP_BUY and OP_SELL

12
To add comments, please log in or register