Attach all the code, can not see what is wrong from being given part code. The usual reason is a forgotton } or ) somewhere in the previous code.
Attach all the code, can not see what is wrong from being given part code. The usual reason is a forgotton } or ) somewhere in the previous code.
ok, all I want is to determing the trade size in pips, so i can use it in the take profit code
//+------------------------------------------------------------------+
//| Take_Profit_v1.mq4 |
//| Michael |
//| |
//+------------------------------------------------------------------+
#property copyright "Michael"
extern int First_Target = 20;
extern int Target_Increment = 20;
extern double Close_Lots = 0.5;
int nextTP;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
nextTP = First_Target;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
nextTP = First_Target;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int orders = OrdersTotal();
int ticket = OrderTicket();
int sl = 0;
double pips = 0;
int dir = OrderType();
double ord = OrderOpenPrice();
int ord_n = (NormalizeDouble(ord,Digits))/(Point);
double bid = MarketInfo (Symbol(), MODE_BID);
string bid_a = DoubleToStr(bid,5);
double ask = MarketInfo (Symbol(), MODE_ASK);
string ask_a = DoubleToStr(ask,5);
double open = MarketInfo (Symbol(), MODE_OPEN);
string open_a = DoubleToStr(bid,5);
//---- determining the value of pips
double getPipValue(double ord,int dir)
{
double val;
RefreshRates();
if(dir == 1) val=(NormalizeDouble(ord,Digits) - NormalizeDouble(Ask,Digits));
else val=(NormalizeDouble(Bid,Digits) - NormalizeDouble(ord,Digits));
val = val/Point;
return(val);
}
double pips = getPipValue(OrderOpenPrice(), OrderType());
//---- Manage Tale Profit
if (orders==1)
{
if (OrderSelect(ticket, SELECT_BY_TICKET)==true)
{
if (pips<nextTP && pips < (nextTP + Target_Increment) )
{
if (OrderType()==1)
{
if (OrderClose(ticket,Close_Lots,Ask,3,Green) )
{
nextTP+=Target_Increment ;
}
else
{
Alert("Error closing order : ",GetLastError() );
}
}
else
{
if (OrderClose(ticket,Close_Lots,Bid,3,Blue) )
{
nextTP+=Target_Increment ;
}
else
{
Alert("Error closing order : ",GetLastError() );
}
}
}
}
}
Comment ("ASK VALUE : ",ask_a, "\n",
"BID VALUE : ",bid_a, "\n",
"OPEN VALUE : ",open_a, "\n",
"PIP VALUE : ",pips, "\n" );
//----
return(0);
}
//+------------------------------------------------------------------+
A few comments to help you understand. - Did not try to understand what code is trying to do.
//+------------------------------------------------------------------+ //| Take_Profit_v1.mq4 | //| Michael | //| | //+------------------------------------------------------------------+ #property copyright "Michael" //GlobalVarables extern int First_Target = 20; extern int Target_Increment = 20; extern double Close_Lots = 0.5; int nextTP; // init() function not needed as could use "int nextTP=First_Target ;" would do the same //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { nextTP = First_Target; return(0); } // end of function init() //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { nextTP = First_Target; // This line is called when the experts is removed from the chart so value in nextTP are lost return(0); } //end of function deinit() //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int orders = OrdersTotal(); //int, double etc reserve memory space for variable name and set its initial value only once int ticket = OrderTicket(); int sl = 0; double pips = 0; int dir = OrderType(); double ord = OrderOpenPrice(); int ord_n = (NormalizeDouble(ord,Digits))/(Point); double bid = MarketInfo (Symbol(), MODE_BID); string bid_a = DoubleToStr(bid,5); double ask = MarketInfo (Symbol(), MODE_ASK); string ask_a = DoubleToStr(ask,5); double open = MarketInfo (Symbol(), MODE_OPEN); string open_a = DoubleToStr(bid,5); //---- determining the value of pips double pips = getPipValue(OrderOpenPrice(), OrderType()); //variable pips has already been declared //---- Manage Tale Profit if (orders==1) { if (OrderSelect(ticket, SELECT_BY_TICKET)==true) { if (pips<nextTP && pips < (nextTP + Target_Increment) ) { if (OrderType()==1) { if (OrderClose(ticket,Close_Lots,Ask,3,Green) ) { nextTP+=Target_Increment ; } else { Alert("Error closing order : ",GetLastError() ); } } else { if (OrderClose(ticket,Close_Lots,Bid,3,Blue) ) { nextTP+=Target_Increment ; } else { Alert("Error closing order : ",GetLastError() ); } //end of if (OrderClose(ticket,Close_Lots,Bid,3,Blue) ) } } } } Comment ("ASK VALUE : ",ask_a, "\n","BID VALUE : ",bid_a, "\n","OPEN VALUE : ",open_a, "\n", "PIP VALUE : ",pips, "\n" ); return(0); } //end of function start() //+------------------------------------------------------------------+ //function origionally declared before finishing start() function ; double getPipValue(double ord,int dir) { double val; RefreshRates(); if(dir == 1) val=(NormalizeDouble(ord,Digits) - NormalizeDouble(Ask,Digits)); //use indent to show consequence of previous line else val=(NormalizeDouble(Bid,Digits) - NormalizeDouble(ord,Digits)); //use indent to show consequence of previous line val = val/Point; return(val); } // end of function getPipValue()
A few comments to help you understand. - Did not try to understand what code is trying to do.
Thankyou very much
I am trying to learn how the TakeProfit EA works. I dont fully understand how functions work so I am stringing the whole thing out into nested if statements so that the logic makes sense.
I would like to eventually modify the TakeProfit EA so that it does more than what it does now. it is all a learning process for me. I sit here all day from about 9am, last night I stopped at 11:30pm.
Thank you for helping me learn this.
Where you put that function - outside the start function, is this where it should be always ?
In some computer languages it is posible to put functions inside functions but NOT in MetaTrader which uses a slight variant on the 'C' programming language.
When you are starting to learn and in complex programs it is important to know where a piece of code begins AND ends So that you can maintain clarity when Debugging a program.
For MetaTrader use print and/or Alerts to watch how the logic of the program works.
MT recieves price data and if there is a chart open then updates that chart the chart has the option of calling functions like an expert advisor, script, or indicator. So every time new price data is recieved then actions functions are processed to accomodate the new data.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
This function I have from another EA, and I am trying to insert it into my own EA, but when I do I get lots of errors, including "function definition unexpected". This is the function
double getPipValue(double ord,int dir)
{
double val;
RefreshRates();
if(dir == 1) val=(NormalizeDouble(ord,Digits) - NormalizeDouble(Ask,Digits));
else val=(NormalizeDouble(Bid,Digits) - NormalizeDouble(ord,Digits));
val = val/Point;
return(val);
}
.
double pips = getPipValue(OrderOpenPrice(), OrderType());
.
Can anyone tell me how to make this work in my EA please ?
Michael