Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Brevo, Today, 01:45 AM
    0 responses
    3 views
    0 likes
    Last Post Brevo
    by Brevo
     
    Started by aussugardefender, Today, 01:07 AM
    0 responses
    3 views
    0 likes
    Last Post aussugardefender  
    Started by pvincent, 06-23-2022, 12:53 PM
    14 responses
    240 views
    0 likes
    Last Post Nyman
    by Nyman
     
    Started by TraderG23, 12-08-2023, 07:56 AM
    9 responses
    384 views
    1 like
    Last Post Gavini
    by Gavini
     
    Started by oviejo, Today, 12:28 AM
    0 responses
    6 views
    0 likes
    Last Post oviejo
    by oviejo
     
    Working...
    X