Donna Forex Forum

General => General Trading Discussion => Topic started by: Humble Trader's Fx on July 30, 2020, 12:15:37 PM

Post by: Humble Trader's Fx on July 30, 2020, 12:15:37 PM
Hello Members.

As per our new member's request, volunteering to help members do their own programming (coding) or analyzing the logic behind EA's, I have initiated a new thread, "Programming EA's".

Let's get started.

Global Moderator
Title: Array Element Moving
Post by: skinner36 on July 31, 2020, 01:20:49 AM

This helped me solve a problem that I had a while ago so I thought that I would share it here.

Sometimes there is a need to add a new element at the beginning of an array and move all the array values up by one element. Coding this the common way can require coding loops withing loops and because MQL is not the fastest language to execute then it can be expensive from the point of view of time required if the array is long.

The following example will simplify the coding for this problem and it is quite fast.

Firstly, define your array as you would normally.
Code: [Select]
int SomeArray[];
int Size = 100000000;

In the OnInit() function you might have something like the following code
Code: [Select]
ArrayResize(SomeArray, Size);    // Size the array to what we want
ArraySetAsSeries(SomeArray, true); // We turn the array around to match element zero with bar zero

Now a new bar comes in and you want to move everything up one element in the array to match the moved bars

The following four lines of code would be somewhere like the OnTick() function
Code: [Select]
// Set the array to read from left to right as a normal array does
ArraySetAsSeries(SomeArray, false);

 // Add a new empty element to the end of the array
ArrayResize(SomeArray, Size+1);

// Put the array back to how we use it so set the array to read from right to left similar to the bars on a chart
ArraySetAsSeries(SomeArray, true);

// Now the array has been turned around delete the last element   
ArrayResize(SomeArray, Size);

Done. Everything is moved up one element



Post by: diyforexskills on July 31, 2020, 09:54:58 AM

Every now and then I let something run and get into trouble - a DD. Before closing out at a largish lot I always like to try and recover. So here is my idea for that.

This version would be useful for when we have a largish number of small trades. Eg Say we have ten 0.01 lot trades that have gone rogue and are in net loss by $200. So we want to open a hedge of at least 0.1 lot and when the hedge goes into profit by set amount, we want it to close and also close at least one of the losing main trades. And then have a new hedge opened, but now only 0.09 lot since we have managed to close a 0.01 losing main trade.
Assuming we are successful, ie the trend continues, we may whittle down the losing main trades to say 5 and have a net loss left of only $50. In that case we sense a market reversal again, so we stop opening hedge trades and let the market hopefully rescue the remaining losing main trades, or just cut our losses and close them manually.

So the Inputs would be:

Open Hedge at Loss of x$
Hedge lot Multiplier (eg if set to 2 it would open a Hedge of twice the net loss size of losing main trades)
Hedge Close at Main Loss of y$ (so if loss has been reduced to an acceptable level, we want to close the Hedge since it would now start losing itself; market has flipped again)
Minimum Loss of Main Trades to Close
Profit Margin for Hedge
(In other words, if Min Loss of Main is set to $20 and Profit Margin for Hedge is set to $5, then when the Hedge reaches $25 in profit, it would start looking for a trade which is at least $20 in loss. When the Hedge has reached $35 in profit, it might see a Main trade at $30 in Loss, and so it would close the Hedge at $35 profit and close the Main trade at $30 in loss, for a Profit Margin of $5. This idea comes form the CloseMinusbyPlus EA on MarketPlace except that here we are dealing with only one Plus (winning) trade. Hence the "Plus" in HedgeBotPlus).

Hedge TP
Hedge SL
Hedge BE at Pips
Hedge BE Profit in Pips
(These are in case the Hedge is left dangling with nothing to do; so we want to close in profit if at all possible and not let it run away to its own DD).

Apply to All Magics true/false
Apply to These Magics (provide option for say 5 magic numbers for main trades, including 0 for manual trades)
Hedge Magic Number

I am thinking this EA would be currency specific but we may be running trades with different magics for that currency pair. So the HedgeBotPlus EA would need to be placed on a chart of the same currency pair as the Main trades.

What do you think?

A second version of this would be if we had a large lot size losing trade and we want to nibble away at it. In that case the Hedge would have to be able to partially close a fraction of a Main trade at a specified Hedge profit value. Eg if Min Loss is set to $50, it would look to see if it can close maybe 0.02 lot (representing $50) of 0.1 lot losing Main trade.

For your consideration.
Post by: skinner36 on July 31, 2020, 10:44:08 AM

I need to calculate the  frequency of a sine wave. I was thinking of using FFT but haven't figured out how to do it. Does anyone have any suggestions on how to do this?

Thanks in advance,

Post by: diyforexskills on August 01, 2020, 03:17:38 AM

I need to calculate the  frequency of a sine wave. I was thinking of using FFT but haven't figured out how to do it. Does anyone have any suggestions on how to do this?

Thanks in advance,


I would not have a clue but my DuckDuckGo search engine (the one that unlike Google does not track you), yields this result
Title: MT4 to MT5 conversions
Post by: skinner36 on August 03, 2020, 01:16:08 PM
I am making all of my future EAís, Indicators, scripts etc compile on MT4 and MT5 and I thought I would share what I am doing here as it progresses. The idea of doing this is nothing new but this is my way of tackling the problem.

Firstly, I am creating a class that maps MQL4 functions and constants to how the same information is returned from MT5. Notice I didnít say its MT5 equivalent. This is because a lot of MT4 functions donít have a direct MT5 equivalent.

Secondly I will write my programs with compiler specific sections where I need to differentiate between MT4 and MT5 commands by using the following sort of thing.

Code: [Select]
    #ifdef __MQL5__
    #ifdef __MQL4__

The MT5 code to return the close for a specific bar looks like this

Code: [Select]
//| Description : Get the closing price for a specific bar.                                                                 |                                                       
//| Emulates    : Close[]                                                                                                   |                                   
//| Parameters  : bar - The bar number for the required price                                                               |
//| Returns     : The closing price of the selected bar                                                                     |
double CMT4to5::Close_(int bar)
    double closePrice[];
    ArraySetAsSeries(closePrice, true);

    CopyClose(_Symbol, _Period, 0, bar+1, closePrice);

    return closePrice[bar];

All of my MT5 commands are the same as the MT4 command except that I put an underscore at the end. This is so that they line up correctly with the intellisence in the coding editor.