so my idea for an algo is pretty simple: I want to buy if two sma cross and i want to close my position and open a sell when the 2 sma cross from the other direction. My probleme here is now that, my algo is not very accurate. The sell or buy happens after one or two candles after the crossover. Is there a way to make it more accurate, so that the bot buys/sells right after the crossover?
thanks in advance
// 7 smastaticint handlesevenMa = iMA(_Symbol,PERIOD_CURRENT,7,0,MODE_SMA,PRICE_CLOSE);
double SevenMaArray[]; // create an arrayCopyBuffer(handlesevenMa,0,1,3,SevenMaArray); // defined 7 SMA, one buffer, current candle, 3 candles, store resultArraySetAsSeries(SevenMaArray,true); // sort the array from the current candle downwards// 2 smastaticint handletwoMa = iMA(_Symbol,PERIOD_CURRENT,2,0,MODE_SMA,PRICE_CLOSE);
double TwoMaArray[]; // create an arrayCopyBuffer(handletwoMa,0,1,3,TwoMaArray); // defined 7 SMA, one buffer, current candle, 3 candles, store resultArraySetAsSeries(TwoMaArray,true); // sort the array from the current candle downwards //buy and sellif(TwoMaArray[1] < SevenMaArray[1] && TwoMaArray[0] > SevenMaArray[0]) // 2 sma is bigger than 7 sma
{
if(PositionsTotal()<1) // no trade open --> buy
{
// buyPrint("Buy");
Print("TwoMaArray = ",TwoMaArray[0]," > ","SevenMaArray = ",SevenMaArray[0]);
trade.Buy(lotsize,_Symbol,ask,0,tpBuy,"This is a buy for");
}
else//if(PositionsTotal()==1) // if trade open --> close trade first and than buy
{
CloseAllPositions();
Print("SELL is closed");
Print("Buy");
Print("After closing a sell position: TwoMaArray = ",TwoMaArray[0]," > ","SevenMaArray = ",SevenMaArray[0]);
trade.Buy(lotsize,_Symbol,ask,0,tpBuy,"This is a buy for");
}
}
if(TwoMaArray[1] > SevenMaArray[1] && TwoMaArray[0] < SevenMaArray[0]) // 7sma is bigger than 2sma
{
if(PositionsTotal()<1) // o trade open --> Sell
{
Print("Sell");
trade.Sell(lotsize,_Symbol,bid,0,tpSell,"This is a sell for");
Print("TwoMaArray = ",TwoMaArray[0]," < ","SevenMaArray = ",SevenMaArray[0]);
}
else//if(PositionsTotal()==1) // if a trade is open --> close trade first and than sell
{
CloseAllPositions();
Print("BUY is closed");
Print("Sell");
Print("After closing a buy position:TwoMaArray = ",TwoMaArray[0]," > ","SevenMaArray = ",SevenMaArray[0]);
trade.Sell(lotsize,_Symbol,bid,0,tpSell,"This is a sell for");
}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
hello,
so my idea for an algo is pretty simple: I want to buy if two sma cross and i want to close my position and open a sell when the 2 sma cross from the other direction. My probleme here is now that, my algo is not very accurate. The sell or buy happens after one or two candles after the crossover. Is there a way to make it more accurate, so that the bot buys/sells right after the crossover?
thanks in advance