Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Continously Update Long Position Until Its The Absolute Bottom In The Trendline

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Continously Update Long Position Until Its The Absolute Bottom In The Trendline

    So this strategy is entirley meant to be just for historical data and what not, not meant to be traded real time, ideally what I am trying to do is buy at the bottom of every support line for a given trend, here is a picture of what it currently does:

    Click image for larger version

Name:	asd.PNG
Views:	223
Size:	36.1 KB
ID:	1173891
    So the red is where I need to be long and then just close say 2 points after, I have this for the most part but the stop loss is generally my problem now. I basically need to enter a position with say a 2 tick stop loss, if it hits the stop loss then it wasnt the "absolute bottom", thus I re enter the trade and sit another 2 tick stop loss, this occurs until it goes back up and hits the take profit. Currently I use Bollinger bands to measure the trend, here is my current code:
    Code:
     public class LabeledDataStrategy : Strategy
    {
    private Bollinger Bollinger20;
    private int totalContracts = 1;
    private int counter = 0;
    public double ConvertToEpochTimestamp(DateTime dateTime)
    {
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
    TimeSpan diff = dateTime.ToUniversalTime() - origin;
    return Math.Floor(diff.TotalSeconds);
    }
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"LabeledDataStrategy";
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    Calculate = Calculate.OnEachTick;
    BarsRequiredToPlot = 15;
    Name = "LabeledDataStrategy";
    period = 5;
    stopLoss = .5;
    
    }
    }
    
    protected override void OnBarUpdate()
    {
    if(counter == 0)
    {
    counter++;
    }
    
    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] >= Position.AveragePrice + 8 * TickSize)
    {
    ExitLong("LONG");
    }
    }
    double lower = Bollinger(2, period).Lower[0];
    
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    if (Close[0] - lower <= .5 || Low[0] <= lower)
    {
    EnterLong(totalContracts, "LONG");
    SetStopLoss(CalculationMode.Ticks, 1);
    }
    }
    }
    
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name = "period", Order = 1, GroupName = "Parameters")]
    public int period
    { get; set; }
    
    
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name = "stopLoss", Order = 1, GroupName = "Parameters")]
    public double stopLoss
    { get; set; }
    
    
    
    }
    What is the best way to achieve this, basically if(stopped out){ buy in with stop loss again}. To reiterate this is not for live trading nor is this for probability or profit, just historical data related, any help would be appreciated!


    #2
    Hello laner107,

    Our support team is not able to write the logic on your behalf but we can help you to use prints to understand why the behavior is not as you expect.

    What is the current behavior that is unexpected that has you stuck?

    Is the entry being placed at the correct time and place?

    One note, set methods should be called before the entry is submitted.

    SetStopLoss(CalculationMode.Ticks, 1);
    EnterLong(totalContracts, "LONG");

    With the stop, this being submitted 1 tick from the entry price is highly likely to fill instantly, is this the behavior that is unexpected?

    Are you having trouble submitting a new entry when the stop loss is filled?
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    88 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    134 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    119 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    69 views
    0 likes
    Last Post PaulMohn  
    Working...
    X