Basic questions ... - page 8

 
 
newdigital:
Look at this thread: https://www.mql5.com/en/forum/178677

really thanks , for file opening instruction and download link

 

10points3

PLEAASEEE, PROGRAMMERS!

We need in 10points3 EA, that when the third trade opens, the first one closes, I'm trying but it's still closing all positions.

actually we have:

if (PreviousOpenOrders>OpenOrders)

{

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

mode=OrderType();

if (OrderSymbol()==Symbol() && OrderMagicNumber() == Magic)

{

if (mode==OP_BUY) { OrderClose(OrderTicket(),OrderLots(),OrderClosePri ce(),slippage,Blue); }

if (mode==OP_SELL) { OrderClose(OrderTicket(),OrderLots(),OrderClosePri ce(),slippage,Red); }

return(0);

}

I was thinking to do this:

1. change magic number for the first trade when second one opens, for example magicnumber+1

2. when third trade opens, close the first one, with the magicnumber+1 assigned.

Am I right?

or which other way I can identify the first trade opened to close it later?

If you can guide me or make the changes, better because I'm not a programmer, I'm just learning.

This change would be the start of the Holly Grail!!!

 

10points3

Sorry, the EA.

ANd it's in this thread:

https://www.mql5.com/en/forum/174975

Thanks!

Files:
10p3v0.03_1.mq4  12 kb
 

programming help - return operator

hello

i`m beginner with mt4 programming and now often i try to found a solution for commit variables from a void function to the mainprogram "start()"

below you can see a sample and the question is , how can i commit value from variable "CountOpenSell", "CountOpenBuy","ProfitSell " and "ProfitBuy" to the start() - mainprogram.........

i have added my suggestion (return`s in BOLD letters) but i do`nt know whether is it correct and how is the right code for the "start()" mainprogram???

sorry for my bad english and all of help

thanks a lot

regards

forex2006

void CallBuySellProfit()

{ ProfitBuy=0;

ProfitSell=0;

CountOpenSell=0;

CountOpenBuy=0;

for (i=OrdersTotal()-1;i>=0;i--)

{if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if (OrderType()==OP_SELL) {ProfitSell = ProfitSell + OrderProfit();CountOpenSell++;}

if (OrderType()==OP_BUY) {ProfitBuy = ProfitBuy + OrderProfit();CountOpenBuy++;}

}

else Print( "Error when order select ", GetLastError());

}

return(CountOpenSell);

return(CountOpenBuy);

return(ProfitSell);

return(ProfitBuy);

}

 

forex2006 programming help - return operator

Hi forex2006,

If really want to learn mql, I suggested you try codersguru tutorials, that's where I started. As for your question, an void function cannot return a value, if you want to get values out of void functions, you have to use global variables. The best way for you to get the values out is use an function that returns an value used with parameters to indicate which values you want. It's an simple example below. Try to avoid using global variables in functions as this could make it difficult for you later on.

I hope it helps

int start()

{

double ProfitBuy = CallBuySellProfit(OP_BUY,false);

double ProfitSell = CallBuySellProfit(OP_SELL,false);

int CountOpenBuy = CallBuySellProfit(OP_BUY,true);

int CountOpenSell = CallBuySellProfit(OP_SELL,true);

Comment( "ProfitBuy: "+DoubleToStr(ProfitBuy,2) +"\n"+

"ProfitSell: "+DoubleToStr(ProfitSell,2) +"\n"+

"CountOpenBuy: "+DoubleToStr(CountOpenBuy,2)+"\n"+

"CountOpenSell: "+DoubleToStr(CountOpenSell,2)+"\n"+

"");

return;

}

double CallBuySellProfit(int iOrderType, bool bCount)

{

double dProfit = 0;

int iCount = 0;

for (int i=OrdersTotal()-1;i>=0;i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if(OrderType()==iOrderType)

{

dProfit = dProfit + OrderProfit();

iCount++;

}

}

else

{

Print( "Error when order select ", GetLastError());

}

}//end for

if(bCount)return(iCount);

else return(dProfit);

}//end CallBuySellProfit

 

Stop EA trading on the same bar

Hello, anyway to put code so that the EA checks if trades were already placed on that bar then to not place anymore trades until a new bar?

Thank you

 

matrixebiz, have you considered the possibility of a trade opening and closing in the same candle before the candle has closed? you should probably check the history list as well.

I've always port this little function I wrote to all my EAs, perhaps you may find it useful too.

bool DecideToOpenTrade()

{

int total = OrdersTotal();

if (total > 0)

{

for(int cnt=0;cnt<total;cnt++)

{

if(OrderSelect(cnt,SELECT_BY_POS))

{

if(OrderSymbol()==Symbol() && OrderMagicNumber() == EA_MAGIC_NUM)

{

return (false);

}

}

}

}

// in case trades has already opened and closed within the candle

int histotal = OrdersHistoryTotal();

if (histotal > 0)

{

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

{

if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY))

{

if(OrderSymbol()==Symbol() && OrderMagicNumber() == EA_MAGIC_NUM)

{

if (Time[0] <= OrderOpenTime()) // don't open a new position if we're still on the same candle

{

return (false);

}

}

}

}

}

return (true);

}

int start()

{

// some time check codes first.. blah blah

// ...

// ...

// ...

// check signals

if (Should_Buy())

{

if (DecideToOpenTrade())

{

//... trade opening codes here

}

}

if (Should_Sell())

{

if (DecideToOpenTrade())

{

//... trade opening codes here

}

}

}

note: this function assumes you've set a unique value to EA_MAGIC_NUM. That way the check won't look at trades opened by other EAs.

Should_Buy() and Should_Sell() are functions I create in all my EAs to determine if a buy or sell signal has occurred.

hope this helps. PM me if you need further clarifications.

regards,

Zen

 

updated post below...

 

This seems to be working for me Thank you

void DesideToOpen()

//----------------------------------------------------------------+

//----------------------------------------------------------------+

{

int total = OrdersTotal();

if (total > 0)

{

for(int cnt=0;cnt<total;cnt++)

{

if(OrderSelect(cnt,SELECT_BY_POS))

{

if(OrderComment() == EA_Name + MagicNumber) DecideToOpenTrade = false;

}

}

}

// in case trades has already opened and closed within the candle

int histotal = OrdersHistoryTotal();

if (histotal > 0)

{

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

{

if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY))

{

if(OrderComment() == EA_Name + MagicNumber)

{

if (Time[0] <= OrderOpenTime()) DecideToOpenTrade = false; // don't open a new position if we're still on the same candle

}

}

}

}

}