I am sharing a raw grid code I used on the DAX m1 timeframe during the last bullish leg.
The logic is stupidly simple: It buys 1 contract every time price drops by 20 points from the last trade price. It closes everything when the total profit reaches a fixed target (Global Target).
I know grids are dangerous, but if you run this only during US market hours and cut it before the close, it acts as a liquidity provider.
Be careful with the leverage, on the DAX it can go fast. I set the max positions to 10 to avoid blowing the account.
// --------------------------------------------------------
// Strategy: Simple Accumulation Grid (Long Only)
// Asset: DAX / Indices
// Risk: HIGH - No Stop Loss per trade
// --------------------------------------------------------
DEFPARAM CumulateOrders = True // Essential for Grid
// Settings
GridStep = 20 // Distance between buys
TakeProfitPoints = 50 // Target in points (Global)
MaxPositions = 10 // Safety limit
// Entry Logic
IF Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
LastEntryPrice = Close
ENDIF
// Add to Grid (Averaging Down)
IF LongOnMarket AND CountOfPosition < MaxPositions THEN
IF Close <= LastEntryPrice - GridStep THEN
BUY 1 CONTRACT AT MARKET
LastEntryPrice = Close // Update reference price
ENDIF
ENDIF
// Exit Logic (Global Profit Target)
// We calculate the average entry price of the whole stack
AvgPrice = PositionPrice
CurrentProfit = (Close - AvgPrice) * CountOfPosition
IF CurrentProfit >= (TakeProfitPoints * PointValue) THEN
SELL AT MARKET
ENDIF
It’s not too bad that the trend is clear (long only on indices)… Of course, be wary of Trump as soon as the US wakes up!
This is below another version, tested for recent NASDAQ data 5-minutes, 0.5 contracts, 1 point spread.
Added a trend filter (simple couple of long term SMA comparison) plus another exit when in profit / breakeven in grid situation.
There is a whole range of things to explore for this kind of strategies! Lots of maths and headaches, but hey we like this so much! š
// --------------------------------------------------------
// Strategy: Simple Accumulation Grid (Long Only)
// Risk: HIGH - No Stop Loss per trade
// --------------------------------------------------------
DEFPARAM CumulateOrders = True // Essential for Grid
// Settings
GridStep = 20 // Distance between buys
TakeProfitPoints = 50 // Target in points (Global)
MaxPositions = 10 // Safety limit
sma1=average[200]
sma2=average[400]
uptrend = sma1>sma2
// Entry Logic
IF Not OnMarket and uptrend THEN
BUY 1 CONTRACT AT MARKET
LastEntryPrice = Close
ENDIF
// Add to Grid (Averaging Down)
IF LongOnMarket AND CountOfPosition < MaxPositions and not uptrend THEN
IF Close <= LastEntryPrice - GridStep THEN
BUY 0.5 CONTRACT AT MARKET
LastEntryPrice = Close // Update reference price
ENDIF
ENDIF
// Exit Logic (Global Profit Target)
// We calculate the average entry price of the whole stack
AvgPrice = PositionPrice
CurrentProfit = (Close - AvgPrice) * CountOfPosition
IF (CurrentProfit >= (TakeProfitPoints * PointValue)) OR (CurrentProfit>0 and countofposition>1) THEN
SELL AT MARKET
ENDIF
Good job Nicolas, as always!
Actually, Iād like to build an algo that would make 40/50 points per day on the DAX, through one or several trades, every day. Between 9:00 a.m and 5:30 p.m Either long or short.
A consistent algo, with the requirement that it takes winning trades every day and achieves those 40/50 points. Stop loss to be defined.
No grid or martingale. Just 1 lot on each trade.Letās do it, letās build this algo together !
@Nicolas
Would it be possible to rewrite this code so that the grid is calculated in percentages instead of pips?
@Lifen, yes! let’s do it! š
@phoentzs
Sure, here is below the new version with a grid step in percentage, along with a profit exit in percentage of price also.
Here is what have changed:
- GridStep (fixed pips) is replaced by GridStepPct (percentage). The trigger price for the next grid buy is now computed as LastEntryPrice * (1 – GridStepPct / 100). With a 1% step and a last entry at 100, the next buy triggers at 99, at 200 it triggers at 198, and so on. The grid automatically scales with price level.
- TakeProfitPoints (fixed points multiplied by PointValue) is replaced by TakeProfitPct. The profit is now expressed as (Close – AvgPrice) / AvgPrice * 100, which gives a pure percentage gain relative to the weighted average entry price of the whole stack. The exit fires when that percentage reaches TakeProfitPct.
- CurrentProfit (absolute currency value) is gone entirely. ProfitPct replaces it in both exit conditions, keeping everything consistent in percentage terms.
// --------------------------------------------------------
// Strategy: Simple Accumulation Grid (Long Only)
// Risk: HIGH - No Stop Loss per trade
// Grid and profit target calculated in PERCENTAGES
// --------------------------------------------------------
DEFPARAM CumulateOrders = True // Essential for Grid
// Settings
GridStepPct = 1.0 // Distance between buys in % (e.g. 1.0 = 1%)
TakeProfitPct = 2.5 // Global profit target in % of average entry price
MaxPositions = 10 // Safety limit
sma1 = average[200]
sma2 = average[400]
uptrend = sma1 > sma2
// Entry Logic
IF Not OnMarket AND uptrend THEN
BUY 1 CONTRACT AT MARKET
LastEntryPrice = Close
ENDIF
// Add to Grid (Averaging Down)
// Trigger when price drops GridStepPct% below last entry price
IF LongOnMarket AND CountOfPosition < MaxPositions AND NOT uptrend THEN
GridTriggerPrice = LastEntryPrice * (1 - GridStepPct / 100)
IF Close <= GridTriggerPrice THEN
BUY 0.5 CONTRACT AT MARKET
LastEntryPrice = Close
ENDIF
ENDIF
// Exit Logic (Global Profit Target in %)
// PositionPrice = weighted average entry price of the whole stack
AvgPrice = PositionPrice
ProfitPct = (Close - AvgPrice) / AvgPrice * 100
IF (ProfitPct >= TakeProfitPct) OR (ProfitPct > 0 AND CountOfPosition > 1) THEN
SELL AT MARKET
ENDIF
Attached is “performance” of the code for NASDAQ 30min CFD, UTC+0 with weekend data. What a ride! 40k drawdown is ugly though