And look at all these braces I put in. This doesn't look right and yet I don't know how to finish the code without getting errors
I take away braces then it says can't express in global
I take away other braces then it says need ending brace ?
Are these braces right ? and why no errors ?
As you can see I still have some major problems understanding how to code properly, but I feel I'm getting closer.
Thanks all for the answers on all the previous questions and sorry for the multitude of questions recently. It's all just very exciting and interesting
Please advise
The {} braces serve a purpose, you need to understand that purpose otherwise you will always be at risk of confusion . . .
Lets see if I can explain,
total = OrdersTotal(); // this gets the total number of open orders for all EAs and manual trades, ALL open orders if(total < 1) // this says if the total number of open orders is less than 1, i.e. no open orders, then do something ...
So, if there are no Open orders what do you want to do ? well if you just wanted to do a print statement life is easy . .
total = OrdersTotal(); if(total < 1) Print("There are no open orders . . . "); // the Print follows on directly from the if
. . . because there is just one line of code to be executed IF the if expression is true then there is no need for { } braces . . . but . . .
total = OrdersTotal(); if(total < 1) { Print("There are no open orders . . . "); // we now want to execute more than one line of code as a result of the IF if(faster > slower) ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"My EA",12345,0,Green); } else Print("There are ", total, " open orders . . . "); // this prints the number of open orders if total is NOT less than 1
if and else work as a pair . . yo need to be careful that you are using the else with the correct if . . .
total = OrdersTotal(); if(total < 1) { if(faster > slower) { ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"My EA",12345,0,Green); if(ticket > 0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); // this else pairs with if(ticket > 0) return(0); } if(faster < slower) { ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask+StopLoss*pips2dbl,Ask-TakeProfit*pips2dbl,"My EA",12345,0,Red); if(ticket > 0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); // this else pairs with if(ticket > 0) return(0); } }
Your code looks OK to me . . .
Maybe you were talking about the other section of your code . . .
for (i = total - 1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //I'll add return codes later, it opens trades currently if(OrderType() == OP_BUY && Symbol() == OrderSymbol()) { if(faster < slower) { result = OrderClose( OrderTicket(), OrderLots(), Bid,3, White); if(result == false) { Print("Order", OrderTicket()," failed to close Error ",GetLastError()); return(0); } if(OrderType() == OP_SELL && Symbol() == OrderSymbol()) // this is within the loop of if(faster < slower) so only happens if(faster < slower) { if(faster > slower) { result = OrderClose(OrderTicket(), OrderLots(), Ask,3, White); if(result == false) { Print("Order", OrderTicket()," failed to close Error ", GetLastError()); return(0); } } } } // end of if(faster < slower) } } return(0);
I adjusted your code to the indenting scheme I use, I find it easier to follow . . . it's now clear to see where your problem lies.
Thanks
I'll start using the { on new lines so I can read it better.
Regarding
// this is within the loop of if(faster < slower) so only happens if(faster < slower)
I seem to have one other misunderstanding on braces and return(0);
Can I end the block all the way back to before the if(faster < slower ) with one brace or do I need to close each block up to that point so that the next (if) statement will be used.
Like so ?
OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //I'll add return codes later, it opens trades currently if(OrderType() == OP_BUY && Symbol() == OrderSymbol()) { if(faster < slower) { result = OrderClose( OrderTicket(), OrderLots(), Bid,3, White); if(result == false) { Print("Order", OrderTicket()," failed to close Error ",GetLastError()); return(0); }//here ? } // or here? if(OrderType() == OP_SELL && Symbol() == OrderSymbol()) { if(faster > slower) {
Or will this one brace only close the last block no matter where you put it if you only use the 1 brace ?
Please advise and thanks,thanks,thanks
//+------------------------------------------------------------------+ //| Dirty_Rat.mq4 | //| Agent86's Dirty Rat learning Trade | //| //+------------------------------------------------------------------+ #property copyright "Agent86" //---- input parameters extern double TakeProfit=300.0; extern double Lots=0.1; extern double StopLoss=40.0; //++++ These are adjusted for 5 digit brokers. int pips2points; // slippage 3 pips 3=points 30=points double pips2dbl; // Stoploss 15 pips 0.0015 0.00150 int Digits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips) // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { if (Digits == 5 || Digits == 3) { // Adjust for five (5) digit brokers. pips2dbl = Point*10; pips2points = 10; Digits.pips = 1; } else { pips2dbl = Point; pips2points = 1; Digits.pips = 0; } // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- int ticket,i,total,result; double faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL //I'm going to have to create some loop total = OrdersTotal(); if(total < 1) { //I'll change this later so it will work with other EA's and other symbols if(faster > slower) { ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"My EA",12345,0,Green); if(ticket > 0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } if(faster < slower) { ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask+StopLoss*pips2dbl,Ask-TakeProfit*pips2dbl,"My EA",12345,0,Red); if(ticket > 0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); return(0); } } for (i = total - 1; i >= 0; i--) { //many have recommmended deincriment due to error 4108, and increment may miss orders //deincrement had no effect with error 4108 //for(i = 0; i < OrdersTotal(); i++) //many say this will miss orders. //anyhow moving on OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //I'll add return codes later, it opens trades currently if(OrderType() == OP_BUY && Symbol() == OrderSymbol()) { if(faster < slower) { result = OrderClose( OrderTicket(), OrderLots(), Bid,3, White); if(result == false) { Print("Order", OrderTicket()," failed to close Error ",GetLastError()); return(0); } } } if(OrderType() == OP_SELL && Symbol() == OrderSymbol()) { if(faster > slower) { result = OrderClose(OrderTicket(), OrderLots(), Ask,3, White); if(result == false) { Print("Order", OrderTicket()," failed to close Error ", GetLastError()); return(0); } } } } return(0); } //+------------------------------------------------------------------+I guess the only other thing that confuses me is about the (for) statement with no indentation ? Is this typical and is this on global since it's outside of the start{ ??
And that should do it, I might be on my way to actually making something now YEA !
And is putting 2 braces to close the if statements one on top of the other OK
like }
Thanks
Alas I have a working example to work from THANKS !!! WHOOO HOOO
I guess the only other thing that confuses me is about the (for) statement with no indentation ? Is this typical and is this on global since it's outside of the start{ ??
The for works like an if in that everything in a pair of braces { } after it will get executed (unless you return part way through) . . . . it is not outside of start . . . check the brace pairs . . .
Beginning part of your start()
int start() { //---- int ticket,i,total,result; double faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL //I'm going to have to create some loop total = OrdersTotal(); if(total < 1) { //I'll change this later so it will work with other EA's and other symbols if(faster > slower) { ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"My EA",12345,0,Green); if(ticket > 0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } if(faster < slower) { ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask+StopLoss*pips2dbl,Ask-TakeProfit*pips2dbl,"My EA",12345,0,Red); if(ticket > 0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); return(0); } } for (i = total - 1; i >= 0; i--) // this is within start
Now when the sl or tp gets hit, it's off by about 10 points.
StopLoss = 40, and the EA is closing with -50
This has really got me confused now ?
Same EA:
//+------------------------------------------------------------------+ //| Dirty_Rat.mq4 | //| Agent86's Dirty Rat learning Trade | //| //+------------------------------------------------------------------+ #property copyright "Agent86" //---- input parameters extern double TakeProfit=300.0; extern double Lots=0.1; extern double StopLoss=20.0; //++++ These are adjusted for 5 digit brokers. int pips2points; // slippage 3 pips 3=points 30=points double pips2dbl; // Stoploss 15 pips 0.0015 0.00150 int Digits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips) // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { if (Digits == 5 || Digits == 3) { // Adjust for five (5) digit brokers. pips2dbl = Point*10; pips2points = 10; Digits.pips = 1; } else { pips2dbl = Point; pips2points = 1; Digits.pips = 0; } // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- int ticket,i,total,result; double faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL //I'm going to have to create some loop total = OrdersTotal(); if(total < 1) { //I'll change this later so it will work with other EA's and other symbols if(faster > slower && faster_2 > slower_2) { ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"My EA",12345,0,Green); if(ticket > 0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } if(faster < slower && faster_2 < slower_2) { ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask+StopLoss*pips2dbl,Ask-TakeProfit*pips2dbl,"My EA",12345,0,Red); if(ticket > 0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))Print("SELL order opened : ",OrderOpenPrice()); } else Print("Error opening SELL order : ",GetLastError()); return(0); } } for (i = total - 1; i >= 0; i--) { //many have recommmended deincriment due to error 4108, and increment may miss orders //deincrement had no effect with error 4108 //for(i = 0; i < OrdersTotal(); i++) //many say this will miss orders. //anyhow moving on OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //I'll add return codes later, it opens trades currently if(OrderType() == OP_BUY && Symbol() == OrderSymbol()) { if(faster < slower) { result = OrderClose( OrderTicket(), OrderLots(), Bid,3, White); if(result == false) { Print("Order", OrderTicket()," failed to close Error ",GetLastError()); return(0); } } } if(OrderType() == OP_SELL && Symbol() == OrderSymbol()) { if(faster > slower) { result = OrderClose(OrderTicket(), OrderLots(), Ask,3, White); if(result == false) { Print("Order", OrderTicket()," failed to close Error ", GetLastError()); return(0); } } } } return(0); } //+------------------------------------------------------------------+I have no idea, cause I only changed the for loop and the brackets I didn't do anything to the 4/5 digits settings or the SendOrder portions of this EA
I did download 1m data from history center -default metatrader - to get rid of the TestGenerator errors, and planned to import other data later but just wanted to get rid of the errors for starters so that I could move on from the errors and start coding
Anyhow I don't see how this would cause the StopLoss to be off by 10points every time.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I recall I need to change this so the EA will work on other time frames and other symbols along side of other EA's
Ok so I know about the MagicNumber slightly and need to learn more about this before I can input this into my code
Anyhow I see this code in a lot of example and lesson and more regarding
Whenever I try to add a second if(faster < slower) or some other variation of a cross statement, the EA quits trading
I can only assume it's because of this if(total < 1)
This statement was in the lesson and other codes I've seen, however I have yet to see a working code that actually trades on crosses.
For some reason it does not trade on crosses.
If I take out the second if statement, and the second part of the loop, then it will trade in ONE direction only and close on reverse cross, or sl and tp
But as soon as I add the second if statement, and second portion of the loop to close open orders on reversals, then the EA trades but very strangely, then quits trading altogether.
NO errors and no journal errors either.
Please advise the proper way to start the code block for a simple EMA cross or macd cross.
Here is my code: