difference between close price on tick and buy price

 

Hi, I'm trying to make an automated trading bot.

Every time I want to make buy order with the last close price in the function "ontick", I see the price bought in my order several pip higher than what I want, and I don't understand why. Somebody can help me?

I'm in a demo account with swissquote as broker.

Here is an example:


here is the code:


#include <Trade\Trade.mqh>                                         //include the library for execution of trades
#include <Trade\PositionInfo.mqh>                                  //include the library for obtaining information on positions

string my_symbol;
ENUM_TIMEFRAMES my_timeframe;
int iMAfast_handle;
int iMAslow_handle;
int iSto_handle;

double MAslow_buffer[];
double MAfast_buffer[];
double Stochastic_buffer[];
double Signal_buffer[];

double            High_buffer[];
double            Low_buffer[];
double            Close_buffer[]; 

CTrade            m_Trade;                                 //structure for execution of trades
CPositionInfo     m_Position;                              //structure for obtaining information of positions

double StopLoss;
double StopLossMarg=0.0002;

int lastMinValue=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   my_symbol="USDCHF";
   my_timeframe=PERIOD_M1;
   iMAfast_handle=iMA(my_symbol,PERIOD_M1,50,0,MODE_EMA,PRICE_CLOSE);
   iMAslow_handle=iMA(my_symbol,PERIOD_M1,100,0,MODE_EMA,PRICE_CLOSE);
   iSto_handle=iStochastic(my_symbol,PERIOD_M1,5,3,3,MODE_SMA,STO_LOWHIGH);
   
   ChartIndicatorAdd(ChartID(),0,iMAfast_handle);
   ChartIndicatorAdd(ChartID(),0,iMAslow_handle);
   ChartIndicatorAdd(ChartID(),0,iSto_handle);
   
   ArraySetAsSeries(MAfast_buffer,true); 
   ArraySetAsSeries(MAslow_buffer,true); 
   ArraySetAsSeries(Stochastic_buffer,true);
   ArraySetAsSeries(Signal_buffer,true);
   ArraySetAsSeries(High_buffer,true);
   ArraySetAsSeries(Low_buffer,true);
   ArraySetAsSeries(Close_buffer,true);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  IndicatorRelease(iMAfast_handle); 
  IndicatorRelease(iMAslow_handle);
  IndicatorRelease(iSto_handle);
  ArrayFree(MAfast_buffer);
  ArrayFree(MAslow_buffer);
  ArrayFree(Stochastic_buffer);
  ArrayFree(High_buffer);
  ArrayFree(Low_buffer);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
  datetime Time[1];
  CopyTime(_Symbol,_Period,0,1,Time);
  MqlDateTime Time_str;
  TimeToStruct(Time[0],Time_str);
  
  CopyClose(my_symbol,my_timeframe,1,2,Close_buffer);
  if(StopLoss>Close_buffer[0])
  {
      if(m_Position.Select(my_symbol))
      {
         m_Trade.PositionClose(my_symbol);
      }
  }
  
  if(Time_str.min!=lastMinValue)
  {
     lastMinValue=Time_str.min;
     CopyBuffer(iMAfast_handle,MAIN_LINE,0,1,MAfast_buffer);
     CopyBuffer(iMAslow_handle,MAIN_LINE,0,1,MAslow_buffer);
     CopyBuffer(iSto_handle,MAIN_LINE,0,1,Stochastic_buffer);
     CopyHigh(my_symbol,my_timeframe,1,2,High_buffer);
     CopyLow(my_symbol,my_timeframe,1,2,Low_buffer);
     
     if(MAfast_buffer[0]>MAslow_buffer[0] && Stochastic_buffer[0]>50)
     {
         if(!m_Position.Select(my_symbol))
         {
            m_Trade.Buy(0.1,my_symbol,Close_buffer[0]);
            StopLoss=Close_buffer[0]-StopLossMarg;
         }
     }
     if((High_buffer[1]-StopLossMarg)>StopLoss)
     {
         StopLoss=High_buffer[1]-StopLossMarg;
     }
  }
}
//+------------------------------------------------------------------+
 
  1. happylulux: Every time I want to make buy order with the last close price in the function "ontick", I see the price bought in my order several pip higher than what I want, and I don't understand why.
    You can't buy at the close price (Bid) you buy at the Ask.
  2. Your posted code does not contain any open order code.

 
happylulux:

Hi, I'm trying to make an automated trading bot.

Every time I want to make buy order with the last close price in the function "ontick", I see the price bought in my order several pip higher than what I want, and I don't understand why. Somebody can help me?

I'm in a demo account with swissquote as broker.

Here is an example:


here is the code:


Because of the spread. Check its value and open a position if it is <= x (such as 2 or lower).
Reason: