PTS Logo

ProRealCode programming examples and bug fixes for Pro Real Time

ProRealTime code that I was using before I stopped using them. Just sharing with the community to be helpful.

It can also be useful to understand pine script or EasyLanguage code to program in PRC language


The easiest  way to find what you are looking for is to click on edit--find and then type the word using your browser

ProRealCode is very similar to EasyLangauge code and some examples here can easily be converted to PRC


CountOfLongShares and CountOfShortShares

In the example below we can see the logic

If countoflongshares < 98 then

buy 2 contracts at market

endif

This is a very useful as once the position is 100 then it will not buy any more as 98 + 2 is 100

Also to stop a strategy trading more than once per signal we can use this code

If countoflongshares =0 then

buy 2 contracts at market

endif

Until those long shares are closed out the strategy will not make any further long trades

Graphonprice feature


This is a very useful item to add as your trading strategy can also act as an indicator without having to copy all the code and make an indicator

In a trading strategy you can display the variables you need to see. A common problem is that the changes do not show up when made.

Simple fix is to detach your strategy and re attach it to see the changes.

Sample code for showing how it works.

If you want to use the lowest low of 50 bars as your stop loss, you can plot it through the strategy thus

MyLow50 = Lowest[50](low)

Graphonprice  MyLow50 as " 50 bar low"

endif

The part between the " inverted  commas " can be any name you wish and it will be displaying the chart window

Graphonprice feature NOT WORKING .......SOLVED July 16th 2021

This was a very hard bug to clear but once you know how it is easy.

Here is what I did.

1. Save your set up and close down Prorealcode

