![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Thank you for you're help, Mladen. I tweaked with the code a little until I got it compiling without any errors, but the EA still stops and reversed after a loss. This is the exact code I'm using now: double GetLastPL() Gives me the following error: '(' - function definition unexpected.
BlackCoq
You must do all the logical comparisons within the loop because functions like OrderProfit() are returning the profit of the currently selected order from a list of closed orders. If it is not within the main loop (for(int i=OrdersHistoryTotal()-1;i>=0;i--)) then only the last selected order will be tested and it does not mean that it is the last order (metaquotes explicitly states in their documentation that the orders in the list of closed (or still opened) orders do not have to be in any particular order - it is a responsibility of coders to check the order)
BlackCoq You must do all the logical comparisons within the loop because functions like OrderProfit() are returning the profit of the currently selected order from a list of closed orders. If it is not within the main loop (for(int i=OrdersHistoryTotal()-1;i>=0;i--)) then only the last selected order will be tested and it does not mean that it is the last order (metaquotes explicitly states in their documentation that the orders in the list of closed (or still opened) orders do not have to be in any particular order - it is a responsibility of coders to check the order)
After many hours failing with this seemingly easy task I've decided to study a lot more programming before continuing. Thank you for your help anyway!
When I tried this the EA wouldn't open any positions at all:
{ double TempLastOrderProfit = 0; datetime CloseTime = 0; for(int i=OrdersHistoryTotal()-1;i>=0;i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime) { CloseTime = OrderCloseTime(); } } { if(StopAndReverse==true && subTotalTrade()>0 && OrderProfit()>0) { if((LastTrade=="BUY" && SellCondition==true) || (LastTrade=="SELL" && BuyCondition==true)) { subCloseOrder(); if(subTotalTrade()>0) subCloseOrder(); if(subTotalTrade()>0) subCloseOrder();
if(IsTesting() && PrintControl==true) Print("STOP AND REVERSE !"); } } } }[/PHP]
And with this, it would "stop and reverse" even if last trade was a loss:
[PHP] { double TempLastOrderProfit = 0; datetime CloseTime = 0; for(int i=OrdersHistoryTotal()-1;i>=0;i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime) { CloseTime = OrderCloseTime(); TempLastOrderProfit = OrderProfit() + OrderSwap()+ OrderCommission(); } } return(TempLastOrderProfit); { if(StopAndReverse==true && subTotalTrade()>0 && TempLastOrderProfit>0) { if((LastTrade=="BUY" && SellCondition==true) || (LastTrade=="SELL" && BuyCondition==true)) { subCloseOrder(); if(subTotalTrade()>0) subCloseOrder(); if(subTotalTrade()>0) subCloseOrder();
if(IsTesting() && PrintControl==true) Print("STOP AND REVERSE !"); } } } }Again, thank you.
After many hours failing with this seemingly easy task I've decided to study a lot more programming before continuing. Thank you for your help anyway!
When I tried this the EA wouldn't open any positions at all:
{ double TempLastOrderProfit = 0; datetime CloseTime = 0; for(int i=OrdersHistoryTotal()-1;i>=0;i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime) { CloseTime = OrderCloseTime(); } } { if(StopAndReverse==true && subTotalTrade()>0 && OrderProfit()>0) { if((LastTrade=="BUY" && SellCondition==true) || (LastTrade=="SELL" && BuyCondition==true)) { subCloseOrder(); if(subTotalTrade()>0) subCloseOrder(); if(subTotalTrade()>0) subCloseOrder();
if(IsTesting() && PrintControl==true) Print("STOP AND REVERSE !"); } } } }[/PHP]
And with this, it would "stop and reverse" even if last trade was a loss:
[PHP] { double TempLastOrderProfit = 0; datetime CloseTime = 0; for(int i=OrdersHistoryTotal()-1;i>=0;i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime) { CloseTime = OrderCloseTime(); TempLastOrderProfit = OrderProfit() + OrderSwap()+ OrderCommission(); } } return(TempLastOrderProfit); { if(StopAndReverse==true && subTotalTrade()>0 && TempLastOrderProfit>0) { if((LastTrade=="BUY" && SellCondition==true) || (LastTrade=="SELL" && BuyCondition==true)) { subCloseOrder(); if(subTotalTrade()>0) subCloseOrder(); if(subTotalTrade()>0) subCloseOrder();
if(IsTesting() && PrintControl==true) Print("STOP AND REVERSE !"); } } } }BlackCoq
If you use the function I posted as is, it will return you the exact profit of the last closed order (no need to change anything in it)
BlackCoq If you use the function I posted as is, it will return you the exact profit of the last closed order (no need to change anything in it)
When I try to copy the function, I get numerous errors no matter how I try to tweak it.
This code:
{
double GetLastPL()
{
double TempLastOrderProfit = 0;
datetime CloseTime = 0;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime)
{
CloseTime = OrderCloseTime();
TempLastOrderProfit = OrderProfit() + OrderSwap()+ OrderCommission();
}
}
return(TempLastOrderProfit);
{
if(StopAndReverse==true && subTotalTrade()>0 && TempLastOrderProfit>0)
{
if((LastTrade=="BUY" && SellCondition==true) || (LastTrade=="SELL" && BuyCondition==true))
{
subCloseOrder();
if(subTotalTrade()>0) subCloseOrder();
if(subTotalTrade()>0) subCloseOrder();
if(IsTesting() && PrintControl==true) Print("STOP AND REVERSE !");
}
}
}
}Gives me the errors:
Compiling 'universalMAcrossECNmartingaleSAR.mq4'...
'(' - function definition unexpected (538, 20)
'TempLastOrderProfit' - variable not defined (548, 15)
'TempLastOrderProfit' - variable not defined (551, 15)
'TempLastOrderProfit' - variable not defined (553, 52)
When I try to copy the function, I get numerous errors no matter how I try to tweak it.
This code:
if(IsTesting() && PrintControl==true) Print("STOP AND REVERSE !"); } } } }[/PHP]
Gives me the errors:
[PHP]
Compiling 'universalMAcrossECNmartingaleSAR.mq4'... '(' - function definition unexpected (538, 20)'TempLastOrderProfit' - variable not defined (548, 15)'TempLastOrderProfit' - variable not defined (551, 15)'TempLastOrderProfit' - variable not defined (553, 52)
BlackCoq
That function must be pasted outside any other function. Here is a very simple ea that shows how it works and how it should be used
BlackCoq That function must be pasted outside any other function. Here is a very simple ea that shows how it works and how it should be used
I'm sure I'm missing something here, but even if I try to copy the function outside any other function in the EA, I still get the following errors:
'(' - function definition unexpected (after double GetLastPL())
'TempLastOrderProfit' - variable not defined (554, 15)
'TempLastOrderProfit' - variable not defined (557, 15)
I'm sure I'm missing something here, but even if I try to copy the function outside any other function in the EA, I still get the following errors:
'(' - function definition unexpected (after double GetLastPL())
'TempLastOrderProfit' - variable not defined (554, 15)
'TempLastOrderProfit' - variable not defined (557, 15)Sory, but without your code I have no idea what are you doing
As you can see from that test EA it works with no problem at all
Sory, but without your code I have no idea what are you doing As you can see from that test EA it works with no problem at all
I get several errors when I try to compile the EA you attached also. Are we using different versions of MT4?
I'm attaching the EA I'm trying to add the function before "stop and reverse" to.
I get several errors when I try to compile the EA you attached also. Are we using different versions of MT4? I'm attaching the EA I'm trying to add the function before "stop and reverse" to.
If you are using build 509 then replace "void start(void)" with "void start()" and you will get no compiling errors at all. See the examples for both builds build 509 and build 610 (metaeditor version for build 610 is 887)
Also attaching the EA with the GetLastPL() function already attached to it. All you have to do is to make a call to GetLastPL() at a place in the code where you wish it to check if the profit of the last order was positive or negative
_________________________
PS: it will compile in build 509 as well as in build 610. In 610 you will get 3 warnings but those are irrelevant for EA operation
If you are using build 509 then replace "void start(void)" with "void start()" and you will get no compiling errors at all. See the examples for both builds build 509 and build 610 (metaeditor version for build 610 is 887)
Also attaching the EA with the GetLastPL() function already attached to it. All you have to do is to make a call to GetLastPL() at a place in the code where you wish it to check if the profit of the last order was positive or negative
_________________________
PS: it will compile in build 509 as well as in build 610. In 610 you will get 3 warnings but those are irrelevant for EA operationWorking perfectly now. Thank you so much for your help and patience!