Student Question: My Code works for FX, US500, XAUUSD but not FSTE100.....why?

 

Hi guys,


New to algo trading still learning day by day :)

Thanks for stopping to have a look at this. the code works fine for most the securities I look at and investin with the

expection of the index UK100......why is that? 


//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+

#include <Trade\Trade.mqh>
CTrade trade;



//+------------------------------------------------------------------+
//| Execute Once                                                     |
//+------------------------------------------------------------------+


void OnInit()
  {
   
   //Test
   
   Alert("Expert Advisor has been launched"); //display message when robot is launched 
   
   
   
 
   //Order Setup
   
   int MagicNumber=123456; //--- set MagicNumber for your orders identification
   trade.SetExpertMagicNumber(MagicNumber);

   int deviation=10; //--- set available slippage in points when buying/selling
   trade.SetDeviationInPoints(deviation);
 
   trade.SetTypeFilling(ORDER_FILLING_RETURN); //--- order filling mode, the mode allowed by the server should be used
 
   trade.LogLevel(1); //--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
   
   trade.SetAsyncMode(true); //--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
   
   
   
   
   //Trading Critera 
 
   int x = 0;
   
   if (x == 1){
      HeraBuy();
   }
   
   else{
      HeraSell();
   }   
   
   
  }




//+------------------------------------------------------------------+
//| End                                                             |
//+------------------------------------------------------------------+


void OnDeinit(const int reason)
  {
      
      Alert("Expert Advisor terminated");
      
  }
  
  
  
 
//+------------------------------------------------------------------+
//| Main Loop                                                        |
//+------------------------------------------------------------------+  



void OnTick()
  {
      
      /*
      double Bid;
      Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
      Alert("Your new Bid price is: " + string(Bid)); //Display the bid price continuously
      */
   
  }
  
  
  
  
  
  
 
  
//+------------------------------------------------------------------+
//| External Functions                                               |
//+------------------------------------------------------------------+


int HeraSell(){
   Alert("Hera is Selling");
   
   
   //StopLoss & TakeProfit Setup
   double Bid;
   double Ask;

   double TakeProfit;
   double StopLoss;

   double TakeProfitLevel;
   double StopLossLevel;

   Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   TakeProfit = 60;
   StopLoss = 60;
   
   TakeProfitLevel = Bid - TakeProfit*Point();   //0.0001 // Take Profit value defined
   StopLossLevel = Bid + StopLoss*Point(); // Stop loss value defined
   
   /*
   //Code For 5-Digit Brokers
   //Here we are assuming that the TakeProfit and StopLoss are entered in Pips
   
   TakeProfitLevel = Bid + TakeProfit*Point*10;   //0.00001 * 10 = 0.0001
   StopLossLevel = Bid - StopLoss*Point*10;
   */
   
   
   
   if(!trade.Sell(0.1,_Symbol, Bid, StopLossLevel, TakeProfitLevel))
     {
      //--- failure message
      Print("Sell() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
     }
   else
     {
      Print("Sell() method executed successfully. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
     }

   return(0);
}



int HeraBuy(){
   Alert("Hera is Buying");
   
   
   //StopLoss & TakeProfit Setup
   double Bid;
   double Ask;

   double TakeProfit;
   double StopLoss;

   double TakeProfitLevel;
   double StopLossLevel;

   Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   TakeProfit = 60;
   StopLoss = 60;
   
   TakeProfitLevel = Ask + TakeProfit*Point();   //0.0001 // Take Profit value defined
   StopLossLevel = Ask - StopLoss*Point(); // Stop loss value defined
   
   /*
   //Code For 5-Digit Brokers
   //Here we are assuming that the TakeProfit and StopLoss are entered in Pips
   
   TakeProfitLevel = Bid + TakeProfit*Point*10;   //0.00001 * 10 = 0.0001
   StopLossLevel = Bid - StopLoss*Point*10;
   */
   
   
   
   if(!trade.Buy(0.1,_Symbol, Ask, StopLossLevel, TakeProfitLevel))
     {
      //--- failure message
      Print("Buy() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
     }
   else
     {
      Print("Buy() method executed successfully. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
     }
     
   return(0);
}
 

“Doesn't work” - these words do not say anything.

Look in the Terminal Log - there will be a description of the error. (tabs "Experts" and "Journal").

 
NUWETIS:

Hi guys,


New to algo trading still learning day by day :)

Thanks for stopping to have a look at this. the code works fine for most the securities I look at and investin with the

expection of the index UK100......why is that? 


void OnInit()
  {
   
   //Test
   
   Alert("Expert Advisor has been launched"); //display message when robot is launched 
   
   
   
 
   //Order Setup
   
   int MagicNumber=123456; //--- set MagicNumber for your orders identification
   trade.SetExpertMagicNumber(MagicNumber);

   int deviation=10; //--- set available slippage in points when buying/selling
   trade.SetDeviationInPoints(deviation);
 
   trade.SetTypeFilling(ORDER_FILLING_RETURN); //--- order filling mode, the mode allowed by the server should be used
 
   trade.LogLevel(1); //--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
   
   trade.SetAsyncMode(true); //--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
   
   
   
   
   //Trading Critera 
 
   int x = 0;
   
   if (x == 1){
      HeraBuy();
   }
   
   else{
      HeraSell();
   }   
   
   
  }

Move that part from the OnInit() to the OnTick() : 

void OnTick()
  {
      
   if (x == 1){
      HeraBuy();
   }
   
  if (x == -1) {
      HeraSell();
   }   
   
  }

Use 1 for buy, -1 for sell, 0 for nothing (x == 0 is set during initialization)

then ... just set x in the direction you wish according to your strategy ; when you'll change it, don't forget to always reset it to 0 (neutral) otherwise it will continue buying/selling

 

the messages i am getting in my journal are:


" expert ExpertName (UK100,H1) loaded successfully "

" '50153015': failed market sell 0.10 UK100 sl: 7193.80 tp: 7192.60 [Invaild stops] "

 
NUWETIS:

the messages i am getting in my journal are:


" expert ExpertName (UK100,H1) loaded successfully "

" '50153015': failed market sell 0.10 UK100 sl: 7193.80 tp: 7192.60 [Invaild stops] "

   TakeProfitLevel = Bid - (TakeProfit+SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL))*Point();   //0.0001 // Take Profit value defined
   StopLossLevel = Ask + (StopLoss+SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL))*Point(); // Stop loss value defined

On indexes there's often a minimum stop/freeze level you should respect : check the symbol properties. 

You sell @ bid and buy @ ask take care of that when calculating your levels : when it'll hit the stop, it'll also be on a bid or ask price

You could also add the spread : SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)

and a checking routine to avoid these errors if you plan to push the ea on the mql's market : the automatic validation is particularly bitchy with errors

 
Icham Aidibe:

On indexes there's often a minimum stop/freeze level you should respect : check the symbol properties. 

You sell @ bid and buy @ ask take care of that when calculating your levels : when it'll hit the stop, it'll also be on a bid or ask price

You could also add the spread : SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)

and a checking routine to avoid these errors if you plan to push the ea on the mql's market : the automatic validation is particularly bitchy with errors

That's a completely incorrect advice. You need to check the STOPS_LEVEL but not include them in the SL/TP.

And both SL/TP have to be calculated from the same price.

See the above link to learn how it works.

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks, as a small error in the expert or indicator logic can cause losses on the trading account. That is why we have developed a series of basic checks to ensure the required quality level of the Market products. If any errors are identified by the Market...
Reason: