Multiple trades when running code each tick

 

Hello! I'm having struggles with my EA. 

Right now, it runs the code each tick, not every bar. With doing so, I get ALOT of orders in the same place, effectivly draining my account balance in the backtesting.

Here is a picture for demonstration.

 

 As you can see in the picture above, it opens up multiple sell and buy positions, where i really just want one.

 The problem was solved previously by just running the code each bar, but in doing so, I got in the positions one bar late and out of the positions one bar late.

Which is a big problem when I use the daily graphs, which my program is made for.

 

How would you solve this matter?

Sorry if this post was badly made, it's my first one.

Ask me for more info if needed!

 

Thank you for reading! 

 

Make sure that you use an individual magic number for the EA orders.

Loop through the open orders and check if there is already a trade open with the magic number.

Only open a new trade if one does not exist already. 

 
Jalex: How would you solve this matter?
  1. Once per bar
  2. Test if an order is already open
  3. Test last open datetime
 

I don't know if I am missunderstanding what you two are saying, but I am already looking to see if an order is open. 
But the problem is that it opens an order, then closes it and then opens another one. 

I'm guessing it's because the signal crosses the macd line multiple times in the same bar sometimes.

Do you think I might be right in this? And is it a feesable solution to just put the program on hold for one bar after each order? 

 
Jalex:

I don't know if I am missunderstanding what you two are saying, but I am already looking to see if an order is open. 
But the problem is that it opens an order, then closes it and then opens another one. 

I'm guessing it's because the signal crosses the macd line multiple times in the same bar sometimes.

Do you think I might be right in this? And is it a feesable solution to just put the program on hold for one bar after each order? 


If your close condition is working per-tick, it can be. One thing you can do is change your close order condition to work per bar as well.
 
amir_avatar:

If your close condition is working per-tick, it can be. One thing you can do is change your close order condition to work per bar as well.
But then I am back to exiting my positions up to one bar too late. Which plays a huge role when using the daily period graphs. Am I even right in this? Thank you for all the replies by the way.
 

it depends on your signals. 

If your strategy demands closing as soon as the signal comes even without bar close, then the midway is to open deal only on the bar after the closed deal.
This demands that you check the closing time.
Right now you have open and close many times inside a bar, so obviously something with the strategy is not working right.
You should revise if and when you can count on the signal, obviously if you get many opposite signals within a short time, then something is wrong, even if you filter them at the end with new bar. 

 

The code concerning the signals is right, I'm sure. And I also do realize why they give me mixed signals, it should. 

The question, though, is how to deal with it? 

Is there some way to set a time limit the program must hold the order for? For example, the order must be held atleast one bar? 

 

I believe your question has already been answered by me and others. You can set the close condition on the next bar only and not on the bar where the deal was opened.

I also suggested that but you said:
"But then I am back to exiting my positions up to one bar too late. Which plays a huge role when using the daily period graphs. Am I even right in this? Thank you for all the replies by the way." 

So, you need to set your mind as to what you want, you cant hold the rope on both sides. 

 
amir_avatar:

I believe your question has already been answered by me and others. You can set the close condition on the next bar only and not on the bar where the deal was opened.

I also suggested that but you said:
"But then I am back to exiting my positions up to one bar too late. Which plays a huge role when using the daily period graphs. Am I even right in this? Thank you for all the replies by the way." 

So, you need to set your mind as to what you want, you cant hold the rope on both sides. 

OK, I nos understand what you're saying. Thank you.