Download MetaTrader 5
To add comments, please log in or register
Do you want to become a well-known developer? Execute the orders of traders in the Freelance service!
serz89
5
serz89 2011.11.23 23:20 

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
Alexey Da
Moderator
6587
Alexey Da 2011.11.24 11:15  

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
/
To add comments, please log in or register