issues with mt4 code (stoploss and take profit)

 

Good morning from the uk everyone. So am looking for a bit of help regarding some code I've been working on. My code is attached and as you'll quickly tell I'm still new to this and its definitely not the neatest/most efficient right now. However the reason for the post is I'm having stop loss and take profit issues.

When I'm back testing I can see that my stop loss isn't actually appearing and ill see the EA make 5 winning trades and then make a massive loss. The second issue with my take profit is even if i change the take profit in the code it always stays the same when back testing (around 50 pips).

I'm aware that it could be something quite simple that I'm missing so I would really appreciate if someone could have a look at it and explain what it is I've been doing wrong.

Regarding the stop loss issue i have also attempted to exit trades in another way and that is when the moving averages cross over but still no luck.

Files:
CABLE.mq4  7 kb
 
  1. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  2. bool isnewcandle;
    ⋮
    void OnTick(void){
    ⋮
        if (isnewcandle)
    

    Uninitialized variable. Never set.

  3.    double previousStovalue = iStochastic (NULL, 0, 14 , 3, 3, MODE_SMA, MODE_MAIN, 0, 1);
    ⋮
           double stopLossPrice = OrderStopLoss;             //Ask - OrderStopLoss * GetPipValue();
           openOrderID = OrderSend(NULL,OP_BUY,lotSize,Ask,10,OrderStopLoss,TakeProfit,NULL,magicNB);
    ⋮
    ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
     

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020.07.25)

  4. Stoploss is a price not an amount.

  5. Why are you opening two orders?

  6. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  7. MTJholmes: When I'm back testing I can see that my stop loss isn't actually appearing

    Did you enable them?
              ToolsOptions (control+O) → Charts → enable Show trade levels

  8. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
William Roeder #:
  1. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  2. Uninitialized variable. Never set.

  3. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020.07.25)

  4. Stoploss is a price not an amount.

  5. Why are you opening two orders?

  6. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  7. Did you enable them?
              ToolsOptions (control+O) → Charts → enable Show trade levels

  8. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

thank you so much for your advice this will be very useful and has helped me understand that little bit more so its appreciated... however William, I never asked anyone to debug my code, I only asked for some advice which you so kindly provided. I really don't feel the last comment was called for as I never asked ANYONE to debug it, I wouldn't expect someone to take their own time to do that. I was just looking for some advice which you so kindly provided , so thanks for the advice you gave as its actually very helpful.

Reason: