Simple program to buy on open and sell on close

 

Hi,  I am trying just to write a code that buys on the open of a candle and sells on the close of that same candle.  Can anyone provide a snipit of a code that does this?

 
void work_new_bar(void)
  {
   static datetime t_bar=iTime(_Symbol,PERIOD_CURRENT,0);
   datetime _time=iTime(_Symbol,PERIOD_CURRENT,0);
   if(t_bar==_time)
      return;
   t_bar=_time;
/* work */
  }
 

Thanks Konstantin,


How do I implement this?  Where do I place this in the code?

 

Hi Konstantin, 

The heart of the code is below.  After "golong" I would just want to close the position at the highest value of 3 cases on the same candle (1: bullish candle high, 2: bearish candle close, 3: bearish candle stoploss value).

void OnTick() {


   if (use_trailing) {

      trailingStop();

   }


   if (close_friday) {

      closeFriday();

   }


   // if position closed

   if (last_position > PositionsTotal()) checkAlert();

   

   // check time of current bar to determine new bar comes to run logic

   datetime current_bar = iTime(Symbol(),Period(),0); 

   

   // if new bar comes

   if (current_bar > last_bar) {


      int firstbearcandle;

      int bodybearcandle;

      int firstbullcandle;

      int bodybullcandle;

      bool golong = false;

      bool goshort = false;

      bool closelong = false;

      double closingprice;

      double MA1;

      double MA2;

      int goodslope;

   

      if (iOpen(Symbol(),Period(),2) < iClose(Symbol(),Period(),2)){

         firstbearcandle=1;}

         else {

         firstbearcandle=0;}

      if (iOpen(Symbol(),Period(),2) - iClose(Symbol(),Period(),2) > longbear){

         bodybearcandle=1;}

         else {

         bodybearcandle=0; }

      if (iClose(Symbol(),Period(),1) < iOpen(Symbol(),Period(),1)){

         firstbullcandle=1;}

         else {

         firstbullcandle=0;}

      if (iClose(Symbol(),Period(),1) - iOpen(Symbol(),Period(),1) > longbull){

         bodybullcandle=1;}

         else {

         bodybullcandle=0;}

      

      MA1=iMA(Symbol(),0,7,1,averageType,PRICE_CLOSE);

      MA2=iMA(Symbol(),0,7,2,averageType,PRICE_CLOSE);

      

      if (MA1>MA2){

         goodslope=1;}

         else{

         goodslope=0;}

         

      golong=true;   

         

      if (firstbearcandle == 1 && bodybearcandle == 1 && firstbullcandle == 1 && bodybullcandle ==1 && goodslope ==1) {

         golong = true;

         closelong = true;}

   

      closingprice=MathMax(iClose(Symbol(),Period(),0),iOpen(Symbol(),Period(),0)-stoploss);

      // check for opening

     

     if (golong == true) {

         Alert("golong");

         PlaySound("stops.wav");

         if (close_on_reverse) closePosition(POSITION_TYPE_SELL);

         openBuy();

      }

 

      if (goshort == true) {

         Alert("goshort");

         PlaySound("stops.wav");

         if (close_on_reverse) closePosition(POSITION_TYPE_BUY);

         openSell();

      } 


      

      // plot dots to figure out the logic

      if (plot_lines) {

         

         cnt++;

         

         string name;

         

         name = "firstbearcandle"+cnt;

      

         ObjectCreate(0,name,OBJ_ARROW,0,TimeCurrent(),firstbearcandle+2806);  

         ObjectSetInteger(0, name, OBJPROP_COLOR, clrCyan);  

         ObjectSetInteger(0,name,OBJPROP_ARROWCODE,115);      

         ObjectSetInteger(0,name,OBJPROP_WIDTH,1);  // dot size

         ObjectSetInteger(0,name,OBJPROP_SELECTABLE,true);

         ObjectSetInteger(0,name,OBJPROP_SELECTED,false); // set this TRUE to highlight these dots

         

      }

   }


   last_position = PositionsTotal();

   

   return;

}

 
dclark24:

...

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Attached is the code.  Lines 167... mention what it is I am trying to do.  Thanks for the help!
#property copyright "Copyright 2019"
#property link      ""

// libraries included for coding routines
#include <Trade\Trade.mqh> 
//#include <MovingAverages.mqh>
#include <initmql4.mqh>

input int contracts = 1; 
input bool close_on_reverse = false;

// for TP and SL step is 0.25

input double takeprofit = 6.0; 
input double stoploss = 3.0; 

//slopes for going short and long

input double shortslope = 1.5;
input double longslope = 1.5;

//bodywidths for long and short positions
 
 input double longbull = 2;
 input double longbear = 1.5;
 input double shortbull = 2;
 input double shortbear = 1.5;


input ENUM_MA_METHOD averageType = MODE_SMMA;
input ENUM_TIMEFRAMES Aggregation = PERIOD_M12;

input bool use_trailing = false;

input double sell_trailing_start = 6.0; 
input double sell_trailing_distance = 5.0; 
input double sell_trailing_step = 1.0; 

input double buy_trailing_start = 6.0; 
input double buy_trailing_distance = 5.0; 
input double buy_trailing_step = 1.0; 

input bool close_friday = true;
input int friday_hour = 20;
input int friday_minute = 55;

input bool plot_lines = true;

input bool alert_on_stoploss = true;
input bool stop_on_alert = true;
input bool open_long = true;
input bool close_long = true;
input bool close_profit_in_pips = true;
input bool close_loss_in_pips = true;

// unique ID of trade
int magic_number = 19042019;
// max acceptable slippage 
int slippage = 3;

datetime last_bar = 0;

//int firstbearcandle, firstbullcandle, bodybullcandle, bodybearcandle;

CTrade trade; 

double global_SL; // uses for "close_on_HL_and_stoploss" option

int cnt = 0; // counter for objects on chart

int last_position = 0;

//+------------------------------------------------------------------+
// Initialization of empty values on start
int OnInit() {
   last_bar = TimeCurrent();
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
}
   void work_new_bar(void)
  {
   static datetime t_bar=iTime(_Symbol,PERIOD_CURRENT,0);
   datetime _time=iTime(_Symbol,PERIOD_CURRENT,0);
   if(t_bar==_time)
      return;
   t_bar=_time;
/* work */
  }
//+------------------------------------------------------------------+
void OnTick() {

   if (use_trailing) {
      trailingStop();
   }

   if (close_friday) {
      closeFriday();
   }

   // if position closed
   if (last_position > PositionsTotal()) checkAlert();
   
   // check time of current bar to determine new bar comes to run logic
   datetime current_bar = iTime(Symbol(),Period(),0); 
   
   // if new bar comes
   if (current_bar > last_bar) {

      int firstbearcandle;
      int bodybearcandle;
      int firstbullcandle;
      int bodybullcandle;
      bool golong = false;
      bool goshort = false;
      bool closelong = false;
      double closingprice;
      double MA1;
      double MA2;
      double ADX1;
      double ADX2;
      int goodslope;
   
      if (iOpen(Symbol(),Period(),2) < iClose(Symbol(),Period(),2)){
         firstbearcandle=1;}
         else {
         firstbearcandle=0;}
      if (iOpen(Symbol(),Period(),2) - iClose(Symbol(),Period(),2) > longbear){
         bodybearcandle=1;}
         else {
         bodybearcandle=0; }
      if (iClose(Symbol(),Period(),1) < iOpen(Symbol(),Period(),1)){
         firstbullcandle=1;}
         else {
         firstbullcandle=0;}
      if (iClose(Symbol(),Period(),1) - iOpen(Symbol(),Period(),1) > longbull){
         bodybullcandle=1;}
         else {
         bodybullcandle=0;}
      
      MA1=iMA(Symbol(),0,7,1,averageType,PRICE_CLOSE);
      MA2=iMA(Symbol(),0,7,2,averageType,PRICE_CLOSE);
      ADX1=iADXWilder(Symbol(),0,7,1,averageType,PRICE_CLOSE);
      ADX2=iADXWilder(Symbol(),0,7,2,averageType,PRICE_CLOSE);
      
      if (MA1>MA2){
         goodslope=1;}
         else{
         goodslope=0;}
         
      //golong=true;   
         
      if (firstbearcandle == 1 && bodybearcandle == 1 && firstbullcandle == 1 && bodybullcandle ==1 && goodslope ==1) {
         golong = true;
         closelong = true;}
   
      closingprice=MathMax(iClose(Symbol(),Period(),0),iOpen(Symbol(),Period(),0)-stoploss);
      // check for opening
     
     if (golong == true) {
         Alert("golong");
         PlaySound("stops.wav");
         openBuy();
      }
 //openBuy will buy at open of next candle.  I want now to be able to close the position at the maximum of the following conditions...
 //1.) at close if candle is long or doji
 //2.) at close if close <=open-stoploss
 //3.) at open-stoploss if open-stoploss is < close.
  

      
      // plot dots to figure out the logic
      if (plot_lines) {
         
         cnt++;
         
         string name;
         
         name = "firstbearcandle"+cnt;
      
         ObjectCreate(0,name,OBJ_ARROW,0,TimeCurrent(),firstbearcandle+2806);  
         ObjectSetInteger(0, name, OBJPROP_COLOR, clrCyan);  
         ObjectSetInteger(0,name,OBJPROP_ARROWCODE,115);      
         ObjectSetInteger(0,name,OBJPROP_WIDTH,1);  // dot size
         ObjectSetInteger(0,name,OBJPROP_SELECTABLE,true);
         ObjectSetInteger(0,name,OBJPROP_SELECTED,false); // set this TRUE to highlight these dots
         
      }
   }

   last_position = PositionsTotal();
   
   return;
}
Reason: