Stuck! How to code simple currency backtest - futures io
futures io



Stuck! How to code simple currency backtest


Discussion in MultiCharts

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




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Stuck! How to code simple currency backtest

(login for full post details)
  #1 (permalink)
radiusvector
New York City + New York
 
 
Posts: 3 since Dec 2017
Thanks: 4 given, 0 received

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

Journal Challenge April 2021 results (now extended!):
Competing for $1800 in prizes from Jigsaw
looks_oneMaking a Living with the Microsby sstheo
(206 thanks from 24 posts)
looks_twoSalao's Journalby Salao
(53 thanks from 11 posts)
looks_3Deetee’s DAX Trading Journal (time based)by Deetee
(48 thanks from 18 posts)
looks_4Learning to Profit - A journey in algorithms and optionsby Syntax
(36 thanks from 15 posts)
looks_5Maybe a little bit different journalby Malykubo
(14 thanks from 16 posts)
 
Best Threads (Most Thanked)
in the last 7 days on futures io
Big Mike in Ecuador
76 thanks
The Crude Dude Oil Trading System
73 thanks
Help improve the FIO community
38 thanks
The New Micro Contract - MICRO BITCOIN coming May 2021
29 thanks
futures io site changelog and issues/problem reporting
27 thanks
 
(login for full post details)
  #2 (permalink)
 SMCJB 
Legendary Market Wizard
Houston, TX
 
Experience: Advanced
Platform: Trading Technologies
Broker: Primary Advantage Futures. Also ED&F and Tradestation
Trading: Primarily Energy but also a little GE, GC, SI & Bitcoin
 
Posts: 4,046 since Dec 2013
Thanks: 3,346 given, 7,994 received

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
The following user says Thank You to SMCJB for this post:
 
(login for full post details)
  #3 (permalink)
 Al3cs 
Cagliari, Italy
 
Experience: Intermediate
Platform: Multicharts, Jigsaw
Broker: AMP+CQG, Directa
Trading: FDAX
 
Posts: 12 since May 2016
Thanks: 20 given, 4 received


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
The following user says Thank You to Al3cs for this post:
 
(login for full post details)
  #4 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

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
The following user says Thank You to ABCTG for this post:
 
(login for full post details)
  #5 (permalink)
radiusvector
New York City + New York
 
 
Posts: 3 since Dec 2017
Thanks: 4 given, 0 received

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
 
(login for full post details)
  #6 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

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
The following user says Thank You to ABCTG for this post:
 
(login for full post details)
  #7 (permalink)
radiusvector
New York City + New York
 
 
Posts: 3 since Dec 2017
Thanks: 4 given, 0 received


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
 
(login for full post details)
  #8 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

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


futures io Trading Community Platforms and Indicators MultiCharts > Stuck! How to code simple currency backtest


Last Updated on December 5, 2017


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Journal Challenge w/$1,800 in prizes!

April
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts