How to get ask bid price on history array with copyclose?

To add comments, please log in or register
happylulux
19
happylulux  

Hi,

I want to calculate a vector who represent the profitability to buy now a forex cross (like EURUSD) and sell it in a futur period of time pre-defined.

To calculate this, I made a for loop who calculate the difference between the close price now and the close price in 20minutes(for exemple).

This is working now, i get the data from the function CopyClose().

Now I want to put the spread on my model, i have two solutions: calculate the difference between the ask/close price now and the bid/close price in 20minutes, or I can calculate the difference between the close now and the close in 20minute minus the spread.

But I don't understand which price is returned from the copyclose function (ask or bid? is it possible to configure it?) and/or how to get the spread (I tried with copyspread, i suppose that the unit is in 1/10000, but guess what?it's never profitable to buy and sell in 20minutes with the spread given by copyspread function).

I hope somebody can help me.

Best regards.

whroeder1
17943
whroeder1  
happylulux: But I don't understand which price is returned from the copyclose function (ask or bid?
  1. All charts are bid charts.  The spread if provided is in points.
  2. I'm on MT4 so I don't have the spread. I maintain my own.
    //{ Typedefs
    #define  PRICE    double         // A PRICE
    #define     PRICE_MAX   (PRICE)EMPTY_VALUE
    #define     PRICE_MIN   (PRICE)0.0
    #define     CHANGE      PRICE    // Difference of two prices.
    #define  INDEX    uint           // Zero based.
    #define  COUNT    uint           //  One based.
    #define  MONEY    double
    #define  SYMBOL   string
    //} Typedefs
       //{ Average maximum Spread.   (Must run per tick.)
    #define EMA(P, C, L) ((P) + ((C)-(P))*2./(L+1))
    // https://en.wikipedia.org/wiki/Generalized_mean#Special_cases (Power Mean)
    #define PMA(P, C, L, PM) MathPow(EMA(MathPow(P,PM), MathPow(C,PM), L), 1.0/PM)
          // AMS is saved in a global variable for the case of TF change.
          static const string  ms = "MaxSpread_" + _Symbol;
          static const double  PM = 10;
          CHANGE curSpread        = (Ask - Bid) / _Point;
          static CHANGE preSpread = 0;
          if(preSpread == 0.0){   GlobalVariableGet(ms, preSpread);
                                  if(preSpread == 0.0)    preSpread   = curSpread;
          }
          preSpread   = 
          curSpread   = PMA(preSpread, curSpread, Volume[1]+Volume[2], PM);
             GlobalVariableSet(ms, preSpread);   
       //} Average maximum Spread.

happylulux
19
happylulux  

Hi,

maybe my calculus is wrong...a point is 1/10000 of the price right? I checked and the difference of price on 20min is approximately 5 to 15pip, and the spread is everytime approximately 20 (but increase to 150 pip during a certain time), is that usual values?? if yes, it mean that "fast" trading (less than 30min between buy and sell) is never profitable...I think I make an error somewhere... here is my function, it write the "rentability" vector on a csv file.

void writeOutputToFile(string symbol,string filename,datetime startdate,int Ndata,int mean)
{
   Print("date d'output: ",startdate);
   double data[],t_spread[],rentable[];
   ArraySetAsSeries(data,1);
   int spread[];
   while(CopyClose(symbol,TIMEPERIOD,startdate,Ndata,data)!=Ndata)
   {
      Print("reacquisition du close du symbole: ",symbol);
   }
   while(CopySpread(symbol,TIMEPERIOD,startdate,Ndata,spread)!=Ndata)
   {
      Print("reacquisition du spread du symbole: ",symbol);
   }
   ArrayResize(t_spread,Ndata);
   for(int i=0;i<Ndata;i++)
   {
      t_spread[i]=double(spread[i]);
   }
   Print("spread moyen: ",getAvg(spread)," spread max: ",spread[ArrayMaximum(spread)]," spread minimum: ",spread[ArrayMinimum(spread)]);
   int N=ArraySize(data)-mean;
   ArrayResize(rentable,N);
   for(int i=0;i<N;i++)
   {
      rentable[i]=10000*(data[i+mean]-data[i])-spread[i];//unit: pip
   }
   
   string strdata="";
   for(int i=0;i<N;i++)
   {
      strdata=strdata+string(rentable[i])+"\n";
   }
   if(FileIsExist(filename,FILE_COMMON))
   {
      Print("suppression du fichier d'output");
      do{
      }while(FileDelete(filename,FILE_COMMON)==false);
   }
   
   int file=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_ANSI|FILE_TXT|FILE_COMMON);
   if(file==INVALID_HANDLE)
      Print("impossible d'ouvrir le fichier d'output");
   
   FileWrite(file,strdata);
}

I use XM as broker, I tried on usdchf cross, maybe this change something i don't know...

To add comments, please log in or register