dap711
Active member
Hi All!
I have spent hundreds of hours developing the "Moving Average Master" strategy. Note: This strategy is best used on the lower time frames. Begin by selecting a time frame you want to trade and apply this strategy and the floating P/L study to the chart. You should see a BUNCH of trades on the chart! Right click on one of them and select "Edit Strategy" (It's important to do it from the chart! This way you can apply the new settings without having to close the edit window and you can instantly see the results of the change.)
Video about how to optimize this (or any) strategy: https://drive.google.com/file/d/1UJsCk5a2yE8kAREz429Oob0rU7R9DER_/view?usp=sharing
Video about using Marco Recorder to auto trade. https://drive.google.com/file/d/1uq1IMBaUw96mFSAgL02d1LQcJT_3V8W_/view?usp=sharing
Video of Macro Recorder in action trading paper: https://drive.google.com/file/d/1aNj1nqizMi54Tu6LuDlmGZbDfQbQU5HI/view?usp=sharing
Moving Average Master Strategy shared link: https://tos.mx/vL2BUhC
Latest code for Moving Average Master:
I have spent hundreds of hours developing the "Moving Average Master" strategy. Note: This strategy is best used on the lower time frames. Begin by selecting a time frame you want to trade and apply this strategy and the floating P/L study to the chart. You should see a BUNCH of trades on the chart! Right click on one of them and select "Edit Strategy" (It's important to do it from the chart! This way you can apply the new settings without having to close the edit window and you can instantly see the results of the change.)
Video about how to optimize this (or any) strategy: https://drive.google.com/file/d/1UJsCk5a2yE8kAREz429Oob0rU7R9DER_/view?usp=sharing
Video about using Marco Recorder to auto trade. https://drive.google.com/file/d/1uq1IMBaUw96mFSAgL02d1LQcJT_3V8W_/view?usp=sharing
Video of Macro Recorder in action trading paper: https://drive.google.com/file/d/1aNj1nqizMi54Tu6LuDlmGZbDfQbQU5HI/view?usp=sharing
Moving Average Master Strategy shared link: https://tos.mx/vL2BUhC
Latest code for Moving Average Master:
Ruby:
# Author - dap711
# FloatingPL by MerryDay
#1. Set the Trend MA type, price, and length. We only take buys if price is above the trend line and sells below it.
#2. Adjust the MA lengths to achieve the best profit from the floating P/L.
#3. You can now turn off the "ShowStrategyPositions"
#Create a new strategy and copy/paste this code into it.
#Charting
input ShowStategyPositions = yes;
#Hint ShowStategyPositions: Plot the historical trades on the chart. Needed to calculate the floating P/L.
input ShowCalculatedBarColor = yes;
#Hint ShowCalculatedBarColor: Change the color of the chart candles to match the strategy.
input ShowMovingAverages = no;
#Hint ShowMovingAverages: Show the moving averages on the chart.
input ShowFloating_pl_Labels = yes;
#Hint ShowFloating_pl_Labels: Show the floatingPL labels. "show stategy positions" must be set to "yes".
#Moving averages
input MA_AverageType = AverageType.HULL;
input MA_Price = close;
input MA1_Length = 8;
input MA2_Length = 21;
input UseMA3 = yes;
input MA3_Length = 33;
input UseMA4 = no;
input MA4_Length = 54;
#Trading Times
input RiskAmount = 1000;
#Hint RiskAmount: This should be between 1-5% of your total account amount. (Recommend 1% to start with)
input UseTradingTimes = no;
input FirstStartTradingEST = 0930;
input FirstStopTradingEST = 1200;
input SecondStartTradingEST = 1159;
input SecondStopTradingEST = 1557;
def OkToTrade = if UseTradingTimes then if(secondsFromTime(FirstStartTradingEST) >= 0 and
secondsFromTime(FirstStopTradingEST) <= 0) or
(secondsFromTime(SecondStartTradingEST) >= 0 and
secondsFromTime(SecondStopTradingEST) <= 0) then yes else no else yes;
#Plots
plot MA1 = MovingAverage(MA_AverageType,MA_Price,MA1_Length);
MA1.SetHiding(!ShowMovingAverages);
MA1.SetStyle(Curve.SHORT_DASH);
MA1.SetLineWeight(1);
MA1.SetDefaultColor(Color.GRAY);
plot MA2 = MovingAverage(MA_AverageType,MA_Price,MA2_Length);
MA2.SetHiding(!ShowMovingAverages);
MA2.SetStyle(Curve.SHORT_DASH);
MA2.SetLineWeight(1);
MA2.SetDefaultColor(Color.DARK_GRAY);
plot MA3 = MovingAverage(MA_AverageType,MA_Price,MA3_Length);
MA3.SetHiding(!ShowMovingAverages or !UseMA3);
MA3.SetStyle(Curve.SHORT_DASH);
MA3.SetLineWeight(1);
MA3.SetDefaultColor(Color.DARK_GREEN);
plot MA4 = MovingAverage(MA_AverageType,MA_Price,MA4_Length);
MA4.SetHiding(!ShowMovingAverages or !UseMA4);
MA4.SetStyle(Curve.SHORT_DASH);
MA4.SetLineWeight(1);
MA4.SetDefaultColor(Color.DARK_ORANGE);
#Trading
def Buy = if MA1>MA2 and close>MA2 and
(if UseMA3 then (if MA2>MA3 then yes else no) else yes) and
(if UseMA4 then (if MA3>MA4 then yes else no) else yes)
then yes else no;
def Sell = if MA1<MA2 and close<MA2 and
(if UseMA3 then (if MA2<MA3 then yes else no) else yes) and
(if UseMA4 then (if MA3<MA4 then yes else no) else yes)
then yes else no;
AddOrder(OrderType.BUY_TO_OPEN, OkToTrade and Buy and ShowStategyPositions, open[-1], RiskAmount/close, Color.CYAN, Color.CYAN, "EA");
AddOrder(OrderType.SELL_TO_CLOSE, (!OkToTrade or !Buy) and ShowStategyPositions, open[-1], RiskAmount/close, Color.CYAN, Color.CYAN,"");
AddOrder(OrderType.SELL_TO_OPEN, OkToTrade and Sell and ShowStategyPositions, open[-1], RiskAmount/close, Color.RED, Color.RED,"EA");
AddOrder(OrderType.BUY_TO_CLOSE, (!OkToTrade or !Sell) and ShowStategyPositions, open[-1], RiskAmount/close, Color.RED,Color.RED, "");
#Labels
AddLabel(yes, " Buy ", if ShowCalculatedBarColor and OkToTrade and Buy then Color.Cyan else Color.White);
AddLabel(yes, " Sell ", if ShowCalculatedBarColor and OkToTrade and Sell then Color.Red else Color.White);
AddLabel(yes, " Close ", if ShowCalculatedBarColor and !OkToTrade and !Sell and !Buy then Color.Yellow else Color.White);
AddLabel(ShowMovingAverages,"MA1="+MA1_Length,Color.GRAY);
AddLabel(ShowMovingAverages,"MA2="+MA2_Length,Color.DARK_GRAY);
AddLabel(ShowMovingAverages and UseMA3,"MA3="+MA3_Length,Color.DARK_GREEN);
AddLabel(ShowMovingAverages and UseMA4,"MA4="+MA4_Length,Color.DARK_ORANGE);
#//FloatingProfitLoss Labels
script incrementValue {
input condition = yes;
input increment = 1;
input startingValue = 0;
def _value = CompoundValue(1,if condition then _value[1] + increment else _value[1], startingValue);
plot incrementValue = _value;
}
def fplTargetWinLoss = .50;
def fplTargetWinRate = 1;
input HidePrices = no;
#Hint HidePrices: Useful to see posistions without the candles on the chart. Must have "show strategy positions" set to yes.
def fplHideFPL = yes;
def targetWinLoss = fplTargetWinLoss;
def targetWinRate = fplTargetWinRate;
def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
HidePricePlot(hidePrices);
def FPL = FPL();
# Entry Calculations. Note: Only parses on a Strategy Chart
def entry = EntryPrice();
def entryPrice = if !IsNaN(entry) then entry else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];
#is active trade
def highFPL = HighestAll(FPL);
def lowFPL = LowestAll(FPL);
def fplreturn = (FPL - FPL[1]) / FPL[1];
def cumsum = Sum(fplreturn);
def highBarNumber = CompoundValue(1, if FPL == highFPL then bn else highBarNumber[1], 0);
def lowBarNumber = CompoundValue(1, if FPL == lowFPL then bn else lowBarNumber[1], 0);
#Win/Loss ratios
def entryBarsTemp = if hasEntry then bn else nan;
def entryBarNum = if hasEntry and isNewEntry then bn else entryBarNum[1];
def isEntryBar = entryBarNum != entryBarNum[1];
def entryBarPL = if isEntryBar then FPL else entryBarPL[1];
def exitBarsTemp = if !hasEntry and bn > entryBarsTemp[1] then bn else nan;
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];
def isExitBar = exitBarNum != exitBarNum[1];
def exitBarPL = if isExitBar then FPL else exitBarPL[1];
def entryReturn = if isExitBar then exitBarPL - exitBarPL[1] else entryReturn[1];
def isWin = if isExitBar and entryReturn >= 0 then 1 else 0;
def isLoss = if isExitBar and entryReturn < 0 then 1 else 0;
def entryReturnWin = if isWin then entryReturn else entryReturnWin[1];
def entryReturnLoss = if isLoss then entryReturn else entryReturnLoss[1];
def entryFPLWins = if isWin then entryReturn else 0;
def entryFPLLosses = if isLoss then entryReturn else 0;
def entryFPLAll = if isLoss or isWin then entryReturn else 0;
#Counts
def entryCount = incrementValue(entryFPLAll);
def winCount = incrementValue(isWin);
def lossCount = incrementValue(isLoss);
def highestReturn = if entryReturnWin[1] > highestReturn[1] then entryReturnWin[1] else highestReturn[1];
def lowestReturn = if entryReturnLoss[1] < lowestReturn[1] then entryReturnLoss[1] else lowestReturn[1];
def winRate = if winCount == 0 and lossCount == 0 then 0 else if lossCount == 0 then 1 else winCount / lossCount;
def winLossRatio = winCount / entryCount;
def avgReturn = TotalSum(entryFPLAll) / entryCount;
def avgWin = TotalSum(entryFPLWins) / winCount;
def avgLoss = TotalSum(entryFPLLosses) / lossCount;
#Floating P/L labels
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"Total Trades: " + entryCount + " ", Color.Cyan);
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"WinCount: " + winCount + " | LossCount: " + lossCount , color = if winRate >= targetWinRate then Color.Green else Color.Red);
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"W/L: " + AsPercent(winLossRatio) + " ", color = if winLossRatio > targetWinLoss then Color.Green else Color.Red);
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"AvgWin: "+AsDollars(avgWin)+" | AvgLoss: "+AsDollars(avgLoss),color = if TotalSum(entryFPLAll) >= 0 then color.Green else Color.Red);
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"Total Profit: " + AsDollars(TotalSum(entryFPLAll)), color = if TotalSum(entryFPLAll) > 0 then Color.Green else Color.Red);
#Candle Colors
def Candle_Plot = if Buy then 1 else if Sell then -1 else 0;
AssignPriceColor(if ShowCalculatedBarColor then if Candle_Plot > 0 then Color.CYAN
else if Candle_Plot <0 then Color.RED else Color.GRAY
else Color.CURRENT);
#“Make everything as simple as possible, but not simpler.” Albert Einstein.
Last edited: