Trying to change the Momemtum indicator into an EA - Unable to fix a basic bug of variable initialisation - page 2

 
matttt:

Hello,

Update :

> Have fixed the stop loss problem

> Added a maximum number of lots with

> Added some security on those points


Will post shortly the code with stop loss and maximum lots indeed.

But now I am trying to store the previous order trade with its type (buy or short), so that it only take position once after the signal, then stay out of market until the opposite signal for trade is formed.

I guess i need to store somewhere the data in a txt file and then call it as a condition to trade, but seems not so simple.

Would some work with the "history" file make it ? (gethistory?)



I'm glad you stuck at it... are you trying to stay out of the market whilst an existing trade is running (ie don't take more than 1 trade on the long signal) or don't enter another long if the last order to close was also long?

OdresTotal() for the former and OrdersHistoryTotal() for the later

 
Viffer:

I'm glad you stuck at it... are you trying to stay out of the market whilst an existing trade is running (ie don't take more than 1 trade on the long signal) or don't enter another long if the last order to close was also long?

OdresTotal() for the former and OrdersHistoryTotal() for the later

Hi Viffer and others,

Thanks for the new tip. I'll have a look asap. As you can see, I cannot search for the solutions so often and definitively not full time, but I am still trying to stick at it... :)


So as I have said, I have added the following "security-checks" :

--------------------------------------------------------------------------------------------------------------------------------


>>> To make sure the stoploss is fixed at a good level and to make sure we are not trading too many lots we need to add the following in the paragraph

// initial data checks

if(StopLoss>300)
{
Print("StopLoss > 300");
return(0); // check if stoploss is at the right level. need to be the same value than in the primary parameters
}
if(Lots>3)
{
Print("Too many lots");
return(0); // check if we are not trying to trade too many lots. value need to be set correctly with respect to this rule in the primary parameters at the top

}


--------------------------------------------------------------------------------------------------------------------------------


>>>> the max lots security also need to be added as a condition in the two paragraph

      // check for long position (BUY) possibility

and in

      // check for short position (SELL) possibility

just add as a last condition :

&& total<3


--------------------------------------------------------------------------------------------------------------------------------


>>> then, correct the following which was set incorrectly before in the two places where "ordersend" function is used :

IN    // check for long position (BUY) possibility

we had written that :

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,...............

we need to have that instead : ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,....

IN      // check for short position (SELL) possibility

we had written that :

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,

we need to have that instead : ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,


--------------------------------------------------------------------------------------------------------------------------------


>>> Once this is done, you can delete the following at the end of the code (not useful, so not needed)

if(StopLoss>0)
{
if(Bid-OrderOpenPrice()>Point*StopLoss)
{
if(OrderStopLoss()<Bid-Point*StopLoss)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*StopLoss, OrderTakeProfit(), 0,Green);
}
}
}

return(0);
}

--------------------------------------------------------------------------------------------------------------------------------

>>> Note that other indicators can be added in the same format than the Momemtum in this EA. Simply "copy paste" the procedures for the momemtum, i.e. use the iRSI, and do not forget to add a condition on the trades i.e && RSICurrent>RSIPrevious .

--------------------------------------------------------------------------------------------------------------------------------


Now I'll try to tackle the main problem of the moment, stay out of market when a trade is already running and not take two positions in the same direction in a row (wait for the opposite signal).

cheers,

 

Also forget to say that you can delete :

those lines :

   double SignalCurrent, SignalPrevious;

&

   SignalCurrent=iMomentum(NULL,0,14,PRICE_CLOSE,0);
   SignalPrevious=iMomentum(NULL,0,14,PRICE_CLOSE,1);

Does not help either, so no need to have them.

 

I think the way i set up manually the max number of lots to be traded at the same could also be done with the Ordertotal more easily, but I do not get the use of orderhistorytotal()

I just found this link which may be of some help to start conditionning the "buy then sell then buy then sell...." but looks quite complex to me for the moment : https://book.mql4.com/build/orders

Do not know if the link works, but type order accounting in the search bar and it is the 10th link returned