NexusFi: Find Your Edge


Home Menu

 





Stuck! How to code simple currency backtest


Discussion in MultiCharts

Updated
      Top Posters
    1. looks_one radiusvector with 3 posts (0 thanks)
    2. looks_two ABCTG with 3 posts (2 thanks)
    3. looks_3 SMCJB with 1 posts (1 thanks)
    4. looks_4 Al3cs with 1 posts (1 thanks)
    1. trending_up 1,404 views
    2. thumb_up 4 thanks given
    3. group 5 followers
    1. forum 7 posts
    2. attach_file 0 attachments




 
Search this Thread

Stuck! How to code simple currency backtest

  #1 (permalink)
radiusvector
New York City + New York
 
Posts: 3 since Dec 2017
Thanks Given: 4
Thanks Received: 0

Hi,

New to MC + EasyLanguage.

I want to try a simple backtest for EUR/USD, which
- Buys when the current price is just above the previous daily bar's high, and has a fixed stop loss / take profit / break even
- Adjusts position size to be 10% of the total equity in that account per trade

I have intrabar generation as true because I want to buy when the 'current price' of the bar exceeds the previous high. Not the O/H/L/C numbers. Running the code below on the backtest tends to generate way too few trades, only when the daily open is above the previous day's high (even with bar magnifier enabled). Please help!

How can I achieve this? Here's where I stand currently. Thanks in advance!

 
Code
[IntrabarOrderGeneration = True]

vars: Pips(MinMove/PriceScale *10);

condition1 = Open > High[1];
if condition1 then
	Buy ( "8HTT" ) next bar at market ;
setstoploss(Pips * 50);
setprofittarget(Pips * 100);
setbreakeven(Pips * 50);

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
Trade idea based off three indicators.
Traders Hideout
Better Renko Gaps
The Elite Circle
How to apply profiles
Traders Hideout
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
  #2 (permalink)
 
SMCJB's Avatar
 SMCJB 
Houston TX
Legendary Market Wizard
 
Experience: Advanced
Platform: TT and Stellar
Broker: Advantage Futures
Trading: Primarily Energy but also a little Equities, Fixed Income, Metals and Crypto.
Frequency: Many times daily
Duration: Never
Posts: 5,049 since Dec 2013
Thanks Given: 4,388
Thanks Received: 10,207

May not answer your question completely but if you replace
 
Code
condition1 = Open > High[1];
if condition1 then
	Buy ( "8HTT" ) next bar at market ;
With
 
Code
Buy ( "8HTT" ) next bar at high stop ;
I think you'll see something similair to what you want.

Your code is looking at the high of bar 1, comparing it to the open of bar 2, and then sending an order on the open of bar 3. Using the stop code, it looks at the high of bar 1, and enters a buy (stop) order to buy immediately if the market equals the high of bar 1 DURING bar 2.

Can't help on position sizing cause I trade futures.

Reply With Quote
Thanked by:
  #3 (permalink)
 Al3cs 
Cagliari, Italy
 
Experience: Intermediate
Platform: Multicharts, Jigsaw
Broker: AMP+CQG, Directa
Trading: FDAX
Posts: 12 since May 2016
Thanks Given: 21
Thanks Received: 4


The previous day’s high is HighD(1).
If you use High[1] you’re watching at previous candle. It is the same only in daily TF but you can use HighD in every TF and it will always return previous day’s High.
Notice that HighD is a function and High is an array.
About your entry think about this: do you want a market entry or a breakout entry at HighD[1] + 1 stop ?


Sent from my iPhone using futures.io

Reply With Quote
Thanked by:
  #4 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

radiusvector,

are you applying the code to a daily chart or an intraday chart?



radiusvector View Post
Hi,
Running the code below on the backtest tends to generate way too few trades, only when the daily open is above the previous day's high (even with bar magnifier enabled).

Your code tells MC to do exactly what you are seeing as you are using the reserved word "Open", which just returns the open of the bar and does not change during the bar. The reserved word Close on the other hand will get updated throughout the bar and return the last traded price.
As @SMCJB and @Al3cs have pointed out the correct approach will depend whether you want to issue a market order when your condition is met or place a stop order upfront at the desired entry price.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
radiusvector
New York City + New York
 
Posts: 3 since Dec 2017
Thanks Given: 4
Thanks Received: 0

So helpful! Thanks so much

Switching to

 
Code
condition1 = Close > High[1];
if (condition1 and MarketPosition = 0) then
	Buy ( "8HTT" ) next bar at market ;
Got it to do exactly what was needed. I wanted the flexibility of testing on multiple timeframes so went with High[1] rather than HighD, but appreciate the suggestion.

Any ideas on how to get started on the position sizing?

Right now, trading 1 contract (and no leverage settings) seems to be tiny and I am trying to get it to trade 10% of the equity on my account. I'm also assuming that leverage will be calculated in my algo rather than an MC setting.

Reply With Quote
  #6 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

radiusvector,

for your backtests it might be simpler to create an input allowing you to specify the start account equity and then
size your position based on that (maybe store the starting equity in a variable and add the NetProfit to keep track of the changes in the account equity).
The actual formula for the sizing depends on your broker as some deal in units and others in lots.

Regards,

ABCTG

Follow me on Twitter Reply With Quote
Thanked by:
  #7 (permalink)
radiusvector
New York City + New York
 
Posts: 3 since Dec 2017
Thanks Given: 4
Thanks Received: 0


ABCTG View Post
radiusvector,

for your backtests it might be simpler to create an input allowing you to specify the start account equity and then
size your position based on that (maybe store the starting equity in a variable and add the NetProfit to keep track of the changes in the account equity).
The actual formula for the sizing depends on your broker as some deal in units and others in lots.

Regards,

ABCTG

Thanks ABCTG, here's what I came up with but it tends to buy the same amount of units each trade. What am I doing wrong?

 
Code
[IntrabarOrderGeneration = True]

inputs: start$(100000), riskpct(1), lev(50);

vars: Pips(MinMove/PriceScale *10), Equity (0), Amt (0); 

condition1 = Close > High[1];
if (condition1 and MarketPosition = 0) then
	Equity = start$ + NetProfit;
	Amt = Equity * riskpct * 0.01 * lev;
	Buy ( "Buy" ) Amt contracts next bar at market ;

setstoploss(Pips * 50);
setprofittarget(Pips * 100);
setbreakeven(Pips * 50);

Reply With Quote
  #8 (permalink)
 ABCTG   is a Vendor
 
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629

radiusvector,

I would suggest to use the print reserved word to keep track of the values that your code sees and what you compute internally.
Besides that, if you want to include more than one expression in your conditions, you will have to use "if... then begin...end" statements. https://www.multicharts.com/trading-software/index.php/If

Regards,

ABCTG

Follow me on Twitter Reply With Quote




Last Updated on December 5, 2017


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts