In the spirit of learning and sharing, I am going to convert my Catch a Trend Strategy into a JForex trading robot. I did not find it challenging implementing on Visual JForex compared to automating the Day Scalper strategy, which utilizes moving average envelopes and complex entry methods.

As I always state when sharing my trading strategies, there is no holy grail in forex trading. Every strategy will lose at a point no matter how advanced it is. What carries the day is proper risk management.

Multiple Bots Lowers Risk

With automated strategies, I have learned that spreading the risk across multiple robots with different entry methods and trade management systems greatly lowers my losses even if it is on the same pair. I risk 10% of my live account and run 10 to 15 bots concurrently on a VPS.

As seen from the following image on a demo account with Dukascopy, multiple bots were able to cover losses and end the period in profit. If I was running only 1 bot, the chances of profit or loss would have been 50%.



The next image shows the performance of many bots over a period of 30 days, but not on Dukascopy.
Note: I count pips made not the money.



As you can see, one bot trading GBPUSD lost 125 pips but was covered by another which made 1300+ pips. One opened 0 EURAUD trades, but the other made over 1300 pips on the same pair. Kind of like it is in the social trading contest.

Let’s Get Into Strategy Building!

While the actual strategy uses bar charts, there are no bars in Visual JForex so, I use candles and ‘onCandle’ as the start point. The strategy uses ADX (14, 14), 9-day and 18-day SMA and 5-day ATR (Average True Range).

Start with 4 ‘If’ blocks with the following: - Connect 'onCandle' to the 1st 'if' block with the input values as ‘defaultInstrument’ and ‘Last Bid Candle.Candle instrument’ through the equal sign. The 2nd 'if' block should have the variables ‘candleTime’ (1 Min) and ‘Last Bid Candle.Candle Period’ that flows through the equal sign to the 3rd 'if' block.

The 3rd 'if' block separates the strategy into signal analysis and trade management by checking if there is an open position, the input fields should be ‘Open Positons.Positions Amount’ and 1.

Use ‘LastTradeEvent.Trade Message Position.Position is long’ and true for the 4th 'if' block. This block will separate sell and buy trades if need be. Connect the 3rd and 4th blocks through the greater than and equal to signs.



Change the following default variables to dynamic by deleting the values before continuing, defaultTradeAmount, defaultStopLoss and defaultTakeProfit. Also, change the defaultPeriod to daily.

Filtering Time to Evaluate Signals (New York Close)

Since the strategy should only evaluate the market at New York close, I have to prevent it from running any signal evaluation until the market closes. This uses time blocks and checks if its 1 minute into the new trading day 21:01 GMT. This is why I set the 'candleTime' variable to 1 minute and not daily.

Use two 'GetTimeUnit' blocks with 'Last Bid Candle.Candle Time' and 'TimeUnit' as hour and minute and two ‘if’ blocks to check when the hour is 21 and the minute equal to or greater than 1.



Note: The time blocks are only applicable to signal evaluation and not trade management.

Setting up The Indicators

Drag the following to the workspace, 4 SMA blocks, 1 ADX and 1 ATR. The SMA time period should be 9 and 18 with shift 0 and 1 and output variables as sma9Day, sma18Day, sma9DayPrev, and sma18DayPrev.

Use shift 14 and time period 14 for the ADX block with adx14Day as the output. The time period should be defaultPeriod. Use an ATR block with the following, defaultPeriod, shift 0, time period 5 and output values atr5day.



Converting ATR to Pips

Using the Average True Range block requires an additional step to convert the ATR data into pips. A calculation expression block is required to multiply atr5day, 10000 and 2. The output can be used to replace the initial variable - atr5day.



Money Management

Instead of using fixed trade amounts, I prefer using a dynamic value that changes in relation to the equity. For example, when risking 10% of an account and running 10 bots, each bot will trade 1%. For Catch a Trend strategy I will use 2% (0.02). The calculation to get the trade amount is the % to risk * Equity * leverage / 1000000. This requires a calculation expression block.



The above calculation allows the strategy to always trade 2% of the equity without my intervention. While I do not cover it here, it is good to set the max trade amount allowed. This is fairly easy using 'if' blocks.

Checking For Divergence

This strategy first checks for the existence of bearish divergence when considering a buy trade and bullish divergence for a sell trade. I haven’t found an easy way to do this in Visual JForex without bogging the code. Since VJF is still ‘growing’ I might add this in the future, but I am skipping it for now.

To prevent false positives, I will first check if ADX is greater than 16 and only consider trades where 9-day SMA has turned up or down from the previous day and not equal or flat like in the manual strategy. I consider an ADX value lower than 16 to be indicative of a weak or dead trend - this uses an 'if' block.



Buy Signal Evaluation

It checks if current 9-day SMA is greater than previous day SMA and 18-day SMA is up or flat (equal to). It then assigns the 5-day ATR to the variable 'halfClose', which will be used in trade management.



The second part is setup day low minus 2 pips and comparing if it is greater than 102 pips. If higher than 102, then 102 pips are used as the stop loss. This will require fetching last daily bid candle, which will be yesterday (setup day) at 21:01 GMT and subtracting it from last tick bid and converting the result to pips by dividing the result by pipsSize using calculation expression.



Lastly, it opens a trade with the 'Open at Market' block using default variables since I changed all default variables to dynamic.

Note:
Both assign blocks for the defualtStopLoss should flow into the 'Open at Market' block. However, only one will be executed at a time after the ‘if’ block evaluation runs.



Sell Signal Evaluation

The same rules as those used in a buy trade are applicable, but they are reversed. Current day 9-day SMA should be lower than previous day SMA. Instead of yesterdayLow its yesterdayHigh and last daily ask candle is added to last tick ask not subtracted.



Trade Management

Once an order is filled, the trade management side is activated. It keeps checking if the current position profit is equal to or greater than 5-day ATR. If true, it closes 50% of the position and trails the rest of the position using 3-day low. The 3-day low is gotten by looping through 3 historical candles.

I have run out of writing space, however, the strategy be availed on my JStore once completed and tested.
Translate to English Show original