How to stop trading after profit made?

 

I am trying to get an EA to stop placing buy or sell orders for 1 month if it has made 1 profitable trade for that month. I.E. it is the start of month 1, EA makes a buy trade, which lost, then makes another trade this one made profit. So I would then like to to stop trading for that entire month untill the next month arrives, then start trading again.

I always seem to have problems when using datetime functions it is very frustrating.

This is what I have tried to use:

for(int i=1; i<=OrdersTotal(); i++)         
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) 
        {                                       
         if (OrderSymbol()!= Symb) continue;    
         int Tip=OrderType(); 
         if (Tip == OP_BUY)
         {
            if(TimeMonth(TimeCurrent()) == TimeMonth(OrderCloseTime()))
            {
              bool OrderBuyProfit = true
            }
         }
       }

if (OrderBuyProfit == false && OrdersTotal() < 1)
{
// Then here would be the trade decision .....
}

dd The Problem I am getting is that when the order makes a profit it still continues to make trades throughout the month.

Also i wud like it to perhaps stop trading if it makes so many loss trades, but the main thing is that it stops trading when profit is made.

Any help always appreciated, thanks.

 
fryfly:

I am trying to get an EA to stop placing buy or sell orders for 1 month if it has made 1 profitable trade for that month. I.E. it is the start of month 1, EA makes a buy trade, which lost, then makes another trade this one made profit. So I would then like to to stop trading for that entire month untill the next month arrives, then start trading again.

I always seem to have problems when using datetime functions it is very frustrating.

This is what I have tried to use:

dd The Problem I am getting is that when the order makes a profit it still continues to make trades throughout the month.

Also i wud like it to perhaps stop trading if it makes so many loss trades, but the main thing is that it stops trading when profit is made.

Any help always appreciated, thanks.

The Other problem I am now facing too, is that I have made it so the EA will not carry a trade over to the next new month. i.e. If a trade has been opened near end of a month, as soon as the next month arrives the ea will close the order.

It is kind of working, but with every new order at the start of a month it closes that order immediatly afterwards.



datetime OOT = OrderOpenTime();

for (i = 0;i<OrdersTotal();i++)
{
  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
  if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
  {
    if(OrderType()==OP_BUY)   // long position is opened
    {
      if (TimeMonth(TimeCurrent()) != TimeMonth(OOT))
      {
        OrderClose(OrderTicket(),OrderLots(),Bid,4,Violet); // close position
         // exit
      }


As you can see in the code I have placed :

if (TimeMonth(TimeCurrent()) != TimeMonth(OOT)).

So if the current month is not the same month as the order open month it will close the order. So why would it close an order that has been opened in the same month? It alludes me how code like this should work yet it doesnt.....
 

Ok so I have kind of made a mistake in my code. I have used bool, instead of using a static bool. And because I was seeking the answer of the calculation outside of the function it didnt keep the value of true. Basically the value wasnt passed to the function call.

Ok so I changed the bool to static bool now.

Now the problem is that I cant get it to change back to false . So it suddenly stops making trades all together once it makes the first profitable trade. The problem is :

OrderBuyProfit = true;

Once this is triggered it will not change back to false. I have tried to use an if statement for the opposite for when it is false, but still doesnt work. I do have this at the top of my code also :

static bool OrderBuyProfit = false;

So I would have thought that once init start has finished, and the programme returns to the start again it should read this code at the top and change the static bool back to false untill the true statement becomes valid again, but it does not?

I hope I am typing clearly I find this all quite hard to put down in words, i try my best to make it easy to understand what I mean.

Reason: