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:
IF currentTime = 093000 THEN //not sure if current time is my time or market time
MyBuyprice = high[40]//on 3 min bars it would be 40 bars ago to get to 7:30 AM US
IF 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 price
BUY 1 CONTRACTS AT MARKET
ENDIF
ENDIF
Moderator’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
- For a new topic, your title should be explicit enough to prompt the community to read your discussion.
“simple code” means nothing to me … I changed it.
Hello and welcome
not 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
GraHal
Great 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
BTW for itme #2 above I used
IF currentTime => 093000 THEN
to 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
MyBuyprice = 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
EricParticipant
Master
Thanks, 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?
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 090000
DEFPARAM FLATAfter = 230000
DEFPARAM PreLoadBars = 10000
ONCE FirstHour = 143000
IF time = FirstHour THEN
MyBuyprice = high[1]
END IF
IF NOT ONMARKET AND close>MyBuyprice THEN
BUY 1 CONTRACTS AT MARKET
MyBuyprice=999999
ENDIF
You place a market order when your condition comes true. This order is executed at the open of the next bar. Try replacing your market order with a limit order.
Thanks, 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]
Line 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
IF NOT ONMARKET THEN
BUY 1 CONTRACTS AT MyPrice STOP
MyBuyprice=999999
ENDIF
but 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!
First 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
DEFPARAM CumulateOrders = false
DEFPARAM FLATBefore = 090000
DEFPARAM FLATAfter = 230000
DEFPARAM PreLoadBars = 10000
MyBuyprice=999999
ONCE FirstHour = 143000
IF time = FirstHour THEN
MyBuyprice = high[1]
ENDIF
IF NOT ONMARKET THEN
BUY 1 CONTRACTS AT MyBuyprice STOP
MyBuyprice=999999
ENDIF
but 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
STOP 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
GRAPH MyBuyPrice
GRAPH time
GRAPH OnMarket
embedded 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
IF IntraDayBarIndex = 0 then //if it's a new trading day, then ...
MyBuyprice = high[1] //... the previous bar is yesterday's last
ENDIF
I hope this helps further.
Yes “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
POSITIONPERF (https://www.prorealcode.com/documentation/positionperf/) will do the trick together with a variable to Enable/Disable trading
ONCE TradingON = 1 //trading enabled by default
ONCE FirstTrade = 0 //tell us when a trade has already finished today
IF IntraDayBarIndex = 0 THEN //reset variable to default values each day
TradingON = 1
FirsrTrade = 0
ENDIF
IF 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 successful
TradingON = 0
ENDIF
ENDIF
ENDIF
IF Your_Conditions AND Not OnMarket AND TradingON THEN
BUY/SELLSHORT ...
FirstTrade = 1 //mark any trade opened
ENDIF
This 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.
Thanks 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