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 XXtrader, Yesterday, 11:30 PM
    2 responses
    11 views
    0 likes
    Last Post XXtrader  
    Started by Waxavi, Today, 02:10 AM
    0 responses
    6 views
    0 likes
    Last Post Waxavi
    by Waxavi
     
    Started by TradeForge, Today, 02:09 AM
    0 responses
    11 views
    0 likes
    Last Post TradeForge  
    Started by Waxavi, Today, 02:00 AM
    0 responses
    2 views
    0 likes
    Last Post Waxavi
    by Waxavi
     
    Started by elirion, Today, 01:36 AM
    0 responses
    7 views
    0 likes
    Last Post elirion
    by elirion
     
    Working...
    X