Made a block in which there is a partial closing of the position, when the price reaches a certain level:
for (int i=0; i {
if (OrderSelect(i, SELECT_BY_POS))
{
if (OrderType()==1)
{
Lot_61_8 = OrderLots()*Lot_Percent_61_8/100;if(Lot_61_8>OrderLots()||Lot_61_8<0.01){Lot_61_8 = OrderLots();if(Lot_61_8>100)Lot_61_8=100;}
Lot_61_8 = (NormalizeLot(Lot_61_8, False));
Ask_Value_61_8=OrderOpenPrice()-(61.8*OrderOpenPrice())/10000;
if (Lot_61_8>0)
error=OrderClose(OrderTicket(), Lot_61_8, MarketInfo(Symbol(), MODE_BID), 20, CLR_NONE);
}
}
}
But the problem is that this piece works out every time the price reaches this level, because this way, in the end, I close the entire volume ))
How can I make this close happen once and no longer be processed for this order ?
Partial closing of the order
Share
1qefvnhm
HOW DO I ANIMATE THIS FUNCTION ? Wrote so!
TimeBar=Time[0];
ClosePartPosition(tr);
if(stoploss!=0) SL=NormalizeDouble(Bid-stoploss*Point,Digits); else SL=0;
LOT=LOT(risk,1);
if(ORDERS(-1)==1) CLOSEORDER(-1);//Close orders opened for sale
mr_lsv
it turned out. thanks
ya_programmer
I liked Your first idea more with a comment to the order – well, so as not to write it to global variables – analyze only the comment, without doing anything with it, and that’s it.
integer
See the difference between the comment of a fresh order and a partial closed one. A partially closed order can be distinguished by a comment.
dken
with checkboxes, this is a primitive. check in the history by the order comment which part is already closed.
expert advisors should not depend on variables that can be reset when restarting. I somewhere in a similar topic gave here my code with the search for parts and checking which part is closed.
ya_programmer
So… ?
What is the logic, and the code and will work.
….
Each order has a ticket number. The order ticket will change when partially closed. So you will only need to remember it if you successfully close it partially.
If error>0, this will be the order number, i.e. the ticket
error=OrderClose(OrderTicket(), Lot_61_8, MarketInfo(Symbol(), MODE_BID), 20, CLR_NONE);
artmedia70
Sergey Lapshov:
Made a block in which there is a partial closing of the position, when the price reaches a certain level:
for (int i=0; i
{
if (OrderSelect(i, SELECT_BY_POS))
{
if (OrderType()==1)
{
Lot_61_8 = OrderLots()*Lot_Percent_61_8/100;if(Lot_61_8>OrderLots()||Lot_61_8<0.01){Lot_61_8 = OrderLots();if(Lot_61_8>100)Lot_61_8=100;}
Lot_61_8 = (NormalizeLot(Lot_61_8, False));
Ask_Value_61_8=OrderOpenPrice()-(61.8*OrderOpenPrice())/10000;
if (Lot_61_8>0)
error=OrderClose(OrderTicket(), Lot_61_8, MarketInfo(Symbol(), MODE_BID), 20, CLR_NONE);
}
}
}
But the problem is that this piece works out every time the price reaches this level, because this way, in the end, I close the entire volume ))
How can I make this close happen once and no longer be processed for this order ?
Before closing, check the volume of the order being closed as an option. But this is if the lots are fixed, or if the lots are not fixed, then check the volume of the closed order with the current current lot of the expert Advisor – it must be stored in the variable.
mr_lsv
for some reason, it turns out like this:
sergey1294
rf as it is, the flag variable must be reset when opening this order
//+------------------------------------------------------------------+
/ / / test. mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "
#property version "1.00"
#property strict
bool flag=true;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS))
{
if(OrderType()==1)
{
Lot_61_8=OrderLots()*Lot_Percent_61_8/100;if(Lot_61_8>OrderLots() || Lot_61_8<0.01){Lot_61_8=OrderLots();if(Lot_61_8>100)Lot_61_8=100;}
Lot_61_8=(NormalizeLot(Lot_61_8,False));
Ask_Value_61_8=OrderOpenPrice()-(61.8*OrderOpenPrice())/10000;
if(Lot_61_8>0 && flag)
{
error=OrderClose(OrderTicket(),Lot_61_8,MarketInfo(Symbol(),MODE_BID),20,CLR_NONE);
if(error)flag=false;
else PrintFormat("Error # %d closing the order. Ticket: %d", GetLastError(),OrderTicket());
}
}
}
}
}
//+------------------------------------------------------------------+