Supertrend on MTF
Forums › ProRealTime English forum › ProOrder support › Supertrend on MTF
- This topic has 19 replies, 2 voices, and was last updated 3 years ago by
Jodal.
-
-
05/27/2022 at 8:20 AM #193929
Hello everyone,
I would like to create an algo with the following functions (I tried to come up with something, see my code below).
Please note that I am a newbie in coding.BUY ORDER
ENTRY
2 conditions
>Price is above Supertrend (TF 30min)
>Previous candle closes above Supertrend (TF 1min)
EXIT
>Previous candle closes below Supertrend (TF 1min)SELL ORDER
ENTRY
2 conditions
>Price is below Supertrend (TF 30min)
>Previous candle closes below Supertrend (TF 1min)
EXIT
>Previous candle closes above Supertrend (TF 1min)OTHER: Stop the algo when earnings reach 100 points on the day.
There are 3 things I am struggling with
>Multi TimeFrame
> Use the indicator of the higher timeframe as a “constant”
> Code a daily max profitA couple questions
1. Multi TimeFrame
It seems my code below is not working, can someone please have a look? I can’t spot what is wrong2. Use the indicator of the higher timeframe as a “constant”
If we take a buy order for example, I would like to place a buy order only when the price crosses above the Supertrend M1 and is ALREADY above Supertrend M30.
I don’t want to enter long when the price crosses above the Supertrend M1 and then above the Supertrend M30
Same logic for a sell order but vice versa.
Is this the case here? If not, do you know how I could add this small difference?The Supertrend M30 should be a “constant” in the sense that the price is above or below the Supertrend M30 (except if price closes right on it)
So all buy orders will be placed when the price is above the Supertrend M30 and the candle closes > Supertrend M1
If the price goes back below the Supertrend M1 (still above the Supertrend M30), then we exit the trade. Later when it goes back above the Supertrend M1, we enter again to buy (if still above the Supertrend M30).My code below does not seem to work that way.
Is there a way to write “when price is above the supertrend” instead of “when price closes above the supertrend”?3. Code a daily max profit
What should I add for the maximum number of points per day (say 100)? (No more new trades if we have reached a gain of 100 points)Your help will be highly appreciated!!
Enjoy your day12345678910111213141516171819202122232425262728293031323334353637383940// Cumulating positions deactivatedDEFPARAM CumulateOrders = False// Higher TimeFrame to indicate if we enter long or short on the lower TimeFrameTimeframe (30 minutes, updateonclose)// Variablesindicator1 = SuperTrend[3,10]c1 = (close > indicator1)c2 = (close < indicator1)// Lower TimeFrame on which we enter long or shortTimeframe (default)// Variablesindicator2 = SuperTrend[3,10]c3 = (close > indicator2)c4 = (close < indicator2)// Conditions to enter longIF NOT LongOnMarket AND c1 AND c3 THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c4 THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c2 AND c4 THENSELL 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c3 THENBUY AT MARKETENDIF05/27/2022 at 9:34 AM #193936There you go:
1234567891011121314151617181920212223242526272829303132333435363738394041424344DEFPARAM CumulateOrders = False//Timeframe (30 minutes, updateonclose)indicator1 = SuperTrend[3,10]c1 = (close > indicator1)c2 = (close < indicator1)//Timeframe (1 minute,UpdateOnClose)indicator2 = SuperTrend[3,10]c3 = close CROSSES OVER indicator2c3a= c1 AND c3c4 = close CROSSES UNDER indicator2c4a= c2 AND c4//Timeframe (default)ONCETradeON = 1IF IntradayBarIndex = 0 THENTradeON = 1MyProfit = STRATEGYPROFITPips = 0ENDIF//Pips = Pips + ((STRATEGYPROFIT - STRATEGYPROFIT[1]) / PipValue)IF Pips >= 100 THENTradeON = 0ENDIF// Conditions to enter longIF NOT LongOnMarket AND c3a AND TradeON THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c4 THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c4a AND TradeON THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c3 THENEXITSHORT AT MARKETENDIF//graph Pips coloured(255,0,0,255)//graph Pips >= 100 coloured(0,0,255,255)I want to point out that:
– to enter a LONG trade you have to use BUY
– to exit a LONG trade you have to use SELL
– to enter a SHORT trade you have to use SELLSHORT
– to exit a SHORT trade you have to use EXITSHORT.2 users thanked author for this post.
05/27/2022 at 11:01 AM #19395006/01/2022 at 7:11 AM #194369Hi Roberto,
I would like your advice on the code below.
I used the same “principles” as you did above for the Supertrend indicator.
Now the logic is the same but with 2 changes:1. Indicators are EMAs
2. I want to add the latent gain/loss in the equation.(I don’t know if I had to create a new topic because it was related to your previous answer, so I put it here.)
Do you spot what is wrong in the code below?
It is not entering and exiting the way I want it to.Thanks
Enjoy your day123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051DEFPARAM CumulateOrders = False//// Here I want the code to be updated at every 1-minute bar, so I removed "updateonclose" in the higher timeframe, is that correct?Timeframe (30 minutes)indicator1 = ExponentialAverage[100](close)c1 = (close > indicator1)c2 = (close < indicator1)//Timeframe (1 minute,UpdateOnClose)indicator2 = ExponentialAverage[20](close)c3 = close CROSSES OVER indicator2c3a= c1 AND c3c4 = close CROSSES UNDER indicator2c4a= c2 AND c4// I added the latent gain component in "Myprofit" because I want the robot to stop at the close of the current 1-minute bar as soon as we reach 100 points in daily profit .// Formula floatingprofitfloatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsizeTimeframe (default)ONCETradeON = 1IF IntradayBarIndex = 0 THENTradeON = 1MyProfit = (STRATEGYPROFIT+floatingprofit)Pips = 0ENDIF// Here below I replaced STRETAGYPROFIT by MyProfit. I don't know if I should change STRATEGYPROFIT[1]?Pips = Pips + ((MyProfit - STRATEGYPROFIT[1]) / PipValue)IF Pips >= 100 THENTradeON = 0ENDIF// Conditions to enter longIF NOT LongOnMarket AND c3a AND TradeON THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c4a THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c4a AND TradeON THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c3a THENEXITSHORT AT MARKETENDIF06/01/2022 at 7:12 AM #194370I also tried with the code below but not working either.
Happy to read your take on that.
Thank you!
123456789101112131415161718192021222324252627282930313233343536373839404142434445DEFPARAM CumulateOrders = False//// Here I want the code to be updated at every 1-minute bar, so I removed "updateonclose" in the higher timeframe, is that correct?Timeframe (30 minutes)indicator1 = ExponentialAverage[100](close)//Timeframe (1 minute,UpdateOnClose)indicator2 = ExponentialAverage[20](close)c1 = (indicator1 > indicator2)c2 = (indicator1 < indicator2)// I added the latent gain component in "Myprofit" because I want the robot to stop at the close of the current 1-minute bar as soon as we reach 100 points in daily profit .// Formula floatingprofitfloatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsizeTimeframe (default)ONCETradeON = 1IF IntradayBarIndex = 0 THENTradeON = 1MyProfit = (STRATEGYPROFIT+floatingprofit)Pips = 0ENDIF// Here below I replaced STRETAGYPROFIT by MyProfit. I don't know if I should change STRATEGYPROFIT[1]?Pips = Pips + ((MyProfit - STRATEGYPROFIT[1]) / PipValue)IF Pips >= 100 THENTradeON = 0ENDIF// Conditions to enter longIF NOT LongOnMarket AND 1 AND TradeON THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c2 THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c2 AND TradeON THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c1 THENEXITSHORT AT MARKETENDIF06/02/2022 at 9:34 AM #194498Hi @<span class=”bbp-author-name”>robertogozzi</span>,
Do you have any idea about the code to incorporate the latent gain? (strategy profit + floating profit)
So I do not wait for the “exit condition” to exit the trade. It will go out automatically when I reach my daily goal at the end of the current 1-minute bar.
Please let me know if you need any additional information.
Thank you
Jo
06/04/2022 at 10:34 AM #194612There you go:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263DEFPARAM CumulateOrders = False//// Here I want the code to be updated at every 1-minute bar, so I removed "updateonclose" in the higher timeframe, is that correct?Timeframe (30 minute, updateonclose)indicator1 = ExponentialAverage[100](close)c1 = (close > indicator1)c2 = (close < indicator1)//Timeframe (1 minute,UpdateOnClose)ONCE MyGOAL = 100 //100 currency units (be them Euros, US dollars or alseIF OnMarket AND ((PositionPerf * PositionPrice * PipValue) >= MyGOAL) THENSELL AT MarketEXITSHORT AT MarketENDIFindicator2 = ExponentialAverage[20](close)c3 = close CROSSES OVER indicator2c3a= c1 AND c3c4 = close CROSSES UNDER indicator2c4a= c2 AND c4// I added the latent gain component in "Myprofit" because I want the robot to stop at the close of the current 1-minute bar as soon as we reach 100 points in daily profit .// Formula floatingprofitfloatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsizeTimeframe (default)//ONCETradeON = 1IF IntradayBarIndex = 0 THENTradeON = 1MyProfit = (STRATEGYPROFIT+floatingprofit)Pips = 0ENDIF// Here below I replaced STRETAGYPROFIT by MyProfit. I don't know if I should change STRATEGYPROFIT[1]?Pips = Pips + ((MyProfit - STRATEGYPROFIT[1]) / PipValue)IF Pips >= 100 THENTradeON = 0ENDIF// Conditions to enter longIF NOT LongOnMarket AND c3a AND TradeON THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c4a THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c4a AND TradeON THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c3a THENEXITSHORT AT MARKETENDIF////graphonprice indicator1 coloured(0,0,255,255)//graphonprice indicator2 coloured(0,255,0,255)//graph PositionPerf * PositionPrice * PipValue1 user thanked author for this post.
06/04/2022 at 6:40 PM #194643Hi Roberto,
Thank you for taking the time … again 🙂
I see 2 issues
- I have several green days with over 100 in profit (which I guess should not happen?)
- Some trades were executed for no reason
See attached screenshots
I find the profit strategy a bit confusing so I tried to simplify my system and changed a EMA 200 on a 10min timeframe into a EMA 2000 on a 1min (they are really similar).
So I think it will be less complicated to use my strategy in one timeframe (and easier to backtest / check as well).
I amended your code with the one below but I still have weird entry/exit points so I think something is wrong.
I feel we are almost there but there is something wrong with the profit strategy…
Can you spot anything wrong?
Enjoy your evening
Jo
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647DEFPARAM CumulateOrders = False//indicator1 = ExponentialAverage[20](close)indicator2 = ExponentialAverage[2000](close)c1 = (indicator1 > indicator2)c2 = (indicator1 < indicator2)ONCE MyGOAL = 100 //100 currency units (be them Euros, US dollars or alseIF OnMarket AND ((PositionPerf * PositionPrice * PipValue) >= MyGOAL) THENSELL AT MarketEXITSHORT AT MarketENDIF// Formula floatingprofitfloatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsizeTimeframe (default)//ONCETradeON = 1IF IntradayBarIndex = 0 THENTradeON = 1MyProfit = (STRATEGYPROFIT+floatingprofit)Pips = 0ENDIF// Here below I replaced STRETAGYPROFIT by MyProfit. I don't know if I should change STRATEGYPROFIT[1]?Pips = Pips + ((MyProfit - STRATEGYPROFIT[1]) / PipValue)IF Pips >= 100 THENTradeON = 0ENDIF// Conditions to enter longIF NOT LongOnMarket AND c1 AND TradeON THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c2 THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c2 AND TradeON THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c1 THENEXITSHORT AT MARKETENDIF06/05/2022 at 5:18 PM #194707Tell me:
- what TF and instrument you are using
- what are the dates and time a wrong trade was entered or exited
- what are the dates and times there was any issue with the profit.
1 user thanked author for this post.
06/05/2022 at 6:42 PM #194714Hi Roberto,
Thanks a lot for your reply.
Instrument is Crude Oil (CL)
Timeframe is 1 minuteBacktest was done on 200k units (22/11/21 – 03/06/22)
Regarding the green days > 100 in profit, please see attached screenshot of the detail report, a lot of bars are above the 100 mark.
For example: 23/11, 30/11, 01/03, 06/03, 09/03, 25/04Regarding the wrong entry/exit for a trade, here are a few:
02/06/22 Entry at 16:09 (See Graph 020622)
It happens way too late.
Exit is good (exit a the close of the candle once we reach 100).
However, it takes another trade a few minutes later.
Why?08/04/2022 at 20:40
11/04/2022 at 2:28 (See attached Graph 11042022)Also, many times it executed a trade at the very beginning of the day (without EMA crossing)
A few dates:
23/04/2022
13/04/2022
31/05/2022 (See Graph 310522),
02/06/22Or is it because the calculation of the indicator (EMA200 on 30minutes) is not well displayed on my chart?
Here is the code I added:TIMEFRAME(30 minutes)
ind=ExponentialAverage[100](close)
return ind
06/06/2022 at 10:35 AM #194756The 100-pip limit was applied only NOT to trade anymore for the day, but the current trade was not stopped, now it is.
There was also a glitch in the calculation of Pips (I also replaced the variable Pips with Money):12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455DEFPARAM CumulateOrders = False//indicator1 = ExponentialAverage[20](close)indicator2 = ExponentialAverage[2000](close)c1 = (indicator1 > indicator2)c2 = (indicator1 < indicator2)ONCE MyGOAL = 100 //100 currency units (be them Euros, US dollars or alseIF OnMarket AND ((PositionPerf * PositionPrice * PipValue) >= MyGOAL) THENSELL AT MarketEXITSHORT AT MarketENDIF// Formula floatingprofitfloatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsizeTimeframe (default)//ONCETradeON = 1IF IntradayBarIndex = 0 THENTradeON = 1MyProfit = STRATEGYPROFITMoney = 0ENDIF// Here below I replaced STRETAGYPROFIT by MyProfit. I don't know if I should change STRATEGYPROFIT[1]?Money = (StrategyProfit + floatingprofit) - MyProfitIF Money >= 100 THENTradeON = 0SELL AT MARKETEXITSHORT AT MARKETENDIF// Conditions to enter longIF NOT LongOnMarket AND c1 AND TradeON THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c2 THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c2 AND TradeON THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c1 THENEXITSHORT AT MARKETENDIF//graph floatingprofit//graph Money//graph Money >= 100//graph TradeON//graph c1//graph c21 user thanked author for this post.
06/06/2022 at 7:49 PM #194818Thank you Roberto for your quick reply and work, I appreciate it.
Here I am in 100TICKS, SMA38 and ZLEMA4000
The entries are perfect, really at the crossing!
However, I still have 2 issues
1. It will open a trade at the first candle of each new day (even if there is no crossing).
If the faster MA is above the slow one (no crossing) it will open a buy order, if the faster MA is below the slow one (no crossing), it will open a sell order.
How can we avoid that?2. Most of the days it will indeed stop at 100 but not all of them.
For example, on March 21, it opens at the crossing (great), closes 100 pts later (great) but also opens a new trade (no crossing), and then it does it again later
See attached graph 21032022See also on April 11, first it opens on the first candle of the day, then it places an order at the first crossing (to close the first buy position). The sell position is opened and then it closes it 100 pips late (which is good) but again opens a new trade again (no crossing, just opens a new one when closing the good one) It should have just stopped for the day.
This makes bigger earning days but also big losing days because it keeps trading while it should have stopped (because in profit earlier)
Do you see what’s wrong?
Almost there 🙂
Thank you!!!!!06/07/2022 at 9:12 AM #194824You are using 100 ticks, which are not allowed in autotrading.
Please post the complete code (or attach the ITF file).
1 user thanked author for this post.
06/07/2022 at 1:49 PM #194853Hi @robertogozzi,
Too bad for the 100 ticks.
I can backtest it but you mean it won’t work when I go “live autotrading”?Can I do autotrading on the TF 30 seconds or the minimum is 1 minute?
I tried to backtest on the 1min timeframe, I still have the same issues mentioned above (trade at day start + does not always stop once I reach 100 pips/currency)
Here is the code
Thank you Roberto
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455DEFPARAM CumulateOrders = False//indicator1 = Average[38](close)indicator2 = ZLEMA[4000](close)c1 = (indicator1 > indicator2)c2 = (indicator1 < indicator2)ONCE MyGOAL = 100 //100 currency units (be them Euros, US dollars or alseIF OnMarket AND ((PositionPerf * PositionPrice * PipValue) >= MyGOAL) THENSELL AT MarketEXITSHORT AT MarketENDIF// Formula floatingprofitfloatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsizeTimeframe (default)//ONCETradeON = 1IF IntradayBarIndex = 0 THENTradeON = 1MyProfit = STRATEGYPROFITMoney = 0ENDIF// Here below I replaced STRETAGYPROFIT by MyProfit. I don't know if I should change STRATEGYPROFIT[1]?Money = (StrategyProfit + floatingprofit) - MyProfitIF Money >= 100 THENTradeON = 0SELL AT MARKETEXITSHORT AT MARKETENDIF// Conditions to enter longIF NOT LongOnMarket AND c1 AND TradeON THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c2 THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c2 AND TradeON THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c1 THENEXITSHORT AT MARKETENDIF//graph floatingprofit//graph Money//graph Money >= 100//graph TradeON//graph c1//graph c206/07/2022 at 5:22 PM #194862You can trade from 1 second on… (non time-based charts can’t be usedf in autotrading).
I modified the code so that a CROSSOVER is required each new day.
The LIMIT is checked when the candle closes, so it’s quiter rare that it’s exactly 100, most often it will be greater. It depends on the last candle, the greater the spike, the greter the profit/loss.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364DEFPARAM CumulateOrders = False//ONCE CrossOver = 0IF IntraDayBarIndex = 0 THENCrossOver = 0ENDIFindicator1 = ExponentialAverage[20](close)indicator2 = ExponentialAverage[2000](close)IF (indicator1 CROSSES OVER indicator2) OR (indicator1 CROSSES UNDER indicator2) THENCrossOver = 1ENDIFc1 = (indicator1 > indicator2)c2 = (indicator1 < indicator2)ONCE MyGOAL = 100 //100 currency units (be them Euros, US dollars or alseIF OnMarket AND ((PositionPerf * PositionPrice * PipValue) >= MyGOAL) THENSELL AT MarketEXITSHORT AT MarketENDIF// Formula floatingprofitfloatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsizeTimeframe (default)//ONCETradeON = 1IF IntradayBarIndex = 0 THENTradeON = 1MyProfit = STRATEGYPROFITMoney = 0ENDIF// Here below I replaced STRETAGYPROFIT by MyProfit. I don't know if I should change STRATEGYPROFIT[1]?Money = (StrategyProfit + floatingprofit) - MyProfitIF Money >= 100 THENTradeON = 0SELL AT MARKETEXITSHORT AT MARKETENDIF// Conditions to enter longIF NOT LongOnMarket AND c1 AND TradeON AND CrossOver THENBUY 1 CONTRACT AT MARKETENDIF// Conditions to exit a buy positionIf LongOnMarket AND c2 THENSELL AT MARKETENDIF// Conditions to enter shortIF NOT ShortOnMarket AND c2 AND TradeON AND CrossOver THENSELLSHORT 1 CONTRACT AT MARKETENDIF// Conditions to exit a sell positionIF ShortOnMarket AND c1 THENEXITSHORT AT MARKETENDIF//graph floatingprofit//graph Money//graph (Money >= 100) * 50 coloured(255,0,0,255)//graph TradeON//graph c1//graph c2 -
AuthorPosts
Find exclusive trading pro-tools on