NexusFi: Find Your Edge


Home Menu

 





Limit trades per direction


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Big Mike with 1 posts (0 thanks)
    2. looks_two Silver Dragon with 1 posts (4 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 tradingds with 1 posts (0 thanks)
    1. trending_up 4,355 views
    2. thumb_up 4 thanks given
    3. group 2 followers
    1. forum 3 posts
    2. attach_file 1 attachments




 
Search this Thread

Limit trades per direction

  #1 (permalink)
 tradingds 
Zurich, Switzerland
 
Experience: Intermediate
Platform: Ninjatrader
Trading: Futures
Posts: 5 since Apr 2011
Thanks Given: 1
Thanks Received: 1

Hello everyone!

I'm currently developing an automated trading strategy. I have no previous history in programming, so that's all pretty new to me.
It actually worked all just the way it was supposed to (opening/closing trades), but now i tried to add the possibility (with a trade counter, one for short, one for long), to tell the strategy how many trades per direction (long/short) it's supposed to take, before not executing any more trades. But it doesn't take any trades at all at this point...

I have it set to: CalculateOnBarClose = false; so once it gets the trade signal, it executes the trade. Once the target(s) gets filled it often opens a new one trade because the signal is still there. So sometimes I have 2 trades within one bar. That's OK, but like i said before, I'd like to be able to tell the program only to trade for example 3 times and then just to ignore the trade signal.

Once the trade signal (the trade conditions) is not true anymore, it should reset the trade counter immediately, so in case an other signal comes a (few) bar(s) later , it will open a new position again (even if it's in the same direction as the trades were before.)

I work with 3 targets (sometime all the same, sometimes different). So those 3 trades for the 3 targets should count as ONE trade taken, not 3.

Below i have a copy of the ninjascript, the way i tried to write it:


#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
///<summary>
///
Enter the description of your strategy here
///</summary>
[Description("")]
publicclass BMEXTTSv3SOXEasy : Strategy
{
...
...
...
privateint tradeCounterLong= 0; // This variable represents the number of trades taken.
privateint tradeCounterShort= 0; // This variable represents the number of trades taken.
privateint maxTradesLong = 3; // This variable sets the maximum number of trades to take.
privateint maxTradesShort = 3; // This variable sets the maximum number of trades to take.
privatebool EntryConditionLong;
privatebool EntryConditionShort;


///
<summary>
///
This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
CalculateOnBarClose =
false;
EntryHandling = EntryHandling.UniqueEntries;
SetTrailStop(CalculationMode.Ticks, trailstop);
}


private
void GoLong()
{

tradeCounterLong++;

SetProfitTarget(
"target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
SetProfitTarget(
"target2", CalculationMode.Price, Close[0] + (Target2*TickSize));
SetProfitTarget(
"target3", CalculationMode.Price, Close[0] + (Target3*TickSize));

EnterLong(
"target1");
EnterLong(
"target2");
EnterLong(
"target3");
}

private
void GoShort()
{
tradeCounterShort++;

SetProfitTarget(
"target1", CalculationMode.Price, Close[0] - (Target1*TickSize));
SetProfitTarget(
"target2", CalculationMode.Price, Close[0] - (Target2*TickSize));
SetProfitTarget(
"target3", CalculationMode.Price, Close[0] - (Target3*TickSize));

EnterShort(
"target1");
EnterShort(
"target2");
EnterShort(
"target3");
}

protected
overridevoid OnBarUpdate()
{ EntryConditionLong = Position.MarketPosition == MarketPosition.Flat
&& ...
&& ...
&& ...
.
.
. ;

EntryConditionShort = Position.MarketPosition == MarketPosition.Flat
&& ...
&& ...
&& ...
.
.
. ;


// Reset the tradeCounter value of each session.
//...for long
if (EntryConditionLong == false)
{
tradeCounterLong =
0;
}
//...and for short
if (EntryConditionShort == false)
{
tradeCounterShort =
0;
}

// Entry Condition set 1 (long)
if (EntryConditionLong && (tradeCounterLong < maxTradesLong))
{
GoLong();
}
// Entry Condition set 2 (short)
if (EntryConditionShort &&(tradeCounterShort < maxTradesShort))
{
GoShort();
}
}


#region
Properties
[Description(
"Maximum number of trades to take per Direction")]
[Category(
"Max Trades per Direction")]
publicint MaxTradesShort
{
get { return maxTradesShort; }
set { maxTradesShort = Math.Max(1, value); }
}
[Description(
"Maximum number of trades to take per Direction")]
[Category(
"Max Trades per Direction")]
publicint MaxTradesLong
{
get { return maxTradesLong; }
set { maxTradesLong = Math.Max(1, value); }
}
...
...
...
...
#endregion
}
}

Could somebody please tell me what i did wrong? Thank you all for your help in advance!

Dave

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
ZombieSqueeze
Platforms and Indicators
NexusFi Journal Challenge - April 2024
Feedback and Announcements
Online prop firm The Funded Trader (TFT) going under?
Traders Hideout
Build trailing stop for micro index(s)
Psychology and Money Management
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
59 thanks
Funded Trader platforms
37 thanks
NexusFi site changelog and issues/problem reporting
24 thanks
GFIs1 1 DAX trade per day journal
22 thanks
The Program
19 thanks
  #3 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,399 since Jun 2009
Thanks Given: 33,175
Thanks Received: 101,539


If you only want one entry per bar, just keep track of BarsSinceEntry or set your own flag. If you use BarsSinceEntry you need to check for -1 which means 'null' or not yet set, and then any other value is the number of bars since the last entry. It would need to be > 0 to not be an intrabar multiple entry.

Setting a flag would be something like

if (MarketPosition[0] != 0) entrybar = CurrentBar;

then test that entrybar != CurrentBar in your entry code.

I have written C# in a couple years so above code needs some tweaking.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
 
Silver Dragon's Avatar
 Silver Dragon 
Cincinnati Ohio
Legendary Master Data Manipulator
 
Experience: Intermediate
Platform: TastyWorks / NT
Broker: TastyWorks /NT
Trading: FX, Stocks, Options
Posts: 2,107 since Feb 2011
Thanks Given: 6,422
Thanks Received: 5,238


tradingds View Post
Hello everyone!

I'm currently developing an automated trading strategy. I have no previous history in programming, so that's all pretty new to me.
It actually worked all just the way it was supposed to (opening/closing trades), but now i tried to add the possibility (with a trade counter, one for short, one for long), to tell the strategy how many trades per direction (long/short) it's supposed to take, before not executing any more trades. But it doesn't take any trades at all at this point...

I have it set to: CalculateOnBarClose = false; so once it gets the trade signal, it executes the trade. Once the target(s) gets filled it often opens a new one trade because the signal is still there. So sometimes I have 2 trades within one bar. That's OK, but like i said before, I'd like to be able to tell the program only to trade for example 3 times and then just to ignore the trade signal.

Once the trade signal (the trade conditions) is not true anymore, it should reset the trade counter immediately, so in case an other signal comes a (few) bar(s) later , it will open a new position again (even if it's in the same direction as the trades were before.)

I work with 3 targets (sometime all the same, sometimes different). So those 3 trades for the 3 targets should count as ONE trade taken, not 3.

Could somebody please tell me what i did wrong? Thank you all for your help in advance!

Dave

I posted this in the NT forum as well. The attached strategy is a example where it will limit 3 consecutive short trades in row.

The trick is to have a trigger that will update when the trade is executed. Once it gets to a certain number of trades it will not execute until the trigger is reset.

In the below code the trades will be triggered everytime CCI20 crosses above zero. Each time it does the longtrade variable will be increased by 1. The code will only allow 3 long trades until the condition is reset. The condition is reset when SMA(20) crosses below SMA(89).

Let me know if this makes sense. I have attached proof of concept strategy. When run it will limit the strategy to 3 trades.

SD

 
Code
 
protected override void OnBarUpdate()
{
//Reset condition
 
if (CrossBelow(SMA(20), SMA(89), 1))
{
 
longtrade= 0; //reset long trigger
 
}
 
 
 
 
//Enter Trade
if (CrossAbove(CCI(20), 0, 1) && longtrade <= 3)
{
     EnterLong(1, "Long 1a");
 
 
       if (longtrade == 0)
         {
           longtrade = 1;
         }
       if (longtrade >0)
         {
           longtrade = longtrade + 1;
          }
}
 
}

nosce te ipsum

You make your own opportunities in life.
Attached Files
Elite Membership required to download: Limit3TradesPerDirection.zip
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:




Last Updated on October 13, 2011


© 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