Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

I need to fix my logic

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

    I need to fix my logic

    Hello,

    The below script is working fine - It buys above 20 SMA and sells below 20 SMA. I'm trying to add a new logic to reverse my position from long to short if the closed bar is below the executed position, and vice versa. Please add it to my code

    Something like this?
    if (Position.MarketPosition == MarketPosition.Short && shortEntry.OrderState == OrderState.Filled && Close[0] > shortEntry.AverageFillPrice)
    {
    EnterLong();


    BELOW IS MY CODE


    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class TESTTEST : Strategy
    {
    private SMA SMA1;
    private Order longEntry;
    private Order shortEntry;





    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "TESTTEST";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    SMA1 = SMA(20);
    SMA1.Plots[0].Brush = Brushes.Goldenrod;
    AddChartIndicator(SMA1);
    SMA1 = SMA(20);

    }

    }



    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if (CrossAbove(Close, SMA1, 1))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");




    }
    // Set 2
    if (CrossBelow(Close, SMA1, 1))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");


    }

    }
    }
    }

    #2
    Hello kamalnour,

    Thanks for writing in.

    While we are certainly willing to help answer your questions on using NinjaScript, developing code for a client is outside the scope of the support we may provide.

    The condition you have outlined can be placed within OnBarUpdate() as a separate condition to reverse your position.

    If you would like I may refer you to a NinjaScript consultant that may do the coding for you.

    Please let me know if I may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    51 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    128 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X