How to code? - page 287

 

...

Take aq look at this post : https://www.mql5.com/en/forum/174385/page12

It was about similar problem and you can use that same countOpenedOnACurrentBar

function to limit the to open only one trade per crossover

dipu:
hi friends, I am a newbie. I have a simple strategy. I just scalping when EMA crossover. I just take only one trade in every crossover. I need some code which can take trade only one trade in every EMA crossover. Please......
 

Simply EMA crossover code need........

Thank you very much, MLADEN...

I just check your function. It's look good. I also need some help. I have need to add RSI(50) value with EMA crossover....how I can..?

Pairs: GBP/USD & EUR/USD

Timeframe: 15min,1 hour and above;

-------------------------------

BUY: EMA 5 Cross EMA 12 to upside, RSI greater than 50

SELL: EMA 5 Cross EMA 12 to downside, RSI less than 50

(only one trade in every EMA crossover)

I am waiting for reply. Please.......

 

You can do that on a couple of ways.

One way (directly in EA) could be something like this :

double macdc = iMA(NULL,0,EmaFast,0,MODE_SMA,EmaPrice,0)-iMA(NULL,0,EmaSlow,0,MODE_SMA,EmaPrice,0);

double macdp = iMA(NULL,0,EmaFast,0,MODE_SMA,EmaPrice,1)-iMA(NULL,0,EmaSlow,0,MODE_SMA,EmaPrice,1);

double rsi = iRSI(NULL,0,RsiPeriod,RsiPrice,0);

if (macdc*macdp<0) // change of sign, signal for a new order

{

if (macdc>0 && rsi>50) ... code for buy

if (macdc<0 && rsi<50) ... code for sell

}

[/PHP]

The good thing about it is that it is small (the code) and does not take any significant process time. The bad thing when using conditions like that in an EA is that you have to imagine (and test, test, test ...) what is it going to perform like. I prefer making a "binary" indicator with which the it is very easy to visually inspect if the condition has any logic in it and if it is going to be profitable or not and then call that indicator (using iCustom()) from the EA.

Here is how would the indicator with the above conditions look like in "binary" form :

You have all the information needed : the gray line is the "trend". Green and Red lines can be used as entry and re-entry points. You can "optimize" it visually in an instant, and then use those parameters in the EA (much faster "optimization" visually than the painful "change parameters, run it, see what you got and then same thing over and over". And it is rather easy to call it from the EA. It would go like this :

[PHP] double trendc = iCustom(NULL,0,"ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,0);

double trendp = iCustom(NULL,0,"ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,1);

if (trendc!=trendp)

{

if (trendc== 1) ... signal for buy

if (trendc==-1) ... signal for sell

}

So all in all, I always recommend using an indicator usage from an Ea. You can even change the logic of the Ea without touching it just by altering and refining your indicators code in which case the EA is "just" a framework for orders and money management (since the signals are coming from the indicator) which in my opinion is a most efficient way of using EAs. Attaching the indicator too, so you decide which way (the direct or the "by indicator" way) you want to use in your Ea

PS: in the above examples, tests are conduucted on the current (still opened) bar. If you wiish to test closed bar, replace the 0 and 1 with 1 and to in calls to iCustom(),iMa() and iRSI()

dipu:
Thank you very much, MLADEN...

I just check your function. It's look good. I also need some help. I have need to add RSI(50) value with EMA crossover....how I can..?

Pairs: GBP/USD & EUR/USD

Timeframe: 15min,1 hour and above;

-------------------------------

BUY: EMA 5 Cross EMA 12 to upside, RSI greater than 50

SELL: EMA 5 Cross EMA 12 to downside, RSI less than 50

(only one trade in every EMA crossover)

I am waiting for reply. Please.......
Files:
 

1st of the month balance?

Gidday I have been sitting here trying to figure out if there is a way to extract the starting balance for the 1st of the month for an on screen display.

What I am trying to do is get the percentage difference between the balance on the 1st and the current balance.

I have the over all percentage difference.

(Balance - Deposit)/Deposit*100

I would like to add a monthly difference.

(Balance - 1st)/1st*100

Any hints tips or actual code would be great.

Cheers

Beno

 

Thanks again, MALADEN...

Thanks for your indicator and advice. I just try to follow your instruction. But as I am unskilful on mq4, I failed to do it................

HERE IS MY..............

------------------------------------

-----------------------------------

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 LimeGreen

#property indicator_color2 PaleVioletRed

#property indicator_color3 DarkGray

#property indicator_width1 2

#property indicator_width2 2

#property indicator_minimum -1.1

#property indicator_maximum +1.1

extern int EmaFast = 5;

extern int EmaSlow = 12;

extern int EmaPrice = PRICE_CLOSE;

extern int RsiPeriod = 14;

extern int RsiPrice = PRICE_CLOSE;

double signup[];

double signdn[];

double trend[];

extern double TakeProfit=15.0;

extern double Lots=0.1;

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int init()

{

return(0);

}

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int start()

{

int count,counted_bars=IndicatorCounted();

if(counted_bars < 0) return(-1);

if(counted_bars>0) counted_bars--;

int limit = MathMin(Bars-counted_bars,Bars-1);

int cnt, ticket, total;

double trendc = iCustom(NULL,0,"ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,0);

double trendp = iCustom(NULL,0,"ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,1);

if (trendc!=trendp)

total = OrdersTotal();

if(total < 1)

{

if (trendc== 1) //... signal for buy

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA",12345,0,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if (trendc==-1) //... signal for sell

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

}

return(0);

}

---------------------------------------

---------------------------------------

I need your help....

I apologized to u if I aspect more to u as you fell disturb. But I really helpless...

I am waiting for reply. Please.......

 

...

Try using something like this :

double balanceAtStartOfMonth(int forMonth)

{

double current = AccountBalance();

datetime startTime = iTime(NULL,PERIOD_MN1,forMonth);

//

//

//

//

//

for (int k=OrdersHistoryTotal()-1; k>=0; k--)

{

if (!OrderSelect(k,SELECT_BY_POS,MODE_HISTORY)) break;

if (OrderCloseTime()<startTime) continue;

current -= (OrderProfit()+OrderSwap());

}

return(current);

}

forMonth is the month backward for which you wish to see the balance 0 being the current month. It then calculates what was the balance on the first day of the required month. Function will take into account deposits and withdrawals too (OrderType() == 6) so it should work OK.

Beno:
Gidday I have been sitting here trying to figure out if there is a way to extract the starting balance for the 1st of the month for an on screen display.

What I am trying to do is get the percentage difference between the balance on the 1st and the current balance.

I have the over all percentage difference.

(Balance - Deposit)/Deposit*100

I would like to add a monthly difference.

(Balance - 1st)/1st*100

Any hints tips or actual code would be great.

Cheers

Beno
 

...

You can not manage orders from indicator. You can manage orders only from scripts or EAs (which is the case you are looking for). Code from your post is an indicator.

Examples I posted are to be used from an EA (as far as I understood you already have an EA in which you want to limit number of orders and wished to test ema and rsi conditions for entries).

dipu:
Thanks again, MALADEN...

Thanks for your indicator and advice. I just try to follow your instruction. But as I am unskilful on mq4, I failed to do it................

HERE IS MY..............

------------------------------------

-----------------------------------

#property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 LimeGreen

#property indicator_color2 PaleVioletRed

#property indicator_color3 DarkGray

#property indicator_width1 2

#property indicator_width2 2

#property indicator_minimum -1.1

#property indicator_maximum +1.1

extern int EmaFast = 5;

extern int EmaSlow = 12;

extern int EmaPrice = PRICE_CLOSE;

extern int RsiPeriod = 14;

extern int RsiPrice = PRICE_CLOSE;

double signup[];

double signdn[];

double trend[];

extern double TakeProfit=15.0;

extern double Lots=0.1;

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int init()

{

return(0);

}

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int start()

{

int count,counted_bars=IndicatorCounted();

if(counted_bars < 0) return(-1);

if(counted_bars>0) counted_bars--;

int limit = MathMin(Bars-counted_bars,Bars-1);

int cnt, ticket, total;

double trendc = iCustom(NULL,0,"ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,0);

double trendp = iCustom(NULL,0,"ema + rsi binary",EmaFast,EmaSlow,EmaPrice,RsiPeriod,RsiPrice,2,1);

if (trendc!=trendp)

total = OrdersTotal();

if(total < 1)

{

if (trendc== 1) //... signal for buy

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA",12345,0,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if (trendc==-1) //... signal for sell

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

}

return(0);

}

---------------------------------------

---------------------------------------

I need your help....

I apologized to u if I aspect more to u as you fell disturb. But I really helpless...

I am waiting for reply. Please.......
 

Thanks for reply, MLADEN,

Sorry, I think you told me to put that code to a new EA.

I just miss understand your reply....

YES. You are right....

I have EA and wanted to modify. With some new condition.......

my condition is :

------------------------------------------------------

BUY: EMA 5 Cross EMA 12 to upside, RSI greater than 50

SELL: EMA 5 Cross EMA 12 to downside, RSI less than 50

Exit: with small Take-Profit

(only one trade in every EMA crossover)

here my EA.....

---------------------

---------------------

extern double Lots=0.1;

extern int TakeProfit=5;

extern int StopLoss=5;

extern int RSIPos=50;

extern int RSINeg=50;

extern int Slippage=2;

extern int abandon=101;

datetime bartime=0;

int bartick=0;

double p;

int cnt=0;

int OrdersPerSymbol=0;

double bullMA3=0;

double bearMA7=0;

double RSI=0;

bool RSIPOS=0;

bool RSINEG=0;

double TP;

double SL;

int init()

{

if (Symbol()=="AUDUSD") {TakeProfit= 60; StopLoss= 23; abandon=103;}

if (Symbol()=="EURAUD") {TakeProfit= 95; StopLoss=141; abandon=33;}

if (Symbol()=="EURCHF") {TakeProfit= 81; StopLoss= 77; abandon=97;}

if (Symbol()=="EURGBP") {TakeProfit= 11; StopLoss= 77; abandon=108;}

if (Symbol()=="EURJPY") {TakeProfit= 38; StopLoss= 75; abandon=183;}

if (Symbol()=="EURUSD") {TakeProfit=35; StopLoss= 35; abandon=5;}

if (Symbol()=="GBPCHF") {TakeProfit= 79; StopLoss= 98; abandon=113;}

if (Symbol()=="GBPJPY") {TakeProfit= 13; StopLoss= 98; abandon=117;}

if (Symbol()=="GBPUSD") {TakeProfit= 55; StopLoss=100; abandon=69;}

if (Symbol()=="USDCAD") {TakeProfit= 66; StopLoss= 76; abandon=106;}

if (Symbol()=="USDCHF") {TakeProfit=117; StopLoss= 78; abandon=111;}

if (Symbol()=="USDJPY") {TakeProfit= 53; StopLoss= 74; abandon=110;}

}

int deinit()

{

}

int start()

{

p=Point;

// Error checking & bar counting

if(AccountFreeMargin()<(200*Lots)) {Print("-----NO MONEY"); return(0);}

if(Bars<100) {Print("-----NO BARS "); return(0);}

if(bartime!=Time[0]) {bartime=Time[0]; bartick++;}

bullMA3=iMA(Symbol(),0,3,0,MODE_EMA,PRICE_CLOSE,1);

bearMA7=iMA(Symbol(),0,7,0,MODE_EMA,PRICE_CLOSE,1);

RSI=iRSI(Symbol(),0,2,PRICE_CLOSE,2);

if(RSI>RSIPos) {RSIPOS=true; RSINEG=false;}

if(RSI<RSINeg) {RSIPOS=false; RSINEG=true;}

OrdersPerSymbol=0;

for(cnt=OrdersTotal();cnt>=0;cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if( OrderSymbol()==Symbol() )

{

OrdersPerSymbol++;

}

}

if(OrdersPerSymbol==0)

{

if(bullMA3>(bearMA7+p) && RSINEG)

{

SL=Ask-(StopLoss*p);

TP=Ask+(TakeProfit*p);

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,"BUY"+CurTime(),0,0,White);

bartick=0;

}

if(bullMA3<(bearMA7-p) && RSIPOS)

{

SL=Bid+(StopLoss*p);

TP=Bid-(TakeProfit*p);

OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,"SELL"+CurTime(),0,0,Red);

bartick=0;

}

}

if(OrdersPerSymbol==1 && bartick==abandon)

{

if(OrderType()==OP_BUY)

{

OrderClose(OrderTicket(),Lots,Bid,Slippage,White);

SL=Bid+(StopLoss*p);

TP=Bid-(TakeProfit*p);

OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,"SELL"+CurTime(),0,0,Red);

bartick++;

}

if(OrderType()==OP_SELL)

{

OrderClose(OrderTicket(),Lots,Ask,Slippage,Red);

SL=Ask-(StopLoss*p);

TP=Ask+(TakeProfit*p);

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,"BUY"+CurTime(),0,0,White);

bartick++;

}

}

for(cnt=0;cnt<OrdersTotal();cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if( OrderSymbol()==Symbol() )

{

if(OrderType()==OP_BUY)

{

if (OrderMagicNumber()==0)

{

if( Bid-OrderOpenPrice() > 3*Point )

{

OrderClose(OrderTicket(),Lots,Bid,0,White);

return(0);

}

}

}

if(OrderType()==OP_SELL)

{

if (OrderMagicNumber()==0)

{

if( OrderOpenPrice()-Ask > (3*Point) )

{

OrderClose(OrderTicket(),Lots,Ask,0,Red);

return(0);

}

}

}

}

}

return(0);

}

-------------------

---------------------

How can I modify it....?Waiting for reply. Please.......

 

dipu

As far as entries are concerned, this one works OK

Now take care of exits since the way you wrote it you are closing orders too soon Also 5 digit broker is take care of now too, and the entries are working as they should. Default period for rsi is set to 14 (I did not know which one you want to use, in code you use period 2 but that is probably to short). Ema periods are by default 5 and 12. Also, it is testing a closed bar now (it waits till the bar closes and if then the signal for buy r sell exist, it enters the order)

Files:
dipu.mq4  5 kb
 

Thanks for reply, MLADEN,

sorry not for clear information.......

BUY: EMA 5 Cross EMA 12 to upside, RSI[14] greater than 50

SELL: EMA 5 Cross EMA 12 to downside, RSI[14] less than 50

Exit: with small TP/Sl....(5pip)

(only one trade in every EMA crossover)

I just try out it & will inform u the reasult....

May it also modify.. or not..

Thanks again for your help.....

Reason: