B
Try something like this - works on full and sub-pip accounts
Good Luck
-BB-
Try something like this - works on full and sub-pip accounts
#property copyright "Copyright © 2010, SFX" #property link "http://www.selectfx.net" extern int MagicNumber = 454545; extern int Slippage = 5; // #################### CHANGE FOR SUB-PIP PRICING ###################### extern int OrderRetries = 10; extern int SleepBetweenTrades = 500; // In milliseconds. Avoids complaints from the broker of <stacking the server> extern int SleepWhenBusy = 500; // In milliseconds. Time to wait if broker server too busy to handle instruction extern color Close.ByPrice.Arrow=Yellow; // Globals string strSymbol; int iError, Real.Slippage; #include <stderror.mqh> //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- strSymbol = Symbol(); HandleDigits(); CloseAllBuyOrders(MagicNumber, OrderRetries, SleepBetweenTrades, strSymbol); CloseAllSellOrders(MagicNumber, OrderRetries, SleepBetweenTrades, strSymbol); //---- return(0); } //+------------------------------------------------------------------+ void CloseAllBuyOrders(int MN, int iNR, int iMTS, string sSymbol) { int i, iTotalOrders, iNumRetries; iTotalOrders=OrdersTotal()-1; // Rosh line for (i=iTotalOrders; i>=0; i--) // Rosh line { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber()==MN && OrderSymbol()== sSymbol) // Magic Number Block { iNumRetries=iNR; // Reset number of retries to max for next order to close while(iNumRetries>0) // Retries Block { if (!IsTradeAllowed()) Sleep(SleepWhenBusy); RefreshRates(); if (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,Real.Slippage,Close.ByPrice.Arrow); iError = GetLastError(); if (iError != 0) // retry if error is "busy", otherwise give up { if(iError==ERR_SERVER_BUSY || iError==ERR_TRADE_CONTEXT_BUSY || iError==ERR_BROKER_BUSY || iError==ERR_NO_CONNECTION || iError == ERR_COMMON_ERROR || iError==ERR_TRADE_TIMEOUT || iError==ERR_INVALID_PRICE || iError==ERR_OFF_QUOTES || iError==ERR_PRICE_CHANGED || iError==ERR_REQUOTE) Sleep(iMTS); else { iNumRetries = 0; Print("OrderClose Type: ", OrderType(), " Error: ", iError ); } } else iNumRetries = 0; iNumRetries--; } // Retries Block } // Magic Number Block } } } void CloseAllSellOrders(int MN, int iNR, int iMTS, string sSymbol) { int i, iTotalOrders, iNumRetries; iTotalOrders=OrdersTotal()-1; // Rosh line for (i=iTotalOrders; i>=0; i--) // Rosh line { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber()==MN && OrderSymbol()== sSymbol) // Magic Number Block { iNumRetries=iNR; // Reset number of retries to max for next order to close while(iNumRetries>0) // Retries Block { if (!IsTradeAllowed()) Sleep(SleepWhenBusy); RefreshRates(); if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,Real.Slippage,Close.ByPrice.Arrow); iError = GetLastError(); if (iError != 0) // retry if error is "busy", otherwise give up { if(iError==ERR_SERVER_BUSY || iError==ERR_TRADE_CONTEXT_BUSY || iError==ERR_BROKER_BUSY || iError==ERR_NO_CONNECTION || iError == ERR_COMMON_ERROR || iError==ERR_TRADE_TIMEOUT || iError==ERR_INVALID_PRICE || iError==ERR_OFF_QUOTES || iError==ERR_PRICE_CHANGED || iError==ERR_REQUOTE) Sleep(iMTS); else { iNumRetries = 0; Print("OrderClose Type: ", OrderType(), " Error: ", iError ); } } else iNumRetries = 0; iNumRetries--; } // Retries Block } // Magic Number Block } } } void HandleDigits() { // Automatically handles full-pip and sub-pip accounts if (Digits == 4 || Digits == 2) { Real.Slippage = Slippage; } if (Digits == 5 || Digits == 3) { Real.Slippage = Slippage*10; } }
Good Luck
-BB-
Wow thanks barrowboy but I would have no idea how to incoperate my code with this one as I am a novice at coding.
If you could supply a version with my code together that would be excellent.
This is just what I have been looking for to get rid of errors, also does this code delete pending orders.
Thanks for your help.
George
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
Hello
I am using the code below to close all trades when a percentage of account is hit then wait for a delay before trading again.
The problem is if I get a requote it does not close the trade, is there any code I can add to check that all trades are closed.
Thanks very much for your help.
George
void CheckDrawdown()
{
double totalProfit = 0;
int i;
for(i=0; i <= OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == MagicNumber)
{
totalProfit=totalProfit+OrderProfit();
}
}
while(totalProfit<0 && MathAbs(totalProfit) >= (DrawdownPercentage/100.0*AccountBalance()))
{
int total = OrdersTotal();
for(i=0; i < total ; i++)
{
if(OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == MagicNumber)
{
if (OrderType() == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 5);
}
else if (OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 5);
}
else
{
OrderDelete(OrderTicket());
}
}
}
Stopped = true;
StopTime = TimeCurrent();
string message = "Stopped. Stop time:" + TimeToStr(StopTime, TIME_MINUTES) + " Start time: " + TimeToStr(StopTime + Delay*3600, TIME_MINUTES);
ObjectSetText("comment14", message, 10, "Times New Roman", Red);
}
}