more accurate SMA

 

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


 // 7 sma
       static int handlesevenMa = iMA(_Symbol,PERIOD_CURRENT,7,0,MODE_SMA,PRICE_CLOSE);
       double SevenMaArray[]; // create an array
       CopyBuffer(handlesevenMa,0,1,3,SevenMaArray); // defined 7 SMA, one buffer, current candle, 3 candles, store result
       ArraySetAsSeries(SevenMaArray,true); // sort the array from the current candle downwards
        
       // 2 sma
       static int handletwoMa = iMA(_Symbol,PERIOD_CURRENT,2,0,MODE_SMA,PRICE_CLOSE);
       double TwoMaArray[]; // create an array
       CopyBuffer(handletwoMa,0,1,3,TwoMaArray); // defined 7 SMA, one buffer, current candle, 3 candles, store result
       ArraySetAsSeries(TwoMaArray,true); // sort the array from the current candle downwards   
       
       //buy and sell
       if(TwoMaArray[1] < SevenMaArray[1] && TwoMaArray[0] > SevenMaArray[0]) // 2 sma is bigger than 7 sma
       {
           if(PositionsTotal()<1) // no trade open --> buy
             {
              // buy
              Print("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");
             }
          }
Reason: