I have a problem with my code and I am not sure how to resolve this.
I want to open a single position every time a condition is met. This I have been able to accomplish.
The other part is to set a trailing stop loss on this open position. Here is where the problem lies.
My current code sets the trailing stop loss to the position but only once and does not adjust it accordingly there after.
Please shed some light on where I am failing and how I can resolve this.
//Import Trade Library#include<Trade\Trade.mqh>
//Create an instance for Ctrade
CTrade trade;
//Get account balancedouble Balance=AccountInfoDouble(ACCOUNT_BALANCE);
//Get account equitydouble Equity=AccountInfoDouble(ACCOUNT_EQUITY);
//Get Ask Pricedouble Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
//Get Bid Pricedouble Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
//Create string for the signalstring signal="";
int OpenBuy()
{
trade.Buy(0.01,NULL,Ask,(Ask-30000 * _Point),(Ask+30000 * _Point),NULL);
//Set desired stop loss to 5000 pointsdouble SL=NormalizeDouble(Ask-5000*_Point,_Digits);
//Check all open positions for the current symbolfor(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol=PositionGetSymbol(i);
if (_Symbol==symbol)
{
//Get position ticket numberulong PositionTicket=PositionGetInteger(POSITION_TICKET);
//Get current stoplossdouble CurrentStopLoss=PositionGetDouble(POSITION_SL);
if(CurrentStopLoss<SL)
{
//Print("Trail SL Buy");
trade.PositionModify(PositionTicket,(CurrentStopLoss+10000*_Point),0);
}
}
}
return(1);
}
int OpenSell()
{
trade.Sell(0.01,NULL,Bid,(Bid+30000 * _Point),(Bid-30000 * _Point),NULL);
//Set desired stop loss to 5000 pointsdouble SL=NormalizeDouble(Bid+5000*_Point,_Digits);
//Check all open positions for the current symbolfor(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol=PositionGetSymbol(i);
if (_Symbol==symbol)
{
//Get position ticket numberulong PositionTicket=PositionGetInteger(POSITION_TICKET);
//Get current stoplossdouble CurrentStopLoss=PositionGetDouble(POSITION_SL);
if(CurrentStopLoss>SL)
{
//Print("Trail SL Sell");
trade.PositionModify(PositionTicket,(CurrentStopLoss-10000*_Point),0);
}
}
}
return(1);
}
voidOnTick()
{
//Check if a single order has been executed for signalstaticbool Buy_Once=false,Sell_Once=false;
//Price Arraydouble SmallMovingAverageArray[],BigMovingAverageArray[];
//Define properties for Small MAint SmallMovingAverageArrayDefinition = iMA (_Symbol,_Period,5,0,MODE_SMA,PRICE_CLOSE);
//Define properties for Big MAint BigMovingAverageArrayDefinition = iMA (_Symbol,_Period,300,0,MODE_SMA,PRICE_CLOSE);
//Define MA1, one line, current candle, 3 candles, store resultCopyBuffer(SmallMovingAverageArrayDefinition,0,0,3,SmallMovingAverageArray);
//Define MA2, one line, current candle, 3 candles, store resultCopyBuffer(BigMovingAverageArrayDefinition,0,0,3,BigMovingAverageArray);
//Check buy signalif (BigMovingAverageArray[1] < SmallMovingAverageArray[1])
{
signal="BUY";
}
//Check sell signalif (BigMovingAverageArray[1] > SmallMovingAverageArray[1])
{
signal="SELL";
}
//Buy 1 Microlot on buy signalif (signal =="BUY" && Equity>=Balance && PositionsTotal()==0 && !Buy_Once)
{
int Buy=OpenBuy();
//Check if a buy order has been opened. If so, does not open another if (Buy>0)
{
Comment("An order opened. Current signal is: ",signal);
Buy_Once=true;
Sell_Once=false;
}
}
//Check for buy positions to close on sell signal if (signal =="SELL" && PositionsTotal()>0)
CloseAllBuyPositionsThisPair();
//Sell 1 Microlot on sell signalif (signal =="SELL" && Equity>=Balance && PositionsTotal()==0 && !Sell_Once)
{
int Sell=OpenSell();
//Check if a sell order has been opened. If so, does not open anotherif (Sell>0)
{
Comment("An order opened. Current signal is: ",signal);
Buy_Once=false;
Sell_Once=true;
}
}
//Check for sell positions to close on buy signal if (signal =="BUY" && PositionsTotal()>0)
CloseAllSellPositionsThisPair();
}
void CloseAllBuyPositionsThisPair()
{
//Go through all positions until there are no positions left for(int b=PositionsTotal()-1; b>=0; b--)
{
//Get ticket number for the current positionint ticket=PositionGetTicket(b);
//Identify current currency pairstring CurrentPair=PositionGetSymbol(b);
//Get position directionint PositionDirection=PositionGetInteger(POSITION_TYPE);
//If it is a buy positionif (PositionDirection==POSITION_TYPE_BUY)
//If symbol on chart equals position symbolif (_Symbol==CurrentPair)
{
//Close current position
trade.PositionClose(ticket);
}
}
}
void CloseAllSellPositionsThisPair()
{
//Go through all positions until there are no positions left for(int s=PositionsTotal()-1; s>=0; s--)
{
//Get ticket number for the current positionint ticket=PositionGetTicket(s);
//Identify current currency pairstring CurrentPair=PositionGetSymbol(s);
//Get position directionint PositionDirection=PositionGetInteger(POSITION_TYPE);
//If it is a buy positionif (PositionDirection==POSITION_TYPE_SELL)
//If symbol on chart equals position symbolif (_Symbol==CurrentPair)
{
//Close current position
trade.PositionClose(ticket);
}
}
}
Hey guys,
I have a problem with my code and I am not sure how to resolve this.
I want to open a single position every time a condition is met. This I have been able to accomplish.
The other part is to set a trailing stop loss on this open position. Here is where the problem lies.
My current code sets the trailing stop loss to the position but only once and does not adjust it accordingly there after.
Please shed some light on where I am failing and how I can resolve this.