Hi everyone
I used an online code genetor to generate some code with a long and short entry signal.
Lots, I have learnt that its very similar to C and since I can program in C# and VB.net it was a quick way to get a start, and there are many mods to the entry logic that I have made myself, I am just trying to get my head around how the orders are checked and managed, and was hoping someone would point me in the right direction for how its done. I am also learning that MT4 might be the wrong platform for auto trading 2 min charts. If that is the case then thanks to the online code generator I have only wasted a few hours.
Lots, I have learnt that its very similar to C and since I can program in C# and VB.net it was a quick way to get a start, and there are many mods to the entry logic that I have made myself, I am just trying to get my head around how the orders are checked and managed, and was hoping someone would point me in the right direction for how its done. I am also learning that MT4 might be the wrong platform for auto trading 2 min charts. If that is the case then thanks to the online code generator I have only wasted a few hours.
OK, to do what you describe in your first post you simply need to let the long and short be opened but only open them once you have first checked how many long and short trades your EA has already opened, if there is less than one long trade you can open the long, if there is less than one short trade you can open a short.
So use a loop to loop through the open orders, OrderSelect() them in the loop, check their OrderSymbol() to make sure it matches the symbol your EA is running on, check their OrderMagicNumber() if you are using one . . . count up the longs, count up the sells . . . now you can decided if you are placing new trades or not.
If you read this and understand it you will get some coding suggestions: Loops and Closing or Deleting Orders
You should also read this and check your trade function return values and report your errors: What are Function return values ? How do I use them ?
Can I open long, and then short if long is still open with EA?
I used an online code genetor to generate
- Hedging is not allowed in the US
- Bad, already explained
- The generator can only signal one chart so of course only one updates. Why would you want to duplicate all the code just to change direction?
Raptor Thanks.
WH
1. I dont live in the US.
2. Efficient or perhaps lazy, but not bad, the code works great and took 5 mins to create.
3. For my 5 min strategy I have two 5min charts open, one with a long EA and one with a short EA, each with its own magic number. Works fine. If I could get two 2min charts running at the same time I wouldnt need to do it in one EA.
Your EABuilder is making code
trying modifying every Point
doing it this way
will fail often on an account.....
For those that may be interesed in something similar here is the code that I added. Not as complicated as suggested above because I only trade 1 pair so no need to check symbols, and there will only be 1 open trade when the type test is done. Tested on a 1min chart, seems to work ok.
//if a trade is open determine its type if( TotalOrdersCount()<2 ) // no trades or one trade open int tradetype=3; //any number other than 1 or 2 to to declare it as something if ( TotalOrdersCount()>0 ) //then a trade is open so get its type tradetype=TradeTypeTest(); //entry conditions for long if(tradetype!=1//plus rest of conditions for long entry //entry conditions for short if(tradetype!=2//plus rest of conditions for short entry MagicNumber=MagicNumber*2 //to give it a different number for short position // need to allow for new magic number in stop loss control if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber ||OrderMagicNumber()==MagicNumber*2 //add or statement to deal with both mag nums //rest of stop loss control here //called to determine if open trade is long or short int TradeTypeTest() //1 =long, 2 = short { int tradetype=2; //set default to 2 for case where there is open short trade if(OrderTakeProfit()>OrderStopLoss()) //change to 1 as open trade is long tradetype=1; return (tradetype); }
For those that consider this thread to be a waste of their precious time, feel free to move on to the next post without replying.
For those that consider this thread to be a waste of their precious time, feel free to move on to the next post without replying.
Raptor Thanks.
WH
1. I dont live in the US.
2. Efficient or perhaps lazy, but not bad, the code works great and took 5 mins to create.
3. For my 5 min strategy I have two 5min charts open, one with a long EA and one with a short EA, each with its own magic number. Works fine. If I could get two 2min charts running at the same time I wouldnt need to do it in one EA.
In the US by law you are not allowed to hedge. For the rest of the world hedging is broker dependent. I am not in the US either, but my broker insists on using a "net" based trading system (going long, then short results in no position). It obviously makes them more money as you pay the spread twice, and they also do not need to cover your margin twice (even though your net margin is zero).
I am not going to go into the merits or demerits of hedging as there are some strong opinions for and against. My stance is that I am an adult and I should be able to loose (or gain) money as quickly as I like. And some strategies like grid systems cannot work without hedging (unless you have two accounts and alot of complication).
Look for a broker that uses a "position" based system.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone
I used an online code genetor to generate some code with a long and short entry signal. Here's the scenario, it triggers long and a trade is opened with stops and targets set. Then the market reverses, stop is not hit but a short is triggered. I would like it to open a short (keeping long open) with stops and target set and then just wait for the direction to confirm. I want no more than one long and one short at any time. I do not want multiple entries in the same direction.
I know I could just make an EA for short and another for long and run on 2 charts but when I create a 2min chart, only one of them updates, the other stays static, so I need one EA to handle both directions on the 2 min chart (I think).
Is there some way to modify this bit of the code to make this happen.
Thanks