Hi,
I’m new to programming and have problems with the following idea (and I hope that I’m at the right place here):
Start: Buying one share on breakout above the highest of the preceding n bars.
Selling 2 shares on breakout below the lowest of the preceding n bars- then again buying two shares above the breakout and so on.
This means to be invested with one share all of the time.
Code:
DEFPARAM CUMULATEORDERS = FALSE
IF PRICE CROSSES BELOW LOWEST[n](low) THEN
IF LONGONMARKET THEN
SELL 500 cash AT MARKET
ENDIF
IF NOT LONGONMARKET THEN
SELL 250 cash AT MARKET
ENDIF
ENDIF
IF PRICE CROSSES ABOVE HIGHEST[n](high) THEN
IF SHORTONMARKET THEN
BUY 500 cash AT MARKET
ENDIF
IF NOT SHORTONMARKET THEN
BUY 250 cash AT MARKET
ENDIF
ENDIF
Problems: 1. This gives no results for different n on backtesting
2. What is the correct term for Bitcoin/USD (cash, share, unit…)?
3. On IG, the spread for BTCUSD is 50 USD presently. Does this mean I have to type in 50 for the spread?
Thanks a lot if someone could help, it’s driving me crazy.
ligand
thanks, I got an answer via prorealtime
DEFPARAM CUMULATEORDERS = FALSE
IF PRICE CROSSES BELOW LOWEST[n](low) THEN
IF LONGONMARKET THEN
SELL 500 cash AT MARKET
ENDIF
IF NOT LONGONMARKET THEN
SELL 250 cash AT MARKET
ENDIF
ENDIF
IF PRICE CROSSES ABOVE HIGHEST[n](high) THEN
IF SHORTONMARKET THEN
BUY 500 cash AT MARKET
ENDIF
IF NOT SHORTONMARKET THEN
BUY 250 cash AT MARKET
ENDIF
ENDIF
Hello, the [Insert PRT Code] is your friend 🙂
Hello, the [Insert PRT Code] is your friend
Thanks swapping – I have tidied up the post to make it easier for others to read 🙂 ligand – please use the ‘Insert PRT Code’ button in your future posts. 🙂
Sorry, thanks. I hope it’s alright now.
Here is the improved code from prorealcode. Backtesting shows this code to perform trades only in one direction.
Therefore it doesn’t work in the way I hoped it would. I’d like to be always in the market: buy, when the price breaks above the highest of the last n bars and sell when it breaks below the lowest of the last n bars. Therefore the first buy (or sell) is with 1 share or contract, and every following trade with 2 shares or contracts and always in the opposite direction.
If someone could help, it would be very nice.
DEFPARAM CUMULATEORDERS = FALSE
n=5
IF low < LOWEST[n](low[1]) THEN
IF LONGONMARKET THEN
Sell 2 contracts AT MARKET
ENDIF
IF NOT LONGONMARKET THEN
Sell 1 contracts AT MARKET
ENDIF
ENDIF
IF high > HIGHEST[n](high[1]) THEN
IF SHORTONMARKET THEN
BUY 2 Contracts AT MARKET
ENDIF
IF NOT SHORTONMARKET THEN
BUY 1 Contracts AT MARKET
ENDIF
ENDIF
DEFPARAM CUMULATEORDERS = FALSE
n=5
IF low < LOWEST[n](low[1]) THEN
SellShort 1 contracts AT MARKET
ENDIF
IF high > HIGHEST[n](high[1]) THEN
BUY 1 Contracts AT MARKET
ENDIF
BUY = enter long. Closes any short trades.
SELL = exit long trade.
SELLSHORT = enter short. Closes any long trades.
EXITSHORT = exit any short trades.
Thanks Vonasi, that’s very helpful… I’ll check it out over the weekend.
Hi, one more question regarding realtime data please, since the helpful code above refers to data from the last days:
What is the right coding word for “the present price”? For example:
If xxxx crosses over highest[1] then
buy 1 contract
The phrase “price” for xxxx is not accepted. Tanks!
Use ‘close’ instead of ‘price’.
‘close’ is the price at the close of any candle which is when all strategy decisions are made.
Thank you very much for the quick response!
So if I’d like to code a system which trades on a hourly basis using a signal on a daily basis – for example buy if the price crosses over the last day’s close – it would be best to code using two timeframes?
MTF would work or you could just use DClose(1)
DClose
Thanks a lot. In one of the advanced videos it is shown how to define a line through 2 defined points (e.g. local lows). If I want to code a system to buy if the close crosses a line like that, how can I describe this line in code, please?
Hi Vonasi,
I’m just a physician but if I calculate right and if I know two points defining a line (x1/y1) and (x2/y2) this line is
y=mx+b
with
m=(y2-y1)/(x2-x1) and
b=y1-((y2-y1)x1/(x2-x1))
and the line is therefore: y=((y2-y1)/(x2-x1))x + y1-((y2-y1)x1/(x2-x1))
Given my little knowledge of coding I couldn’t find anything in your link (except of the basal equation) which helped me to force the above equation in code. Could you please show me how this definition of a line is done, if possible?
I’m afraid that you might be asking the wrong person! The last time I played with trigonometry calculations was when I was at school – and I ruled out using any form of trend line in my trading a long time ago so I have never coded anything myself with any form of trend line calculations.
Maybe Nicolas or juanj will join this topic and help you out with your request.