Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1093

 
evillive:
Then there is an error code in the tester's log and it can be used for further investigation.

No error. It just hangs on entering the procedure. And there's also StartSell, it enters it normally. This is some kind of mess.

I updated the quotes and it works. It is not correct because of some holes, but at least it doesn't hang.

 

Greetings, could you please tell me the best way to prevent the simultaneous opening of more than a set number of series for different instruments (the bot is attached to several charts of different instruments at the same time) in binary options?

extern double Lots = 1; // Lots

extern int Exp = 1; // Expiry

extern int Wait = 0; // Number of candlesticks of one direction

extern int Timeout = 1; // Time slot

extern double Multiplier = 3; // Multiplier

extern int int Slippage = 5; // Slippage

extern int MaxOpenOrders = 1; // Maximum number of simultaneously opened orders

extern int Magic = 774274; // Magic


int ticket, Type;

double Price, Lot;

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

//| Expert initialization function |

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

int OnInit()

{

return(INIT_SUCCEEDED);

}

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

//| Expert deinitialization function |

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

void OnDeinit(const int reason)

{

}

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

//| expert tick function |

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

void OnTick()

{

// --------------- Open trades ---------------

if (CountTrades() == 0) // Number of orders must equal zero

{

if ((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) < PriceOpenLastHistOrder(OP_BUY))

|| (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) > PriceOpenLastHistOrder(OP_SELL))

// If the last trade is losing, the same one is opened, but with bigger lot

{

Type = TypeLastHistOrder();

if (Type == OP_BUY) Price = Ask;

if (Type == OP_SELL) Price = Bid;

Lot = NormalizeDouble(LotsLastHistOrder()*Multiplier, 2);

ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

if (PriceCloseLastHistOrder() == PriceOpenLastHistOrder() && CountHistTrades() > 0)

// if last trade's profit is equal to zero, the same trade will be opened

{

Type = TypeLastHistOrder();

if (Type == OP_BUY) Price = Ask;

if (Type == OP_SELL) Price = Bid;

Lot = NormalizeDouble(LotsLastHistOrder(), 2);

ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

if (((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) > PriceOpenLastHistOrder(OP_BUY))

|| (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) < PriceOpenLastHistOrder(OP_SELL))

|| CountHistTrades() == 0)// If the last trade is profitable, the order is opened

{

if (SignalBuy() && MaxOpenOrders > OrdersTotal())

{

ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

if (SignalSell() && MaxOpenOrders > OrdersTotal())

{

ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

}

}

}

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

int CountTrades(int type = -1)

{

int cnt = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

cnt++;

}

}

return(cnt);

}

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

int CountHistTrades(int type = -1)

{

int cnt = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

cnt++;

}

}

return(cnt);

}

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

bool SignalBuy()

{

for (int i=1; i<=Wait; i++)

{

if (Close[i] > Open[i]) return(false)

}

if ((iBarShift(Symbol(), 0, TimeLastHistOrder()+Timeout) >= Wait || (Wait == 0 && TimeCurrent() >= TimeLastHistOrder()+Timeout))

&& CountHistTrades() > 0) return(true);

if (CountHistTrades() == 0) return(true);

return(false);

}

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

bool SignalSell()

{

for (int i=1; i<=Wait; i++)

{

if (Close[i] < Open[i]) return(false)

}

if ((iBarShift(Symbol(), 0, TimeLastHistOrder()+Timeout) >= Wait || (Wait == 0 && TimeCurrent() >= TimeLastHistOrder()+Timeout))

&& CountHistTrades() > 0) return(true);

if (CountHistTrades() == 0) return(true);

return(false);

}

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

datetime TimeLastHistOrder(int type = -1)

{

datetime lasttime = 0;

datetime opentime = 0;


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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

{

if (OrderCloseTime() > lasttime)

{

lasttime = OrderCloseTime();

opentime = OrderOpenTime();

}

}

}

}

return(opentime);

}

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

int TypeLastHistOrder()

{

datetime time = 0;

int type = -1;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

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

{

if (OrderCloseTime() > time)

{

time = OrderCloseTime();

type = OrderType();

}

}

}

}

return(type);

}

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

double LotsLastHistOrder(int type = -1)

{

datetime time = 0;

double lots = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

{

if (OrderOpenTime() > time)

{

time = OrderOpenTime();

time = OrderLots();

}

}

}

}

return(lots);

}

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

double PriceCloseLastHistOrder(int type = -1)

{

datetime time = 0;

double price = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

{

if (OrderCloseTime() > time)

{

time = OrderCloseTime();

price = OrderClosePrice();

}

}

}

}

}

}

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

double PriceOpenLastHistOrder(int type = -1)

{

datetime time = 0;

double price = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

{

if (OrderCloseTime() > time)

{

time = OrderCloseTime();

price = OrderOpenPrice();

}

}

}

}

}

}

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

 
alvlaf:

Simply delete the condition OrderSymbol() == Symbol() from CountTrades function and add OrderType() == OP_BUY || OrderType() == OP_SELL, no?

And change condition if (SignalBuy() && CountTrades() < MaxOpenOrders) in OnTick before OrderSend... The same for Sell.
 
A13ksandr:
Thanks, tried it - the bot started opening a bunch of orders on every tick.
 
A13ksandr:

if (OrderMagicNumber() == Magic && (OrderType() == OrderType() == OP_BUY || OrderType() == OP_SELL))

Did I change it correctly?

 
alvlaf:

Greetings, could you please tell me the best way to prevent the simultaneous opening of more than a set number of series for different instruments (the bot is attached to several charts of different instruments at the same time) in binary options?

extern double Lots = 1; // Lots

extern int Exp = 1; // Expiry

extern int Wait = 0; // Number of candlesticks of one direction

extern int Timeout = 1; // Time slot

extern double Multiplier = 3; // Multiplier

extern int int Slippage = 5; // Slippage

extern int MaxOpenOrders = 1; // Maximum number of simultaneously opened orders

extern int Magic = 774274; // Magic


int ticket, Type;

double Price, Lot;

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

//| Expert initialization function |

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

int OnInit()

{

return(INIT_SUCCEEDED);

}

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

//| Expert deinitialization function |

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

void OnDeinit(const int reason)

{

}

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

//| expert tick function |

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

void OnTick()

{

// --------------- Open trades ---------------

if (CountTrades() == 0) // Number of orders must equal zero

{

if ((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) < PriceOpenLastHistOrder(OP_BUY))

|| (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) > PriceOpenLastHistOrder(OP_SELL))

// If the last trade is losing, the same one is opened, but with bigger lot

{

Type = TypeLastHistOrder();

if (Type == OP_BUY) Price = Ask;

if (Type == OP_SELL) Price = Bid;

Lot = NormalizeDouble(LotsLastHistOrder()*Multiplier, 2);

ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

if (PriceCloseLastHistOrder() == PriceOpenLastHistOrder() && CountHistTrades() > 0)

// if last trade's profit is equal to zero, the same trade will be opened

{

Type = TypeLastHistOrder();

if (Type == OP_BUY) Price = Ask;

if (Type == OP_SELL) Price = Bid;

Lot = NormalizeDouble(LotsLastHistOrder(), 2);

ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

if (((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) > PriceOpenLastHistOrder(OP_BUY))

|| (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) < PriceOpenLastHistOrder(OP_SELL))

|| CountHistTrades() == 0)// If the last trade is profitable, the order is opened

{

if (SignalBuy() && MaxOpenOrders > OrdersTotal())

{

ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

if (SignalSell() && MaxOpenOrders > OrdersTotal())

{

ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

}

}

}

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

int CountTrades(int type = -1)

{

int cnt = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

cnt++;

}

}

return(cnt);

}

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

int CountHistTrades(int type = -1)

{

int cnt = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

cnt++;

}

}

return(cnt);

}

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

bool SignalBuy()

{

for (int i=1; i<=Wait; i++)

{

if (Close[i] > Open[i]) return(false)

}

if ((iBarShift(Symbol(), 0, TimeLastHistOrder()+Timeout) >= Wait || (Wait == 0 && TimeCurrent() >= TimeLastHistOrder()+Timeout))

&& CountHistTrades() > 0) return(true);

if (CountHistTrades() == 0) return(true);

return(false);

}

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

bool SignalSell()

{

for (int i=1; i<=Wait; i++)

{

if (Close[i] < Open[i]) return(false)

}

if ((iBarShift(Symbol(), 0, TimeLastHistOrder()+Timeout) >= Wait || (Wait == 0 && TimeCurrent() >= TimeLastHistOrder()+Timeout))

&& CountHistTrades() > 0) return(true);

if (CountHistTrades() == 0) return(true);

return(false);

}

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

datetime TimeLastHistOrder(int type = -1)

{

datetime lasttime = 0;

datetime opentime = 0;


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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

{

if (OrderCloseTime() > lasttime)

{

lasttime = OrderCloseTime();

opentime = OrderOpenTime();

}

}

}

}

return(opentime);

}

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

int TypeLastHistOrder()

{

datetime time = 0;

int type = -1;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

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

{

if (OrderCloseTime() > time)

{

time = OrderCloseTime();

type = OrderType();

}

}

}

}

return(type);

}

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

double LotsLastHistOrder(int type = -1)

{

datetime time = 0;

double lots = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

{

if (OrderOpenTime() > time)

{

time = OrderOpenTime();

time = OrderLots();

}

}

}

}

return(lots);

}

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

double PriceCloseLastHistOrder(int type = -1)

{

datetime time = 0;

double price = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

{

if (OrderCloseTime() > time)

{

time = OrderCloseTime();

price = OrderClosePrice();

}

}

}

}

}

}

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

double PriceOpenLastHistOrder(int type = -1)

{

datetime time = 0;

double price = 0;

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

{

if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && (OrderType() == type || type == -1))

{

if (OrderCloseTime() > time)

{

time = OrderCloseTime();

price = OrderOpenPrice();

}

}

}

}

}

}

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

Please tell me what purpose you are using MagicNumber for, what does it give in this strategy? IMHO, you can also throw out the OnInit OnDeinit function here
 
evillive:


Time can be set directly in datetime format, no need to waste machine time on conversion, works like this:

extern datetime StartTime = D'07:00';

In this format of time, when we start the EA, we get the date of compilation, while we want the compilation date to be today.

How to correct it?

 
RichLux:

This time format gives the EA compilation date when starting the EA, but I would like to have today's date.

How to fix it?

You change the parameter to the desired one at startup, in the same way as the other user parameters.

And for some reason it shows me the current date at the time of running the script.

 
LRA:
Please tell me, what is the purpose of using MagicNumber, what does it give in this strategy? IMHO, you can also throw out the OnInit OnDeinit functions here
What is the purpose of the words "in this strategy"? Magic is needed if you make trades manually in the account or run another Expert Advisor so that these orders are not touched. I think so.
 
alvlaf:

Do you manually place the EA on the charts and want it to open MaxOpenOrders orders or TOTAL MaxOpenOrders orders on EVERY chart? And what orders: just the market ones or all of them including limit and stop?