How to code? - page 6

 

Error Code: 4109

Hello Everybody:

I wrote a script to close all trades including the pending orders, but was disapointed after testing it. The functions: OrderClose, and Orderdelete fail in closing the trades. By calling the GetLastError function I get the error code: 4109 which is defined in the error header file as TRADE_NOT_ALLOWED.

I even tried to execute those scripts which come with the platform but with no success.

I hope someone can solve the problem.

Thanks,

Files:
closeall2.mq4  2 kb
 

Okay, try this one. I think Lots and Price variable should be double not integer. Hope this help

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

//| CloseAll.mq4 |

//| FX5 |

//| |

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

#property copyright "FX5"

#property link " "

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

//| script program start function |

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

int start()

{

//----

int Ticket,Lots,Price,Type,Result;

int Error=0;

int Total=OrdersTotal();

for(int i=0;i<Total;i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==0)

{

Alert("Can''t select the order indexed: ",i);

Alert("Error code is ", GetLastError());

Error++;

}

else

{

/* Lots=OrderLots();

Ticket=OrderTicket();

Type=OrderType();*/

Result=0;

/* if(Type==OP_BUY)

Price=Bid;

if(Type==OP_SELL)

Price=Ask;*/

// if(Type==OP_BUY || Type==OP_SELL)

if(OrderType()<=OP_SELL)

{

Alert(OP_BUY," ",OP_SELL," ",Type);

// Result=OrderClose(Ticket,Lots,Price,5,Blue);

if(OrderType()==OP_BUY) Result=OrderClose(OrderTicket(),OrderLots(),Bid,5,Blue);

if(OrderType()==OP_SELL) Result=OrderClose(OrderTicket(),OrderLots(),Ask,5,Blue);

}

else

{

Result=OrderDelete(OrderTicket());

}

if(Result==0)

{

Alert("Can''t close the Order number: ",i," with Ticket: ",Ticket);

Alert("Error Code: ",GetLastError());

Error++;

}

}

}

if(Error!=0)

Alert(Error," Errors have occured");

else

Alert("Close all Orders Succeeded with no Errors");

//----

return(0);

}

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

try this

FX5:
Hello Everybody:

I wrote a script to close all trades including the pending orders, but was disapointed after testing it. The functions: OrderClose, and Orderdelete fail in closing the trades. By calling the GetLastError function I get the error code: 4109 which is defined in the error header file as TRADE_NOT_ALLOWED.

I even tried to execute those scripts which come with the platform but with no success.

I hope someone can solve the problem.

Thanks,

In metatrader, go to tools -> options, click the expert advisor tab and select allow live trading, and click the allow dll imports, and uncheck confirm calls. I wrote a few scripts and was having the same problem.

 
firedave:
Okay, try this one. I think Lots and Price variable should be double not integer. Hope this help
//+------------------------------------------------------------------+

//| CloseAll.mq4 |

//| FX5 |

//| |

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

#property copyright "FX5"

#property link " "

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

//| script program start function |

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

int start()

{

//----

int Ticket,Lots,Price,Type,Result;

int Error=0;

int Total=OrdersTotal();

for(int i=0;i<Total;i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==0)

{

Alert("Can''t select the order indexed: ",i);

Alert("Error code is ", GetLastError());

Error++;

}

else

{

/* Lots=OrderLots();

Ticket=OrderTicket();

Type=OrderType();*/

Result=0;

/* if(Type==OP_BUY)

Price=Bid;

if(Type==OP_SELL)

Price=Ask;*/

// if(Type==OP_BUY || Type==OP_SELL)

if(OrderType()<=OP_SELL)

{

Alert(OP_BUY," ",OP_SELL," ",Type);

// Result=OrderClose(Ticket,Lots,Price,5,Blue);

if(OrderType()==OP_BUY) Result=OrderClose(OrderTicket(),OrderLots(),Bid,5,Blue);

if(OrderType()==OP_SELL) Result=OrderClose(OrderTicket(),OrderLots(),Ask,5,Blue);

}

else

{

Result=OrderDelete(OrderTicket());

}

if(Result==0)

{

Alert("Can''t close the Order number: ",i," with Ticket: ",Ticket);

Alert("Error Code: ",GetLastError());

Error++;

}

}

}

if(Error!=0)

Alert(Error," Errors have occured");

else

Alert("Close all Orders Succeeded with no Errors");

//----

return(0);

}

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

This may be wrong : to delete multiple orders, you must begin from the last one as their indexes are decrementing following the deleting process.

So, in place of " for(int i=0;i=0;i--) "

 

Thanks firedave for the code modification. It was a small bug which I didn't pay attention to.

Crepuscular, thank you so much for hint. I could have spent a century before figuring out the solution. Finally, I have a working code.

 

Michel, that was a very clever note, thank you very much.

 

get max drawdown function

i would like to write a function which gets the max drawdown from trade that have been closed. i have come up with two solutions.

1. calculate the drawdown while the trade is still active. saving the number to a global variable and writing the data to file according to ticket number. MT4 would always have to be online.

2. get the drawdown by geting the highest high or lowest low from the 1 min chart. the problem here is we have to have the 1 min history for the trade. (i would use this method if i could get the 1 min bars neccessary if they werent already downloaded)

I would like to come up with a nicer way of getting max drawdown.

Any input is appreciated.

 
harriss:
i would like to write a function which gets the max drawdown from trade that have been closed. i have come up with two solutions.

1. calculate the drawdown while the trade is still active. saving the number to a global variable and writing the data to file according to ticket number. MT4 would always have to be online.

2. get the drawdown by geting the highest high or lowest low from the 1 min chart. the problem here is we have to have the 1 min history for the trade. (i would use this method if i could get the 1 min bars neccessary if they werent already downloaded)

I would like to come up with a nicer way of getting max drawdown.

Any input is appreciated.

Salam harriss,

What did you mean by "drawdown"?

Do you mean the maximum lose one of closed trades has been made?

 

drawdown

What i mean by drawdown is, what is the lowest point in the trade. For example, if the trade hit the stoploss, then the stoploss would be the drawdown. Now if the trade ended up on the positive side and was -15 pips at one point then the drawdown would be 15. Lastly, if the trade close at -15 pips but was -30 at one point then the drawdown would be 30.

 

How to Code Alerts/Alarms for indicators?

Hello I'd like to learn more about how to code alerts.

I'm not a programmer but I have a head on my shoulders and am generally able to tinker with something, pick it apart, and put it back together again closer to the way I want it.

I've gone through some of the indicators with alerts and have copied the general alert functions I've seen to other indicators I've wanted to have alerts. I've had mixed results.

I notice that the main functions called for alerts are the Alert() function, the Comment() function, and the Playsound() function. So far I'm most familiar with the Alert() function. Is there a significant difference between Alert() and Playsound() for example?

The general problem I have is that I don't know how to code the alerts enough so that they are both noticeable on the one hand but not ridiculously distracting on the other. For example if using the Alert() function for some crosses and the indicator has not printed with finality on the current bar the alert might repeat over and over and over and I have trouble turning the alert off. Usually I have to change the time frame of the chart.

First of all is there a simple way to disable/deactivate the alert for the current bar only?

Second, is there a way to code an alert so that it isn't an unending annoying nuisance by introducing code that restricts the number of iterations or gives the alert a fixed duration? How would you code it?

One solution I see is to delay the alert until the next bar so that it isn't constantly changing but that does have the disadvantage of not being timely.

To avoid the hassle of berserk alarms on indicators, I've used the predetermined price alerts that one can set using the terminal window and it helps but it isn't ideal.

I'm also aware that Expert Advisors could be created to give alerts and that there is a button that can turn their alerts on and off easily. The problem I have with EAs, however, is that I notice that another important program I run concurrently with MetaTrader seems to experience interference when Expert Advisors are activated and runs sluggishly.

Are there any other solutions to this problem?

If there any indicators out there that have an alert code that avoids the pitfalls I have enumerated and can serve as a good example of how to code and alert could you please refer me to it and/or post it?

Thank you very much.

Reason: