NewOrder after last lose

 

Good Evening

i'm not able to do: Make a new order after the last lost, the new order must be sended when the price crosses the last order close price.

the code:


//+------------------------------------------------------------------+

//| OnTick function                                                  |

//+------------------------------------------------------------------+

     

      void OnTick()

        {

      //--- calculate open orders by current symbol

         if(Lastloseposition(Symbol())<0)        NewOrderOpen ();

      //---

        }

//+------------------------------------------------------------------+

//| Check for last order Lose Money and Buy Again                    |

//+------------------------------------------------------------------+



int Lastloseposition(string symbol)

  {

   int win=1,lose=0;

//---

   for(int i=OrdersHistoryTotal()-1;i>=0;i--)

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;

      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

        {

         if(OrderProfit()>0)  win++;

         if(OrderProfit()<0)  lose++;

        }

     }

//--- return orders volume

   if(win>0) return(win);

   else       return(-lose);

  }

  

//+------------------------------------------------------------------+

//| NewOrderOpen                                                       |

//+------------------------------------------------------------------+



void NewOrderOpen ()

  {



  int    Ordine;

//--- go trading only for first tiks of new bar

   if(Volume[0]>1) return;

//--- finding last order and join again with the lvl of last order lost   

      for(int i=OrdersHistoryTotal()-1;i>=0;i--)

        {

            if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;

            {

                if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

                { 

                   // se si va ad eseguire un ordine buy - if is a buy order

                   

                   bool crossoverSL=(iClose (NULL,PeriodTF,1) < OrderStopLoss() && iClose (NULL,PeriodTF,0) > OrderStopLoss());

                   if (crossoverSL)

                        { 

                         Ordine=OrderSend(Symbol(),OP_BUY,Positionsize,Ask,3,Ask-SL,Ask+OrderOpenPrice(),"",MagicNumber,0,Blue);

                         if(!Ordine)

                         Print("Error in NewOrderOpen",GetLastError());

                         else

                         Print("NewOrderOpen successfully.");;

                        }

                }

            }

         }

  }

Hope Someone may help me,


Thank you 

Regards

Dario

 

Greetings Falko,

Sorry I didn't give your code much reading, but judging from what you mentioned above, you just need to see if your order's stop loss ( you can save the order's ticket to reference it later) is hit, then just initiate an OrderSend() on condition if your ask/bid ( depends buy/sell) is higher/lower than last order's close price. ( You can grab the close price from the array Close[1]).


Best Regards,

Lord Odin

 
f4lk0:

Good Evening

i'm not able to do: Make a new order after the last lost, the new order must be sended when the price crosses the last order close price.

the code:


Hope Someone may help me,


Thank you 

Regards

Dario

Dear Odin

thank your for show me the Close[]  variables. 

btw  i have to resolve  this one, was usefull read in the log if  something goes wronge.


Regards

MQL4 Reference / Predefined Variables / Close
 
f4lk0:

Good Evening

i'm not able to do: Make a new order after the last lost, the new order must be sended when the price crosses the last order close price.

the code:


Hope Someone may help me,


Thank you 

Regards

Dario


This function is not correct and I don't think you need it:

Lastloseposition(string symbol)

It will always return wins, because you have:

int win=1,lose=0;

at the top of function, so it will never be equal to 0 then the function will always return the win. 

  if(win>0) return(win);

   else       return(-lose);


I wrote this for you, and I think you know how to use it, But I didn't check it, so I hope I didn't miss any thing.

double last_Lose_Close_Price()
  {
   double cl_Price=0;
   for(int i=OrdersHistoryTotal();i>=0;i--)
     {
      if(OrderSelect(NULL,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
           {
            if(OrderProfit()<0)
              {
               cl_Price=OrderClosePrice();
               break;
              }
           }
        }
     }
   return(cl_Price);
  }

I hope it helps.

 
  1. if(Volume[0]>1) return;
    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. for(int i=OrdersHistoryTotal()-1;i>=0;i--) if(
       OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
    && OrderMagicNumber()==MagicNumber
    && OrderSymbol()==Symbol() ){
    
    Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
              Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. bool crossoverSL=(iClose (NULL,PeriodTF,1) < OrderStopLoss() && iClose (NULL,PeriodTF,0) > OrderStopLoss());
    if (crossoverSL){ 
      Ordine=OrderSend(Symbol(),OP_BUY,
    There can be multiple, temporary cross overs on the forming candle. Do you really want to open when there is no permanent cross?

  4. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing prices.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

    On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum

 
Reza nasimi:


This function is not correct and I don't think you need it:

It will always return wins, because you have:

at the top of function, so it will never be equal to 0 then the function will always return the win. 


I wrote this for you, and I think you know how to use it, But I didn't check it, so I hope I didn't miss any thing.

I hope it helps.

Thank you so much, i much appreciate ur help

Regards

Dario

 
William Roeder:
  1. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
              Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. There can be multiple, temporary cross overs on the forming candle. Do you really want to open when there is no permanent cross?

  4. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing prices.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

    On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum

Thank you so much for the time u spent to improve my know-how.
 
William Roeder:
  1. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
              Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. There can be multiple, temporary cross overs on the forming candle. Do you really want to open when there is no permanent cross?

  4. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing prices.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum

    On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum


After studying your analyzes, I simply arrived at this script, but it seems not to take the losses even if it seems to be written correctly.

The First order its ok, but the second one dont start as the third.

Some ideas?

Obviously i've inserted in the folder Strings MQL4 OrderPool.mqh file



#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <Strings\MQL4OrderPool.mqh>
#include      <Strings\MQL4OrderPool.mqh>
extern int    MagicNumber=    1;
extern int     PeriodTF=       5;
extern int     SL =            100;
extern int     TP =            240;
extern int     SLnew =         50;
extern int     TPnew =         50;

extern int    MagicNumber=    1;

//-- Identifing last losing position

   MQL4OrderPool pool;
   pool.Init(  Symbol(),
               MagicNumber,             // magic
               MODE_HISTORY,  // mode
               OP_BUY, // filter by order type filled = OP_BUY+OP_SELL
               SORT_CLOSE_TIME_DESCENDING);// default sort mode
   
   int losing_orders = 0;
   double close_Price=0;
   double open_price=0;
   double order_profit=TP;
   
   for(int i=0;i<pool.Total();i++)
   {
      if(pool[i].OrderProfit() < 0 )
      {
         losing_orders++; 
         close_Price=OrderClosePrice(); 
         open_price=OrderOpenPrice();
         order_profit=OrderProfit();
         
           //-- Second kind of order
            if( (losing_orders>=1 && losing_orders<=3 ) /*&& (iOpen(NULL,PeriodTF,1) < close_Price && iClose(NULL,PeriodTF,1) >=close_Price )*/ ) 
            {
            bool Neworder=OrderSend(Symbol(),OP_BUY,Positionsize,Ask,3,Ask-SLnew,Ask+order_profit,"",MagicNumber,0,Blue);
            if (!Neworder) Print ("Error, Send new order",GetLastError());
            }
            
         //-- third kind of order
           // if( (losing_orders>=4 )&&(CrossOverLower) ) 
            //{
           // bool Ordine=OrderSend(Symbol(),OP_BUY,Positionsize,Ask,3,Ask-SL,Ask+TP,"",MagicNumber,0,Blue);
           // if (!Ordine) Print ("Error,Send Order", GetLastError());
           // }

      }
      else 
         break;
   }
 //-- First kind of order
      if( (losing_orders<=0 )&&(Condition) ) 
      {
      bool Ordine=OrderSend(Symbol(),OP_BUY,Positionsize,Ask,3,Ask-SL,Ask+order_profit,"",MagicNumber,0,Blue);
      if (!Ordine) Print ("Error,Send Order", GetLastError());
      }
     // End

}


Regards

Dario

 
This capacity isn't right and I don't think you need it:

It will dependably return wins, since you have:

at the highest point of capacity, so it will never be equivalent to 0 then the capacity will dependably restore the success.

I composed this for you, and I think you realize how to utilize it, But I didn't check it, so I trust I didn't miss anything.

I trust it makes a difference.
 
rohim98:
This capacity isn't right and I don't think you need it:

It will dependably return wins, since you have:

at the highest point of capacity, so it will never be equivalent to 0 then the capacity will dependably restore the success.

I composed this for you, and I think you realize how to utilize it, But I didn't check it, so I trust I didn't miss anything.

I trust it makes a difference.


I'm curious to read your post, I guess you forgot to paste  the code 

Regards

Dario

 
f4lk0:


I'm curious to read your post, I guess you forgot to paste  the code 

Regards

Dario

no one may help me?  im not able to  resolve this issue :-(