First, thank you to all of your help with code. There are some amazing coders here and your help is valuable for nonprogrammers like myself.
Recently, I've been working on a routine that looks like this and up to this point it works perfectly...but I need to add a more robust stop out and reverse strategy to the code which could make it more effective. Here's the part that works as intended...Basically it says if (condition is met) then buy, and if opposite condition is met then sell. I want to add 2 things to this code:
1. a stop out level based on: the Low[iLowest(NULL,0,MODE_LOW,3,0)] from the time of the trade only for buys, and High[iHighest(NULL,0,MODE_HIGH,3,0)] for sells.
2. if stopped out at the level above, I want it to reverse into an new opposite sell or buy and continue using the same exit strategy.
Here's the code without 1 and 2 above that works great, but I've tried numerous versions of trying to add #1 above and it messes up the strategy, not sure if it was my logic or syntax or placement.
RefreshRates();
{
if (TurnUp)
{
fBuy();
}
if (TurnDn)
{
fSell();
}
int ticket;
if (OrdersTotal()==0) return(0);
for (int i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
{
//Print ("order ticket: ", OrderTicket(), "order magic: ", OrderMagicNumber(), " Order Symbol: ", OrderSymbol());
if (OrderType()==0 && TurnDn)
{
ticket=OrderClose(OrderTicket(),OrderLots(), MarketInfo(OrderSymbol(),MODE_BID), 3,Red);
if (ticket==-1) Print ("Error: ", GetLastError());
if (ticket>0) Print ("Position ", OrderTicket() ," closed.");
}
if (OrderType()==1 && TurnUp)
{
ticket=OrderClose(OrderTicket(),OrderLots(), MarketInfo(OrderSymbol(),MODE_ASK), 3,Red);
if (ticket==-1) Print ("Error: ", GetLastError());
if (ticket>0) Print ("Position ", OrderTicket() ," closed.");
}
}
}
}
}
//--------------------------------------------------
// ORDER FUNCTION SEND BUY AND RETURN RESULTS
//--------------------------------------------------
void fBuy()
{
// RefreshRates();
int result = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Open LONG",Magic,0,Blue);
OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES);
Print("BUY ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]);
if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "BUY ORDER OPENED!" + Symbol() + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits));
if (result == -1)
{
int e = GetLastError();
Print(e);
}
}
//--------------------------------------------------
// ORDER FUNCTION SEND SELL RETURN RESULTS
//--------------------------------------------------
void fSell()
{
// RefreshRates();
int result = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Open SHORT",Magic,0,Red);
OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES);
Print("SELL ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]);
if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "SELL ORDER OPENED!" + Symbol() + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits));
if (result == -1)
{
int e = GetLastError();
Print(e);
}
}
Recently, I've been working on a routine that looks like this and up to this point it works perfectly...but I need to add a more robust stop out and reverse strategy to the code which could make it more effective. Here's the part that works as intended...Basically it says if (condition is met) then buy, and if opposite condition is met then sell. I want to add 2 things to this code:
1. a stop out level based on: the Low[iLowest(NULL,0,MODE_LOW,3,0)] from the time of the trade only for buys, and High[iHighest(NULL,0,MODE_HIGH,3,0)] for sells.
2. if stopped out at the level above, I want it to reverse into an new opposite sell or buy and continue using the same exit strategy.
Here's the code without 1 and 2 above that works great, but I've tried numerous versions of trying to add #1 above and it messes up the strategy, not sure if it was my logic or syntax or placement.
RefreshRates();
{
if (TurnUp)
{
fBuy();
}
if (TurnDn)
{
fSell();
}
int ticket;
if (OrdersTotal()==0) return(0);
for (int i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
{
//Print ("order ticket: ", OrderTicket(), "order magic: ", OrderMagicNumber(), " Order Symbol: ", OrderSymbol());
if (OrderType()==0 && TurnDn)
{
ticket=OrderClose(OrderTicket(),OrderLots(), MarketInfo(OrderSymbol(),MODE_BID), 3,Red);
if (ticket==-1) Print ("Error: ", GetLastError());
if (ticket>0) Print ("Position ", OrderTicket() ," closed.");
}
if (OrderType()==1 && TurnUp)
{
ticket=OrderClose(OrderTicket(),OrderLots(), MarketInfo(OrderSymbol(),MODE_ASK), 3,Red);
if (ticket==-1) Print ("Error: ", GetLastError());
if (ticket>0) Print ("Position ", OrderTicket() ," closed.");
}
}
}
}
}
//--------------------------------------------------
// ORDER FUNCTION SEND BUY AND RETURN RESULTS
//--------------------------------------------------
void fBuy()
{
// RefreshRates();
int result = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Open LONG",Magic,0,Blue);
OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES);
Print("BUY ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]);
if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "BUY ORDER OPENED!" + Symbol() + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits));
if (result == -1)
{
int e = GetLastError();
Print(e);
}
}
//--------------------------------------------------
// ORDER FUNCTION SEND SELL RETURN RESULTS
//--------------------------------------------------
void fSell()
{
// RefreshRates();
int result = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Open SHORT",Magic,0,Red);
OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES);
Print("SELL ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]);
if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "SELL ORDER OPENED!" + Symbol() + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits));
if (result == -1)
{
int e = GetLastError();
Print(e);
}
}