[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 282

 
Can you tell me which one? I only have two. MACD and Moving Averages.
 

Please help a beginner. How to write this code section:

Check market orders and if there are no market orders for our financial instrument, we buy it?

And we also need a code line that rearranges the Trailing Stop 500 pips away from the real price.

Help!!!

And what is the distance (from where and to where exactly)?

//+------------------------------------------------------------------+
//|                                                  MACD Sample.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double MacdCurrent, MacdPrevious, SignalCurrent;
   double SignalPrevious, MaCurrent, MaPrevious;
   int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
// to simplify the coding and speed up access
// data are put into internal variables
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);

   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      // check for long position (BUY) possibility
      if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
         MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && 
         MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
               MacdCurrent>(MACDCloseLevel*Point))
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            // should it be closed?
            if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
               MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point))
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
// the end.

Well, this is just nonsense! If there is any order: either pending or market, and at that for any financial instrument, no order will open in this code:

   total=OrdersTotal();
   if(total<1)

We can see that if there is less than one order, it will be executed. Or am I wrong?

 

From the moving average, take CalculateCurrentOrders(Symbol()), it returns values = number of open orders, filtered by symbol and magic, with sign = direction of transaction: for example -1 means 1 order in sale. If needed, use MathAbs() to ignore the sign.

From the above code, take the trailing stop:

// check for trailing stop - here, there are even comments.

if(TrailingStop>0) { ... }, it is in 2 places - for buying and selling. Everything is simple. Or, you can use ready-made functions of Igor Kim's. There is a special colour here. They are more universal and correct for real trading, but also a little more difficult to understand for a beginner. If you have a good faith in the new version, it will help you find the right one.

p.s. For debugging, use Print(), it helps a lot.

 
rusa: I would like to ask you to insert this function into Expert Advisor yourself. On the image, you can see that 3 orders have been opened on one bar. I do not know how to order my forex robots and I do not want to rewrite them.

Started to understand your code. I'm surprised. What you have attached cannot work. I can see that it is assembled from the pieces, but at least brackets should be correctly placed, variables should be defined. And the signal - it is unclear, what means, for example, "rsi>30" - indicator RSI value, or something else? The Trade_BUY() function is attached inside the function start... etc.

Either give me complete code which worked (opened orders) or fill out TOR for signals, trailing, etc. It will be easier for me to write from scratch.

 
nuan:

Do you have detailed instructions on how to download quotes for MICEX for 2 years, for example for Lukoil.

how to further convert them correctly and open them in mt4, if there are links give links how to do this.

P.S. Thanks in advance.


Here's a mamba for a few years
http://zalil.ru/31909547
 
kolyango:

Please help a beginner. How to write the following code section:

Check market orders and if there are no market orders for our financial instrument, we buy it?

And we also need a code line that rearranges the Trailing Stop 500 pips away from the real price.

Help!!!

And what is the distance (from where and to where exactly)?

Well, this is just nonsense! If there is any order: either pending or market, and at that for any financial instrument, no order will open in this code:

We can see that if there is less than one order, it will be executed. Or am I wrong?


Or maybe you are wrong?

for (int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i,SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == . pair)
{
if(OrderType() == OP_BUY || OrderType() == OP_SELL)
{
OrderSend(Symbol(),OP_BUY, .......

}
}
}

 

I need some help:

a regular Moving Average indicator when the line breaks down the Buy(up) or Sell(down) signal appears,

Does anyone have a finished code of the graphical object, can you post it!

Thank you.

 
these are in the database
 
ZZZEROXXX:
These are in the database.
A link would be appreciated.
 

Hello! Can you tell me how to "loop" the up/down bar check? That is, how can I describe this condition "shorter"?

Close[0]>Open[0]&& Close[1]>Open[1]&& Close[2]>Open[2]&& Close[3]>Open[3]&&& Close[4]>Open[4]&& Close[5]>Open[5]&& Close[6]>Open[6]&& Close[7]>Open[7]

Thanks in advance.

Reason: