extern double Lots =0.1;
extern double Loss =500;
extern double TakeProfit =200;
extern double TrailingStop=500;
//--------------------------------------------------------------- 2 --
int start()
{
int tick;
double SL, iTP;
//opening buy order
SL=NormalizeDouble(Ask-Loss*Point, Digits); //Don't need brackets here, refer to below for an explanation
iTP=NormalizeDouble(Ask+TakeProfit*Point, Digits);
tick=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,SL,iTP,NULL,0,0,Blue);
Alert("Tick ",tick);
//OrderTicket() will only work if you used OrderSelect() beforehand
//Note: ticket returned will always be -1 on error, otherwise it went through successfully, even if it's 0
if (tick == -1)
{
Alert ("buy error ");
}
else
{
Alert ("Opened order Buy ", tick);
}
return(0);
}
As for the brackets that I removed.. I just only use them when necessary to put a certain priority in a calculation, otherwise it,s not needed since the old BEDMAS (brackets, exponents, division, multiplication, addition, subtraction) math priority rule applies. I never actually had to test the priority of additions and subractions so I can't tell you with 100% certainty that additions takes priority over subtractions in MQL.
Jon
extern double Lots =0.1;
extern double Loss =500;
extern double TakeProfit =200;
extern double TrailingStop=500;
//--------------------------------------------------------------- 2 --
int start()
{
int tick;
double SL, iTP;
//opening buy order
SL=NormalizeDouble(Ask-Loss*Point, Digits); //Don't need brackets here, refer to below for an explanation
iTP=NormalizeDouble(Ask+TakeProfit*Point, Digits);
tick=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,SL,iTP,NULL,0,0,Blue);
Alert("Tick ",tick);
//OrderTicket() will only work if you used OrderSelect() beforehand
//Note: ticket returned will always be -1 on error, otherwise it went through successfully, even if it's 0
if (tick == -1)
{
Alert ("buy error ");
}
else
{
Alert ("Opened order Buy ", tick);
}
return(0);
}
As for the brackets that I removed.. I just only use them when necessary to put a certain priority in a calculation, otherwise it,s not needed since the old BEDMAS (brackets, exponents, division, multiplication, addition, subtraction) math priority rule applies. I never actually had to test the priority of additions and subractions so I can't tell you with 100% certainty that additions takes priority over subtractions in MQL.
Jon
Thanks for your response. I inserted the changes but the script does not generate an order. There is something basic I am not understanding. I tried the program as a EA and it works but creates lots of orders. What am I doing wrong?
Rudy
change --
Alert ("buy error ");
to
Alert ("buy error code = ", GetLastError());
And you might find out.
See https://docs.mql4.com/constants/errors to see what the error means, if you get one
Yep, what phy said. Tracking errors is not super easy in MQL4. You've got to pay close attention to error-catching.. code it to give details about errors as well and watch the journal for your own errors caught and the default errors reported. A lot of people like Print() but when I'm debugging, I usually use Alert() instead so I don't have to scroll through the journal. When everything is debugged, I switch back to Print().
I have error 4109. The error is a mystery because I do not have a dialog box to check for "allowing live trading" on scripts. I do have the dialog box when attaching a EA.
Rudy.
Menu --------Tools -- Options -- Expert Advisors tab
or, in your script
#property show_inputs
Menu --------Tools -- Options -- Expert Advisors tab
or, in your script
#property show_inputs
I tried your suggestion and it works.
Thank you!
Rudy
It's not a suggestion, it's how it works.
Re-read the manual.
- 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 do not understand why the script does not generate an order. Any suggestions appreciated.
extern double Lots =0.1;extern double Loss =500;
extern double TakeProfit =200;
extern double TrailingStop=500;
//--------------------------------------------------------------- 2 --
int start()
{
int
tick;
double
SL,
iTP;
//opening buy order
SL=NormalizeDouble(Ask-(Loss*Point),Digits);
iTP=NormalizeDouble(Ask + (TakeProfit*Point),Digits);
tick=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,SL,iTP,NULL,0,0,Blue);
Alert("Tick ",tick);
if (OrderTicket() >= 1)
{
Alert ("Opened order Buy ",OrderTicket());
}
if (tick<= 0)
{
Alert ("buy error ");
}
return(0);
}