HELP with programming - page 2

 
Ian Smith:

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

Thanks a lot for your help and the time you took to give me advice!
 
You are very welcome. If you have any more questions feel free to ask. If I don't respond here, feel free to PM me.
 
Ian Smith:
You are very welcome. If you have any more questions feel free to ask. If I don't respond here, feel free to PM me.

Hello again Ian.  Is it possible to have multiple "else" statements in the same code?  If so, would it be order specific, like when doing an "if" in excel?  So, let's say I wanted to code that if the original order was a BUY order and the commodity channel reaches a pre-determined value, close then trade, ELSE if the original order was a BUY order and the next crossing of fast/slow EMA occurs, close the trade ELSE if the original order was a SELL order and the commodity channel falls below a pre-determined value, close then trade, ELSE if the original order was a SELL order and the next crossing of fast/slow EMA occurs, close the trade?

The original coding you saw included a single ELSE, but as you can see from the above, I want 3.  I don't see why this would not be possible, but can't seem to get it right.

 

Here's the logic I think you're asking for. closemytradething() would be a function that you write and declare somewhere else. Ideally you would pass the order ticket to it from the current info. So in your if blocks you do closemytradething(OrderTicket());

And you would create a function:

bool closemytradething( int ticket) { thisishowIclosetradeswith OrderClose stuff }  

if(OrderType() == OP_BUY){
  if (CCIwhatever > 70) {
     closemytradething();
  }
} else if (OrderType()==OP_SELL) {
  if (CCIwhatever < 30) {
     closemytradething();
  }
  if (MAgoescrazy() ) {
     closemytradething();
  }
}
I'm trying to give you hints instead of full answers, but make sure you don't get stuck anywhere :)
 
Ian Smith:

Here's the logic I think you're asking for and the CloseTicket function.

I created CloseTicket() for you because I realized getting it right might be complicated. I haven't tested it, but it should work. It requires a magic number set by "OurMagic" to function, but you can just comment out that line if you don't have one.

if(OrderType() == OP_BUY){
  if (CCIwhatever > 70) {
     CloseTicket(OrderTicket());
  }
} else if (OrderType()==OP_SELL) {
  if (CCIwhatever < 30) {
     CloseTicket(OrderTicket());
  }
  if (MAgoescrazy() ) {
     CloseTicket(OrderTicket());
  }
}

void CloseTicket( int ticket ) {

   if(OrderSelect(ticket,SELECT_BY_TICKET)==false) return(); 

   if(OrderMagicNumber() != OurMagic) return();

   if(OrderType()==OP_BUY ) OrderClose(ticket,OrderLots(),Bid,0,Violet);

   if(OrderType()==OP_SELL) OrderClose(ticket,OrderLots(),Ask,0,Violet);   


 
Ian Smith:
Off to bed now as my brain is hurting!  I'm going to look at this tomorrow evening and get my head round it before trying it - determined to understand it, before applying and testing it.  Thanks again Ian.
Reason: