Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Automated trading for Futures

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

    Automated trading for Futures

    Hello I am trying to convert my equities strategy for futures.

    When I try to run a back test it doesn't work. Can you tell me what is wrong with my code. To me it should work.

    Code:
    	// See the Help Guide for additional information
    				IsInstantiatedOnEachOptimizationIteration	= true;
    				
    				AddPlot(Brushes.Orange, "PLOTA");
    				AddPlot(Brushes.Green, "PLOTB");
    			}
    			else if (State == State.Configure)
    			{
    				//SetProfitTarget(CalculationMode.Currency, 5000);
    				//SetStopLoss(CalculationMode.Currency, 1500);
    				AddDataSeries(BarsPeriodType.Month, 1);
    				AddDataSeries(BarsPeriodType.Day, 1);
    			}
    			else if (State == State.DataLoaded)
    			{
    				midbb = Bollinger(Close, 2, 20);
    				SMAmonth = SMA(BarsArray[1], 5);
    				DMI = DM(Close, 14);
    				ATRdaily = ATR(Close, 14);
    				ADXweek = ADX(BarsArray[2], 14);
    				StoConfirmWeek = Stochastics(5, 15, 3);
    				StoConfirmMonth = Stochastics(BarsArray[1], 5, 15, 3);
    				RSIweek = RSI(Close, 5, 3);
    				RSIdaily = RSI(BarsArray[2], 5, 3);
    				
    				
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBars[0] < 5 || CurrentBars[1] < 5)
    				return;
    			
    			Values[0][0] = midbb.Middle[0];
    			Values[1][0] = SMAmonth[0];
    			
    			if (holdValue == 0
    			&& (Highs[0][4] < Highs[0][3])
    			&& (Lows[0][4] > midbb.Middle[4])
    			&& (Lows[0][3] > midbb.Middle[3])
    			&& (Lows[0][2] > midbb.Middle[2])
    			&& (Lows[0][1] > midbb.Middle[1])
    			&& (Highs[0][3] > Highs[0][2])
    			&& (Highs[0][3] > Highs[0][1]))
    			{
    				holdValue = Highs[0][3];
    			}
    			
    	
    			
    			if (holdValue != 0 && Lows[0][0] < midbb.Middle[0])
    			{
    				holdValue = 0;
    				Print("Reset of holdValue " + holdValue);
    			}
    			
    		
    			
    			if (holdValue != 0)
    			{
    				Print("High price at last condition was " + holdValue);
    			}
    			
    			if ((holdValue > midbb.Middle[0])
    			&& (Lows[1][2] >= SMAmonth[2])
    			&& (Lows[1][1] >= SMAmonth[1])
    			&& (Lows[1][0] >= SMAmonth[0])
    			&& (holdValue != 0)
    			//&& (ATRdaily[0] >= .75)
    			&& (RSIweek[0] >= 75)
    			//&& (RSIdaily[0] >= 75)
    			//&& (Slope(DMI.DiPlus, 1, 0) > 1)
    			//&& (Slope(DMI.DiMinus, 1, 0) < -1)
    			//&& (DMI.DiPlus[0] >= 50)
    			//&& (StoConfirmWeek.K[0] > StoConfirmWeek.D[0])
    			//&& (StoConfirmMonth.K[0] > StoConfirmMonth.D[0])
    			&& (Highs[0][0] > holdValue))
    			{
    				int myQuantity = (int) ((1200)/(holdValue - midbb.Middle[0]));
    				EnterLong(1, @"");
    				holdValue = 0;
    			} else if (holdValue != 0 && Highs[0][0] > holdValue + .02)
    			{
    				holdValue = 0;
    				Print("Reset of holdValue" + holdValue);
    			}
    			
    			if (Lows[0][0] < SMAmonth[0])
    			{
    				ExitLong();
    			}
    			
    			if (holdValueShort == 0
    			&& (Lows[0][4] > Lows[0][3])
    			&& (Highs[0][4] < midbb.Middle[4])
    			&& (Highs[0][3] < midbb.Middle[3])
    			&& (Highs[0][2] < midbb.Middle[2])
    			&& (Highs[0][1] < midbb.Middle[1])
    			&& (Lows[0][3] < Lows[0][2])
    			&& (Lows[0][3] < Lows[0][1]))
    			{
    				holdValueShort = Lows[0][3];
    			}
    			
    			if (holdValueShort != 0 && Highs[0][0] > midbb.Middle[0])
    			{
    				holdValueShort = 0;
    				Print("Reset of holdValueShort " + holdValueShort);
    			}
    			
    			
    			if (holdValueShort != 0)
    			{
    				Print("Low price at last condition was " + holdValueShort);
    			}
    			
    			if ((holdValueShort < midbb.Middle[0])
    			&& (Highs[1][2] <= SMAmonth[2])
    			&& (Highs[1][1] <= SMAmonth[1])
    			&& (Highs[1][0] <= SMAmonth[0])
    			&& (holdValueShort != 0)
    			//&& (ATRdaily[0] >= .75)
    			&& (RSIweek[0] <= 25)
    			//&& (RSIdaily[0] <= 25)
    			//&& (Slope(DMI.DiPlus, 1, 0) < -1)
    			//&& (Slope(DMI.DiMinus, 1, 0) > 1)
    			//&& (DMI.DiMinus[0] >= 50)
    			//&& (StoConfirmWeek.K[0] < StoConfirmWeek.D[0])
    			//&& (StoConfirmMonth.K[0] < StoConfirmMonth.D[0])
    			&& (Lows[0][0] < holdValueShort))
    			{
    				int myQuantity = (int) ((1200)/(midbb.Middle[0]) - holdValue);
    				EnterShort(1, @"");
    				holdValueShort = 0;
    			} else if (holdValueShort != 0 && Lows[0][0] < holdValueShort - .02)
    			{
    				holdValueShort = 0;
    				Print("Reset of holdValueShort" + holdValueShort);
    			}
    			
    			if (Highs[0][0] > SMAmonth[0])
    			{
    				ExitShort();
    			}
    		}
    	}
    }

    #2
    Hello dorony,

    Thanks for your post.

    As a check of your data, please run the strategy SampleMACrossover on the same data and the same period. If the strategy places orders then that would confirm you have the data for the instrument.

    When you run the strategy in the analyzer, do you see any error messages listed in the "log" tab of the control center related to the strategy?

    It appears the strategy is adding Monthly bars and daily bars, what bars are you setting the strategy on in the analyzer (I'm referencing these as chart bars)?

    Does your analysis period exceed the current bars check considering whatever the chart bars are and the 5 months of monthly bars?

    You have a few print statements, do you see any print output?

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    77 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    63 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    53 views
    0 likes
    Last Post CarlTrading  
    Working...
    X