I want to open and close a trade on each candle.
With “IF on market Then sell at market” it only trades every second candle.
Is there another way? Tried MTF but it would only trade longs or shorts! Not both.
Any help gratefully appreciated.
There you go:
defparam cumulateorders = false
sell at market
buy at market
Basically Roberto’s code will open a trade on the first candle and then after that every BUY order will be cancelled by a SELL order so you will just stay on the market forever!
Do you mean that you want a position to be opened at start of a candle if conditions are met and then closed mid candle or at the close of the candle if not closed mid candle?
You mention long and short. It is not possible to be both long and short at the same time in an auto strategy.
I still cannot get this to work. Started with a new code. See attachment “MySystem19”. This traded every candle.
Then I put in “my conditions” and copied and pasted the closing arguments from MySystem19 (see attachment “MySystems21″)
My conditions is 200ma <> previous candle.
When I ran it, it does not trade the next candle even if the conditions are met. (see attachment” 877 screen copy”)
It should trade every candle!
Can you please help me work out what I am doing wrong?
Thanks in advance.
If the code is not massive then you are better to use the ‘Insert PRT Code’ button and post the strategy code directly in your post. Just attaching an ITF file means that someone who wants to help you has to download a file, open their platform, import a file, guess which instrument and time frame you are applying it to and then finally look at the code to see where the error is. This is even less likely to happen if the markets are closed and no one has their platform open! 🙂
Inserting the code in your post will get far more eyes on it far sooner.
OT LongOnMarket THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
My apologies Vonasi. I had been corrected by the moderator to attach the code as it was more readable.
But I am aware of the different level, and I can understand where you are coming from.
I will add the codes below
Here is the first code that trades every candle-
// Conditions to enter short positions
IF NOT ShortOnMarket THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit short positions
IF ShortOnMarket THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
Here is the second code, with conditions, that only trades every other candle – see screenshot
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = Average[200](close)
indicator2 = Average[200](close)
c1 = (indicator1 > indicator2[1])
IF c1 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator3 = Average[200](close)
indicator4 = Average[200](close)
c2 = (indicator3 < indicator4[1])
IF c2 THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
IF ShortOnMarket THEN
EXITSHORT AT MARKET
ENDIF
This will enter every other candle:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// exit any open position
SELL AT MARKET
EXITSHORT AT MARKET
// Conditions to enter positions
indicator1 = Average[200](close)
indicator2 = Average[200](close)
c1 = (indicator1 > indicator2[1])
IF c1 THEN
BUY 1 CONTRACT AT MARKET
else
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
This will enter each candle:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// exit any open position
SELL AT MARKET
EXITSHORT AT MARKET
// Conditions to enter positions
//indicator1 = Average[200](close)
//indicator2 = Average[200](close)
//c1 = (indicator1 > indicator2[1])
//IF c1 THEN
BUY 1 CONTRACT AT MARKET
//else
SELLSHORT 1 CONTRACT AT MARKET
//ENDIF
Code in text format.
MySystem19. This trades every candle
// Conditions to enter long positions
IF NOT LongOnMarket THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit short positions
IF ShortOnMarket THEN
EXITSHORT AT MARKET
ENDIF
MySystem21. This one only trades every candle.
How do I make this trade every candle?
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = Average[200](close)
indicator2 = Average[200](close)
c1 = (indicator1 > indicator2[1])
IF c1 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator3 = Average[200](close)
indicator4 = Average[200](close)
c2 = (indicator3 < indicator4[1])
IF c2 THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
IF ShortOnMarket THEN
EXITSHORT AT MARKET
ENDIF
This code will open/close each candle, provided conditions are different each candle:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// exit any open position
SELL AT MARKET
EXITSHORT AT MARKET
// Conditions to enter positions
indicator1 = Average[200](close)
indicator2 = Average[200](close)
c1 = (indicator1 > indicator2[1])
IF c1 THEN
BUY 1 CONTRACT AT MARKET
else
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
because when conditions are different it always enters doing Stop & Reverse, while if conditions aree the sane as the previous candle:
- if CumulateOrders = FALSE then it can’t open another trade in the same direction, because it needs one candle before it knows the trade had been closed a few lines above
- if CumulateOrders = TRUE then it will accumulate positions in the same direction.
To recap, you can open/close a trade every candle only by doing Stop & Reverse, that is when conditions are different from the previous candle.
Still not working. There are different conditions to Buy & Sell – 200ma < or > previous candle.
1.Have set cumulating positions to True.
2. Closed any open positions.
The code is still only trading every other candle even when the conditions are true.
See attachment.
Latest code:
// Cumulating positions allowed
DEFPARAM CumulateOrders = True
// Exit any open positions
SELL AT MARKET
EXITSHORT AT MARKET
// Conditions to enter long positions
//c1 = (close > open)
// MAs > previous candle
indicator1 = Average[200](close)
indicator2 = indicator1
c2 = (indicator1 > indicator2[1])
indicator3 = Average[100](close)
indicator4 = indicator3
c3 = (indicator3 > indicator4[1])
indicator5 = Average[50](close)
indicator6 = indicator5
c4 = (indicator5 > indicator6[1])
indicator7 = Average[25](close)
indicator8 = indicator7
c5 = (indicator7 > indicator8[1])
indicator9 = Average[12](close)
indicator10 = indicator9
c6 = (indicator9 > indicator10[1])
indicator11 = Average[6](close)
indicator12 = indicator11
c7 = (indicator11 > indicator12[1])
indicator13 = Average[3](close)
indicator14 = indicator13
c8 = (indicator13 > indicator14[1])
//indicator15 = Average[1](close)
//indicator16 = indicator15
//c9 = (indicator15 > indicator16[1])
IF NOT LongOnMarket AND c2 AND c3 AND c4 AND c5 AND c6 AND c7 AND c8 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit positions
// closes end of candle
IF LongOnMarket THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
// MAs < previous candle
//c10 = (close < open)
indicator17 = Average[200](close)
indicator18 = indicator17
c11 = (indicator17 < indicator18[1])
indicator19 = Average[100](close)
indicator20 = indicator19
c12 = (indicator19 < indicator20[1])
indicator21 = Average[50](close)
indicator22 = indicator21
c13 = (indicator21 < indicator22[1])
indicator23 = Average[25](close)
indicator24 = indicator23
c14 = (indicator23 < indicator24[1])
indicator25 = Average[12](close)
indicator26 = indicator25
c15 = (indicator25 < indicator26[1])
indicator27 = Average[6](close)
indicator28 = indicator27
c16 = (indicator27 < indicator28[1])
indicator29 = Average[3](close)
indicator30 = indicator29
c17 = (indicator29 < indicator30[1])
//indicator31 = Average[1](close)
//indicator32 = indicator31
//c18 = (indicator31 < indicator32[1])
IF NOT ShortOnMarket AND c11 AND c12 AND c13 AND c14 AND c15 AND c16 AND c17 THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// Conditions to exit positions
// closes at end of candle
IF ShortOnMarket then
EXITSHORT AT MARKET
ENDIF
You’ve been posting several code snippets without using the ‘Insert PRT Code’ button, please respect the rules as highlighted below in yellow.
As I explained, if conditions are the same direction as the previous candle a trade cannot be opened the following candle.
There’s no workaround. You either change your conditions so that they always imply doing a Stop & Reverse each candle or change your strategy.
I tidied it all up!
trevor – see the attached image for the location of the ‘Insert PRT Code’ button as you seem to be having a lot of difficulty finding it! 🙂
You’ve been posting several code snippets without using the ‘Insert PRT Code’ button, please respect the rules as highlighted below in yellow.
Wow. Did it maybe occur to you that you refer to a button which you know but others may not ?
Wow. Did it maybe occur to you that you refer to a button which you know but others may not ?
We assume that everyone is aware that the tools available for editing your posts can be found in the tool box attached to the box where you are typing. Perhaps we make too much of an assumption regarding everyone’s abilities.
The button in your image is not the correct one. That is for posting to the library. See the image in my previous post for the correct button. 🙂