Need help with code

 

Hi!

The following code works perfectly with demo accounts, but it doesn't work with real account. It gives 4746 error, when it try to open position.

Help me please.

#include <Trade/Trade.mqh>

input double Lots = 0.1;

string state;
double StopLoss;
double TakeProfit;
string PrevState;

CTrade order;

int OnInit(){
   order.SetExpertMagicNumber(321);
   return(0);
}

void OnDeinit(const int reason){
}

void OnTick(){

   int handle = FileOpen(Symbol() + "_state.txt", FILE_READ|FILE_TXT|FILE_ANSI);
  
   state = FileReadString(handle);
   TakeProfit = (double)FileReadString(handle);
   StopLoss = (double)FileReadString(handle);
  
   FileClose(handle);
  
   if(state == "close" && PositionSelect(Symbol())){
      order.PositionClose(Symbol());
      return;
   }
  
   if(!state || PositionSelect(Symbol()) || state == PrevState){
      return;
   }
  
   PrevState = state;

   if(state == "buy"){
      order.Buy(Lots, Symbol(), NULL, StopLoss, TakeProfit);
   }
   if(state == "sell"){
      order.Sell(Lots, Symbol(), NULL, StopLoss, TakeProfit);
   }
  
   if(PositionSelect(Symbol())){
      order.PositionModify(Symbol(),StopLoss, TakeProfit);
   }

}

Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Account Properties - Documentation on MQL5
 

Might you mean error code 4756?
Are you sure that trading by Experts is enabled on your real account

Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Account Properties - Documentation on MQL5
Reason: