Hi everyone, new to the forum and very new to programming - been mostly teaching myself by doing and reading a LOT of forum posts. Anyway, working on an EA which is based on a simple box breakout strategy - at a certain time, EA establishes high and low price over X bars and places buy stop and sell stop above and below respectively. After X hours the orders are closed. There's other aspects to it but that's the very basics and all that's necessary to include at this stage.
This is the order management part of the EA. I added a OneCancelsTheOther feature - when one of the buy/sell stops is activated, the remaining 'pending' order is deleted. When tested this works.
I have been trying to add a MoveStopLossToBreakEven feature (if half way to profit target, stop loss changed to order open price) which has not been successful. I've added the coding I'm using for this part below but it is uncompiled and simply to demonstrate roughly the method I've been going for.
Though most of it works, it seems inefficient and the MoveStopLossToBreakEven doesn't work when tested. Any suggestions for improvement appreciated.
bool IsTrade = False; // Is there an active trade?int count = 0;
int PositionIndex;
for (PositionIndex = TotalOrders -1; PositionIndex >= 0 ; PositionIndex --)
{
Ticket2 = OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
IsTrade = True; // if order is buy or sell activetrade = true//---CLOSING BUYSif(OrderType() == OP_BUY) // if order is a buy
{
if (TimeHC == PosCloseHr) Order_BUY = SIGNAL_CLOSEBUY; // at user defined time close the buyif (Order_BUY == SIGNAL_CLOSEBUY)
{
Ticket2 = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
continue;
}
}
//---CLOSING SELLSelseif (OrderType() == OP_SELL) //if order is a sell
{
if (TimeHC == PosCloseHr) Order_SELL = SIGNAL_CLOSESELL; // at user defined time close the sellif (Order_SELL == SIGNAL_CLOSESELL)
{
Ticket2 = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
continue;
}
}
}
}
//---CANCEL PENDING ORDER IF TRADE OPENEDfor (PositionIndex = TotalOrders -1; PositionIndex >= 0 ; PositionIndex --)
{
Ticket3 = OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES);
if(OrderType() > OP_SELL ) // if order type is a buy stop or sell stop
{
if (OneCancelsTheOther) // external bool - if user chooses One Cancels The Other
{
if (IsTrade) // if there is an active trade, close pending trades.
{
Ticket3 = OrderDelete( OrderTicket());
Print ("Pending order deleted");
}
}
}
}
//---MOVE STOP LOSS TO BREAKEVEN
for (PositionIndex = TotalOrders -1; PositionIndex >= 0 ; PositionIndex --)
{
Ticket4 = OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() != MAGIC || OrderSymbol() != Symbol()) continue;
if(OrderType() <= OP_SELL )
{
if (MoveToBreakEven)
{
double sl = 0;
{
if ((OrderType() == OP_BUY) && (Bid >OrderOpenPrice()+ HalfProfitTargetPoints) )
{
sl = OrderOpenPrice() ;
}
if ((OrderType() == OP_SELL) && (Ask <OrderOpenPrice())- HalfProfitTargetPoints )
{
sl = OrderOpenPrice() ;
}
if (sl != 0)
{
Ticket4 = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
}
}
}
}
}
Hi everyone, new to the forum and very new to programming - been mostly teaching myself by doing and reading a LOT of forum posts. Anyway, working on an EA which is based on a simple box breakout strategy - at a certain time, EA establishes high and low price over X bars and places buy stop and sell stop above and below respectively. After X hours the orders are closed. There's other aspects to it but that's the very basics and all that's necessary to include at this stage.
This is the order management part of the EA. I added a OneCancelsTheOther feature - when one of the buy/sell stops is activated, the remaining 'pending' order is deleted. When tested this works.
I have been trying to add a MoveStopLossToBreakEven feature (if half way to profit target, stop loss changed to order open price) which has not been successful. I've added the coding I'm using for this part below but it is uncompiled and simply to demonstrate roughly the method I've been going for.
Though most of it works, it seems inefficient and the MoveStopLossToBreakEven doesn't work when tested. Any suggestions for improvement appreciated.