placing hedged position at key levels

 

#include <Expert\Expert.mqh>

MqlTick last_tick;
MqlTick tickarray[2];

double last_bid = last_tick.bid;
double last_ask = last_tick.ask;

int openpositions = PositionsTotal();

bool levelcheck();


double bsl = last_ask - 150*_Point;
double btp = last_bid + 300*_Point;
double ssl = last_bid + 150*_Point;
double stp = last_ask - 300*_Point;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OP_BUY()
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request= {};
   MqlTradeResult  result= {};
//--- parameters of request
   request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
   request.symbol   =Symbol();                              // symbol
   request.volume   =0.1;                                   // volume of 0.1 lot
   request.type     =ORDER_TYPE_BUY;                        // order type
   request.sl       = bsl;                                  // stoploss
   request.tp       = btp;                                  //takeprofit
   request.price    =last_bid;                              // price for opening
   request.deviation=2;                                     // allowed deviation from the price

//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OP_SELL()
  {
//--- declare and initialize the trade request and result of trade request
   MqlTradeRequest request= {};
   MqlTradeResult  result= {};
//--- parameters of request
   request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
   request.symbol   =Symbol();                              // symbol
   request.volume   =0.1;                                   // volume of 0.2 lot
   request.type     =ORDER_TYPE_SELL;                       // order type
   request.sl       = ssl;                                  // stoploss
   request.tp       = stp;                                  //takeprofit
   request.price    =last_ask;                              // price for opening
   request.deviation=2;                                     // allowed deviation from the price

//--- send the request
   if(!OrderSend(request,result))
      PrintFormat("OrderSend error %d",GetLastError());     // if unable to send the request, output the error code
//--- information about the operation
   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   CopyTicks(_Symbol, tickarray, COPY_TICKS_ALL, 0, 2);

  


   if(PositionsTotal() == 0)
     {
      if(levelcheck() == true)
        {

         OP_BUY(); // Opening BUY
         OP_SELL(); // Opening SELL
         return;
        }
      else
         return;
     }
  else
      return;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool levelcheck()
  {

   double currentprice = tickarray[1].bid;
   double currpricetrunc = NormalizeDouble(currentprice, 2);
   double lastprice = tickarray[0].bid;
   double lastpricetrunc = NormalizeDouble(lastprice, 2);

   if(currpricetrunc != lastpricetrunc)
     {
      return true;
     }
   else
      return false;
  }

Hi All, I am attempting to create an EA that places two opposing trades at every 0.01 of price, and only places another after these two have been closed, this my first attempt at building an EA and Im not quite sure why the code attached does not work, could somebody please point me in the right direction?

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 
double last_bid = last_tick.bid;
double last_ask = last_tick.ask;
int openpositions = PositionsTotal();
double bsl = last_ask - 150*_Point;
double btp = last_bid + 300*_Point;
double ssl = last_bid + 150*_Point;
double stp = last_ask - 300*_Point;

Those are not assignments; they are initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 (2013)

 
William Roeder #:

Those are not assignments; they are initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 (2013)

Hi William, thank you for your guidance, all is working now