Testing BUY/SELL on close price previous bar

 

Hello!

I have two questions.

1. I want to buy/sell after crossing two WMA on new bar but for price close prev bar. And after place an order 5minutes later if order will be still active (no transaction), buy/sell for every price. Someone can tell me how to write after 5 minutes buy/sell for every price. Below is my part of code.

2. I wonder, when i testing my system on history date and in last bar I have buy signal, then i will buy but for close price prev. bar off course if it'll be possible( if not then i will buy 5 minutes later for every price). Metastock will show me transaction only if this transaction really was in life ? or show me for example when i have to buy on price 1.33333, new bar open on 1.33334 and rising to 1.33339 and follow down to 1.3330 but it was not any transaction on 1.33333. Metastock will show me my transaction on 1.33333 or not? It show me in this case ? Sorry for English, i not sure is clear...

Thx for help.


pricee=NormalizeDouble( iClose(0,0,1),6);

if (Time[1] != lastbarstart)  // this is true for the first tick of a new bar
  {
   
  
   lastbarstart = Time[1];
   
   
  
      
      if(  ( var1<var2    && flaga==3 ))     
         {
             flaga=1;
             flaga2=3;
             CloseOrders();
         
             OrderSend(Symbol(), OP_SELL,1, pricee,1,0,0,"SHORT!!",Magic,0,Blue);
        
         } 
}                    
 
lukibest:

Hello!

I have two questions.

1. I want to buy/sell after crossing two WMA on new bar but for price close prev bar. And after place an order 5minutes later if order will be still active (no transaction), buy/sell for every price. Someone can tell me how to write after 5 minutes buy/sell for every price. Below is my part of code.


You Buy at Ask and Sell at Bid, unless pricee is the same as Bid your OrderSend will fail.
 

I think you are asking for code that looks something like this.

//+------------------------------------------------------------------+
//|  |
//|  |
//|  |
//+------------------------------------------------------------------+

static datetime AlertTime ;
static bool Mytrigger ;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+

int init() 
{
  Mytrigger=false ;
  return(0);
} // end of function init()

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+

int deinit() 
{
  return(0);
} //end of function deinit()

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+

int start()
{
//code to recognise order to be placed in 5 mins
Mytrigger=true
AlertTime=TimeCurrent()+5*60*60 ;// add 5 Minutes in seconds

pricee=NormalizeDouble( iClose(0,0,1),6); //6 decimal place broker?

if ( (TimeCurrent() > AlertTime) && Mytriger==true)  //nearest tick after 5 minutes , Mytrigger == true is unnecessary but put this way to be clear.
  {  
    
         if(  ( var1<var2    && flaga==3 ))  //code purpose unknown 
         {
             flaga=1; //code purpose unknown
             flaga2=3; //code purpose unknown
             CloseOrders(); //code purpose unknown
         
             if ( OrderSend(Symbol(), OP_SELL,1, pricee,1,0,0,"SHORT!!",Magic,0,Blue)>0 ) Mytrigger=false;        
         } 
}     return(0);
} //end of function start()

//+------------------------------------------------------------------+ 
 
RaptorUK:
You Buy at Ask and Sell at Bid, unless pricee is the same as Bid your OrderSend will fail.

but if i will send buy/stop limit ?
 
lukibest:

but if i will send buy/stop limit ?


Yes, for a Buy Stop you can set any open price you like that meets this: OpenPrice-Ask StopLevel a StopLoss that meets this: OpenPrice-SL StopLevel and a Take Profit that meets this: TP-OpenPrice StopLevel then you will not get Error 130s

All the info is here: https://book.mql4.com/appendix/limits

 

Sugestion start really simple

bool PlaceBuy=false ;

// code to recognise placing buy order

if ( PlaceBuy ) comment( "place buy condition found") ;

if( !PlaceBuy ) comment( "bored bored bored" ) ;

and use the MACD Sample program that comes with MetaTrader for how to place an order and structure an EA.

 
AlertTime=TimeCurrent()+5*60*60 ;// add 5 Minutes in seconds
pricee=...
if ( (TimeCurrent() > AlertTime .. // This condition will always be false.
 
WHRoeder:


Yes slight weakness in the example code.

if ( Mytriger==false) 
    {
     //code to recognise order to be placed in 5 mins
     Mytrigger=true
     AlertTime=TimeCurrent()+5*60*60 ;// add 5 Minutes in seconds
    }
should correct that problem
 

it's possible to check order status when i have selllimit

status= OrderSend(Symbol(), OP_SELLLIMIT ,1, price,1,0,0,"short!!",Magic,0,Yellow);

if (status<0)
     .......


I ask because i see in most examples if status <0 then error but in this case (selllimit) doesn't work because is waiting. How to check order is it finish or still waiting?



Thank you all for answers.

 
lukibest:

it's possible to check order status when i have selllimit

I ask because i see in most examples if status <0 then error but in this case (selllimit) doesn't work because is waiting. How to check order is it finish or still waiting?

There is no status to check, your variable status is the return value from OrderSend(), if it is less than 0 you have an error, do you then print the error to the log ? what error is it ?

What is the value of price compared to Bid at the time you try to place the order ?
 

The broker keeps a record of the trade and pending tades you send to them. So OrderSend(....) to be sucessful must return a ticket value greater than 0 (the tester starts ticket values at 1)

After you have sent a pending order the broker knows that there is a tade waiting to become active at the price you gave them.

That means that if you turned your computer off and the price is reached while your computer is off the trade still goes from waiting for price to active trade.

Reason: