NexusFi: Find Your Edge


Home Menu

 





NinjaTrader ATM in EasyLanguage


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one Arich with 16 posts (1 thanks)
    2. looks_two Bimi with 9 posts (3 thanks)
    3. looks_3 Nicolas11 with 4 posts (5 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
      Best Posters
    1. looks_one Nicolas11 with 1.3 thanks per post
    2. looks_two cbritton with 1 thanks per post
    3. looks_3 Bimi with 0.3 thanks per post
    4. looks_4 Arich with 0.1 thanks per post
    1. trending_up 10,003 views
    2. thumb_up 10 thanks given
    3. group 4 followers
    1. forum 32 posts
    2. attach_file 0 attachments




 
Search this Thread

NinjaTrader ATM in EasyLanguage

  #1 (permalink)
 Arich 
Springfield, Missouri
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: EUR/USD
Posts: 28 since Sep 2012
Thanks Given: 29
Thanks Received: 5

Hello,

I am very new to EasyLanguage and even coding in general, so please bear with me.

I have an ATM strategy in NT that I would like to backtest/automate in MC. I have just started researching how to do this, but I figured if anyone here has some advice, I'd really appreciate it. Allow me to present the facts first and then ask my questions last.

Facts:
Basically the ATM consists of three targets, a stoploss, a breakeven, and finally a trailing stop.
I use MACD as my signal generator. When the fast crosses the slow I enter a trade. The direction of the trade is determined by whether the cross happens above or below the zero line.

Questions:
I don't really know where to start with coding this... Does anyone know of any good resources or threads (I searched futures.io (formerly BMT) but didn't really find anything related to this) that I could use? I'm here to learn, not to leech.

In NT, backtesting this strategy was way beyond my skill level and even some skilled coders I spoke to said this was difficult to code in NT due to the trailing stoploss and breakeven since they need to be triggered intra bar. This is possible to do in MC, right? From what I've read it seems possible...

Is there someplace I can go to learn some basic EasyLanguage coding fundamentals and logic?

Any help you can provide will be much appreciated!

Happy Trading,
ARich

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
ZombieSqueeze
Platforms and Indicators
Better Renko Gaps
The Elite Circle
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
  #3 (permalink)
Bimi
London
 
Posts: 118 since Mar 2010
Thanks Given: 42
Thanks Received: 58



Arich View Post
Hello,

I am very new to EasyLanguage and even coding in general, so please bear with me.

I have an ATM strategy in NT that I would like to backtest/automate in MC. I have just started researching how to do this, but I figured if anyone here has some advice, I'd really appreciate it. Allow me to present the facts first and then ask my questions last.

Facts:
Basically the ATM consists of three targets, a stoploss, a breakeven, and finally a trailing stop.
I use MACD as my signal generator. When the fast crosses the slow I enter a trade. The direction of the trade is determined by whether the cross happens above or below the zero line.

Questions:
I don't really know where to start with coding this... Does anyone know of any good resources or threads (I searched futures.io (formerly BMT) but didn't really find anything related to this) that I could use? I'm here to learn, not to leech.

In NT, backtesting this strategy was way beyond my skill level and even some skilled coders I spoke to said this was difficult to code in NT due to the trailing stoploss and breakeven since they need to be triggered intra bar. This is possible to do in MC, right? From what I've read it seems possible...

Is there someplace I can go to learn some basic EasyLanguage coding fundamentals and logic?

Any help you can provide will be much appreciated!

Happy Trading,
ARich

nothing is impossible, all it takes is little time.

Reply With Quote
  #4 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

@Arich,

This webinar by Big Mike exactly answers your question (3-stage exits) :


Could be a basis.

Nicolas

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #5 (permalink)
 Arich 
Springfield, Missouri
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: EUR/USD
Posts: 28 since Sep 2012
Thanks Given: 29
Thanks Received: 5

Hello,

I finished my first set of code! There were some errors and such, but I finally got it to compile. The only problem is that when applied to a chart it only produced one entry (a "buy") when it should have produced at least 20-30 entries over that time frame. Here is my code:

inputs:
SlowLength (26),
FastLength (12),
MACDLength (3),
Target1 (40),
Target2 (20),
Target3 (40),
StopSize (40),
BE2 (0), // 0=false 1=true
BE3 (0); // 0=false 1=true

vars:
ticksize (MinMove/PriceScale),
MyMACD (0),
MACDAvg (0),
MACDDiff (0),
t1 (Target1 * ticksize),
t2 (Target1 + Target2 *ticksize),
t3 (Target1 + Target2 + Target3 * ticksize),
st1 (0),
st2 (0),
st3 (0);

MyMACD = MACD(Close, FastLength, SlowLength);
MACDAvg = XAverage(MyMACD, MACDLength);
MACDDiff = MyMACD - MACDAvg;

// Open new Positions

if MarketPosition = 0 then begin

if MyMACD > MACDAvg and MyMACD <= 0 and MACDAvg <= 0 then begin
Buy ("Enter Long1") 100000 contract next bar at market;
Buy ("Enter Long2") 100000 contract next bar at market;
Buy ("Enter Long3") 100000 contract next bar at market;
end;

if MyMACD < MACDAvg and MyMACD >= 0 and MACDAvg >= 0 then begin
Sell ("Enter Short1") 100000 contract next bar at market;
Sell ("Enter Short2") 100000 contract next bar at market;
Sell ("Enter Short3") 100000 contract next bar at market;
end;

end;

// Manage open orders

if MarketPosition = 1 then begin

st1 = EntryPrice - (StopSize * TickSize);
st2 = iff(BE2 = 1, EntryPrice, EntryPrice - (StopSize * TickSize));
st3 = iff(BE3 = 1, EntryPrice, EntryPrice - (StopSize * TickSize));

if CurrentContracts = 1 then begin
Sell ("Exit l3-c1 Target") 100000 Contract Next Bar at (EntryPrice + t3) Limit;
Sell ("Exit l3-c1 Stop") 100000 Contract Next Bar at st3 Stop;
end;

if CurrentContracts = 2 then begin
Sell ("Exit l2-c2 Target") 100000 Contract Next Bar at (EntryPrice + t2) Limit;
Sell ("Exit l2-c2 Stop") 100000 Contract Next Bar at st2 Stop;
Sell ("Exit l3-c2 Target") 100000 Contract Next Bar at (EntryPrice + t3) Limit;
Sell ("Exit l3-c2 Stop") 100000 Contract Next Bar at st3 Stop;
end;

if CurrentContracts = 3 then begin
Sell ("Exit l1-c3 Target") 100000 Contract Next Bar at (EntryPrice + t2) Limit;
Sell ("Exit l1-c3 Stop") 100000 Contract Next Bar at st1 Stop;
Sell ("Exit l2-c3 Target") 100000 Contract Next Bar at (EntryPrice + t2) Limit;
Sell ("Exit l2-c3 Stop") 100000 Contract Next Bar at st2 Stop;
Sell ("Exit l3-c3 Target") 100000 Contract Next Bar at (EntryPrice + t3) Limit;
Sell ("Exit l3-c3 Stop") 100000 Contract Next Bar at st3 Stop;
end;
end;

if MarketPosition = -1 then begin

st1 = EntryPrice + (StopSize * TickSize);
st2 = iff(BE2 = 1, EntryPrice, EntryPrice + (StopSize * TickSize));
st3 = iff(BE3 = 1, EntryPrice, EntryPrice + (StopSize * TickSize));

if CurrentContracts = 1 then begin
BuyToCover ("Exit s3-c1 Target") 100000 Contract Next Bar at (EntryPrice - t3) Limit;
BuyToCover ("Exit s3-c1 Stop") 100000 Contract Next Bar at st3 Stop;
end;

if CurrentContracts = 2 then begin
BuyToCover ("Exit s2-c2 Target") 100000 Contract Next Bar at (EntryPrice - t2) Limit;
BuyToCover ("Exit s2-c2 Stop") 100000 Contract Next Bar at st2 Stop;
BuyToCover ("Exit s3-c2 Target") 100000 Contract Next Bar at (EntryPrice - t3) Limit;
BuyToCover ("Exit s3-c2 Stop") 100000 Contract Next Bar at st3 Stop;
end;

if CurrentContracts = 3 then begin
BuyToCover ("Exit s1-c3 Target") 100000 Contract Next Bar at (EntryPrice - t2) Limit;
BuyToCover ("Exit s1-c3 Stop") 100000 Contract Next Bar at st1 Stop;
BuyToCover ("Exit s2-c3 Target") 100000 Contract Next Bar at (EntryPrice - t2) Limit;
BuyToCover ("Exit s2-c3 Stop") 100000 Contract Next Bar at st2 Stop;
BuyToCover ("Exit s3-c3 Target") 100000 Contract Next Bar at (EntryPrice - t3) Limit;
BuyToCover ("Exit s3-c3 Stop") 100000 Contract Next Bar at st3 Stop;
end;
end;

Can anyone see why it is only executing one trade? It only enters one position too (instead of three positions). Is there a simpler way to code this?

All help is highly appreciated!


Started this thread Reply With Quote
  #6 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

Hi,

I have tried it: the first trade is never closed. The problem is probably linked to the exit.

In case of problem, you should simplify the code to identify the issue.
Just keep one side (long or short).
Just keep one lot (not the 3 lots).
And debug.
When the short code will work, add back the other features.

People here will be happy to help you.
But it would be easier with a "simple" but non-working code rather than with a 100-line code.
In other words: a SSCCE.

Nicolas

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #7 (permalink)
 Arich 
Springfield, Missouri
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: EUR/USD
Posts: 28 since Sep 2012
Thanks Given: 29
Thanks Received: 5


Nicolas11 View Post
Hi,

I have tried it: the first trade is never closed. The problem is probably linked to the exit.

In case of problem, you should simplify the code to identify the issue.
Just keep one side (long or short).
Just keep one lot (not the 3 lots).
And debug.
When the short code will work, add back the other features.

People here will be happy to help you.
But it would be easier with a "simple" but non-working code rather than with a 100-line code.
In other words: a SSCCE.

Nicolas

Thank you for the suggestion! A big question for me is why it didn't enter all three positions. That's a big part of the strategy, and I have no idea why it won't enter all three positions... If it entered one, why not the others? Should I write it as:

Buy ("Enter Long1") 3 100000 contracts next bar at market;

instead of:

Buy ("Enter Long1") 100000 contracts next bar at market;
Buy ("Enter Long2") 100000 contracts next bar at market;
Buy ("Enter Long3") 100000 contracts next bar at market;

???

Thanks for your help!
ARich

Started this thread Reply With Quote
  #8 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,769

Yes, it could help. But do not forget "*" : 3 * 100000.

Once more, please post a SSCCE.

Nicolas

Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #9 (permalink)
 Arich 
Springfield, Missouri
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: EUR/USD
Posts: 28 since Sep 2012
Thanks Given: 29
Thanks Received: 5


Nicolas11 View Post
Yes, it could help. But do not forget "*" : 3 * 100000.

Once more, please post a SSCCE.

Nicolas

Ah, thank you for the URL; I didn't know what you were referring to earlier.

This is the shortened code:

// Open new Positions

if MarketPosition = 0 then begin

if MyMACD > MACDAvg and MyMACD <= 0 and MACDAvg <= 0 then begin
Buy ("Enter Long1") 300000 contracts next bar at market;
end;
end;

// Manage open orders

if MarketPosition = 1 then begin

st1 = EntryPrice - (StopSize * TickSize);

if CurrentContracts = 1 then begin
Sell ("Exit l1-c1 Target") 3 * 100000 contracts Next Bar at (EntryPrice + t1) Limit;
Sell ("Exit l1-c1 Stop") 3 * 100000 contracts Next Bar at st1 Stop;
end;
end;

That's as bare-bones as it gets I'm afraid. Is there anything that jumps out at you? I've tried changing the lot size too, but I can't get it to show anything other than "100,000" on the chart. I'm not sure how to troubleshoot the exit part of the code either... Thanks for your help!

Started this thread Reply With Quote
  #10 (permalink)
Bimi
London
 
Posts: 118 since Mar 2010
Thanks Given: 42
Thanks Received: 58


did you restrict the fill to one order per direction?

Reply With Quote




Last Updated on February 28, 2013


© 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