How can I send `Buy` request on Start?

 
ini OnInit() {

        if(PRICE_CLOSE > iMA(Symbol(), Period(), 20, 0, PRICE_CLOSED)) {
        req.action = TRADE_ACTION_DEAL;
        req.price = NormalizedDouble(lastest_price)
        OrderSend(req);
   }
}

I am new to the MQ5 programming. Trying to learn very basic stuffs. 

I want to request sell order with sl 20 tp 20 with lot 0.01 and I am kinda stuck here. 

I tried to look for some documents, but it was bit hard to look it up. 

What are the properties that I need to fill in order to do just this simple transaction?

 
skarlgus8971:

I am new to the MQ5 programming. Trying to learn very basic stuffs. 

I want to request sell order with sl 20 tp 20 with lot 0.01 and I am kinda stuck here. 

I tried to look for some documents, but it was bit hard to look it up. 

What are the properties that I need to fill in order to do just this simple transaction?

Code: open BUY, NO Stop loss, NO Take profit

//+------------------------------------------------------------------+
//|                                                     Open Buy.mq5 |
//|                              Copyright © 2018, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2018, Vladimir Karputov"
#property version   "1.000"
//---
#include <Trade\Trade.mqh>
CTrade         m_trade;                      // trading object
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(Symbol());
   m_trade.Buy(1.0); // open Buy position, volume 1.0 lot
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov:

Code: open BUY, NO Stop loss, NO Take profit

Okay, so, I can add those variables with '  .  ' and then use .Buy to send orders. Thank you so much!
 
skarlgus8971 :
Okay, so, I can add those variables with '  .  ' and then use .Buy to send orders. Thank you so much!

Not everything is simple. To calculate Stop Loss and Take Profit you need to get the current Ask and Bid prices.

 
skarlgus8971: Trying to learn very basic stuffs.

Don't try to use any price or server related functions in OnInit (or on load), 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. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
bool isFirstTick;
OnInit(){ isFirstTick=true; … }
OnTick(){
   if(isFirstTick) …
 

Okay. Thanks for the tip.

Btw, I set SL and TP for buy or sell trade. However, it works on BUY trades but it does not on Sell trades.


double Bid = NormalizedDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
int tp = 60;

trade.Buy(0.01, NULL, Bid, (Bid-tp * _POINT), (Bid+tp * _POINT), NULL);
trade.Sell(0.01, NULL, Bid, (Bid+tp * _POINT), (Bid_tp * _POINT), NULL);

When I just do trade.Buy(0.01) and trade.Sell(0.01); 

Both execute trade. However, when I apply the stop loss and take profit, then only Buy works on eurusd pair. Just wondering, what I have done wrong?

 
skarlgus8971:

Okay. Thanks for the tip.

Btw, I set SL and TP for buy or sell trade. However, it works on BUY trades but it does not on Sell trades.


When I just do trade.Buy(0.01) and trade.Sell(0.01); 

Both execute trade. However, when I apply the stop loss and take profit, then only Buy works on eurusd pair. Just wondering, what I have done wrong?

trade.Buy(0.01, NULL, Bid, (Bid-tp * _POINT), (Bid+tp * _POINT), NULL);
trade.Sell(0.01, NULL, Bid, (Bid+tp * _POINT), (Bid_tp * _POINT), NULL);

What is Bid_tp?

Reason: