Market Regime : Trend or Trading Range

Discussion in 'Automated Trading' started by HawaiianIceberg, Apr 30, 2025.

  1. MarkBrown

    MarkBrown

    i can if you wish i use to have an obsession with markov chains..
     
    #31     May 2, 2025
  2. MarkBrown

    MarkBrown


    yea i hope the code is good, i converted it from multicharts powerlanguage which is what i wrote it in native. i don't have ninja so i can't verify. it has a limit to trade between the hours of 0830-1500 so adjust that for your time zone.

    @SimpleMeLike is a wiz at ninja code so maybe he will help if we run into a snag?
     
    #32     May 2, 2025
  3. Aloha @MarkBrown ,

    Thank you again for the code. I found it to have mixed results. The more data I back tested, the more off target it went. I'm guessing because of the set SL / PT in relation to the ever increasing market volatility (I'm looking at it on ES specifically). What do you think about SL/PT being based off recent volatility (ex average(high-low, 200) or something like that?

    The code runs 8:30-11:30, but you mentioned in the post about it being run from 8:30-15:00?

    Thanks again!
     
    #33     May 5, 2025
    SimpleMeLike likes this.
  4. metotron

    metotron

    @MarkBrown Thanks for the code! May I ask you to post the initial Multicharts code. I want to try in Tradestation and would like to avoid "translation from the translation" If not, is it ok if I convert to Easy and put it here?
     
    #34     May 5, 2025
  5. MarkBrown

    MarkBrown

    will do if i can remember what i named it lol

    //=PK/ST/232/V6 std2 PB

    input:st1(10),dat(c),zn3(4),soundon(true),pftamt(21),stpamt(48),size(1);

    vars:intrabarpersist os(0),intrabarpersist aa(0),intrabarpersist bb(0),intrabarpersist zx(0),
    intrabarpersist v10(0),intrabarpersist v36(0),intrabarpersist v45(0),intrabarpersist v47(0),
    intrabarpersist playonce(0);

    os=Average(dat,st1);
    aa=dat;
    bb=absvalue(aa-os);

    if t >= 0830 and t<1130 then begin


    if BarStatus(1) = 2 then begin
    if aa-os >=0 and bb>=zn3 and bb[1]<=zn3 then buy size contracts this bar at c; end;
    if BarStatus(1) = 2 then begin
    if aa-os <=0 and bb>=zn3 and bb[1]<=zn3 then sell short size contracts this bar at c;end;

    setprofittarget_PT(pftamt);
    setstoploss_PT(stpamt);

    end;
    //has a few extra unused vars in the code fyi set profit target




    --------





    //below indicator code

    //LJ_232_1

    [LegacyColorValue = true];


    [RecoverDrawings = True]
    //[ScaleRange(0, 10, 0)]

    // Input parameters
    input: st1(10), dat((mean[1]+o)/2), damp(0), blackout(false), zn1(.5), zn2(2), zn3(4), soundon(false), barsizePt(4.75);

    // Declare persistent variables
    vars: intrabarpersist os(0), intrabarpersist aa(0), intrabarpersist bb(0), normalizedBB(0);
    vars: MaxValue(100); // Set this to the maximum possible value of 'bb'

    // Calculate the moving average of 'dat' over 'st1' bars
    os = summation(dat, st1) / st1;

    // Calculate other necessary variables
    aa = dat;
    bb = absvalue(aa - os);

    // Normalize the 'bb' values to fit within the 0 to 10 scale
    normalizedBB = (bb / MaxValue) * 10;

    // Plot positive differences with label "diff+"

    plot124(normalizedBB, "diff+");
     
    Last edited: May 5, 2025
    #35     May 5, 2025
    metotron and HawaiianIceberg like this.
  6. Good Evening HawaiianIceberg,

    Are you poor or rich man?

    If you are poor man, stop all this algo development stuff. You have to get in the futures market and bleed daily. You do not have time to be back testing and coding all day. Do not waste time like I did back testing and programming. It will make you VERY poor.

    You need to program it and back test.

    Do not use the Optimization feature or you will lose TONS of money. Optimization make me lose money. If you optimize, optimize about 80% out of sample.

    Do not waste time in algo developement, cause you still need to click the chart manually.

    Also algo developement is for people who are already rich and have a relaxing life, it is not for poor traders who really need to get out here and make some money in the ES future market.

    I was building algos and backtesting for long time, and only got poorer and never made any money with it. I lost about $22,000 trading algos. Complete waste of time for me. I hate algo development and back testing. I really really hate it.

    The only way trading algo makes sense is if you have a $1milion dollars to bet on every trade, then you are free to trade algos all day long other wise if you are poor with only about $20,000, do not do it. Go trade manually in the ES futures by yourself and hustle it up.
     
    #36     May 5, 2025
    MarkBrown likes this.
  7. Tradestation is my primary platform. Already rewrote it

    I started adding the volatility adjustment i referenced, so you can comment out those liens and uncomment the ones above them
    Also changed the variable names

    using elsystem.drawing;
    using elsystem.drawingobjects;
    using elsystem.collections;
    using elsystem.componentmodel;
    using elsystem.data;
    using elsystem.drawing;
    using elsystem.drawingobjects;
    using elsystem.io;
    using elsystem.net;
    using elsystem.office;
    using elsystem.text;
    using elsystem.windows;
    using elsystem.xml;

    inputs:
    int AvgLength (10), // Moving average period
    // double VolatilityThreshold (4.0), // Volatility threshold in points
    double VolatilityThresholdFactor (1.0), // Volatility threshold in points
    // double ProfitTarget (1.0), // Profit target in points
    double ProfitTargetFactor (1.0), // Profit target in points
    // double StopLoss (4.0), // Stop lAvgValues in points
    double StopLossFactor (4.0), // Stop lAvgValues in points
    // double tolerance (0.25); // Tolerance for deviation matching
    double ToleranceFactor (0.25); // Tolerance for deviation matching
    Vars:
    Double AvgValue(0), // Current SMA value.
    Double ClosePrice (0), // Current clAvgValueing price.
    Double DistanceFromSMA (0), // Absolute deviation of the clAvgValueing price from the SMA (|ClAvgValuee - SMA|).
    double AverageVolatility (0),
    double CalculatedProfitTarget (0),
    double CalculatedStopLoss (0),
    double CalculatedTolerance (0),
    double CalculatedVolatilityThreshold (0);


    Once
    Begin
    SetStopContract;
    end;

    AverageVolatility = average(high[0] - Low[0], 500);
    CalculatedProfitTarget = AverageVolatility * ProfitTargetFactor;
    CalculatedStopLoss = AverageVolatility * StopLossFactor;
    CalculatedTolerance = AverageVolatility * ToleranceFactor;
    CalculatedVolatilityThreshold = AverageVolatility * VolatilityThresholdFactor;

    AvgValue = average(Close[0], AvgLength)[0];
    ClosePrice = Close[0];
    DistanceFromSMA = Absvalue(ClosePrice - AvgValue);

    // Trading window: 8:30 AM to 11:30 AM (( CST?)
    if (Time >= 0930 and Time < 1130) Then
    Begin
    // Long entry: price above SMA and deviation within range
    if ( ClosePrice >= AvgValue
    // And DistanceFromSMA >= VolatilityThreshold
    And DistanceFromSMA >= CalculatedVolatilityThreshold
    // And DistanceFromSMA <= VolatilityThreshold + tolerance * BigPointValue) Then
    And DistanceFromSMA <= CalculatedVolatilityThreshold + CalculatedTolerance * BigPointValue) Then
    Begin
    // Enter long
    Buy this bar at close;
    end;
    end
    // Short entry: price below SMA and deviation within range
    else if ( ClosePrice <= AvgValue
    // and DistanceFromSMA >= VolatilityThreshold
    and DistanceFromSMA >= CalculatedVolatilityThreshold
    // and DistanceFromSMA <= VolatilityThreshold + tolerance ) Then
    and DistanceFromSMA <= CalculatedVolatilityThreshold + CalculatedTolerance ) Then
    Begin
    // Enter short
    sell short this bar at close;
    end;


    SetStopContract;
    //SetStopLoss(StopLoss * BigPointValue);
    //SetProfitTarget(ProfitTarget * BigPointValue);
    SetStopLoss(CalculatedStopLoss * BigPointValue);
    SetProfitTarget(CalculatedProfitTarget * BigPointValue);

    if time >=1600 Then
    Begin
    If Marketposition = 1 Then
    Begin
    sell this bar at close;
    End
    else if Marketposition = -1 Then
    Begin
    buy to cover this bar at close;
    end;

    end;
     
    #37     May 5, 2025
    metotron, Sekiyo and SimpleMeLike like this.
  8. Hello HawaiianIceberg,

    Are you rich or poor?

    Read what I wrote you below. Go back to manually trading like you was before if you trying to be rich. Algo development is for traders already rich.

    As soon as the algo go to drawdown you will start crying and whining and complaining, while I am in the ES futures market making money everyday fighting and killing for ticks of profits all day.

    Your algo can not recover drawdown in 2 days, so go click.

    Your back test will show this fails, so go back in the ES futures market and click the charts.

    https://www.elitetrader.com/et/thre...d-or-trading-range.384335/page-4#post-6127418
     
    #38     May 5, 2025