Problems with a StopLoss

 

Hey all.

I am now to coding, and am trying my best, however I seem to have hit a wall with this one. This EA is 'hacked' together with heaps of code from other EA's, so it doesnt flow the best yet, as I have not cleaned it up. (any tips on cleaning it up would be appreciated)

However the main problem I am having is "Open Sell Stop Order - Error 130: Invalid Stops"

It also occurs alot more often on the Sell side rather than the buy. I have attached the EA.

Any ideas would be welcome.

EA:

//+------------------------------------------------------------------+
//|                                                Inside Bar EA.mq4 |
//|                                                    Michael Clegg |
//|                                                                  |
//+------------------------------------------------------------------+

// Preprocessor
#property copyright "Michael Clegg"
#include <IncludeExample.mqh>


// External variables
extern double LotSize = 1;
extern double StopLoss = 0;
extern double TakeProfit = 10;

extern int TrailingStop = 0;
extern int MinimumProfit = 0;
extern int Slippage = 5;
extern int MagicNumber = 123;

extern bool CheckOncePerBar = true;


// Global variables
int BuyTicket;
int SellTicket;

double UsePoint;
int UseSlippage;
int ClostTicket;

datetime CurrentTimeStamp;


// Init function
int init()
        {
                UsePoint = PipPoint(Symbol());
                UseSlippage = GetSlippage(Symbol(),Slippage);
        }
        

// Start function
int start()
        {
                // Execute on bar open
                if(CheckOncePerBar == true)
                        {
                                int BarShift = 1;
                                if(CurrentTimeStamp != Time[0]) 
                                        {
                                                CurrentTimeStamp = Time[0];
                                                bool NewBar = true;
                                        }
                                else NewBar = false;
                        }
                else 
                        {
                                NewBar = true;
                                BarShift = 0;
                        }
                
                // Inside Bar Recognition

                double LastBarHigh=iHigh(NULL,0,1);
                double LastBarLow=iLow(NULL,0,1);
                double LastBarHighTwo=iHigh(NULL,0,2);
                double LastBarLowTwo=iLow(NULL,0,2);
                
                //Calculate Stop Loss & Take Profit
                double BuyStopLoss = LastBarLowTwo;
                double BuyTakeProfit = TakeProfit;
                double SellStopLoss = LastBarHighTwo;
                double SellTakeProfit = TakeProfit; 

                // Begin trade block
                if(NewBar == true){                        
                
                                // Buy & Sell Orders 
                                if(LastBarHigh < LastBarHighTwo && LastBarLow > LastBarLowTwo){      
                                
               // Open Pending Buy 
                                                 double BuyHigh = LastBarHighTwo;
                                                 BuyTicket = OpenBuyStopOrder(Symbol(),LotSize,BuyHigh,BuyStopLoss,BuyTakeProfit,UseSlippage,MagicNumber,0);
                                                                                              
                                        // Open Pending Sell 
                                            double SellLow = LastBarLowTwo;
                                                 SellTicket = OpenSellStopOrder(Symbol(),LotSize,SellLow,SellStopLoss,SellTakeProfit,UseSlippage,MagicNumber,0);
                                                         
                                        }
               // End trade block 
               }
               
               
            // Close Buy & Sell Pending Orders 
            if(BuyMarketCount(Symbol(),MagicNumber) > 0){
            deleteallpendingorders(MagicNumber);
            }
            
            if(SellMarketCount(Symbol(),MagicNumber) > 0){
            deleteallpendingorders(MagicNumber);
                           }
                                                
                // Adjust trailing stops        
                if(BuyMarketCount(Symbol(),MagicNumber) > 0 && TrailingStop > 0)
                        {
                                BuyTrailingStop(Symbol(),TrailingStop,MinimumProfit,MagicNumber);
                        }

                if(SellMarketCount(Symbol(),MagicNumber) > 0 && TrailingStop > 0)
                        {
                                SellTrailingStop(Symbol(),TrailingStop,MinimumProfit,MagicNumber);
                        }
                        
        Comment("LastBarHigh=" + LastBarHigh+"\nLastBarLow=" + LastBarLow+"\nLastBarHighTwo=" + LastBarHighTwo+"\nLastBarLowTwo=" + LastBarLowTwo);
                        
                return(0);
        }
 
NewCoder47:

Hey all.

I am now to coding, and am trying my best, however I seem to have hit a wall with this one. This EA is 'hacked' together with heaps of code from other EA's, so it doesnt flow the best yet, as I have not cleaned it up. (any tips on cleaning it up would be appreciated)

However the main problem I am having is "Open Sell Stop Order - Error 130: Invalid Stops"

It also occurs alot more often on the Sell side rather than the buy. I have attached the EA.

Any ideas would be welcome.

EA:

First: read and implement what is in this thread: What are Function return values ? How do I use them ?

Second: what happens if the bar you are using is tiny ? you can't have your entry and stop at the same level, you need to code a check on this . . . this article tells you what you need to check for: Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

Third: your takeprofit appears to be always 10.000 ?? on GBPUSD that is a long way away . . . on GBPJPY it is a log way away in the other direction . . .

 

Hey Raptor.

Thanks for the "Function Return Values" That will come in handy

As I said, there are a heap of other holes in it, that 10 is simply left in there from another test I did. The issue about the small bar, again, havent developed that check yet either.

The only part of this code that I am focused on at the moment is fixing the stop loss error.

Mike

 
NewCoder47:

Hey Raptor.

1. Thanks for the "Function Return Values" That will come in handy

As I said, there are a heap of other holes in it, that 10 is simply left in there from another test I did.

2. The issue about the small bar, again, havent developed that check yet either.

3. The only part of this code that I am focused on at the moment is fixing the stop loss error.

Mike

1. isn't handly . . . it is essential. Add it to your code.

2. needs to be addressed as it's most probably causing 3.

Read your code, follow through line by line when you have a bar 2 candle that has a high and low with the same value . . . what happens then ?

Reason: