Problem with spread and point

 

Hello everyone,

I am experiencing problems applying variables "points" and "Spread" to a decimal values. What does the value shown in points mean?

How do I get Spread to show only 2 places after the decimal point?    

I have attached the values that I received while I was debugging the code.


void RunTest()
   {
   bid_spread= MarketInfo("GOLD",MODE_BID);   // 1242.19
   ask_spread= MarketInfo("GOLD",MODE_ASK);   // 1242.63
   points = MarketInfo(Symbol(),MODE_POINT);  // 1e-05.0
   Spread = bid_spread - ask_spread;          // -0.4400000000000546
   }
 
GrumpyDuckMan:

Hello everyone,

I am experiencing problems applying variables "points" and "Spread" to a decimal values. What does the value shown in points mean?

How do I get Spread to show only 2 places after the decimal point?    

I have attached the values that I received while I was debugging the code.


//------------ use 

double Spread = NormalizeDouble(fabs(bid_spread - ask_spread),2);
 

Spread is always Ask - Bid (not  Bid - Ask).

You can also retrieve it directly without doing a calculation e.g.

New style:

   long spread = SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);

Old style:

   int spread = MarketInfo(_Symbol,MODE_SPREAD);

That is going to give you a figure like 10 or 20.

That means 10 (or 20) points, which is 1 (or 2) pips in common parlance for most brokers.

GrumpyDuckMan:

What does the value shown in points mean? 1e-05.0

It is just another way to write 0.00001

It means move the decimal point 5 places to the left (to the left because it is negative)

Start with 1.0

1 place left = 0.1

2 places left = 0.01

3 places left = 0.001

4 places left = 0.0001

5 places left = 0.00001

GrumpyDuckMan:

How do I get Spread to show only 2 places after the decimal point?    


Quite often you may come across lots of extra zeros and some random digits at the end of a number.

This isn't a bug, it is the way floating point numbers are stored in binary.

Don't get bogged down in the reasons why (unless you really want to), just control the output when you want to display it.

You can use PrintFormat() or StringFormat() to control the display of numbers

Example to print a number to 2 decimal places:

   double number = 0.6583473459847;
   PrintFormat("%.2f",number);

Result: 0.66

 

Thanks,

You have both been very helpful.

I am just a bit confused about "MetaTrader4" Terminal window.

If I right click "Profit" and move to "profit" I have three options "as Points", "as Term Currency" and "as Deposit Currency".

I usually look at "term Currency" and "Points". I am just trying to work out how "As Points" can be adapted in to code.

 
honest_knave:

Spread is always Ask - Bid (not  Bid - Ask).

You can also retrieve it directly without doing a calculation e.g.

New style:

Old style:

That is going to give you a figure like 10 or 20.

That means 10 (or 20) points, which is 1 (or 2) pips in common parlance for most brokers.

It is just another way to write 0.00001

It means move the decimal point 5 places to the left (to the left because it is negative)

Start with 1.0

1 place left = 0.1

2 places left = 0.01

3 places left = 0.001

4 places left = 0.0001

5 places left = 0.00001


Quite often you may come across lots of extra zeros and some random digits at the end of a number.

This isn't a bug, it is the way floating point numbers are stored in binary.

Don't get bogged down in the reasons why (unless you really want to), just control the output when you want to display it.

You can use PrintFormat() or StringFormat() to control the display of numbers

Example to print a number to 2 decimal places:

Result: 0.66

I'm not sure I totally understand. I will give it at go. 
 
GrumpyDuckMan:
I'm not sure I totally understand. I will give it at go. 

1e = 30

Is that right?

 
GrumpyDuckMan:
I'm not sure I totally understand. I will give it at go. 


Most of it is getting your head around the difference between the values stored in your variables, and how you make them look when you print or otherwise display them.

Using a simple example of an indicator that shows the current spread in the chart comment

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   Comment(""); // clear the chart comment when you remove the indicator
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   double sprd  = (double)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD); // get the spread in points
   if(Digits%2==1) sprd/=10;  // convert points to pips
   Comment(StringFormat("Spread: %.1f pips",sprd)); // display the spread in the chart comment
   return(rates_total);
  }


 
GrumpyDuckMan:

1e = 30

Is that right?


No, don't get confused between the notation "1e-05.0" and the codes you use in StringFormat() and PrintFormat().

StringFormat() and PrintFormat() are very useful tools, but if you find them confusing you could change the code I posted above like this:

   double sprd  = (double)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD); // get the spread in points
   if(Digits%2==1) sprd/=10;  // convert points to pips
   Comment("Spread: ", DoubleToString(sprd,1), " pips"); // display the spread in the chart comment


I think I may be unnecessarily confusing you because I'm not entirely sure what you are trying to achieve...

 

Hello,

This is the entire code I have currently, I tried to make it as tidy as possible to read.

//+------------------------------------------------------------------+
#property copyright "Educational Use"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window 
//+------------------------------------------------------------------+
 input ENUM_ORDER_TYPE ORDERS =MODE_TRADES;
 input ENUM_MA_METHOD Method =MODE_SMA;
 input float Amount,Amount1;
 double Spread,ask_spread,bid_spread,points,Test_Debug;
 int SortLoss,SortProfit;
 //+------------------------------------------------------------------+
 enum symbols {EURUSD,
               USDJPY,
               AUDCAD,
               GOLD,
               AUDUSD,
               };
input symbols symbol;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
   {
  RunTest();
 GetSymbolSpread();

   return(INIT_SUCCEEDED);
   }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
   {
   }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
   {
   }
//+------------------------------------------------------------------+   
void StartBuyOrders()
   { 
   string sym = EnumToString(symbol);
   // you'll also need to deal with broker suffix / prefix
   double ask    = SymbolInfoDouble(sym,SYMBOL_ASK),
          bid    = SymbolInfoDouble(sym,SYMBOL_BID),
          pnt    = SymbolInfoDouble(sym,SYMBOL_POINT);
   int    digits = (int)SymbolInfoInteger(sym,SYMBOL_DIGITS);
   int ticket=OrderSend(sym ,OP_BUY,Amount,ask,4,MODE_STOPLEVEL,0 ,"EA Order",0,0,clrBlue); 
  if(ticket<1) 
   { 
   Print("Debug",GetLastError()); 
   } 
 else 
   Print("Order successfully"); 
   }   
//+------------------------------------------------------------------+  
void StartSellOrders()
   { 
   double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); 
   double stoploss=0;
   double takeprofit=0;
   int ticket=OrderSend(Symbol(),OP_SELL,Amount1,Bid,3,stoploss,takeprofit,"EA Order",0,0,clrBlue); 
   if(ticket<1) 
   { 
   Print("Debug",GetLastError()); 
   } 
 else 
   Print("Order successfully"); 
   }
//+------------------------------------------------------------------+  
void GetSymbolSpread()
   {
   bid_spread= MarketInfo("GOLD",MODE_BID);                          //
   ask_spread= MarketInfo("GOLD",MODE_ASK);                          // 
   points = MarketInfo("GOLD",MODE_POINT);                           //
   Spread = NormalizeDouble((ask_spread-bid_spread),3);              //
   }
//+------------------------------------------------------------------+
void RunTest()
   { 
   Comment("");                                                      // clear the chart comment when you remove the indicator
   }
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
   {
   double sprd  = (double)SymbolInfoInteger(_Symbol,SYMBOL_SPREAD); // get the spread in points
   if(Digits%2==1) sprd/=10;                                        // convert points to pips
   Comment(StringFormat("Spread: %.1f pips",sprd));                 // display the spread in the chart comment
   return(rates_total);
   }
 //+------------------------------------------------------------------+
void GetSymbolPoints()
   {
   }
  //+------------------------------------------------------------------+
  
 

 Hi honest_knave,

"I think I may be unnecessarily confusing you because I'm not entirely sure what you are trying to achieve..."

I am trying to setup a code to trade one or possibly two currencies with a minimal invested amount of about $300 AUD.

 
GrumpyDuckMan: I am trying to setup a code to trade one or possibly two currencies with a minimal invested amount of about $300 AUD.
Which has nothing to do with this thread.
  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  • Do NOT use TickValue by itself - DeltaPerLot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
Reason: