MT4 Expert Advisor places same order twice - page 2

 
William Roeder:
  1. Obviously you have the code running twice on the same chart.

    It can't run twice in the tester. Account type is irrelevant.

    Duplicate chart, code on each? VPS and you didn't disable it on your machine? Terminal running twice?

  2. You buy at the Ask and sell at the Bid.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

Thanks for the pointers Willam. Apologies for taking long to respond. See responses for point No.2 below.

2.1. I asssume that by "specfied amount" you mean that this take profit value '(Ask+(1000*Point())' must be the same as this stop loss value  '(Bid-(3000*Point())' ?

2.2. I have followed the link you provided to figure out how to add average spread to my sell positions. Math is not my strongest points so I struggled to follow the Power Mean calculations you have used. I have since developed the following code to deal with spread and its widening. Please let me know if there are better ways to calculate average spread and manage spread widening. See code below.

input int Spread_MAX = 100;
//+----------------------------+
//| Expert tick function       |
//+----------------------------+
void OnTick()
{
//+-----------------------+
//Spread Variables        |
//+-----------------------+ 
double LowSpread = 100000;
double HighSpread = 0;

int digits=Digits();

//+---------------------------------------------------------------+
//Spread Calculation                                              |
//+---------------------------------------------------------------+ 
int Spread_Points = (int)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD);//Market spread in points 
 
if(Spread_Points<LowSpread)
 LowSpread  = Spread_Points*Point();//Lowest spread in price
 
if(Spread_Points>HighSpread)
 HighSpread = Spread_Points*Point();//Highest spread in price

double Spread_AV = NormalizeDouble((HighSpread+LowSpread)/2,digits);//Average spread in price

//+-------------------------------+
//Spread Filter                   |
//+-------------------------------+
string Spread_Filter = "";

if(Spread_Points > Spread_MAX)
 {
  Spread_Filter = "Abnormal";
 }
 
if(Spread_Points <= Spread_MAX)
 {
  Spread_Filter = "Normal";
 }
//+-----------------------------------------------------------------------------------------------------------------+
// Chart Outputs                                                                                                    |
//+-----------------------------------------------------------------------------------------------------------------+ 
Comment ("HighSpread is:",HighSpread, " | LowSpread is:",LowSpread," | AverageSpread is: ",Spread_AV,
         "\nSpread_Points is:",Spread_Points, " | Spread_MAX. is:",Spread_MAX," | Spread Filter is:",Spread_Filter);

}//Void OnTick parantheses