[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 767

 
FoxUA:

Yes, part of the code works and part of the code says 4107 and 130 error and the most interesting thing is that they take the price from one and the same function and work on one and the same opening function.


Try the following structure somewhere, this example is for my case .

if (OrderSend(Symbol(),OP_BUYSTOP,Lot,OpenPrice,0,Ask,0,0,Mg,0,Red)== -1)
Print("Error 1 OP_BUYSTOP "," Open=",OpenPrice," Stop Loss=",Ask," Trall=",TrallBuy," StopLoss=",StopLoss) ;

see the result in the log at

 

Good afternoon connoisseurs. Help me understand this code...

//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICEA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}

//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotSize(){
double lotMM;
if(PairsTraded==0){
lotMM = MathCeil(AccountFreeMargin() * Risk / 10000) / 10;
} else {
lotMM = MathCeil(AccountFreeMargin() * Risk / 10000 /PairsTraded) / 10 ;
}
if (MM==true){
if (lotMM < 0.1) lotMM = Lots;
if (lotMM > 1.0) lotMM = MathCeil(lotMM);
if (lotMM > 100) lotMM = MaxLots;
}
else lotMM = Lots;
return (lotMM);
}

//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double CCI5,CCI15,CCI30,CCIH1,CCIH4;
int send;

//----MultitimeFrame CCI setting


CCI5 = iCCI(NULL,PERIOD_M5,50,PRICE_CLOSE,0);
CCI15 = iCCI(NULL,PERIOD_M15,50,PRICE_CLOSE,0);
CCI30 = iCCI(NULL,PERIOD_M30,50,PRICE_CLOSE,0);
CCIH1 = iCCI(NULL,PERIOD_H1,50,PRICE_CLOSE,0);


//----


//---- sell conditions
if ((CCIH1<0)&&(CCI30<0)&&(CCI15<0)&&(CCI5<0))//op sell
{
send=OrderSend(Symbol(),OP_SELL,LotSize(),Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,MagicName+"-Sell",MAGICEA,0,Red);
return;
}

//---- buy conditions
if ((CCIH1>0)&&(CCI30>0)&&(CCI15>0)&&(CCI5>0))//op buy
{
send=OrderSend(Symbol(),OP_BUY,LotSize(),Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,MagicName+"-Buy",MAGICEA,0,Blue);
return;
}
}

//----

//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
//----


//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICEA || OrderSymbol()!=Symbol()) continue;

//---- check order type
if (OrderSymbol()==Symbol() && OrderType()==OP_BUY && Bid-TrailingStop*Point > OrderStopLoss()){

if (Bid > OrderOpenPrice()+TrailingStop*Point) {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,0,0,White);
}
} else {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-1*Point,0,0,White);
}
if (OrderSymbol()==Symbol() && OrderType()==OP_SELL && Ask-TrailingStop*Point < OrderStopLoss()){

if (Ask < OrderOpenPrice()-TrailingStop*Point) {
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,0,0,White);
}
} else {
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+1*Point,0,0,White);
}
}

//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;

//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();

//----
}
//+------------------------------------------------------------------+

If i want it to open only one position and i don't want it to open again after i close it, but it opens one position after i close it, and it opens again.

 
mydone:

Good afternoon connoisseurs. Help me to understand this code...

... code ...

I do not understand what I did wrong. I need it to open only one transaction under formed condition and after its closing until the next condition does not open more... but what I have is that it opens, I close for example a certain trade and it opens again in the same place what for?

You have it and it will open as long as the opening condition exists.

Make a check that the position has already been opened by this signal, e.g., bool OpnPose = false; before opening a position by this signal, check the flag and if it is not set (OpnPose == false), open the position. When it opens, if it really opened and is in the market, you set the flag to open by this signal: OpnPose = true; as soon as a new signal comes, you discard this one:
OpnPose = false;

 
Yes, we can make it simpler, after the signal is triggered put a counter so that nothing else opens within 2 or 3 candles, so that if it closes manually nothing else opens, and the counter cancels the opening ban after a couple of candles
 

Thank you

 
Techno:
Yes, we can make it simpler, after the signal is triggered put a counter so that nothing else opens within 2 or 3 candles, so that if it closes manually nothing else opens, and the counter cancels the opening ban after a couple of candles
And after the ban is lifted, the EA will again put the order into the market... What if the EA decides to close the position after 10, 15, or 20 candlesticks and does not open again, even if the signal is still present? On Friday, for example...
 
artmedia70:
And after the ban is lifted, the EA will put the order back into the market... And if it decides to close the position after 10, 15, 20 candles and not to open again, even if the signal is still there? On Friday, for example...
Even after one candle there will be a completely different signal that must be taken into account. It is enough to wait for 3 candlesticks for the first signal to disappear. By the way, you use an indicator with time of 5, 15, 30 and 60, so 3 30 or 15 minute candlesticks will be enough.
 

my question is how to do if there is a new order from the example of just the opposite if there is a closed order "if(TotalHistoryOrders<OrdersHistoryTotal())// another order has appeared in the history" and I need the open one to be also determined

 
FoxUA:

my question is how to do if there is a new order from the example of just the opposite if there is a closed order "if(TotalHistoryOrders<OrdersHistoryTotal())// another order has appeared in the history" and I need the open one to be also determined

when going through all orders and sifting out unnecessary ones, count the number and save it. If the number changes with a new loop, it means that either a new order has opened or has disappeared...
 
Techno:
When trying all orders and sifting out unnecessary ones, count the number, remember it and if the number has changed with a new loop, it means a new order has either appeared or has disappeared.


I just need to call the function and that's it, I just need one line "If the orders are more than one than .... that's all" and you offer me a whole function

This is how I store data about orders

int total_order;            // переменная в которой хранится общее количество открытых ордеров в терминале
int my_total_order;         // переменная в которой хранится общее количество открытых ордеров с нашим Magic
Reason: