Reputation: 9
Hi I want to open and close buy/sell trades based on Moving Average. But here the problem is once the trade is opened its not closing based on my condition. Please assist on my below codes
The doubt which I have in the function buyOrderclose() and the function sellOrderclose(). I have checked my conditions looks pretty good and the buyOrder() and sellOrder() also. I have tested in Mql5 also but its not working properly
#include<Trade\Trade.mqh>
CTrade trade;
input double lots=0.01;
double openCandle = iOpen(_Symbol,PERIOD_CURRENT,0);
double closeCandle = iClose(_Symbol,PERIOD_CURRENT,0);
double Balance = AccountInfoDouble(ACCOUNT_BALANCE);
double Equity = AccountInfoDouble(ACCOUNT_EQUITY);
int period = 10;
int EMA = iMA(_Symbol,PERIOD_CURRENT,period,0,MODE_EMA,0);
void OnTick()
{
buyOrder();
sellOrder();
buyOrderclose();
sellOrderclose();
}
void buyOrder()
{
if(openCandle < closeCandle)
if(closeCandle < EMA)
if(PositionsTotal() == 0)
{
double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
trade.Buy(lots,_Symbol,Ask,NULL,NULL,NULL);
Print("BUY ORDER PLACED");
}
}
void sellOrder()
{
if(openCandle > closeCandle)
if(closeCandle < EMA)
if(PositionsTotal() == 0)
{
double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
trade.Sell(lots,_Symbol,Bid,NULL,NULL,NULL);
Print("SELL ORDER PLACED");
}
}
void buyOrderclose()
{
if(openCandle > closeCandle)
if(closeCandle < EMA)
{
// count until there are no positions left
for(int i=PositionsTotal()-1; i>=0; i--)
{
//Get the ticket number for the currect position
int ticket=PositionGetTicket(i);
//Get the position direction
int PositionDirection = PositionGetInteger(POSITION_TYPE);
//if it is buy postition
if(PositionDirection==POSITION_TYPE_BUY)
//close the currect position
trade.PositionClose(ticket);
}
}
}
void sellOrderclose()
{
if(openCandle < closeCandle)
if(closeCandle < EMA)
{
// count until there are no positions left
for(int i=PositionsTotal()-1; i>=0; i--)
{
//Get the ticket number for the currect position
int ticket=PositionGetTicket(i);
//Get the position direction
int PositionDirection = PositionGetInteger(POSITION_TYPE);
//if it is buy postition
if(PositionDirection==POSITION_TYPE_SELL)
//close the currect position
trade.PositionClose(ticket);
}
}
}
Please assist on this bot.. thanks in advance
Hi I want to open and close buy/sell trades based on Moving Average. But here the problem is once the trade is opened its not closing based on my condition. Please assist on my below codes
The doubt which I have in the function buyOrderclose() and the function sellOrderclose(). I have checked my conditions looks pretty good and the buyOrder() and sellOrder() also. I have tested in Mql5 also but its not working properly
Upvotes: 0
Views: 954
Reputation: 95
PROBLEM
SOLUTION
if a condition met, e.g if the last candle close price is above the ema it will buy, and if there is any profit of 1$ close it. or close it by default stop loss
IMAGINE THE FAKE CODE
onTick(){
if ( close_price > ema )
{
buy();
}
if ( close_price < ema )
{
sell();
}
if ( profit == 0.50 )
{
close_all();
}
}
ARE YOU STILL CONFUSED???
Upvotes: 0