Download MetaTrader 5

Yet another OrderSend idiot ;-)

To add comments, please log in or register
scarr
126
scarr  

Hi All,

I know, I know!! it's probably the most asked question but I am unable to figure it so please take a look at this and tell me what I am doing wrong as it's not working in backtesting

Broker = intertrader

         minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);      
         if(OrderMe == BUY){
            stoploss=NormalizeDouble(Ask-(10*Point),Digits);
            takeprofit=NormalizeDouble(Ask+(10*Point),Digits);      
            OrderPrice = Ask;
            ticket=OrderSend(Symbol(),OP_BUY,0.01,OrderPrice,3,stoploss,takeprofit,"My order",16384,0,clrGreen);

            WriteLog("Ask="+DoubleToStr(OrderPrice,2));
            WriteLog("Stop "+DoubleToString(stoploss,2));
            WriteLog("takeprofit "+DoubleToString(takeprofit,2));
         }
         if(OrderMe == SELL){
            stoploss=NormalizeDouble(Bid+(10*Point),Digits);
            takeprofit=NormalizeDouble(Bid-(10*Point),Digits);      
            OrderPrice = Bid;
            ticket=OrderSend(Symbol(),OP_SELL,0.01,OrderPrice,3,stoploss,takeprofit,"My order",16384,0,clrGreen);

            WriteLog("Bid="+DoubleToStr(OrderPrice,2));
            WriteLog("Stop "+DoubleToString(stoploss,2));
            WriteLog("takeprofit "+DoubleToString(takeprofit,2));
         }

Thanks Steve

JC
1581
JC  
scarr:

I know, I know!! it's probably the most asked question but I am buggered if I can figure it so please take a look at this and tell me what I am doing wrong as it's not working in backtesting

There's multiple possible causes, and the Journal section of the backtesting should tell you why orders are being rejected.

But it's probably the distance to the s/l. You're reading the MODE_STOPLEVEL value, but not doing anything with it. You should also bear in mind that, on a market order, the minimum distance to the s/l is MODE_STOPLEVEL plus the spread. Therefore, your code will fail even when MODE_STOPLEVEL = 0 if the spread is more than 10 * Point.

scarr
126
scarr  

JC:

Journal says #130 (stoploss) and I know I'm not doing anything with minstoplevel but as you can see I have (I think) set the stop level to Ask+10 / Bid-10 points, this should be more than enough right?

Steve

JC
1581
JC  
scarr:

this should be more than enough right?

Probably not, no.

Point will usually be a value such as 0.00001. You may be assuming that it is a "pip", i.e. 10 times larger, at 0.0001. Unless you are using one of the very few remaining 4DP accounts offered by brokers, where Point is 0.0001, then your stop is 1 pip, not 10 pips.

scarr
126
scarr  

JC:


Wow no wonder I am confused! OK printed out what Point is in the log and it's 0.1 so if I want a stop of 10 pips do I just multiply Point by 100? (hang on I tried that and the stop was way off, I then tried * 10 and it appears to work even though I don't understand why 10*0.1 = 10 and not 1) OK this appears to at least create an order but it says "close at stop" however there are days of data left to run so not sure why it closed the order.

Thanks

Steve

JC
1581
JC  
scarr:

Wow no wonder I am confused! OK printed out what Point is in the log and it's 0.1 so if I want a stop of 10 pips do I just multiply Point by 100?

If Point is 0.1 then you're presumably dealing with a non-fx instrument, and there's absolutely no consensus about what a "pip" is on gold, or oil, or the S&P500 etc - it's not a term particularly widely used outside of fx.

Avoiding the term "pip"... If you want your stop-distance to be a change in price of 10 (e.g. a change in the gold price from 1262.5 to 1252.5), and Point is 0.1, then, yes, you would need to multiply Point by 100.

scarr
126
scarr  

JC

No this is UKFTSE :-(

P.S. I amended my previous post as you replied

Steve


P.S. Thanks for your help so far.

whroeder1
17084
whroeder1  
            stoploss=NormalizeDouble(Ask-(10*Point),Digits);
            takeprofit=NormalizeDouble(Ask+(10*Point),Digits);      
            OrderPrice = Ask;
            ticket=OrderSend(Symbol(),OP_BUY,0.01,OrderPrice,3,stoploss,takeprofit,"My order",16384,0,clrGreen);
  1. You buy at the Ask, therefor your stops must be relative to the Bid. If the spread is greater than 10 points, your SL is below market.
  2. You can't move stops closer to the market than the minimum (MODE_STOPLEVEL * _Point.) Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial Is stop level larger than 10?
  3. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
  4. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
To add comments, please log in or register