Smash head on desk frustrated 24 variations down, no solution to programs behavior!!

 

Can someone please take a look at my program and tell me what I am doing wrong? (It's probably a lot!) I am very lost, my head really hurts, I have been working on this for two weeks straight! Help please!!

I have two problems I have repeatedly failed to solve:

+ When the trade signal is present, fire one test trade. If that trade is profitable, fire the real trade. Stop trading until the next signal.

+ When either the test or real trade is less then the trade open, close the trade. Stop trading until the next signal.

That's all I want this EA to do, and I can't seem to get it right!! I have learned a TON about programming in the last two weeks, but I simply lack the technique to solve this problem it feels like. Thank you for your time!

//+------------------------------------------------------------------+
//|                                               PfX-mk3-mql4.mq4   |
//|                    Pipfx,         based off of MA from MQL Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA  20050611

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot;
  
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*0.30/1000.0,1);
   return(lot);
  }

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   double ma2;
   int    res;
   int      i;
   int      x;
//---- this switches from bar trading to time trading, not sure which one I want to use.
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,0);
  ma2=iMA(NULL,0,120,0,MODE_SMA,PRICE_CLOSE,0);

   for(i=0;ma - ma2 < 0;)
   {
//---- sell conditions
     if(ma - ma2 <= 0 && i == 0)  
      {
       res=OrderSend(Symbol(),OP_SELL,0.01,Ask,3,0,0,"",MAGICMA,0,Red);
       i++;
      }
     if(OrderProfit() > 0 && i == 1)
      {
       res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Purple);
       break;
      }
   }

      for(x=0;ma - ma2 > 0;)
   {
//---- buy conditions
     if(ma - ma2 >= 0 && x == 0)  
      {
       res=OrderSend(Symbol(),OP_BUY,0.01,Bid,3,0,0,"",MAGICMA,0,Red);
       x++;
      }
       if(OrderProfit() > 0 && x == 1)
      {
       res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Orange);
       break;
      }
   }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
   double ma2;

//---- sometimes time, sometimes bar, not sure what to use
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,0);
   ma2=iMA(NULL,0,120,0,MODE_SMA,PRICE_CLOSE,0);

//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(ma - ma2 < 0) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(ma - ma2 > 0) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
//----
  }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+------------------------------------------------------------------+
 
if(Volume[0]>1) return;
is not reliable use Time or iTime() (the same by close)

for(i=0;ma - ma2 < 0;)
cycle operator for as to be 3 expression (the same by buy)
if(OrderProfit() > 0 && i == 1)

to use OrderProfit() The order must be previously selected by the OrderSelect() function. (the same by buy)

ma - ma2

use NormalizeDouble() for that

 
qjol:

cycle operator for as to be 3 expression (the same by buy)

An empty expression is valid in a for loop. You can even do it like this:

for(;;) { }


PipfX, you should loop down when closing orders and I suggest you ask specific questions about specific problems.

 

Thank you both for the insight!


@Mr. Gordon - I was concerned about lacking the ability to even phrase the question, the issue was baffling me. I need to get my signals to turn on and off trading before I do anything it seems like.

 
PipfX:

@Mr. Gordon - I was concerned about lacking the ability to even phrase the question, the issue was baffling me. I need to get my signals to turn on and off trading before I do anything it seems like.

The problem is that your question is too broad. Generally, to solve a problem you divide into many small subproblems. For example, your loop to close all orders under specific criteria is a subproblem in your design. It is not done properly, but of course you can't see that because you are concentrating on your overall design which is too broad... You should focus on each and every subproblem in your design, isolate it and verify that it works properly. Only after you are sure all the different sections work properly, focus on your overall design.

That's what I mean about asking specific questions about specific problems...

 

I understand. Here is is a specific question then. A portion of my trading programs system relies on closing a test trade if it is losing, it's just a bait trade, but using SL isn't very elegant, it's wasted money. I want to close the trade when the value is lower then the opening price. Here is my code so far, and it is not working:

//Can you see anything I am doing wrong with this loop? -- I realized that's not a very specific question.

How can I effectively use OrderProfit() to close a trade?

EDIT: Fixed this part. Thanks for your time :)

   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY && ma3 < ma4)
        {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL && ma3 > ma4)
        {
         OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
      if(OrderType()==OP_BUY && OrderProfit() < 0) //Was using 0 to test, doesn't work either.
        {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL && OrderProfit() < 0) //Was using 0 to test, doesn't work either.
        {
         OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
//----if a trade is losing, close it.        
     }
 

I hope you realize that when u 'break' you exit the loop so at most you will only be able to close one order in this loop. If this isn't what u are trying yo achieve, then pay close attention to what I mentioned before - you should loop down. See here for explanation -> https://www.mql5.com/en/forum/120128. You should also use an if-else structure, both to speed things up and to avoid a bug where multiple criteria are valid for the same order (maybe not possible in this case, but it's still a better design).

 

Thanks for the help Mr. Gordon, I had forseen the issue of how I would close multiple orders, but wasn't sure how to tackle the problem. I read the other post, and understand the premise for decrementing it. How would I make that loop decrementing? Yes it's that bad. Thanks for your patience <3.

This code fails, if I make the loop incrementing it works.


for(int i=1;i>OrdersTotal();i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY && ma3 < ma4 || OrderProfit() < OrderOpenPrice() - 10000)
        {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
        }
      if(OrderType()==OP_SELL && ma3 > ma4 || OrderProfit() < OrderOpenPrice() - 10000)
        {
         OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
        }
      if(OrdersTotal()==0)break;
      
//----if a trade is losing, close it.        
     }
 

See example here -> https://www.mql5.com/en/forum/123968.

Note that you are comparing profit with price... Obviously that won't work.

 

PipFx

Why not 'simply' track price movement since the time of the initial signal...?

If the movement confirms, you can put the real order on...?

Or is that too simple?

-BB-

Reason: