premarket open price breakout strategy idea
Forums › ProRealTime English forum › ProOrder support › premarket open price breakout strategy idea
- This topic has 37 replies, 6 voices, and was last updated 7 years ago by
aland.
-
-
04/01/2018 at 1:14 AM #66884
hi there
I am a total newbie here (but an expert programmer)
I would like to test a simple program before i go deeper into the code
the program would:
- take a bar 2 hour prior to the US market open (for example for IBM share)
- get the high of that bar at 7:30 US time
- buy once the US market opens at 9:30 if the price is equal or higher then that high bar price
- stop after the initial buy
any suggestions would be greatly appreciated
Here is my code:
1234567891011IF currentTime = 093000 THEN //not sure if current time is my time or market timeMyBuyprice = high[40]//on 3 min bars it would be 40 bars ago to get to 7:30 AM USIF NOT LongOnMarket AND ?X?>=MyBuyprice THEN//?X? not sure how to get the current price right now - as i don't want the high,low,close,open but the current priceBUY 1 CONTRACTS AT MARKETENDIFENDIFModerator’s edit: code above reformated on your behalf, when adding code to a message please use the “insert PRT code” button located in the message editor at the end of the first line of tools icons, thanks
04/03/2018 at 7:37 AM #6698204/03/2018 at 8:15 AM #66990Hello and welcomenot sure how to get the current price right now – as i don’t want the high,low,close,open but the current price
Close, same as Close[0] is the price at any instant, i.e. the current price.
When a new candle opens then the Close of the previous candle becomes Close[1]
Hope that makes sense, if not just say
Cheers
GraHal1 user thanked author for this post.
04/03/2018 at 11:48 AM #67020Great Thanks GraHal,
I have a few more questions and would very much appreciate your help:
- I understand there is no option to “debug” or trace a program while it is running, is there any “printout” option to make sure the code is doing what I want it to do?
- It seems the code I wrote above continues working endlessly (keeps on buying shares starting at 9:30). how do I stop it after a single buy or single sell (or maybe after 1 buy and 1 sell)
- How can I know if I have a profit at run time (for example I would like to write: BUY 1 CONTRACTS AT MARKET and if you made a profit then stop else buy another contract when the condition is met again)
Thanks again for all your help guys
Cheers,
Alan
04/03/2018 at 1:05 PM #67026BTW for itme #2 above I used
1IF currentTime => 093000 THENto get it to try again after 9:30 but I do want to limit the amount of time it sells/buys
to be more exact, how do I get MyBuyPrice in the example below
1MyBuyprice = high[40]to occur only once a day at the open of the market and then be triggered only a set X amount of times during that day
04/03/2018 at 2:48 PM #67035you dont need to reinvent the wheel
https://www.prorealcode.com/topic/ts-break-prima-ora/#post-56666
1 user thanked author for this post.
04/03/2018 at 9:42 PM #67069Thanks, that was very informative
I always prefer not to invent the wheel if possible
based on the code you sent me I run a quick test and it seems it still doesn’t fully do what I expect it to do.
I wrote the simplest possible program to just get the last high from the bar before now and buy when the market is above that highest buy.
I run it for IBM 15 min bars on March 28 at 14:30
the highest of that 14:15 bar was 15274.5
but the buy order was triggered at 14:30 at 15265.5
it seems like it is ignoring the high[1] and just buying the open of the current bar
what am i missing here?
1234567891011121314DEFPARAM CumulateOrders = falseDEFPARAM FLATBefore = 090000DEFPARAM FLATAfter = 230000DEFPARAM PreLoadBars = 10000ONCE FirstHour = 143000IF time = FirstHour THENMyBuyprice = high[1]END IFIF NOT ONMARKET AND close>MyBuyprice THENBUY 1 CONTRACTS AT MARKETMyBuyprice=999999ENDIF04/03/2018 at 9:47 PM #6707104/03/2018 at 10:49 PM #67077Thanks, Can you elaborate?
I can’t see how my condition could have been met if close>MyBuyprice was not reached yet at market open.
I reread the documentation about limit and tried changing the code to “BUY 1 CONTRACTS AT MyBuyprice limit” but that didn’t work (tried “close-10 limit” and other options too)
So I guess I don”t know how to use limit in this sense here.
my understanding of limit is the amount or points I want to profit where as stoploss are the amount/points I am willing to lose
also, from reading the docs I see that the LIMIT order should be below the close price which is not what I need
It seems no matter what I do the MyBuyprice is set to a number that is different then the high[1]
04/04/2018 at 12:34 AM #67083Line 9 sets MyBuyPrice equal to the high price of the 141500 bar, so that price will be your trigger, no matter if it is higher or lower than the one of the previous or following bar!
To place a pending order you can replace lines 11-14 with
1234IF NOT ONMARKET THENBUY 1 CONTRACTS AT MyPrice STOPMyBuyprice=999999ENDIFbut pending oders only last one bar, so I suggest to remove line 13 to prevent it from being placed again correctly next bar. You should set it when the strategy IS ON MARKET to prevent further trading, at the same price, once the trade is closed.
Effectively line 13 is of not much use since there is never a test against it, nor it has ever been initialized!
1 user thanked author for this post.
04/04/2018 at 7:50 PM #67163First off I’d like to thank all of you for taking the time to reply to my questions. I really appreciate it!
Thanks roberto, I tried the code you suggested as follows
1234567891011121314DEFPARAM CumulateOrders = falseDEFPARAM FLATBefore = 090000DEFPARAM FLATAfter = 230000DEFPARAM PreLoadBars = 10000MyBuyprice=999999ONCE FirstHour = 143000IF time = FirstHour THENMyBuyprice = high[1]ENDIFIF NOT ONMARKET THENBUY 1 CONTRACTS AT MyBuyprice STOPMyBuyprice=999999ENDIFbut it just gets into the market at 14:30 with the open of that bar (not the amount in MyBuyPrice/high[1]) no matter what I do
here is what I need for this simple example to work:
- get the high of the previous bar when the market opens
- get into the market with a buy order if the market reached that high
Thanks again
Alan
04/05/2018 at 12:35 AM #67187STOP pending orders are used to BUY at a less convevient price than the current one (you want to BUY at a higher price), while LIMIT pending orders are used to BUY at a more convenient price (you want to BUY at a lower price). So you’ll have to use STOP or LIMIT according to such cases.
Using
123GRAPH MyBuyPriceGRAPH timeGRAPH OnMarketembedded in your code, you can debug your strategy and might be able to spot any bug/issue you may have encountered.
Anyway, you know that the very first bar of each day is when INTRADAYBARINDEX = 0, so you just need to chek the previous HIGH to know it’s the one before trading started
123IF IntraDayBarIndex = 0 then //if it's a new trading day, then ...MyBuyprice = high[1] //... the previous bar is yesterday's lastENDIFI hope this helps further.
1 user thanked author for this post.
04/05/2018 at 6:52 PM #67342Yes “GRAPH” is exactly the debugging tool I was looking for. Thanks!
Now I see the problem (at 14:30 high[1] is actually giving me the value from 14:00 not 14:15 – not sure why…)
So now to finish my example I have another question:
How can I tell if a position made profit or loss. I want the code to do the following:
WHEN out of the market/position
IF CONDITION <“made profit today”> THEN quit for the day
ELSE attempt another trade
any thoughts?
Thanks
Alan
04/05/2018 at 7:53 PM #67346POSITIONPERF (https://www.prorealcode.com/documentation/positionperf/) will do the trick together with a variable to Enable/Disable trading
1234567891011121314151617ONCE TradingON = 1 //trading enabled by defaultONCE FirstTrade = 0 //tell us when a trade has already finished todayIF IntraDayBarIndex = 0 THEN //reset variable to default values each dayTradingON = 1FirsrTrade = 0ENDIFIF Not OnMarket THEN //when Not OnMarket and...IF FirstTrade THEN //... at least one trade already occurred...IF PositionPerf(1) > 0 THEN //... stop trading if it was successfulTradingON = 0ENDIFENDIFENDIFIF Your_Conditions AND Not OnMarket AND TradingON THENBUY/SELLSHORT ...FirstTrade = 1 //mark any trade openedENDIFThis should do, but it will stop trading as soon as the previous trade of the day was profitable, despite there may have been losing trades before so that your STRATEGYPROFIT (https://www.prorealcode.com/documentation/strategyprofit/) may still be negative for the day.
You’ll have to improve the code with the use of STRATEGYPROFIT to make your strategy more afficient and tailored to your needs.
If you save the current STRATEGYPROFIT at the very beginning of each day, later that day you may tell (by comparing the current and old values) if, despite the POSITIONPERF of any trade, you are still in gain or not.
1 user thanked author for this post.
04/10/2018 at 12:18 AM #67781Thanks again Roberto and all
For the last few days I wrote the basic test code but it is still not finished.
- I am still not sure how to cancel all positions and orders – is there a command (like QUIT but only for a single day) or do I need to manually SELL AT MARKET even when I didn’t buy yet
- I am also not sure if all the code is run for each bar again and again (I am guessing the answer is yes)
- if the bar is a 3 minute bar does it wait till close to run the code is it not in “real time” tick for tick
- if I want to look back and backtest the data from the last 12 month can I do it on a 3 min bar? it seems I have very limited access to the data on a 3 min bar
Any help would be greatly appreciated
Thanks
Alan
-
AuthorPosts
Find exclusive trading pro-tools on