Skip to content

Instantly share code, notes, and snippets.

@CurtisAccelerate
Created January 14, 2025 18:22
Show Gist options
  • Save CurtisAccelerate/4379bbc0b1feeceee47e29b562c5dfd1 to your computer and use it in GitHub Desktop.
Save CurtisAccelerate/4379bbc0b1feeceee47e29b562c5dfd1 to your computer and use it in GitHub Desktop.
EasyLanguage_Features
{
This code focuses on tracking certain “features” for winning and losing trades,
then adjusting buy conditions based on how similar current market conditions are
to past winners or losers (a simplified nearest-neighbor approach).
}
Variables:
WinCount(0), LossCount(0), TradeCount(0),
MarketPositionByBar(0),
EntryBar(0),
// Arrays to store the sum of feature values for winning and losing trades
WinF[5](0),
LossF[5](0),
// Current feature array
F[5](0);
// --------------------------------------------------------------------
// 1. Define or calculate your main entry & exit conditions
// (E.g., a simple RSI(2) crossing under 20 for buy, crossing over 70 for exit).
// --------------------------------------------------------------------
RsiSignal = RSI(Close, 2);
BuyCondition = RsiSignal Cross under 20;
SellCondition = RsiSignal Cross over 70 or BarsSinceEntry > 5;
// --------------------------------------------------------------------
// 2. Define secondary “features” that we want to track for each trade.
// These are just examples. You can use anything that might help classify
// your environment or “learn” from winners vs. losers.
// --------------------------------------------------------------------
F[0] = define your features
F[1] = define your features
F[2] = define your features
// --------------------------------------------------------------------
// 3. Track when a trade closes. If MarketPosition changes from 1/-1 to 0,
// then the prior trade has just ended. We record whether it was a winner
// or loser by adding its feature values to WinF[] or LossF[].
// --------------------------------------------------------------------
MarketPositionByBar = MarketPosition;
if (MarketPositionByBar <> MarketPositionByBar[1]) // a change in position
and (MarketPositionByBar = 0) then // and now flat => means a trade closed
begin
// positionprofit(1) = net profit of the last (just-closed) trade
if positionprofit(1) > 0 then
begin
WinF[0] = WinF[0] + F[0];
WinF[1] = WinF[1] + F[1];
WinF[2] = WinF[2] + F[2];
WinCount = WinCount + 1;
end
else
begin
LossF[0] = LossF[0] + F[0];
LossF[1] = LossF[1] + F[1];
LossF[2] = LossF[2] + F[2];
LossCount = LossCount + 1;
end;
TradeCount = TradeCount + 1;
end;
// --------------------------------------------------------------------
// 4. If we have accumulated enough trades (TradeCount >= 30, for example),
// we compare the current conditions to the average conditions from
// our historical winners and losers. This is a simplified “nearest
// neighbor” or similarity approach: if it looks more like losing
// conditions, we skip the trade, otherwise we allow it.
// --------------------------------------------------------------------
if (TradeCount >= 30) and (BuyCondition) then
begin
// 4a. Compute the difference between current features (F[0..2]) and
// the average for winning or losing trades.
// Note: We guard against divide-by-zero by ensuring WinCount, LossCount > 0.
Value70 = Pos( F[0] - (WinF[0]/MaxList(WinCount,1)) );
Value71 = Pos( F[1] - (WinF[1]/MaxList(WinCount,1)) );
Value72 = Pos( F[2] - (WinF[2]/MaxList(WinCount,1)) );
Value73 = Pos( F[0] - (LossF[0]/MaxList(LossCount,1)) );
Value74 = Pos( F[1] - (LossF[1]/MaxList(LossCount,1)) );
Value75 = Pos( F[2] - (LossF[2]/MaxList(LossCount,1)) );
// 4b. Optionally, you can standardize or scale these differences.
// For illustration, we call a hypothetical standardize function:
Value80 = standardize(Value70 - Value73, 15, 2);
Value81 = standardize(Value71 - Value74, 15, 2);
Value82 = standardize(Value72 - Value75, 15, 2);
// 4c. Based on these comparisons, decide if we still want to BUY
// or if we skip the trade. E.g., if it looks “more like a loser,” skip.
// The example below is simplistic; you can do something more elaborate:
if (Value80 < 0.0) or (Value81 < 0.0) or (Value82 < 0.0) then
begin
// Possibly skip trade if the conditions are “closer” to losing
BuyCondition = false;
end;
end;
// --------------------------------------------------------------------
// 5. Finally, if BuyCondition (still) true, place the order.
// If in a long, and SellCondition triggers, exit.
// --------------------------------------------------------------------
if (MarketPosition < 1) and (BuyCondition) then
begin
Buy next bar at market;
EntryBar = CurrentBar;
TradeCount = TradeCount + 1; // (Optional) increment if you want to track signals
end;
if (MarketPosition > 0) and (SellCondition) then
Sell next bar at market;
// --------------------------------------------------------------------
// 6. Optional: Risk management
// --------------------------------------------------------------------
SetStopContract;
SetStopLoss(BigPoint*Points);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment