Coding help..How do I get indicator to filter instead of alert? - page 8

 

U da man!

so....let's see if I understand this correctly...

if I wanted to close positions based on the back cross of a moving average all I would need to code is this....

if(currentlong>minorts) {CloseOrder(OP_SELL); // Close all sell orders}

where currentlong is the 20ema and minorts is the 150ema so the open position would have been or IS a short which has run it's course and now the 20ema is moving above the 150ema which is the close signal for the short trade.

and...

if (currentlong<minorts) {CloseOrder(OP_BUY); // Close all buy orders}

where currentlong is the 20ema and minorts is the 150ema so the open position would have been or IS a long which has run it's course and now the 20ema is moving below the 150ema which is the close signal for the long trade.

So I could just put these two lines

if (currentlong<minorts) {CloseOrder(OP_BUY); // Close all buy orders}

if(currentlong>minorts) {CloseOrder(OP_SELL); // Close all sell orders}

right after the entry code and before all the other closing and trailing stop stuff like this? and it will work?

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

}

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

return(0);

}

}

//+---------end of order entry-------------------------+

//+------close on moving average cross-----------------+

if(currentlong<minorts) {CloseOrder(OP_BUY);} // Close all buy orders}

if(currentlong>minorts) {CloseOrder(OP_SELL);} // Close all sell orders}

//+--------end of close on moving average cross--------+

//+-------------------------Trailing Stop Code------------------------------------+

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

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {

if(OrderType()==OP_BUY){

the compiler says this:

')' - wrong parameters count C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (85, 43)

')' - wrong parameters count C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (86, 44)

it doesn't like those lines. I don't feel like I'm communicating very well.

 

this is interesting suddenly my search function on the metaeditor works??

bool OrderCloseBy( int ticket, int opposite, color Color=CLR_NONE)

Closes an opened order by another opposite opened order. If the function succeeds, the return value is true. If the function fails, the return value is false. To get the detailed error information, call GetLastError().

Parameters:

ticket - Unique number of the order ticket.

opposite - Unique number of the opposite order ticket.

Color - Color of the closing arrow on the chart. If the parameter is missing or has CLR_NONE value closing arrow will not be drawn on the chart.

Sample:

if(iRSI(NULL,0,14,PRICE_CLOSE,0)>75)

{

OrderCloseBy(order_id,opposite_id);

return(0);

}

so I'm visualizing something like this...

if(currentlong<minorts)

{

OrderCloseBy(order_id,opposite_id);

return(0);

}[/PHP]

thing is this doesn't distinguish what kind of position I'm into first, long or short. So if I put the opposite side of this with it...like this...

[PHP] if(currentlong>minorts)

{

OrderCloseBy(order_id,opposite_id);

return(0);

}

together they would just close everything wouldn't they? there has to be more...I feel as frustrated as a child that can't really speak yet to communicate full sentances, when I HAVE complete ideas inside me that I can't get out.

 
elihayun:
I forget this one
void CloseOrder(int ticket,double numLots,double close_price)

{

int CloseCnt, err;

// try to close 3 Times

CloseCnt = 0;

color clr = Violet;

if (OrderType() == OP_SELL)

clr = Orange;

while (CloseCnt < 3)

{

if (OrderClose(ticket,numLots,close_price,Slippage,clr))

{

CloseCnt = 3;

}

else

{

err=GetLastError();

Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));

if (err > 0) CloseCnt++;

}

}

}

[/PHP]

and dont forget to add this line after #property link

[PHP]#property link "http://www.elihayun.com"

#include

what does this do? is that something else I have to download to have it available to be called? I want to understand how this does what it does, how it distinguishes long from short positions. I want to learn.

 
Aaragorn:
U da man!

so....let's see if I understand this correctly...

So I could just put these two lines

if (currentlong<minorts) {CloseOrder(OP_BUY); // Close all buy orders}

if(currentlong>minorts) {CloseOrder(OP_SELL); // Close all sell orders}

//+------close on moving average cross-----------------+

if(currentlong<minorts) {CloseOrder(OP_BUY);} // Close all buy orders}

if(currentlong>minorts) {CloseOrder(OP_SELL);} // Close all sell orders}

if(OrderType()==OP_BUY){[/PHP]

the compiler says this:

')' - wrong parameters count C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (85, 43)

')' - wrong parameters count C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (86, 44)

it doesn't like those lines. I don't feel like I'm communicating very well.

You have to call CloseOrders with s at the end not CloseOrder (that close only one order)

 
Aaragorn:
what does this do? is that something else I have to download to have it available to be called? I want to understand how this does what it does, how it distinguishes long from short positions. I want to learn.

Its part of MQL4 and contains the ErrorDescription function

 
elihayun:
Its part of MQL4 and contains the ErrorDescription function

ok I still need some code that will allow me to close based on the moving average cross back.

 

This piece of code should get you going...

Of course you will have to modify it to suit your own needs. However, this should provide you with a starting point. This routine uses the SMA1 line as the trailing stop. So take this idea and see what you can make out of it.

//these two lines within start()

SMA1 = iMA(NULL,TimePeriod,SlowPeriod,0,SlowMode,SlowPrice,1);

TrailingAlls(TrailStart, SMA1);

// trailing routine using the value of SMA1

void TrailingAlls(int start, double currvalue)

{

int profit;

double stoptrade;

double stopcal;

// if(stop==0) return;

int trade;

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

{

if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

continue;

if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

continue;

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

{

if(OrderType()==OP_BUY)

{

profit=NormalizeDouble((Bid-OrderOpenPrice())/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

// stopcal=Bid-(stop*Point);

stopcal=NormalizeDouble(currvalue, Digits);

if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);

}//Long

if(OrderType()==OP_SELL)

{

profit=NormalizeDouble((OrderOpenPrice()-Ask)/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

// stopcal=Ask+(stop*Point);

stopcal=NormalizeDouble(currvalue, Digits);

if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);

}//Shrt

}

}//for

}

 

a trailing stop close is possibly useful if I can figure this out first....

this snippet that elihayun gave me I think could work if someone can figure out how to get it responsive to the ma backcross and in the right direction. I'm confused with the op_buy and op_sell which is which. and which to use to close long positions and which to use to close short positions.

//+------close on moving average cross-----------------+

void CloseOrders(int op)

{

int tik[30], t = 0;

for(int i =0;i<OrdersTotal();i++){

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

if(OrderSymbol()==Symbol() && MagicNum==OrderMagicNumber() && OrderType() == op){

tik[t] = OrderTicket(); t++;

}

}

}

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc);

}

}

}

//+--------end of close on moving average cross--------+[/PHP]

I think I see that at this point in the code it has selected the orders by ticket and it's figuring out to set the close price based on the bid or the ask. So I'm not sure which is which for long or short positions. If the ticket is a long position that means it was opened at the ask price right? so it should close on the bid price? so this snippet is saying use the bid price to close so I assume that's for a long position. THEN it asks the

if (op == OP_SELL) prc = Ask;

so I assume this is the first place in this code where we now know if we are looking at a long or a short position ticket.

Then it moves immediately to close. But if I can put my closing criteria in here BEFORE it does that???

here it is as I received it...

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc);

}[/PHP]

so what I'm thinking is that this is the place in the code where I should insert the moving average criteria to trigger the close for long or short positions. Something like this...

[PHP]for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

if (prc == Bid && currentlong minorts);

CloseOrder(tik, OrderLots(), prc);

}

Will this do it? I think it might if I understand correctly..

Please tell me coders if this is correct??

I think this connects the direction of the moving average cross to the type of position that the ticket is identifed as being.

if the bid is to close long positions so we know the ticket is for a long position then it can close long positions if the currentlongema < minortsEMA because it knows that the ticket is for a long position and the 20ema has moved below the 150ema.

If the ask is for closing short positions and the ticket is identified as a short position because it wants to close at the ask price and the currentlongEMA has moved above the minortsEMA because it knows the ticket is for a short position and the 20ema has moved above the 150ema.

if that is correct will adding this line to the code,

if (prc == Bid && currentlong minorts)

stop it from closing UNLESS each ticket fits this criteria?

...that for the long position the 20ema150ema?

if that is ok then the logic for deciding to close long or short based on the emacrossback is ok and it's worth fixing these errors

I get these errors from the compiler...

[PHP]Compiling 'whatever.mq4'...

'(' - function definition unexpected C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (85, 17)

'MagicNum' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (90, 40)

'op' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (90, 87)

'tik' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (91, 13)

't' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (91, 17)

't' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (91, 37)

't' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (96, 18)

'tik' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (98, 22)

'op' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (100, 14)

'tik' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (102, 21)

'cnt' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 5)

'cnt' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 5)

'cnt' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 11)

'cnt' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 11)

'total' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 15)

'total' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 15)

'cnt' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 21)

'cnt' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 21)

'{' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 28)

'i' - variable is already defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (135, 11)

'}' - unbalanced parentheses C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (165, 1)

16 error(s), 5 warning(s)

 

A suggestion to all members, do not use "count up" routines to close trades. For example, do not use something like this:

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc);

}

If you are using multiple orders, it will not close the last order. Use a "count down" routine. Here is my discussion with the developers of Metaquotes when I first stumbled on this irritating "bug".

http://www.metaquotes.net/forum/2018/

 
Maji:
This piece of code should get you going...

Of course you will have to modify it to suit your own needs. However, this should provide you with a starting point. This routine uses the SMA1 line as the trailing stop. So take this idea and see what you can make out of it.

//these two lines within start()

SMA1 = iMA(NULL,TimePeriod,SlowPeriod,0,SlowMode,SlowPrice,1);

TrailingAlls(TrailStart, SMA1);

// trailing routine using the value of SMA1

void TrailingAlls(int start, double currvalue)

{

int profit;

double stoptrade;

double stopcal;

// if(stop==0) return;

int trade;

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

{

if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

continue;

if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

continue;

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

{

if(OrderType()==OP_BUY)

{

profit=NormalizeDouble((Bid-OrderOpenPrice())/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

// stopcal=Bid-(stop*Point);

stopcal=NormalizeDouble(currvalue, Digits);

if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);

}//Long

if(OrderType()==OP_SELL)

{

profit=NormalizeDouble((OrderOpenPrice()-Ask)/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

// stopcal=Ask+(stop*Point);

stopcal=NormalizeDouble(currvalue, Digits);

if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);

}//Shrt

}

}//for

}

thankyou so much. I look forward to dissecting this and seeing what I can make of it...

the reason I want to get the emacrossback working first is that it will basically be my stoploss and my default exit strategy.

Once that is working then I will add things like this to increase the profitability. But since I can't have a stop loss without messing it up and since I'm not willing to allow huge stop loss parameters I want to get the moving average crossback closing working first. If you get a chance to verify what I've done so far with the crossback close I would appreciate it.

Reason: