I am no way near a professional programmer or a financial engineer, but I have a dream of becoming a Quant. I am still learning JForex having started doing so this year and wish to share bits of reusable code that make using JForex and VJForex easier for beginners. As I stated, this is going to be moderately basic stuff suited for beginners so “quants” can skip this.

All functions discussed, are available to download here:

OnCandle as Your Start Point

Most strategies in the contest use the onCandle method to start their strategies, but not all utilize the following two fail-safes.
  1. Matching candle instrument to default instrument
  2. Comparing default period to candle period

Checking if the candle instrument is the same as the current default instrument is necessary for situations where a strategy loops through currency pairs looking for qualifying signals or to create a trading currency basket.

Example of Different OnCandle Period and Instruments

The example below checks first for the 20-day Average True Range (ATR) values of 4 pairs before assigning the instrument to trade. Note: It uses the daily candle while a different part of the strategy uses 1-minute candle.

The above two checks act as filters and prevents unnecessary processor cycles making execution faster.

Getting the Spread in Pips

I often need to check the current spread before placing a market order. This is a must-have for those interested in scalping. While Dukascopy does an exceptional job tightening spreads, a spread greater than 2 pips and slippage in scalping might not cut it.

To prevent opening orders when spreads are above spread threshold, here is the calculation. Get the difference between tick ask price and bid price then divide the result with pipSize. Next, use an “If” block to prevent order execution if the spread is larger than threshold.

Risk and Limit Trade Amounts Using Percentages

Since a trader will not monitor strategies 24/7, it’s good to increase traded amounts as equity grows and vice-versa. It is also sound money management to set a ceiling on maximum amount the strategy should risk. To prevent order errors, a floor should also be set – minimum tradable amount is 0.001.

5% Risk And 10% Limit Example

If creating a strategy that others will use, start by creating global variables that are easy to understand such as riskPercentage and riskLimitPercentage. It's a good practice to offer the users the option of fixed lots which is easily implemented using a Boolean check.

This function requires:
  • 3 If blocks
  • 3 Assign blocks
  • 2 Calculation blocks
  • 2 Calculation Expression blocks
  1. First, check if using percentages is allowed using a Boolean. If not, assign the fixed lot to default trade amount.
  2. Convert the risk and limit to a percentage using calculation blocks to divide them by 100.
  3. Convert the outputs above to tradable amounts using the formula: (((percentage*equity)*leverage))/1000000).
  4. Check if the resulting trade amount is greater or lesser than the set limit or allowed minimum and if true, assign either the limit or minimum.

Getting the Highest and Lowest From Historical Candles

In day trading, I find it more profitable to use the lowest low or highest high of the last 3 days for stop losses when the market is trending. Finding this in Visual JForex is not very straightforward, but can be done through looping historical candles and assignment blocks.

Example of Getting the Highest High and Lowest Low of the Last 72 Hours (3 Days)

For this to work you will need: 1 Get Historical Candles, 4 Assign blocks, 1 Loop Viewer, 1 Multiple Action block and 2 If blocks

  1. Fetch 72 hourly candles on the bid side with the output as candles72 or use the auto created variable.
  2. Assign 0 to the variable hour72High and 2000 to hour72Low.
  3. The two should flow into a loop viewer that outputs the current candle into candle72Array
  4. The loop viewer should then connect to the Multiple Action block through the loop (not the end of cycle).
  5. The first 'If' block compares and assigns the highest candle to the variable hour72High in the Assign block and the reverse is done for hour72Low.
Once the loop viewer cycles through all the 72 hourly candles, you will have the highest high (Candle High) and lowest low (Candle Low) from the array.

It might seem as if the above won’t work but it does, below is the output. With this, you can now set your stop loss and take profit using the highest highs and lowest lows as an alternative to using Average True Range.

From the image, you can tell that, high-lows from historical candles can also be used to trade ranges.

Note: Setting a stop loss and take profit using price instead of pips requires a change on the order block. Simply choose price in the settings as shown in the image below.

Implementing a Draw-down Feature in Visual JForex

If you are running various strategies, you might want to stop a strategy that loses a preset amount either in pips or currency. I have found a way to work this into all my strategies and although it is kind of “primitive” it works. The idea is to keep winning strategies running and stop losing strategies early enough.

The concept is, I start with the draw-down variable as 0 then keep adding every outcome of the trade to the variable using the TradeEvent StartPoint. So, if the last trade lost 10 and the previous made 10, the draw-down is 0. Once the set limit is met or exceeded, stop the strategy. I believe this should be a feature in social trading as most platforms are already doing it.

Example of -USD300 as the Max Draw-down

This runs on TradeEvent and checks if the last trade event state is closed. If true, add the profit or loss in USD to the currentDrawDown variable. Next, compare current draw down to max draw down and stop the strategy if it greater than or equal to max draw down.

The image below explains it visually.

A nice addition to visual JForex would be to send an email or SMS alert once a strategy has been stopped.

The MAX(A:A) Excel Function in Visual JForex

When you want to get the largest value between multiple values, there is the excel function max() which is not fully supported in VJForex but thanks to amine_chourou (Dukascopy Support) I found a way to work with it. The idea is to bracket two MAX values (max(a1,(max(a2,a3)))) in a calculation expression block and you will be able to compare up to 3 variables. See image below, the same works for MIN.

Tying It Altogether

To conclude, I will show you how this is arranged and how you can use it. I have created variable groups named after the function and to test just connect the relevant start point and you are good to go. I welcome code contribution to this as it will make development easier and faster for beginners.

I will be adding break even function, ATR to pips conversion, price difference to pips, don’t trade on Fridays/Friday close, use of leverage and more.
Translate to English Show original