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 trilliantrader, 04-18-2024, 08:16 AM
    4 responses
    18 views
    0 likes
    Last Post trilliantrader  
    Started by mgco4you, Today, 09:46 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by wzgy0920, Today, 09:53 PM
    0 responses
    9 views
    0 likes
    Last Post wzgy0920  
    Started by Rapine Heihei, Today, 08:19 PM
    1 response
    10 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by Rapine Heihei, Today, 08:25 PM
    0 responses
    10 views
    0 likes
    Last Post Rapine Heihei  
    Working...
    X