2. Clear cache on your pc using CCleaner ( the section that removes junk from pc called "custom clean" as it is default settings.

3. Reboot pc

4. Open Prorealcode

5. On the open chart you may have a lot of arrows without the usual equity curve and positions indicator so click on one of the black arrows and delete.

6. If required also right click "delete all drawn item from chart"

7. Click close this window ( on the strategy box in the top left)

8. Re apply the strategy

Graphonprice will be displayed correctly.

Hard to know why this occurs in the first place however. Some of the above steps might not be needed but that was my fix.



Partial trade closing with a strategy

Problem

You cannot do partial trade closes with ProRealTime and IG for some reason they do not allow it. Perhaps as it helps traders make money?

This is true in January 2019 and hopefully they will change it- They never changed this so I dont use them anymore.

Everyone has a right to close part of a trade but not in IG-PRT it seems.

Solution

The current workaround for this is to rebuild your strategy in several parts.

For example if you have one exit signal  that takes profits at a given distance and another exit signal the follows a non defined trend following logic such as moving average cross over, then

you make two strategies and upload both of them.

The first will take profits at your target and the second will follow the trend change exit.

Curiously it is allowed to do multiple entries and thresholds can be set to limit the maximum number of contracts or shares traded. This can be done using the upload features ( at the point

of making your system live, there is a box at the bottom where you can type the value )

To be more precise you can use the other feature called "countoflongshares" and countofshortshares ( see above )

Also you can use this code to allow just one trade per instance

As it is set to FALSE it will take only the first entry. ( Set it to true if you want multiple entries )


DEFPARAM CumulateOrders = False // Cumulating positions deactivated


Round  feature

This returns 11 if you have 10.5 but the description says it returns the integer portion of a number, which is odd

There does not seem to be a function to change 10.5 into 10


Set stop ploss

If we type this code below we get the same stop distance for both as the code seems to accept this command once.

Likely the best way is to find one value for both long and short, or create two separe robots.

If  LongOnMarket then

Set stop ploss 30

endif

If  ShortOnMarket then

Set stop ploss 22

endif



 
StrategyProfit feature

You can use this as a safety feature to disconnect your strategy if it is losing more than the amount you specify.

EG

If strategyprofit < - 1000 then

quit

endif

The code above will not trade once the number is reached.

Alternatively you can use it to increase deal sizes thus

If strategyprofit < 500 then

Size = 200

endif

If strategyprofit > 500 then

Size = 210

endif

You would need to alter the entry signal to state thus

Buy size contracts at market

How to avoid a new trade if daily loss is greater than a specified amount using strategy profit feature

//Gets the strategy profit once a day at the end of the day ( time needs to be tailored for bar type , below gets the equity 10 mins before close)

If time = 235000 then

lastnight = strategyprofit

endif

// Ensures no trades are done if already down 1000 on this day

if strategyprofit +1000 > = lastnight and ( your conditions ) then

buy at market

endif



StrategyProfit feature other useful ideas

You can use this feature to compute trade size automatically and very precisely thus ensuring you don't need to keep switching it off to change the trade size as the account size rises or falls.

If strategyprofit > 3000 then

size = round((strategyprofit+1000) / 500)

endif



Set Stop Ploss feature

This will cause a delay of  5 minutes before it gets applied ( if using a 5 minute chart, or a day if using daily charts )  if it placed below to code that enters a trade, this problem is resolved

by placing it near the top of the page before the entry code is written.

Some stop ordered get adjusted or cancelled on Saturday on Sunday, to avoid this happening some code can be used with

Currentdayofweek feature

If currentdayofweek > 0 and currentdayofweek <6 then ( my code... and at end of code add ) endif

Preloadbars feature

You can run into problems if you do not load enough data to read your indicators, solved by adding this code to the automated strategy

As the error message "The historical data is insufficient to calculate at least one indicator"  will get sent to the brokers and you may not get it unless you phone up to ask.

EG

DEFPARAM Preloadbars = 10000

( I have read that 10,000 bars is the maximum you can load )

BarIndex feature

You can run into problems with counters if you do not add this bar index feature first. As the error message "The historical data is insufficient to calculate at least one indicator"

EG  Correct below

If barindex > 65 and not longonmarket and not shortonmarket then

counter = counter[1]+1

endif

Incorrect ( will cause strategy to be turned off as soon as next bar closes )

If  not longonmarket and not shortonmarket then

counter = counter[1]+1

endif

If strategyprofit < - 1000 then

quit

endif

EG

If strategyprofit < - 1000 then

quit

endif

Drawdown quit off code sample ( if you want the strategy to quit if drawdown is > 20% )

You can create a variable highest strategy profit ( HSP ) to monitor the draw down.

// First assign a value to HSP

HSP=100

If strategyprofit > hsp then

hsp = strategyprofit

endif

// Holds its high value

If hsp < hsp[1] then

hsp = hsp[1]

endif

// quits at 20% drawdown

if strategyprofit > 1000 and  (hsp-strategyprofit ) > (hsp *0.20) then
quit

endif
// the part that reads strategyprofit > 1000 is needed or it will not run and quits instantly



Troubleshooting infinte loop errors

An infinite loop or a loop with too many iterations was detected

Please modify the corresponding loop code

You can run into problems with loops when using the "for" code

EG below gives the infinite loop error

For N = 0 to N+1 do
N= ++N

next

We can  use a different variable name for the 2nd occurance of N

Thus below fixes the error

number=3
For N = 0 to number+1 do
N= ++N

next

Then the infinite loop error goes away as it stops at 4 ( 3 + 1 ).

Dealing with a nulliparous strategy that runs in back test but not in actual

You can can sometimes get a strategy that refuses to give birth to a trade when you activate it.  Sometimes this can be a simple reason and other times the cause is rather abstruse.

Common causes can be not enough data being loaded which can be corrected by using the preload bars feature shown above. The trailing percentage stop is another anomoly that works fine

in back testing but is not allowed by the PRT-IG set up.


Set Stop Ptrailing feature

The syntax is as follows

set stop ptrailing 100

This will work fine until a surprise fast move comes along.......

Today March 3rd 2020 at 3pm UK time the fed did a rate cut, about 30 seconds after my strategy bought the Nasdaq futures.

Whilst I was thankful of a super fast move up, the trailing stops refused to move up and on calling IG they explained that they disable trailing stops when the market goes crazy, and in

some cases cease online trading altogether.

The last time this happened was when I was shorting the pound on the night of Brexit and even when calling up on the phone I was told the price is wrong and not allowed to deal.

In the end I had to complain before I got paid.

The other issue with this feature that I have noticed is that you cannot control it via some extra code, say if you wanted 100 trailing for long and 94 trailing for short then it will ignore any

code that you add and just take the first value for both.

Volume indicators do not function very well when connected to ProRealTime and IG index as they just have tick volume.
 
Trading can be greatly enhanced by using true market volume with volume based indicators such as: Demand Index is highly suitable for this task



 



I had wanted to code up Pi-Osc into ProRealCode but they did not have any method of protecting or licensing it.



It is available for Five other platforms - Please see the Product Guide here


Pi-Osc is the lower plot shown in the TradingView platform - read how it works here



 Precision Index Oscillator on Ripple XRP
 

The elapsure of decay can be utilized in different ways in trading, spending time on testing this was done extensively with the above Precision Index Oscillator indicator (Pi-Osc)

 
View List of other free and paid products for Tradestation

View List of other free and paid products for MultiCharts

View List of other free and paid products for NinjaTrader

View List of other free and paid products for TradingView





IF YOU HAVE ARRIVED AT THIS PAGE AS A BEGINNER THEN HERE ARE SIX TUTORIAL PAGES TO GET YOUR KNOWLEDGE UP TO A BETTER LEVEL

How to trade part 1

How to trade part 2

How to trade part 3

How to trade part 4

How to trade part 5

How to trade part 6

Qstick   

Educational videos

1929 crash

Trading IQ Game tutorial

PLAY FOR FREE  Trading IQ Game  AND WIN PRODUCTS

  




 
 

The help and advice of customers is valuable to me. Have a suggestion? Send it in!

The contact page here has my email address and you can search the site

 

Return to the top of page (In a safe and orderly manner)

 

 If you like what you see, feel free to SIGN UP  to be notified of new products - articles - less than 10 emails a year and zero spam

 

About

Precision Trading Systems was founded in 2006 providing high quality indicators and trading systems for a wide range of markets and levels of experience.

Supporting NinjaTrader, Tradestation and MultiCharts with Trading View coming along and MetaTrader.

 

Bio

Social tags

 

Admin notes

Page updated August 20th 2023 to replace old page from 2006  - New responsive page GA4 added canonical this. 5/5 html baloon  cookie notice added  lower social tags added August 23rd 2023